<commit_before>
import copy
import os
import sys
import weakref
from PyQt5 import QtGui

from PyQt5 import QtCore

from PyQt5.QtCore import QResource
from PyQt5.QtCore import QTimer
from PyQt5.QtWidgets import QMessageBox
from PyQt5.QtWidgets import QWidget, QDesktopWidget, QApplication,QLabel,QLineEdit,QPushButton,QGridLayout,QScrollBar,QScrollArea,QMainWindow,QStackedWidget
from PyQt5.QtGui import QPixmap,QFont
from PyQt5.QtCore import QRect,Qt

from app.events import Eventable
from app.gui.components import GuiComponent
from app.gui.components.QResearchWidget import  ResearchFrame
from app.gui.components.QStackedWidget import ComponentArea
from app.helpers import SingletonMixin

import app.gui.resources.images

import qdarkstyle

class NotAComponent(Exception):
    pass

class ErrorWidget(QWidget):
    def __init__(self,parent = None, exception = None):
        super(ErrorWidget, self).__init__(parent)
        self.setWindowOpacity(0.0)
        QTimer.singleShot(1000,self.reveal)

        lbl = QLabel(self)
        lbl.setStyleSheet("""
                        QLabel{
                            background-color:yellow;
                            border-color:yellow;
                            border-radius:5px;
                            border-width: 1px;
                            border-style: solid;
                        }
                        """)
        lbl.setText(str(exception))
        lbl.show()
        self.show()
        print("showing error widget")
    def reveal(self):
        self.setWindowOpacity(1.0)
        self.move(0,self.height())
        print("shown")
        QTimer.singleShot(5000, self.delete)
    def delete(self):
        print("deleted")
        #self.parent.removeWidget(self)
        self.deleteLater()
class Gui(QWidget,Eventable,SingletonMixin):
    def __init__(self,parent=None):
        super(Gui, self).__init__(parent)
        self.registerExceptions()
        self._components = {}
        self.listeners = weakref.WeakKeyDictionary()  # we don't care about keys, and this might contain more references than 2 components in the futur
        self.componentArea = ComponentArea(self)
    def resizeEvent(self, QResizeEvent):
        self.componentArea.resize(QResizeEvent.size())
        super(Gui, self).resizeEvent(QResizeEvent)
        QResizeEvent.accept()

    def initUI(self):
        content = QGridLayout(self)

        self.setMinimumSize(700,800)
        self.center()
        recherche = ResearchFrame(self)
        #add = AddFilesWidget(self)
        self.setWindowTitle('Find My movie')
        self.errorWidget = QLabel(self)
        self.errorWidget.setVisible(False)
        content.addWidget(self.errorWidget,0,0)
        content.addWidget(self.componentArea,2,0)
        content.addWidget(recherche, 1, 0)
        #content.addWidget(add, 0, 1)

        self.setLayout(content)
        self.show()

    def myCustomHandler(self,ErrorType, ErrorContext, TraceBack):
        print("Qt error found.")
        print("Error Type: " + str(ErrorType))
        print("Error Context: " + str(ErrorContext))
        print("Error traceback: "+ str(TraceBack))
        self.addError("Error [" + str(ErrorType) + "] : " + str(ErrorContext))
        # m = QMessageBox()
        # m.setText("Error : "+str(ErrorType))
        # m.exec()
        #e = ErrorWidget(Gui.instance(),ErrorType)

        # Error logging code
        # Error emailing code

        #os.execv(sys.executable, [sys.executable] + sys.argv)

    def ErrorHandling(self,ErrorType, ErrorValue, TraceBack):
        print("System error found.")
        print("Error Type: " + str(ErrorType))
        print("Error Value: " + str(ErrorValue))
        print("Traceback: " + str(TraceBack))
        self.addError("Error [" + str(ErrorType) + "] : " + str(ErrorValue))
        # m = QMessageBox()
        # m.setText("Error [" + str(ErrorType) + "] : " + str(ErrorValue))
        # m.exec()
        #e = ErrorWidget(Gui.instance(), ErrorType)
        # Error logging code
        # Error emailing code

        #os.execv(sys.executable, [sys.executable] + sys.argv)
    def addError(self,text):
        print("############")
        print("handling error")
        print("############")
        self.errorWidget.setText(text)
        self.errorWidget.setVisible(True)
        self.errorWidget.show()
        QTimer.singleShot(5000, lambda :self.errorWidget.setVisible(False))
    def registerExceptions(self):
        sys.excepthook = self.ErrorHandling
        QtCore.qInstallMessageHandler(self.myCustomHandler)
    @staticmethod
    def start():

        app = QApplication(sys.argv)
        app.setStyleSheet(qdarkstyle.load_stylesheet_pyqt5())
        ex = Gui()

        code = 0
        try:
            ex.initUI()
            #raise Exception("Test exception")
            code = app.exec()
        except:
            ex.initUI()
        sys.exit(code)


    def center(self):
        qr = self.frameGeometry()
        cp = QDesktopWidget().availableGeometry().center()
        qr.moveCenter(cp)
        self.move(qr.topLeft())
    # def addComponent(self, cls):
    #     #c = cls(self)<commit_msg>Error handling + button added<commit_after>
