Skip to content
Snippets Groups Projects
main_window.py 8.39 KiB
Newer Older
  • Learn to ignore specific revisions
  • This python file is the main window of the GUI for B-ASIC.
    
    from os import getcwd, path
    
    from drag_button import DragButton
    from gui_interface import Ui_main_window
    from arrow import Arrow
    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
    
    
    from PyQt5.QtWidgets import QApplication, QWidget, QMainWindow, QLabel, QAction,\
    QStatusBar, QMenuBar, QLineEdit, QPushButton, QSlider, QScrollArea, QVBoxLayout,\
    
    QHBoxLayout, QDockWidget, QToolBar, QMenu, QLayout, QSizePolicy, QListWidget,\
    QListWidgetItem, QGraphicsView, QGraphicsScene, QShortcut, QGraphicsTextItem,\
    QGraphicsProxyWidget
    from PyQt5.QtCore import Qt, QSize
    from PyQt5.QtGui import QIcon, QFont, QPainter, QPen, QBrush, QKeySequence
    
    Jacob Wahlman's avatar
    Jacob Wahlman committed
    MIN_WIDTH_SCENE = 600
    MIN_HEIGHT_SCENE = 520
    
    @decorate_class(handle_error)
    
        def __init__(self):
            super(MainWindow, self).__init__()
            self.ui = Ui_main_window()
            self.ui.setupUi(self)
    
            self.setWindowTitle(" ")
            self.setWindowIcon(QIcon('small_logo.png'))
    
            self.scene = None
            self._operations_from_name = dict()
            self.zoom = 1
            self.operationList = []
            self.signalList = []
            self.pressed_button = []
            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)
            self.add_operations_from_namespace(s_oper, self.ui.special_operations_list)
    
            self.shortcut_core = QShortcut(QKeySequence("Ctrl+R"), self.ui.operation_box)
            self.shortcut_core.activated.connect(self._refresh_operations_list_from_namespace)
    
            self.move_button_index = 0
            self.is_show_names = True
    
            self.check_show_names = QAction("Show operation names")
            self.check_show_names.triggered.connect(self.view_operation_names)
            self.check_show_names.setCheckable(True)
            self.check_show_names.setChecked(1)
            self.ui.view_menu.addAction(self.check_show_names)
    
        def init_ui(self):
            self.ui.core_operations_list.itemClicked.connect(self.on_list_widget_item_clicked)
            self.ui.special_operations_list.itemClicked.connect(self.on_list_widget_item_clicked)
            self.ui.exit_menu.triggered.connect(self.exit_app)
            self.create_graphics_view()
    
        def create_graphics_view(self):
    
    Jacob Wahlman's avatar
    Jacob Wahlman committed
            self.scene = QGraphicsScene(self)
    
            self.graphic_view = QGraphicsView(self.scene, self)
            self.graphic_view.setRenderHint(QPainter.Antialiasing)
    
    Jacob Wahlman's avatar
    Jacob Wahlman committed
            self.graphic_view.setGeometry(self.ui.operation_box.width(), 0, self.width(), self.height())
    
            self.graphic_view.setDragMode(QGraphicsView.ScrollHandDrag)
    
    
    Jacob Wahlman's avatar
    Jacob Wahlman committed
        def resizeEvent(self, event):
            self.ui.operation_box.setGeometry(10, 10, self.ui.operation_box.width(), self.height())
            self.graphic_view.setGeometry(self.ui.operation_box.width() + 20, 0, self.width() - self.ui.operation_box.width() - 20, self.height())
            super(MainWindow, self).resizeEvent(event)
    
    
        def wheelEvent(self, event):
    
    Jacob Wahlman's avatar
    Jacob Wahlman committed
            if event.modifiers() == Qt.ControlModifier:
                old_zoom = self.zoom
                self.zoom += event.angleDelta().y()/2500
                self.graphic_view.scale(self.zoom, self.zoom)
                self.zoom = old_zoom
    
    Jacob Wahlman's avatar
    Jacob Wahlman committed
        def view_operation_names(self, event):
    
            if self.check_show_names.isChecked():
                self.is_show_names = True
            else:
                self.is_show_names = False
            for operation in self.operationList:
                operation.label.setOpacity(self.is_show_names)
                operation.is_show_name = self.is_show_names
    
        def _determine_port_distance(self, length, ports):
            """Determine the distance between each port on the side of an operation.
            The method returns the distance that each port should have from 0.
            """
            return [length / 2] if ports == 1 else linspace(0, length, ports)
    
        def _create_port(self, operation, output_port=True):
            text = ">" if output_port else "<"
            button = PortButton(text, operation, self)
            button.setStyleSheet("background-color: white")
            button.connectionRequested.connect(self.connectButton)
            return button
    
        def add_ports(self, operation):
            _output_ports_dist = self._determine_port_distance(55 - 17, operation.operation.output_count)
            _input_ports_dist = self._determine_port_distance(55 - 17, operation.operation.input_count)
    
            for dist in _input_ports_dist:
                port = self._create_port(operation)
                port.move(0, dist)
                port.show()
    
            for dist in _output_ports_dist:
                port = self._create_port(operation)
                port.move(55 - 12, dist)
                port.show()
    
        def get_operations_from_namespace(self, namespace):
            return [comp for comp in dir(namespace) if hasattr(getattr(namespace, comp), "type_name")]
    
        def add_operations_from_namespace(self, namespace, _list):
            for attr_name in self.get_operations_from_namespace(namespace):
                attr = getattr(namespace, attr_name)
                try:
                    attr.type_name()
                    item = QListWidgetItem(attr_name)
                    _list.addItem(item)
                    self._operations_from_name[attr_name] = attr
                except NotImplementedError:
                    pass
    
        def _create_operation(self, item):
            try:
                attr_oper = self._operations_from_name[item.text()]()
                attr_button = DragButton(attr_oper.graph_id, attr_oper, attr_oper.type_name().lower(), True, self)
                attr_button.move(250, 100)
                attr_button.setFixedSize(55, 55)
                attr_button.setStyleSheet("background-color: white; border-style: solid;\
                border-color: black; border-width: 2px")
                self.add_ports(attr_button)
    
                icon_path = path.join("operation_icons", f"{attr_oper.type_name().lower()}.png")
                if not path.exists(icon_path):
                    icon_path = path.join("operation_icons", f"custom_operation.png")
                attr_button.setIcon(QIcon(icon_path))
                attr_button.setIconSize(QSize(55, 55))
                attr_button.setParent(None)
                attr_button_scene = self.scene.addWidget(attr_button)
                attr_button_scene.moveBy(self.move_button_index * 100, 0)
                self.move_button_index += 1
                operation_label = QGraphicsTextItem(attr_oper.type_name(), attr_button_scene)
                if not self.is_show_names:
                    operation_label.setOpacity(0)
                operation_label.setTransformOriginPoint(operation_label.boundingRect().center())
                operation_label.moveBy(10, -20)
                attr_button.add_label(operation_label)
                self.operationList.append(attr_button)
            except Exception as e:
                print("Unexpected error occured: ", e)
    
        def _refresh_operations_list_from_namespace(self):
            self.ui.core_operations_list.clear()
            self.ui.special_operations_list.clear()
    
            self.add_operations_from_namespace(c_oper, self.ui.core_operations_list)
            self.add_operations_from_namespace(s_oper, self.ui.special_operations_list)
    
        def on_list_widget_item_clicked(self, item):
            self._create_operation(item)
    
        def keyPressEvent(self, event):
            pressed_buttons = []
            for op in self.operationList:
                if op.pressed:
                    pressed_buttons.append(op)
            if event.key() == Qt.Key_Delete:
                for pressed_op in pressed_buttons:
                    self.operationList.remove(pressed_op)
                    pressed_op.remove()
                    self.move_button_index -= 1
            super().keyPressEvent(event)
    
        def connectButton(self, button):
            if len(self.pressed_ports) < 2:
                return
            for i in range(len(self.pressed_ports) - 1):
                line = Arrow(self.pressed_ports[i], self.pressed_ports[i + 1], self)
                self.scene.addItem(line)
                self.signalList.append(line)
    
            self.update()
    
        def paintEvent(self, event):
            for signal in self.signalList:
                signal.moveLine()
    
    
    if __name__ == "__main__":
        app = QApplication(sys.argv)
        window = MainWindow()
        window.show()
    
        sys.exit(app.exec_())