diff --git a/b_asic/GUI/drag_button.py b/b_asic/GUI/drag_button.py index edb2572dba4887fd60292aa77074d3df0a5d2e93..8a018a3d550c81f77c73179386b5544eacd90f33 100644 --- a/b_asic/GUI/drag_button.py +++ b/b_asic/GUI/drag_button.py @@ -3,7 +3,6 @@ Drag button class. This class creates a dragbutton which can be clicked, dragged and dropped. """ - import os.path from properties_window import PropertiesWindow @@ -12,19 +11,22 @@ from PyQt5.QtWidgets import QPushButton, QMenu, QAction from PyQt5.QtCore import Qt, QSize, pyqtSignal from PyQt5.QtGui import QIcon +from utils import decorate_class, handle_error + +@decorate_class(handle_error) class DragButton(QPushButton): connectionRequested = pyqtSignal(QPushButton) moved = pyqtSignal() def __init__(self, name, operation, operation_path_name, is_show_name, window, parent = None): self.name = name self.is_show_name = is_show_name - self.__window = window + self._window = window self.operation = operation self.operation_path_name = operation_path_name self.clicked = 0 self.pressed = False - super(DragButton, self).__init__(self.__window) + super(DragButton, self).__init__(self._window) def contextMenuEvent(self, event): menu = QMenu() @@ -48,7 +50,7 @@ class DragButton(QPushButton): self._mouse_press_pos = event.globalPos() self._mouse_move_pos = event.globalPos() - for signal in self.__window.signalList: + for signal in self._window.signalList: signal.update() self.clicked += 1 @@ -59,7 +61,7 @@ class DragButton(QPushButton): path_to_image = os.path.join('operation_icons', self.operation_path_name + '_grey.png') self.setIcon(QIcon(path_to_image)) self.setIconSize(QSize(55, 55)) - self.__window.pressed_button.append(self) + self._window.pressed_button.append(self) elif self.clicked == 2: self.clicked = 0 @@ -69,7 +71,7 @@ class DragButton(QPushButton): path_to_image = os.path.join('operation_icons', self.operation_path_name + '.png') self.setIcon(QIcon(path_to_image)) self.setIconSize(QSize(55, 55)) - self.__window.pressed_button.remove(self) + self._window.pressed_button.remove(self) super(DragButton, self).mousePressEvent(event) @@ -82,8 +84,8 @@ class DragButton(QPushButton): self.move(new_pos) self._mouse_move_pos = global_pos - - self.__window.update() + + self._window.update() super(DragButton, self).mouseMoveEvent(event) def mouseReleaseEvent(self, event): diff --git a/b_asic/GUI/main_window.py b/b_asic/GUI/main_window.py index 3d759e2421996173a38179fa9857bbeb82f6e157..bc31b1d3403461ca0ecbece1a2d641e256e5a39b 100644 --- a/b_asic/GUI/main_window.py +++ b/b_asic/GUI/main_window.py @@ -14,6 +14,7 @@ from port_button import PortButton from b_asic import Operation import b_asic.core_operations as c_oper import b_asic.special_operations as s_oper +from utils import decorate_class, handle_error from numpy import linspace @@ -25,7 +26,7 @@ QGraphicsProxyWidget from PyQt5.QtCore import Qt, QSize from PyQt5.QtGui import QIcon, QFont, QPainter, QPen, QBrush, QKeySequence - +@decorate_class(handle_error) class MainWindow(QMainWindow): def __init__(self): super(MainWindow, self).__init__() @@ -42,6 +43,7 @@ class MainWindow(QMainWindow): self.portList = [] self.pressed_ports = [] self.source = None + self._window = self self.init_ui() self.add_operations_from_namespace(c_oper, self.ui.core_operations_list) diff --git a/b_asic/GUI/utils.py b/b_asic/GUI/utils.py new file mode 100644 index 0000000000000000000000000000000000000000..721496c7db7d0259d6335681f6a4c95c6c5930b6 --- /dev/null +++ b/b_asic/GUI/utils.py @@ -0,0 +1,19 @@ +from PyQt5.QtWidgets import QErrorMessage +from traceback import format_exc + +def handle_error(fn): + def wrapper(self, *args, **kwargs): + try: + return fn(self, *args, **kwargs) + except Exception as e: + QErrorMessage(self._window).showMessage(f"Unexpected error: {format_exc()}") + + return wrapper + +def decorate_class(decorator): + def decorate(cls): + for attr in cls.__dict__: + if callable(getattr(cls, attr)): + setattr(cls, attr, decorator(getattr(cls, attr))) + return cls + return decorate \ No newline at end of file