import copy
import os
import sys
import weakref
from PyQt5 import QtGui

from PyQt5 import QtCore
from PyQt5.QtCore import QByteArray
from PyQt5.QtCore import QEasingCurve
from PyQt5.QtCore import QPropertyAnimation

from PyQt5.QtCore import QResource
from PyQt5.QtCore import QTimer
from PyQt5.QtWidgets import QGraphicsOpacityEffect
from PyQt5.QtWidgets import QMessageBox
from PyQt5.QtWidgets import QSizePolicy
from PyQt5.QtWidgets import QWidget, QDesktopWidget, QApplication,QLabel,QLineEdit,QPushButton,QGridLayout,QScrollBar,QScrollArea,QMainWindow,QStackedWidget
from PyQt5.QtGui import QPixmap,QFont
from PyQt5.QtCore import QRect,Qt

from app.events import Eventable
from app.gui.components import GuiComponent
from app.gui.components.QResearchWidget import  ResearchFrame
from app.gui.components.QStackedWidget import ComponentArea
from app.helpers import SingletonMixin

import app.gui.resources.images

import qdarkstyle

class NotAComponent(Exception):
    pass

class ErrorWidget(QWidget):
    def __init__(self,parent = None):
        super(ErrorWidget, self).__init__(parent)
        self.errorWidget = QLabel(self)
        if parent is None:
            width = 500
        else:
            width = parent.width()
        self.errorWidget.setFixedSize(width, 20)
        self.errorWidget.setSizePolicy(QSizePolicy.Ignored, QSizePolicy.Ignored)

        self.errorWidget.resizeEvent = self.resizeLabel
        self.errorWidget.setAlignment(Qt.AlignTop | Qt.AlignLeft)
        self.errorWidget.setVisible(False)

        self.animationProp = b"opacity"  # QByteArray(len(s),s)
        # self.errorWidget.setFixedWidth(300)
        self.errorWidget.setStyleSheet("""
                    QLabel{
                        color:#1E8BC3;
                        width:inherit;
                        background-color:black;
                        border-color:#6BB9F0;
                        font-weight:bold;
                        border-bottom-right-radius:5px;
                        border-bottom-left-radius:5px;
                        border-width: 1px;
                        border-style: solid;

                    }
                """)
    def updateText(self,txt):
        self.errorWidget.setText(txt)
    def display(self):
        self.fadeIn()
        QTimer.singleShot(5000, lambda: self.fadeOut())

    def resizeLabel(self, evt):
        font = self.font()
        font.setPixelSize(self.height() * 0.8)
        self.setFont(font)
        
    def fadeIn(self):
        g = QGraphicsOpacityEffect(self)
        self.errorWidget.setGraphicsEffect(g)
        a = QPropertyAnimation(g, self.animationProp,self)
        a.setDuration(350)
        a.setStartValue(0)
        a.setEndValue(1)
        a.setEasingCurve(QEasingCurve.InBack)
        #a.valueChanged.connect(lambda:self.errorWidget.show())
        self.errorWidget.show()
        a.start()
    def fadeOut(self):
        g = QGraphicsOpacityEffect(self)
        self.errorWidget.setGraphicsEffect(g)
        a = QPropertyAnimation(g, self.animationProp,self)
        a.setDuration(350)
        a.setStartValue(1)
        a.setEndValue(0)
        a.setEasingCurve(QEasingCurve.OutBack)
        a.finished.connect(lambda : self.errorWidget.setVisible(False))
        a.start(QPropertyAnimation.DeleteWhenStopped)
