Skip to content
Snippets Groups Projects
improved_main_window.py 20.8 KiB
Newer Older
  • Learn to ignore specific revisions
  • """@package docstring
    B-ASIC GUI Module.
    This python file is the main window of the GUI for B-ASIC.
    """
    
    import sys
    
    from drag_button import DragButton
    from gui_interface import Ui_main_window
    from arrow import Arrow
    
    
    from b_asic import Constant, Addition, Subtraction, Absolute,\
    Multiplication, Division, ConstantMultiplication, SquareRoot, ComplexConjugate,\
    Max, Min, Butterfly
    from b_asic import Input, Output, Register
    
    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
    from PyQt5.QtCore import Qt, QSize
    from PyQt5.QtGui import QIcon, QFont, QPainter, QPen, QBrush
    
    
    class MainWindow(QMainWindow):
        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.init_ui()
            self.add_counter = 0
            self.sub_counter = 0
            self.mul_counter = 0
            self.div_counter = 0
            self.const_counter = 0
            self.cmul_counter = 0
            self.sqr_counter = 0
            self.cc_counter = 0
            self.abs_counter = 0
            self.max_counter = 0
            self.min_counter = 0
            self.butterfly_counter = 0
            self.input_counter = 0
            self.output_counter = 0
            self.reg_counter = 0
    
            self.operationList = []
            self.signalList = []
            self.pressed_button = []
    
            self.source = None
    
        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):
            self.scene = QGraphicsScene()
    
            self.graphic_view = QGraphicsView(self.scene, self)
            self.graphic_view.setRenderHint(QPainter.Antialiasing)
            self.graphic_view.setGeometry(250, 40, 600, 520)
            self.graphic_view.setDragMode(QGraphicsView.ScrollHandDrag)
    
        def wheelEvent(self, event):
            old_zoom = self.zoom
            self.zoom += event.angleDelta().y()/2500
            self.graphic_view.scale(self.zoom, self.zoom)
            self.zoom = old_zoom
    
    
        def exit_app(self, checked):
            QApplication.quit()
    
        def add_ports(self, operation):
            if operation.operation.input_count == 2:
    
                self.input_port_1 = PortButton(">", operation, window)
                self.input_port_1.setFixedSize(15, 15)
    
                self.input_port_1.setStyleSheet("background-color: white")
                self.input_port_1.show()
    
                self.input_port_2 = PortButton(">", operation, window)
                self.input_port_2.setFixedSize(15, 15)
    
                self.input_port_2.setStyleSheet("background-color: white")
    
                self.portList.append(self.input_port_1)
                self.portList.append(self.input_port_2)           
                self.input_port_1.connectionRequested.connect(self.connectButton)
                self.input_port_2.connectionRequested.connect(self.connectButton) 
    
                self.input_port_1 = PortButton(">", operation, window)
                self.input_port_1.setFixedSize(15, 15)
    
                self.input_port_1.setStyleSheet("background-color: white")
                self.input_port_1.move(0, 16)
                self.input_port_1.show()
    
                self.portList.append(self.input_port_1)
                self.input_port_1.connectionRequested.connect(self.connectButton)
    
            if operation.operation.output_count == 2:
    
                self.output_port = PortButton(">", operation, window)
                self.output_port.setFixedSize(15, 15)
    
                self.output_port.setStyleSheet("background-color: white")
                self.output_port.move(38, 0)
                self.output_port.show()
    
                self.output_port2 = PortButton(">", operation, window)
                self.output_port_2.setFixedSize(15, 15)
    
                self.output_port2.setStyleSheet("background-color: white")
                self.output_port2.move(38, 33)
                self.output_port2.show()
    
                self.portList.append(self.output_port)
                self.portList.append(self.output_port_2)
                self.output_port.connectionRequested.connect(self.connectButton)
                self.output_port_2.connectionRequested.connect(self.connectButton)
    
                self.output_port = PortButton(">", operation, window)
                self.output_port.setFixedSize(15, 15)
    
                self.output_port.setStyleSheet("background-color: white")
    
                self.portList.append(self.output_port)
                self.output_port.connectionRequested.connect(self.connectButton)
    
    
        def create_addition_operation(self):
            self.add_counter += 1
            addition_object = Addition()
            self.addition_operation = DragButton("Add" + str(self.add_counter),\
                addition_object, "addition", self)
            self.addition_operation.move(250, 100)
            self.addition_operation.setFixedSize(50, 50)
            self.addition_operation.setStyleSheet("background-color: white; border-style: solid;\
    
            self.addition_operation.setIcon(QIcon(r"operation_icons\addition.png"))
            self.addition_operation.setIconSize(QSize(50, 50))
            self.addition_operation.setParent(None)
            self.scene.addWidget(self.addition_operation)
            self.operationList.append(self.addition_operation)
    
            self.operationList.append(self.addition_operation)
            self.add_ports(self.addition_operation)
    
        def create_subtraction_operation(self):
            self.sub_counter += 1
            subtraction_object = Subtraction()
            self.subtraction_operation = DragButton("Subb" + str(self.sub_counter),\
                subtraction_object, "subtraction", self)
            self.subtraction_operation.move(250, 100)
            self.subtraction_operation.setFixedSize(50, 50)
            self.subtraction_operation.setStyleSheet("background-color: white; border-style: solid;\
                border-color: black; border-width: 2px; border-radius: 10px")
            self.subtraction_operation.setIcon(QIcon(r"operation_icons\subtraction.png"))
            self.subtraction_operation.setIconSize(QSize(50, 50))
            self.subtraction_operation.setParent(None)
            self.scene.addWidget(self.subtraction_operation)
            self.operationList.append(self.subtraction_operation)
            self.add_ports(self.subtraction_operation)
    
        def create_multiplication_operation(self):
            self.mul_counter += 1
            multiplication_object = Multiplication()
            self.multiplication_operation = DragButton("Mul" + str(self.mul_counter),\
                multiplication_object, "multiplication", self)
            self.multiplication_operation.move(250, 100)
            self.multiplication_operation.setFixedSize(50, 50)
            self.multiplication_operation.setStyleSheet("background-color: white; border-style: solid;\
                border-color: black; border-width: 2px; border-radius: 10px")
            self.multiplication_operation.setIcon(QIcon\
                (r"operation_icons\multiplication.png"))
            self.multiplication_operation.setIconSize(QSize(50, 50))
            self.multiplication_operation.setParent(None)
            self.scene.addWidget(self.multiplication_operation)
            self.operationList.append(self.multiplication_operation)
            self.add_ports(self.multiplication_operation)
    
        def create_division_operation(self):
            self.div_counter += 1
            division_object = Division()
            self.division_operation = DragButton("Div" + str(self.div_counter),\
                division_object, "division", self)
            self.division_operation.move(250, 100)
            self.division_operation.setFixedSize(50, 50)
            self.division_operation.setStyleSheet("background-color: white; border-style: solid;\
                border-color: black; border-width: 2px; border-radius: 10px")
            self.division_operation.setIcon(QIcon(r"operation_icons\division.png"))
            self.division_operation.setIconSize(QSize(50, 50))
            self.division_operation.setParent(None)
            self.scene.addWidget(self.division_operation)
            self.operationList.append(self.division_operation)
            self.add_ports(self.division_operation)
    
        def create_constant_operation(self):
            self.const_counter += 1
            constant_object = Constant()
            self.constant_operation = DragButton("Const" + str(self.const_counter),\
                constant_object, "constant", self)
            self.constant_operation.move(250, 100)
            self.constant_operation.setFixedSize(50, 50)
            self.constant_operation.setStyleSheet("background-color: white; border-style: solid;\
                border-color: black; border-width: 2px; border-radius: 10px")
            self.constant_operation.setIcon(QIcon(r"operation_icons\constant.png"))
            self.constant_operation.setIconSize(QSize(50, 50))
            self.constant_operation.setParent(None)
            self.scene.addWidget(self.constant_operation)
            self.operationList.append(self.constant_operation)
            self.add_ports(self.constant_operation)
    
        def create_constant_multiplication_operation(self):
            self.cmul_counter += 1
            constant_multiplication_object = ConstantMultiplication()
            self.constant_multiplication_operation = DragButton("Cmul" + str(self.cmul_counter),\
                constant_multiplication_object, "constant_multiplication", self)
            self.constant_multiplication_operation.move(250, 100)
            self.constant_multiplication_operation.setFixedSize(50, 50)
            self.constant_multiplication_operation.setStyleSheet("background-color: white; \
                border-style: solid; border-color: black; border-width: 2px; border-radius: 10px")
            self.constant_multiplication_operation.setIcon(QIcon\
                (r"operation_icons\constant_multiplication.png"))
            self.constant_multiplication_operation.setIconSize(QSize(50, 50))
            self.constant_multiplication_operation.setParent(None)
            self.scene.addWidget(self.constant_multiplication_operation)
            self.operationList.append(self.constant_multiplication_operation)
            self.add_ports(self.constant_multiplication_operation)
    
        def create_square_root_operation(self):
            self.sqr_counter += 1
            square_root_object = SquareRoot()
            self.square_root_operation = DragButton("Sqr" + str(self.sqr_counter),\
                square_root_object, "square_root", self)
            self.square_root_operation.move(250, 100)
            self.square_root_operation.setFixedSize(50, 50)
            self.square_root_operation.setStyleSheet("background-color: white; border-style: solid;\
                border-color: black; border-width: 2px; border-radius: 10px")
            self.square_root_operation.setIcon(QIcon(r"operation_icons\square_root.png"))
            self.square_root_operation.setIconSize(QSize(50, 50))
            self.square_root_operation.setParent(None)
            self.scene.addWidget(self.square_root_operation)
            self.operationList.append(self.square_root_operation)
            self.add_ports(self.square_root_operation)
    
        def create_complex_conjugate_operation(self):
            self.cc_counter += 1
            complex_conjugate_object = ComplexConjugate()
            self.complex_conjugate_operation = DragButton("Cc" + str(self.cc_counter),\
                complex_conjugate_object, "complex_conjugate", self)
            self.complex_conjugate_operation.move(250, 100)
            self.complex_conjugate_operation.setFixedSize(50, 50)
            self.complex_conjugate_operation.setStyleSheet("background-color: white;\
                border-style: solid; border-color: black; border-width: 2px; border-radius: 10px")
            self.complex_conjugate_operation.setIcon(QIcon\
                (r"operation_icons\complex_conjugate.png"))
            self.complex_conjugate_operation.setIconSize(QSize(50, 50))
            self.complex_conjugate_operation.setParent(None)
            self.scene.addWidget(self.complex_conjugate_operation)
            self.operationList.append(self.complex_conjugate_operation)
            self.add_ports(self.complex_conjugate_operation)
    
        def create_absolute_operation(self):
            self.abs_counter += 1
            absolute_object = Absolute()
            self.absolute_operation = DragButton("Abs" + str(self.abs_counter),\
                absolute_object, "absolute", self)
            self.absolute_operation.move(250, 100)
            self.absolute_operation.setFixedSize(50, 50)
            self.absolute_operation.setStyleSheet("background-color: white; border-style: solid;\
                border-color: black; border-width: 2px; border-radius: 10px")
            self.absolute_operation.setIcon(QIcon(r"operation_icons\absolute.png"))
            self.absolute_operation.setIconSize(QSize(50, 50))
            self.absolute_operation.setParent(None)
            self.scene.addWidget(self.absolute_operation)
            self.operationList.append(self.absolute_operation)
            self.add_ports(self.absolute_operation)
    
        def create_max_operation(self):
            self.max_counter += 1
            max_object = Max()
            self.max_operation = DragButton("Max" + str(self.max_counter),\
                max_object, "max", self)
            self.max_operation.move(250, 100)
            self.max_operation.setFixedSize(50, 50)
            self.max_operation.setStyleSheet("background-color: white; border-style: solid;\
                border-color: black; border-width: 2px; border-radius: 10px")
            self.max_operation.setIcon(QIcon(r"operation_icons\max.png"))
            self.max_operation.setIconSize(QSize(50, 50))
            self.max_operation.setParent(None)
            self.scene.addWidget(self.max_operation)
            self.operationList.append(self.max_operation)
            self.add_ports(self.max_operation)
    
        def create_min_operation(self):
            self.min_counter += 1
            min_object = Min()
            self.min_operation = DragButton("Min" + str(self.min_counter),\
                min_object, "min", self)
            self.min_operation.move(250, 100)
            self.min_operation.setFixedSize(50, 50)
            self.min_operation.setStyleSheet("background-color: white; border-style: solid;\
                border-color: black; border-width: 2px; border-radius: 10px")
            self.min_operation.setIcon(QIcon(r"operation_icons\min.png"))
            self.min_operation.setIconSize(QSize(50, 50))
            self.min_operation.setParent(None)
            self.scene.addWidget(self.min_operation)
            self.operationList.append(self.min_operation)
            self.add_ports(self.min_operation)
    
        def create_butterfly_operation(self):
            self.butterfly_counter += 1
            butterfly_object = Butterfly()
            self.butterfly_operation = DragButton("Butterfly" + str(self.butterfly_counter),\
                butterfly_object, "butterfly", self)
            self.butterfly_operation.move(250, 100)
            self.butterfly_operation.setFixedSize(50, 50)
            self.butterfly_operation.setStyleSheet("background-color: white; border-style: solid;\
                border-color: black; border-width: 2px; border-radius: 10px")
            self.butterfly_operation.setIcon(QIcon(r"operation_icons\butterfly.png"))
            self.butterfly_operation.setIconSize(QSize(50, 50))
            self.butterfly_operation.setParent(None)
            self.scene.addWidget(self.butterfly_operation)
            self.operationList.append(self.butterfly_operation)
            self.add_ports(self.butterfly_operation)
    
        def create_input_operation(self):
            self.input_counter += 1
            input_object = Input()
            self.input_operation = DragButton("In" + str(self.input_counter),\
                input_object, "input", self)
            self.input_operation.move(250, 100)
            self.input_operation.setFixedSize(50, 50)
            self.input_operation.setStyleSheet("background-color: white; border-style: solid;\
                border-color: black; border-width: 2px; border-radius: 10px")
            self.input_operation.setIcon(QIcon(r"operation_icons\input.png"))
            self.input_operation.setIconSize(QSize(50, 50))
            self.input_operation.setParent(None)
            self.scene.addWidget(self.input_operation)
            self.operationList.append(self.input_operation)
            self.add_ports(self.input_operation)
    
        def create_output_operation(self):
            self.output_counter += 1
            output_object = Output()
            self.output_operation = DragButton("Out" + str(self.output_counter),\
                output_object, "output", self)
            self.output_operation.move(250, 100)
            self.output_operation.setFixedSize(50, 50)
            self.output_operation.setStyleSheet("background-color: white; border-style: solid;\
                border-color: black; border-width: 2px; border-radius: 10px")
            self.output_operation.setIcon(QIcon(r"operation_icons\output.png"))
            self.output_operation.setIconSize(QSize(50, 50))
            self.output_operation.setParent(None)
            self.scene.addWidget(self.output_operation)
            self.operationList.append(self.output_operation)
            self.add_ports(self.output_operation)
    
        def create_register_operation(self):
            self.reg_counter += 1
            register_object = Register()
            self.register_operation = DragButton("Reg" + str(self.reg_counter),\
                register_object, "register", self)
            self.register_operation.move(250, 100)
            self.register_operation.setFixedSize(50, 50)
            self.register_operation.setStyleSheet("background-color: white; border-style: solid;\
                border-color: black; border-width: 2px; border-radius: 10px")
            self.register_operation.setIcon(QIcon(r"operation_icons\register.png"))
            self.register_operation.setIconSize(QSize(50, 50))
            self.register_operation.setParent(None)
            self.scene.addWidget(self.register_operation)
            self.operationList.append(self.register_operation)
            self.add_ports(self.register_operation)
    
        def create_custom_operation(self):
            self.custom_operation = DragButton(self)
            self.custom_operation.move(250, 100)
            self.custom_operation.setFixedSize(50, 50)
            self.custom_operation.setStyleSheet("background-color: white; border-style: solid;\
                border-color: black; border-width: 2px; border-radius: 10px")
            self.custom_operation.setIcon(QIcon(r"operation_icons\custom_operation.png"))
            self.custom_operation.setIconSize(QSize(50, 50))
            self.custom_operation.show()
    
        def on_list_widget_item_clicked(self, item):
            if item.text() == "Addition":
                self.create_addition_operation()
            elif item.text() == "Subtraction":
                self.create_subtraction_operation()
            elif item.text() == "Multiplication":
                self.create_multiplication_operation()
            elif item.text() == "Division":
                self.create_division_operation()
            elif item.text() == "Constant":
                self.create_constant_operation()
            elif item.text() == "Constant multiplication":
                self.create_constant_multiplication_operation()
            elif item.text() == "Square root":
                self.create_square_root_operation()
            elif item.text() == "Complex conjugate":
                self.create_complex_conjugate_operation()
            elif item.text() == "Max":
                self.create_max_operation()
            elif item.text() == "Min":
                self.create_min_operation()
            elif item.text() == "Absolute":
                self.create_absolute_operation()
            elif item.text() == "Butterfly":
                self.create_butterfly_operation()
            elif item.text() == "Input":
                self.create_input_operation()
            elif item.text() == "Output":
                self.create_output_operation()
            elif item.text() == "Register":
                self.create_register_operation()
            else:
                print("Block for this operation is not implemented yet.")
    
        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()
            super().keyPressEvent(event)
    
        def connectButton(self, button):
    
            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_())