class Gui(QWidget,Eventable,SingletonMixin):
    def __init__(self,parent=None):
        super(Gui, self).__init__(parent)

        self._components = {}
        self.listeners = weakref.WeakKeyDictionary()  # we don't care about keys, and this might contain more references than 2 components in the futur
        self.componentArea = ComponentArea(self)
        self.initUI()
        self.registerExceptions()
    def resizeEvent(self, QResizeEvent):
        self.componentArea.resize(QResizeEvent.size())
        super(Gui, self).resizeEvent(QResizeEvent)
        QResizeEvent.accept()

    def initUI(self):
        content = QGridLayout(self)

        self.setMinimumSize(700,800)
        self.center()
        recherche = ResearchFrame(self)
        #add = AddFilesWidget(self)
        self.setWindowTitle('Find My movie')
        self.errorWidget = ErrorWidget(self)
        content.addWidget(self.componentArea,2,0)
        content.addWidget(recherche, 1, 0)

        self.setLayout(content)
        self.show()

    def myCustomHandler(self,ErrorType, ErrorContext, TraceBack):
        print("Qt error found.")
        print("Error Type: " + str(ErrorType))
        print("Error Context: " + str(ErrorContext))
        print("Error traceback: "+ str(TraceBack))
        self.addError("Error [" + str(ErrorType) + "] : " + str(ErrorContext))
        # m = QMessageBox()
        # m.setText("Error : "+str(ErrorType))
        # m.exec()
        #e = ErrorWidget(Gui.instance(),ErrorType)

        # Error logging code
        # Error emailing code

        #os.execv(sys.executable, [sys.executable] + sys.argv)

    def ErrorHandling(self,ErrorType, ErrorValue, TraceBack):
        print("System error found.")
        print("Error Type: " + str(ErrorType))
        print("Error Value: " + str(ErrorValue))
        print("Traceback: " + str(TraceBack))
        self.addError("Error [" + str(ErrorType) + "] : " + str(ErrorValue))
        # m = QMessageBox()
        # m.setText("Error [" + str(ErrorType) + "] : " + str(ErrorValue))
        # m.exec()
        #e = ErrorWidget(Gui.instance(), ErrorType)
        # Error logging code
        # Error emailing code

        #os.execv(sys.executable, [sys.executable] + sys.argv)

    def addError(self,text):
        print("############")
        print("handling error")
        print("############")
        #self.errorWidget.show()
        self.errorWidget.updateText(text)
        self.errorWidget.display()


    def registerExceptions(self):
        sys.excepthook = self.ErrorHandling
        QtCore.qInstallMessageHandler(self.myCustomHandler)
    @staticmethod
    def start():

        app = QApplication(sys.argv)
        app.setStyleSheet(qdarkstyle.load_stylesheet_pyqt5())
        ex = Gui()

        code = 0
        try:
            #raise Exception("Test exception")
            code = app.exec()
        except:
            ex.initUI()
        sys.exit(code)


    def center(self):
        qr = self.frameGeometry()
        cp = QDesktopWidget().availableGeometry().center()
        qr.moveCenter(cp)
        self.move(qr.topLeft())
    # def addComponent(self, cls):
    #     #c = cls(self)
{'fraction_non_alphanumeric': 0.11172992888355754, 'fraction_numerical': 0.008825293462428241, 'mean_word_length': 2.4977524722804914, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '57224', 'n_tokens_mistral': 3510, 'n_tokens_neox': 3403, 'n_words': 651}