diff --git a/src/simudator/gui/breakpoint_window.py b/src/simudator/gui/breakpoint_window.py index 68d42253e539952ac851145434fe7892b439da0e..ca19345436f974868d30ba1bdd3539bd02b60068 100644 --- a/src/simudator/gui/breakpoint_window.py +++ b/src/simudator/gui/breakpoint_window.py @@ -13,7 +13,6 @@ from simudator.core.processor import Processor class BreakpointWindow(QWidget): - def __init__(self, cpu: Processor): super().__init__() self.cpu = cpu diff --git a/src/simudator/gui/color_scheme.py b/src/simudator/gui/color_scheme.py index 5deeb5ec9f67dded4876682dab2114056cb0345a..7fb6210016c9a8b0c887919e528d3b5a66294b04 100644 --- a/src/simudator/gui/color_scheme.py +++ b/src/simudator/gui/color_scheme.py @@ -6,7 +6,7 @@ to edit. from qtpy.QtGui import QColor -class ColorScheme(): +class ColorScheme: Window = QColor(125, 174, 163) ModuleBackground = QColor(212, 190, 152) ModuleOutline = QColor(0, 0, 0) diff --git a/src/simudator/gui/cpu_graphics_scene.py b/src/simudator/gui/cpu_graphics_scene.py index 0d951782919391db90d6aaaf77f52f4b41bc5b6d..47292c4ec7552e6f75e0243cbb44009a5bdee77b 100644 --- a/src/simudator/gui/cpu_graphics_scene.py +++ b/src/simudator/gui/cpu_graphics_scene.py @@ -13,6 +13,7 @@ class CpuGraphicsScene(QGraphicsScene): and handels mouse inputs for interacting with these graphicsitems. Can create a default layout on creation and can save/load new layouts. """ + MODULE_SPACEING = 100 def __init__(self, cpu: Processor): @@ -44,20 +45,17 @@ class CpuGraphicsScene(QGraphicsScene): is equal to the y value. Used to place all graphics items in a diagonal. """ - placement = len(self.module_graphics_items)*self.MODULE_SPACEING + placement = len(self.module_graphics_items) * self.MODULE_SPACEING graphics_item.setPos(placement, placement) self.addItem(graphics_item) - def replaceModuleGraphicsItem(self, - graphics_item: ModuleGraphicsItem, - pos_x: int, - pos_y: int - ) -> None: + def replaceModuleGraphicsItem( + self, graphics_item: ModuleGraphicsItem, pos_x: int, pos_y: int + ) -> None: """ Changes the postions of an existing modules graphics item. """ - graphics_item.setPos(pos_x*self.MODULE_SPACEING, - pos_y*self.MODULE_SPACEING) + graphics_item.setPos(pos_x * self.MODULE_SPACEING, pos_y * self.MODULE_SPACEING) def updateGraphicsItems(self): """ @@ -97,7 +95,6 @@ class CpuGraphicsScene(QGraphicsScene): port_1.toggled.connect(signal_w.toggleVisibility) port_2.toggled.connect(signal_w.toggleVisibility) - def getModulesGraphicsItems(self) -> list[ModuleGraphicsItem]: return list(self.module_graphics_items.values()) @@ -121,7 +118,6 @@ class CpuGraphicsScene(QGraphicsScene): # Go through each line in the file for line in file.readlines(): - # If no name currently saved then get name from current line if graphics_item_name is None: graphics_item_name = line.partition(":")[0] @@ -163,12 +159,10 @@ class CpuGraphicsScene(QGraphicsScene): file.close() def setAllSignalsVisibility(self, is_signals_visible: bool) -> None: - for item in self.signal_graphics_items: item.setVisible(is_signals_visible) def setPortNamesVisibility(self, is_ports_visible: bool) -> None: - for item in self.module_graphics_items.values(): for port in item.ports: port.setNameVisibility(is_ports_visible) diff --git a/src/simudator/gui/custom_toolbar.py b/src/simudator/gui/custom_toolbar.py index 9fe477b52cb6f95b22e390b1b4dd24386357b18f..b55049f2cb0848b4ca1b8b294e734d06fd9a429b 100644 --- a/src/simudator/gui/custom_toolbar.py +++ b/src/simudator/gui/custom_toolbar.py @@ -10,6 +10,5 @@ class CustomToolBar(QToolBar): def __init__(self, text): super().__init__(text) - def contextMenuEvent(self, event): pass diff --git a/src/simudator/gui/dialogs/lambda_breakpoint_dialog.py b/src/simudator/gui/dialogs/lambda_breakpoint_dialog.py index f0628ef69d2a7afd8fe7a472e9a6b9440c847b22..824131527bb809a225dc46337a9d0f4b96c2f0e6 100644 --- a/src/simudator/gui/dialogs/lambda_breakpoint_dialog.py +++ b/src/simudator/gui/dialogs/lambda_breakpoint_dialog.py @@ -15,11 +15,12 @@ from qtpy.QtWidgets import ( class LambdaBreakpointDialog(QDialog): accepted = pyqtSignal(str, str, name="accepted") - def __init__(self, - lambdas: list[str], - parent: Optional['QWidget'] = None, - flags: Qt.WindowFlags | Qt.WindowType = Qt.WindowFlags() - ) -> None: + def __init__( + self, + lambdas: list[str], + parent: Optional['QWidget'] = None, + flags: Qt.WindowFlags | Qt.WindowType = Qt.WindowFlags(), + ) -> None: super().__init__(parent, flags) self.lambdaSelectWidget = QComboBox() diff --git a/src/simudator/gui/dialogs/memory_content_dialog.py b/src/simudator/gui/dialogs/memory_content_dialog.py index a1e895673073fd24cf14e03ba551d846a1d10269..4bbeb63196470fbe1de36d5b53ecf1109531cb7e 100644 --- a/src/simudator/gui/dialogs/memory_content_dialog.py +++ b/src/simudator/gui/dialogs/memory_content_dialog.py @@ -17,15 +17,16 @@ from simudator.core import Module class MemoryContentDialog(QDialog): accepted = pyqtSignal(str, str, str, name="accepted") - def __init__(self, - memory: Module, - parent: Optional['QWidget'] = None, - flags: Qt.WindowFlags | Qt.WindowType = Qt.WindowFlags() - ) -> None: + def __init__( + self, + memory: Module, + parent: Optional['QWidget'] = None, + flags: Qt.WindowFlags | Qt.WindowType = Qt.WindowFlags(), + ) -> None: super().__init__(parent, flags) self.memory = memory - self.memory_name =memory.get_state()['name'] + self.memory_name = memory.get_state()['name'] adresses = [str(i) for i in range(len(memory.get_state()['memory']))] @@ -57,7 +58,6 @@ class MemoryContentDialog(QDialog): value = memory_content[selectedAdress] self.valuesWidget.setText(str(value)) - def signalAccepted(self) -> None: adress = self.adressSelectWidget.currentText() enteredValues = self.valuesWidget.text() diff --git a/src/simudator/gui/dialogs/module_state_dialog.py b/src/simudator/gui/dialogs/module_state_dialog.py index 7c54d0dc0c9dd644db38c2a724041daacee7f766..622112d17d1d88ac6ec09483cd4b13e0d4a95c00 100644 --- a/src/simudator/gui/dialogs/module_state_dialog.py +++ b/src/simudator/gui/dialogs/module_state_dialog.py @@ -17,11 +17,12 @@ from simudator.core import Module class ModuleStateDialog(QDialog): accepted = pyqtSignal(str, str, str, name='okSignal') - def __init__(self, - module: Module, - parent: Optional['QWidget'] = None, - flags: Qt.WindowFlags | Qt.WindowType = Qt.WindowFlags() - ) -> None: + def __init__( + self, + module: Module, + parent: Optional['QWidget'] = None, + flags: Qt.WindowFlags | Qt.WindowType = Qt.WindowFlags(), + ) -> None: super().__init__(parent, flags) self.module = module diff --git a/src/simudator/gui/gui.py b/src/simudator/gui/gui.py index f38cdc8274eadf9f0c73c3fe21b159aeef9582f2..2fd6d85d78d75a2932c0f1a333aa35d39e5cf7f8 100644 --- a/src/simudator/gui/gui.py +++ b/src/simudator/gui/gui.py @@ -42,6 +42,7 @@ class View(QGraphicsView): This allows the users to navigate the view with their trackpads and zoom in/out with their trackpads + ctrl. """ + def __init__(self, QGraphicsScene): super().__init__(QGraphicsScene) self.scene = QGraphicsScene @@ -52,7 +53,6 @@ class View(QGraphicsView): """ modifiers = QtWidgets.QApplication.keyboardModifiers() if modifiers == QtCore.Qt.ControlModifier: - # Factor above 1 zooms in, below zooms out factor = 1.03 if event.angleDelta().y() < 0: @@ -63,10 +63,7 @@ class View(QGraphicsView): return view_pos = event.globalPosition() - scene_pos = self.mapToScene( - int(view_pos.x()), - int(view_pos.y()) - ) + scene_pos = self.mapToScene(int(view_pos.x()), int(view_pos.y())) self.centerOn(scene_pos) self.scale(factor, factor) @@ -79,7 +76,6 @@ class View(QGraphicsView): self.centerOn(scene_pos - delta) else: - # Default behaviour super().wheelEvent(event) @@ -104,7 +100,7 @@ class GUI(QMainWindow): self.setWindowTitle("SimuDator") self.cpu_graphics_scene = CpuGraphicsScene(cpu) self.graphics_view = View(self.cpu_graphics_scene) - #self.graphics_view.setDragMode(True) + # self.graphics_view.setDragMode(True) self.moduleActions: dict[str, QAction] = {} self.setCentralWidget(self.graphics_view) @@ -170,7 +166,9 @@ class GUI(QMainWindow): # create load default layout action load_default_layout_action = QAction("Load default layout", self) load_default_layout_action.setStatusTip("Loads the default layout from file.") - load_default_layout_action.triggered.connect(self.loadDefaultLayoutToolBarButtonClick) + load_default_layout_action.triggered.connect( + self.loadDefaultLayoutToolBarButtonClick + ) # create save layout action save_layout_action = QAction("Save layout", self) @@ -183,13 +181,13 @@ class GUI(QMainWindow): self.lock_layout_action.triggered.connect(self.toggleLayoutLockMenuButtonClick) # Create show signals actions - self.signal_vis_action= QAction("Show signals", self, checkable=True) + self.signal_vis_action = QAction("Show signals", self, checkable=True) self.signal_vis_action.setChecked(True) self.signal_vis_action.setStatusTip("Toggle the visibility of signal.") self.signal_vis_action.triggered.connect(self.showSignalsMenuButtonClick) # Create show port name actions - self.port_vis_action= QAction("Show port names", self, checkable=True) + self.port_vis_action = QAction("Show port names", self, checkable=True) self.port_vis_action.setChecked(True) self.port_vis_action.setStatusTip("Toggle the visibility of port names.") self.port_vis_action.triggered.connect(self.showPortNamesBarButtonClick) @@ -204,7 +202,7 @@ class GUI(QMainWindow): layout_menu.addAction(self.port_vis_action) # Create breakpoint window action - self.breakpoint_action= QAction("Breakpoints", self) + self.breakpoint_action = QAction("Breakpoints", self) self.breakpoint_action.setStatusTip("Open breakpoint window.") self.breakpoint_action.triggered.connect(self.openBreakpointWindow) @@ -228,7 +226,7 @@ class GUI(QMainWindow): # Add undo button on toolbar backward_arrow_icon = self.style().standardIcon(QStyle.SP_MediaSeekBackward) - self.undo_action= QAction(backward_arrow_icon, "Undo", self) + self.undo_action = QAction(backward_arrow_icon, "Undo", self) self.undo_action.setStatusTip("Undo the last processor tick") self.undo_action.triggered.connect(self.undoToolBarButtonClick) toolbar.addAction(self.undo_action) @@ -295,7 +293,6 @@ class GUI(QMainWindow): self.cpu_graphics_scene.updateGraphicsItems() self.clock_label.setText("Clockcycle: " + str(self.cpu.get_clock())) - def lambdaBreakpointDialog(self) -> None: """ Opens dialog window for user to create a breakpoint. @@ -317,6 +314,7 @@ class GUI(QMainWindow): and specify a C++ signature for it, which is used most commonly in order to select a particular overload. """ + @Slot(str, str, str) def editModuleState(self, module_name, state, value) -> None: """ @@ -346,9 +344,9 @@ class GUI(QMainWindow): except SyntaxError as e: self.errorMessageWidget.showMessage(str(e)) except ValueError: - self.errorMessageWidget.showMessage("You must enter a hexadecimal" - "number preceeded by '0x' (e.g." - "0xc3).") + self.errorMessageWidget.showMessage( + "You must enter a hexadecimal" "number preceeded by '0x' (e.g." "0xc3)." + ) else: module = self.cpu.get_module(module_name) module_state = module.get_state() @@ -356,7 +354,6 @@ class GUI(QMainWindow): module.set_state(module_state) self.cpu_graphics_scene.updateGraphicsItems() - @Slot(str, str, str) def addStateBreakpoint(self, module_name: str, state: str, value: str) -> None: """ @@ -425,10 +422,7 @@ class GUI(QMainWindow): self.cpu_running = True self.setDisabledWhenRunning(True) self.cpu.unstop() - simultaion_thread = RunThread(self.cpu, - self.halted_signal, - False, - steps) + simultaion_thread = RunThread(self.cpu, self.halted_signal, False, steps) self.threadpool.start(simultaion_thread) self.updateCpuListeners() @@ -482,7 +476,7 @@ class GUI(QMainWindow): dialog = QFileDialog() dialog.setFileMode(QFileDialog.AnyFile) dialog.setAcceptMode(QFileDialog.AcceptOpen) - dialog.setDirectory("~/simudator") # TODO: does this work when exported? + dialog.setDirectory("~/simudator") # TODO: does this work when exported? # this is static so none of the above matters... return dialog.getSaveFileName()[0] @@ -494,7 +488,7 @@ class GUI(QMainWindow): dialog = QFileDialog() dialog.setFileMode(QFileDialog.AnyFile) dialog.setAcceptMode(QFileDialog.AcceptOpen) - dialog.setDirectory("~/simudator") # TODO: does this work when exported? + dialog.setDirectory("~/simudator") # TODO: does this work when exported? return dialog.getOpenFileName()[0] def loadToolBarButtonClick(self) -> None: @@ -533,8 +527,9 @@ class GUI(QMainWindow): if self.cpu_running: return - answer = QMessageBox.question(self, "Reset Processor", - "Are you sure you want to reset the processor?") + answer = QMessageBox.question( + self, "Reset Processor", "Are you sure you want to reset the processor?" + ) if answer == QMessageBox.Yes: self.cpu.reset() @@ -549,10 +544,11 @@ class GUI(QMainWindow): if self.cpu_running: return - cycles, ok = QInputDialog(self).getInt(self, - "Input number of cycles to run", - "Input number of cycles to run", - ) + cycles, ok = QInputDialog(self).getInt( + self, + "Input number of cycles to run", + "Input number of cycles to run", + ) if ok: if cycles < 1: self.errorBox("Please input a number larger than 0.") @@ -617,9 +613,9 @@ class GUI(QMainWindow): module_graphic = self.cpu_graphics_scene.module_graphics_items[key] module_graphic.showPorts() counter += 1 - self.cpu_graphics_scene.replaceModuleGraphicsItem(module_graphic, - counter, - counter) + self.cpu_graphics_scene.replaceModuleGraphicsItem( + module_graphic, counter, counter + ) self.cpu_graphics_scene.resetSignals() def loadLayoutFromFile(self, file) -> None: @@ -663,11 +659,12 @@ class GUI(QMainWindow): fp.close() - def loadSignal(self, - graphic_signal: SignalGraphicsItem, - signal_points: list[tuple[float, float]], - visibility: bool - ) -> None: + def loadSignal( + self, + graphic_signal: SignalGraphicsItem, + signal_points: list[tuple[float, float]], + visibility: bool, + ) -> None: """ Changes the graphical signal to have the positions given as argument. @@ -682,11 +679,12 @@ class GUI(QMainWindow): graphic_signal.setPoints(qpoints) graphic_signal.setVisible(visibility) - def loadGraphicsModule(self, - graphics_module: ModuleGraphicsItem, - graphics_module_x: float, - graphics_module_y: float, - ) -> None: + def loadGraphicsModule( + self, + graphics_module: ModuleGraphicsItem, + graphics_module_x: float, + graphics_module_y: float, + ) -> None: """ Changes the positions of graphical modules to the ones given as argument. @@ -694,13 +692,14 @@ class GUI(QMainWindow): graphics_module.setX(graphics_module_x) graphics_module.setY(graphics_module_y) - def loadPort(self, - port: PortGraphicsItem, - x: float, - y: float, - orientation: Orientation, - visibility: bool - ) -> None: + def loadPort( + self, + port: PortGraphicsItem, + x: float, + y: float, + orientation: Orientation, + visibility: bool, + ) -> None: port.setOrientation(orientation) port.setX(x) port.setY(y) @@ -736,22 +735,21 @@ class GUI(QMainWindow): """ Toggles showing port names in the graphics scene. """ - self.cpu_graphics_scene.setPortNamesVisibility( - self.port_vis_action.isChecked()) + self.cpu_graphics_scene.setPortNamesVisibility(self.port_vis_action.isChecked()) def showSignalsMenuButtonClick(self): """ Toggle shoing the signals in the graphics scene. """ self.cpu_graphics_scene.setAllSignalsVisibility( - self.signal_vis_action.isChecked()) + self.signal_vis_action.isChecked() + ) def toggleLayoutLockMenuButtonClick(self): """ Toggles so the layout can not be edited. """ - self.cpu_graphics_scene.setLayoutLock( - self.lock_layout_action.isChecked()) + self.cpu_graphics_scene.setLayoutLock(self.lock_layout_action.isChecked()) def loadLayoutToolBarButtonClick(self) -> None: """ @@ -760,7 +758,6 @@ class GUI(QMainWindow): and the default layout will be loaded. """ - path = self.folderLoadDialog() # If no file was selected, do nothing diff --git a/src/simudator/gui/module_graphics_item/memory_graphic.py b/src/simudator/gui/module_graphics_item/memory_graphic.py index 86c04c57157fd8c50567341b556c5b88c5db6c55..f888b0b77a26345317e5908ad7390a7a17cae1ac 100644 --- a/src/simudator/gui/module_graphics_item/memory_graphic.py +++ b/src/simudator/gui/module_graphics_item/memory_graphic.py @@ -22,7 +22,7 @@ class MemoryWindow(QWidget): Widget for showing content of memory """ - ROW_LENGTH = 5 + _ROW_LENGTH = 5 def __init__(self, memory_module: Memory): super().__init__() @@ -38,13 +38,12 @@ class MemoryWindow(QWidget): def update(self): memory_str = "" for adress, value in enumerate(self.module.memory): - # Add adress and content to string # Make sure its unifrom lenght so rows are consistent memory_str += f"{adress}" + ": " + f"{value}" + " " + "\t" # Make new line when we reach end of row - if adress % self.ROW_LENGTH == self.ROW_LENGTH-1: + if adress % self._ROW_LENGTH == self._ROW_LENGTH - 1: memory_str += "\n" self.text.setText(memory_str) @@ -55,48 +54,50 @@ class MemoryGraphicsItem(ModuleGraphicsItem): Graphics module for a Memory module. """ - RECT_WIDTH = 150 - RECT_HEIGHT= 200 - new_memory_breakpoint_signal = pyqtSignal(str, str, str) - def __init__(self, memory_module: Memory): + def __init__(self, memory_module: Memory, width: int = 150, height: int = 200): + self._width = width + self._height = height super().__init__(memory_module) self.memory_window = None def draw_graphics_item(self) -> None: - # Create base backgrond rect - self.baserect = QGraphicsRectItem(0, 0, self.RECT_WIDTH, self.RECT_HEIGHT, self) + self.baserect = QGraphicsRectItem(0, 0, self._width, self._height, self) # Make ports - self.input = PortGraphicsItem(self.module.get_input_signal(), - Orientation.RIGHT, self) - self.input.setPos(self.RECT_WIDTH, self.RECT_HEIGHT/8) - - self.output = PortGraphicsItem(self.module.get_output_signal(), - Orientation.RIGHT, self) - self.output.setPos(self.RECT_WIDTH, self.RECT_HEIGHT/4) - - self.adress = PortGraphicsItem(self.module.get_adress_signal(), - Orientation.DOWN, self) - self.adress.setPos(self.RECT_WIDTH/4, self.RECT_HEIGHT) - - self.control = PortGraphicsItem(self.module.get_control_signal(), - Orientation.DOWN, self) - self.control.setPos(self.RECT_WIDTH*3/4, self.RECT_HEIGHT) + self.input = PortGraphicsItem( + self.module.get_input_signal(), Orientation.RIGHT, self + ) + self.input.setPos(self._width, self._height / 8) + + self.output = PortGraphicsItem( + self.module.get_output_signal(), Orientation.RIGHT, self + ) + self.output.setPos(self._width, self._height / 4) + + self.adress = PortGraphicsItem( + self.module.get_adress_signal(), Orientation.DOWN, self + ) + self.adress.setPos(self._width / 4, self._height) + + self.control = PortGraphicsItem( + self.module.get_control_signal(), Orientation.DOWN, self + ) + self.control.setPos(self._width * 3 / 4, self._height) self.ports = [self.input, self.output, self.adress, self.control] # Create name lable name_text = QGraphicsSimpleTextItem(self.state["name"], self) - name_text.setPos(self.RECT_WIDTH/2 - - len(self.state["name"])*self.CHAR_LEN, 0) + name_text.setPos(self._width / 2 - len(self.state["name"]) * self._CHAR_LEN, 0) # Create adress lable - self.adress_text = QGraphicsSimpleTextItem("adress: " - + str(self.state["current_adress"]), self) - self.adress_text.setPos(self.RECT_WIDTH/20, self.RECT_HEIGHT/8) + self.adress_text = QGraphicsSimpleTextItem( + "adress: " + str(self.state["current_adress"]), self + ) + self.adress_text.setPos(self._width / 20, self._height / 8) def update(self): """ @@ -107,7 +108,6 @@ class MemoryGraphicsItem(ModuleGraphicsItem): if self.memory_window is not None: self.memory_window.update() - def mousePressEvent(self, event): """ Show content window if module is right clicked @@ -163,13 +163,13 @@ class MemoryGraphicsItem(ModuleGraphicsItem): """ Opens dialog window for user to edit memory. """ - self.memory_edit_dialog= MemoryContentDialog(self.module) + self.memory_edit_dialog = MemoryContentDialog(self.module) self.memory_edit_dialog.accepted.connect(self.editMemoryAccepted) @Slot(str, str, str) - def memoryBreakpointAccepted(self, module_name: str, - adress: str, - value: str) -> None: + def memoryBreakpointAccepted( + self, module_name: str, adress: str, value: str + ) -> None: """ Takes the info from a breakpoint dialog and sends it to the gui """ diff --git a/src/simudator/gui/module_graphics_item/mia/__init__.py b/src/simudator/gui/module_graphics_item/mia/__init__.py index ae9bfec0ceaec5bedf85da4325457e04e66ad9b4..23fa55a545e67ba7cb5923817debab54d9ae5929 100644 --- a/src/simudator/gui/module_graphics_item/mia/__init__.py +++ b/src/simudator/gui/module_graphics_item/mia/__init__.py @@ -16,8 +16,18 @@ from simudator.gui.module_graphics_item.mia.pc_graphic import PcGraphicsItem from simudator.gui.module_graphics_item.mia.supc_graphic import SupcGraphicsItem from simudator.gui.module_graphics_item.mia.upc_graphic import uPcGraphicsItem -__all__ = ["MiaMemoryGraphicsItem", "MicroMemoryGraphicsItem", "ArGraphicsItem", - "PcGraphicsItem", "AsrGraphicsItem", "HrGraphicsItem", - "SupcGraphicsItem", "uPcGraphicsItem", "FlagGraphicsItem", - "BusGraphicsItem", "AluGraphicsItem", "GrxGraphicsItem", "IrGraphicsItem" - ] +__all__ = [ + "MiaMemoryGraphicsItem", + "MicroMemoryGraphicsItem", + "ArGraphicsItem", + "PcGraphicsItem", + "AsrGraphicsItem", + "HrGraphicsItem", + "SupcGraphicsItem", + "uPcGraphicsItem", + "FlagGraphicsItem", + "BusGraphicsItem", + "AluGraphicsItem", + "GrxGraphicsItem", + "IrGraphicsItem", +] diff --git a/src/simudator/gui/module_graphics_item/mia/ar_graphic.py b/src/simudator/gui/module_graphics_item/mia/ar_graphic.py index 2c7c9deb982292970beef4edf1412abc7bb3f46e..06dcdb07c61c9aa0efd98e98ce8eadd0ef6e6569 100644 --- a/src/simudator/gui/module_graphics_item/mia/ar_graphic.py +++ b/src/simudator/gui/module_graphics_item/mia/ar_graphic.py @@ -13,23 +13,25 @@ class ArGraphicsItem(IntegerRegisterGraphicsItem): def draw_graphics_item(self): self.draw_rect() - width = len(self.text.text())*self.RECT_WIDTH_PER_CHAR + self.TEXT_WIDTH_MARGIN + width = ( + len(self.text.text()) * self._RECT_WIDTH_PER_CHAR + self._TEXT_WIDTH_MARGIN + ) # Draw bus port - bus_port = PortGraphicsItem(self.module.output_s, parent = self) - bus_port.setPos( - width, - self.RECT_HEIGHT/2) + bus_port = PortGraphicsItem(self.module.output_s, parent=self) + bus_port.setPos(width, self._RECT_HEIGHT / 2) self.ports.append(bus_port) # Draw to alu port - to_alu_port = PortGraphicsItem(self.module.alu_output_signal, - Orientation.LEFT, parent = self) - to_alu_port.setPos(0, self.RECT_HEIGHT/2) + to_alu_port = PortGraphicsItem( + self.module.alu_output_signal, Orientation.LEFT, parent=self + ) + to_alu_port.setPos(0, self._RECT_HEIGHT / 2) self.ports.append(to_alu_port) # Draw from alu port - from_alu_port = PortGraphicsItem(self.module.input_s, - Orientation.UP, parent = self) - from_alu_port.setPos(width/2, 0) + from_alu_port = PortGraphicsItem( + self.module.input_s, Orientation.UP, parent=self + ) + from_alu_port.setPos(width / 2, 0) self.ports.append(from_alu_port) diff --git a/src/simudator/gui/module_graphics_item/mia/asr_graphic.py b/src/simudator/gui/module_graphics_item/mia/asr_graphic.py index 13c6c152737cc80c6509093197d7a3c4f23f61cd..752643f1e7d83340e8531d8260c210aaff5dce34 100644 --- a/src/simudator/gui/module_graphics_item/mia/asr_graphic.py +++ b/src/simudator/gui/module_graphics_item/mia/asr_graphic.py @@ -11,21 +11,21 @@ class AsrGraphicsItem(IntegerRegisterGraphicsItem): """ def draw_graphics_item(self): - # Draw register rect self.draw_rect() - width = len(self.text.text())*self.RECT_WIDTH_PER_CHAR + self.TEXT_WIDTH_MARGIN + width = ( + len(self.text.text()) * self._RECT_WIDTH_PER_CHAR + self._TEXT_WIDTH_MARGIN + ) # Draw bus port - bus_port = PortGraphicsItem(self.module.input_s, parent = self) - bus_port.setPos( - width, - self.RECT_HEIGHT/2) + bus_port = PortGraphicsItem(self.module.input_s, parent=self) + bus_port.setPos(width, self._RECT_HEIGHT / 2) self.ports.append(bus_port) # Draw to memory port - to_memory_port = PortGraphicsItem(self.module.output_s, - Orientation.LEFT, parent = self) - to_memory_port.setPos(0, self.RECT_HEIGHT/2) + to_memory_port = PortGraphicsItem( + self.module.output_s, Orientation.LEFT, parent=self + ) + to_memory_port.setPos(0, self._RECT_HEIGHT / 2) self.ports.append(to_memory_port) diff --git a/src/simudator/gui/module_graphics_item/mia/bus_graphic.py b/src/simudator/gui/module_graphics_item/mia/bus_graphic.py index 036a309033366cff69da94b462f648d87e5fc53c..476a14d19eab26fe06b80b2997fd9df0e19f0de8 100644 --- a/src/simudator/gui/module_graphics_item/mia/bus_graphic.py +++ b/src/simudator/gui/module_graphics_item/mia/bus_graphic.py @@ -20,15 +20,15 @@ class BusGraphicsItem(ModuleGraphicsItem): PAIR_MARGIN = 60 PORT_MARGIN = 20 - def __init__(self, module: Module, signal_pairs: [(Signal)] ): + def __init__(self, module: Module, signal_pairs: [(Signal)]): self.signal_pairs = signal_pairs super().__init__(module) def draw_graphics_item(self) -> None: - # Calculate height according to number of signals and states - height = (math.ceil(len(self.signal_pairs)/2) - )*(self.PAIR_MARGIN+self.PORT_MARGIN)-self.PAIR_MARGIN + height = (math.ceil(len(self.signal_pairs) / 2)) * ( + self.PAIR_MARGIN + self.PORT_MARGIN + ) - self.PAIR_MARGIN # Create base background rectangle self.baserect = QGraphicsRectItem(0, 0, self.WIDTH, height, self) @@ -36,7 +36,6 @@ class BusGraphicsItem(ModuleGraphicsItem): # add port pairs for index, signals in enumerate(self.signal_pairs): - # x coordinates and orientation will be # diffrent for every other signal pair x_pos = self.WIDTH @@ -50,16 +49,18 @@ class BusGraphicsItem(ModuleGraphicsItem): output_port = PortGraphicsItem(signals[0], orientation, self) output_port.setPos( - x_pos, - (index//2)*(self.PAIR_MARGIN+self.PORT_MARGIN)) + x_pos, (index // 2) * (self.PAIR_MARGIN + self.PORT_MARGIN) + ) self.ports.append(output_port) if signals[1] is not None: input_port = PortGraphicsItem(signals[1], orientation, self) input_port.setPos( - x_pos, - (index//2)*(self.PAIR_MARGIN+self.PORT_MARGIN)+self.PORT_MARGIN) + x_pos, + (index // 2) * (self.PAIR_MARGIN + self.PORT_MARGIN) + + self.PORT_MARGIN, + ) self.ports.append(input_port) def update(self): diff --git a/src/simudator/gui/module_graphics_item/mia/hr_graphic.py b/src/simudator/gui/module_graphics_item/mia/hr_graphic.py index 1ca6e3684a1a37b982c4e8e6866cd07eefe9825c..5b3331194bcd799b08339e9789dac56c1511263e 100644 --- a/src/simudator/gui/module_graphics_item/mia/hr_graphic.py +++ b/src/simudator/gui/module_graphics_item/mia/hr_graphic.py @@ -12,18 +12,16 @@ class HrGraphicsItem(IntegerRegisterGraphicsItem): def draw_graphics_item(self): self.draw_rect() - width = len(self.text.text())*self.RECT_WIDTH_PER_CHAR + self.TEXT_WIDTH_MARGIN + width = ( + len(self.text.text()) * self._RECT_WIDTH_PER_CHAR + self._TEXT_WIDTH_MARGIN + ) # Draw to bus port - to_bus_port = PortGraphicsItem(self.module.input_s, parent = self) - to_bus_port.setPos( - width, - self.RECT_HEIGHT/4) + to_bus_port = PortGraphicsItem(self.module.input_s, parent=self) + to_bus_port.setPos(width, self._RECT_HEIGHT / 4) self.ports.append(to_bus_port) # Draw from bus port - from_bus_port = PortGraphicsItem(self.module.output_s, parent = self) - from_bus_port.setPos( - width, - self.RECT_HEIGHT*3/4) + from_bus_port = PortGraphicsItem(self.module.output_s, parent=self) + from_bus_port.setPos(width, self._RECT_HEIGHT * 3 / 4) self.ports.append(from_bus_port) diff --git a/src/simudator/gui/module_graphics_item/mia/mia_alu_graphic.py b/src/simudator/gui/module_graphics_item/mia/mia_alu_graphic.py index 3fb8cf17925878ccf32f75f6ac4de36bf311b727..3bcd78a49acde6c541745bc96fc2e65b64373f22 100644 --- a/src/simudator/gui/module_graphics_item/mia/mia_alu_graphic.py +++ b/src/simudator/gui/module_graphics_item/mia/mia_alu_graphic.py @@ -9,33 +9,36 @@ class AluGraphicsItem(ModuleGraphicsItem): """ Graphics module for mia's ALU module. """ + RECT_WIDTH = 120 RECT_HEIGHT = 60 def draw_graphics_item(self): - # Base rectangle self.baserect = QGraphicsRectItem(0, 0, self.RECT_WIDTH, self.RECT_HEIGHT, self) # Create name lable name_text = QGraphicsSimpleTextItem(self.state["name"], self) - name_text.setPos(self.RECT_WIDTH/2-len(self.state["name"])*self.CHAR_LEN, - self.RECT_HEIGHT/2-len(self.state["name"])*self.CHAR_LEN) + name_text.setPos( + self.RECT_WIDTH / 2 - len(self.state["name"]) * self._CHAR_LEN, + self.RECT_HEIGHT / 2 - len(self.state["name"]) * self._CHAR_LEN, + ) # make port for input a port_a = PortGraphicsItem(self.module.input_signal_a, Orientation.UP, self) - port_a.setPos(self.RECT_WIDTH*3/4, 0) + port_a.setPos(self.RECT_WIDTH * 3 / 4, 0) self.ports.append(port_a) # make port for input b port_b = PortGraphicsItem(self.module.input_signal_b, Orientation.UP, self) - port_b.setPos(self.RECT_WIDTH/4, 0) + port_b.setPos(self.RECT_WIDTH / 4, 0) self.ports.append(port_b) # make port for output - output_port = PortGraphicsItem(self.module.output_signal, - Orientation.DOWN, self) - output_port.setPos(self.RECT_WIDTH/2, self.RECT_HEIGHT) + output_port = PortGraphicsItem( + self.module.output_signal, Orientation.DOWN, self + ) + output_port.setPos(self.RECT_WIDTH / 2, self.RECT_HEIGHT) self.ports.append(output_port) def shouldIgnoreAction(self, action: QAction) -> bool: diff --git a/src/simudator/gui/module_graphics_item/mia/mia_flag_graphic.py b/src/simudator/gui/module_graphics_item/mia/mia_flag_graphic.py index ee9563ff3f241a22b89527473f23875d4d3fb105..33f6c022d8b70fe6f71c7cd114fd9e4eb0cb151d 100644 --- a/src/simudator/gui/module_graphics_item/mia/mia_flag_graphic.py +++ b/src/simudator/gui/module_graphics_item/mia/mia_flag_graphic.py @@ -8,6 +8,7 @@ class FlagGraphicsItem(IntegerRegisterGraphicsItem): """ Graphics module for mia's flag module. """ + def __init__(self, module: IntegerRegister): super().__init__(module) diff --git a/src/simudator/gui/module_graphics_item/mia/mia_grx_graphic.py b/src/simudator/gui/module_graphics_item/mia/mia_grx_graphic.py index 7dadecb974facb3f034fa8ca176cecf6bfacf87c..3b4bbf9027ac0f904f2446d2e25f44bbd4c70e7b 100644 --- a/src/simudator/gui/module_graphics_item/mia/mia_grx_graphic.py +++ b/src/simudator/gui/module_graphics_item/mia/mia_grx_graphic.py @@ -34,7 +34,6 @@ class GrxGraphicsItem(ModuleGraphicsItem): super().__init__(moudle) def draw_graphics_item(self): - # The width of the register # will be changed to fit text in registers but we # need it here to get the position @@ -42,96 +41,101 @@ class GrxGraphicsItem(ModuleGraphicsItem): # Draw each register for index, register_value in enumerate(self.module.registers): - # Make text to display for register name = "Gr" + str(index) - hex_length = math.ceil(self.state["bit_length"]/4) + hex_length = math.ceil(self.state["bit_length"] / 4) value_text = f"0x{register_value:0{hex_length}x}" full_text = name + ": " + value_text[2:] # Make text label with text text = QGraphicsSimpleTextItem(full_text, self) - text.setPos(self.TEXT_WIDTH_MARGIN, - self.TEXT_HEIGHT_MARGIN + - index*(self.REGISTER_MARGIN + self.RECT_HEIGHT)) + text.setPos( + self.TEXT_WIDTH_MARGIN, + self.TEXT_HEIGHT_MARGIN + + index * (self.REGISTER_MARGIN + self.RECT_HEIGHT), + ) self.register_text_labels.append(text) - rect_width = (len(full_text)*self.RECT_WIDTH_PER_CHAR - + self.TEXT_WIDTH_MARGIN) + rect_width = ( + len(full_text) * self.RECT_WIDTH_PER_CHAR + self.TEXT_WIDTH_MARGIN + ) # Make rect that fits text QGraphicsRectItem( - - # rect posisition - 0, - index*(self.REGISTER_MARGIN + self.RECT_HEIGHT), - - # rect size - rect_width, - self.RECT_HEIGHT, self) + # rect posisition + 0, + index * (self.REGISTER_MARGIN + self.RECT_HEIGHT), + # rect size + rect_width, + self.RECT_HEIGHT, + self, + ) # Add line to mux part, is purely cosmetical QGraphicsLineItem( - - # set first point pos - rect_width, - index*(self.REGISTER_MARGIN + self.RECT_HEIGHT)+self.RECT_HEIGHT/2, - - # set other point pos - rect_width+self.LINE_LENGTH, - index*(self.REGISTER_MARGIN + self.RECT_HEIGHT)+self.RECT_HEIGHT/2, - self) + # set first point pos + rect_width, + index * (self.REGISTER_MARGIN + self.RECT_HEIGHT) + + self.RECT_HEIGHT / 2, + # set other point pos + rect_width + self.LINE_LENGTH, + index * (self.REGISTER_MARGIN + self.RECT_HEIGHT) + + self.RECT_HEIGHT / 2, + self, + ) # Make mux polygon, is also purely cosmetical - mux_height = (len(self.register_text_labels)*self.RECT_HEIGHT + - (len(self.register_text_labels)-1)*self.REGISTER_MARGIN) - - mux = QGraphicsPolygonItem(QPolygonF( - - # top left corner - [QPointF( - 0, - 0), - - # bottom left corner - QPointF( - 0, - mux_height), - - # bottom right corner - QPointF( - self.MUX_WIDTH, - mux_height-self.RECT_HEIGHT/2), - - # top right corner - QPointF( - self.MUX_WIDTH, - self.RECT_HEIGHT/2)]), self) + mux_height = ( + len(self.register_text_labels) * self.RECT_HEIGHT + + (len(self.register_text_labels) - 1) * self.REGISTER_MARGIN + ) + + mux = QGraphicsPolygonItem( + QPolygonF( + # top left corner + [ + QPointF(0, 0), + # bottom left corner + QPointF(0, mux_height), + # bottom right corner + QPointF(self.MUX_WIDTH, mux_height - self.RECT_HEIGHT / 2), + # top right corner + QPointF(self.MUX_WIDTH, self.RECT_HEIGHT / 2), + ] + ), + self, + ) # Move mux into position - mux.setPos(rect_width+self.LINE_LENGTH, 0) + mux.setPos(rect_width + self.LINE_LENGTH, 0) # Have a invisible rect for ports bounding box self.baserect = QGraphicsRectItem(0, 0, self.MUX_WIDTH, mux_height) - self.baserect.setPos(rect_width+self.LINE_LENGTH, 0) + self.baserect.setPos(rect_width + self.LINE_LENGTH, 0) self.baserect.setVisible(False) # Add mux label name = "MUX" mux_text = QGraphicsSimpleTextItem(name, self) - mux_text.setPos(rect_width+self.MUX_WIDTH/2+len(name)*self.RECT_WIDTH_PER_CHAR/2, - mux_height/2-self.CHAR_HEIGHT) + mux_text.setPos( + rect_width + self.MUX_WIDTH / 2 + len(name) * self.RECT_WIDTH_PER_CHAR / 2, + mux_height / 2 - self._CHAR_HEIGHT, + ) # Add ports to and from bus from_bus_port = PortGraphicsItem(self.module.from_bus, Orientation.RIGHT, self) - from_bus_port.setPos(rect_width+self.LINE_LENGTH+self.MUX_WIDTH, - # Use the buses port margins so the ports align nicely - mux_height/2-BusGraphicsItem.PORT_MARGIN/2) + from_bus_port.setPos( + rect_width + self.LINE_LENGTH + self.MUX_WIDTH, + # Use the buses port margins so the ports align nicely + mux_height / 2 - BusGraphicsItem.PORT_MARGIN / 2, + ) to_bus_port = PortGraphicsItem(self.module.to_bus, Orientation.RIGHT, self) - to_bus_port.setPos(rect_width+self.LINE_LENGTH+self.MUX_WIDTH, - # Use the buses port margins so the ports align nicely - mux_height/2+BusGraphicsItem.PORT_MARGIN/2) + to_bus_port.setPos( + rect_width + self.LINE_LENGTH + self.MUX_WIDTH, + # Use the buses port margins so the ports align nicely + mux_height / 2 + BusGraphicsItem.PORT_MARGIN / 2, + ) self.ports.append(from_bus_port) self.ports.append(to_bus_port) @@ -141,7 +145,7 @@ class GrxGraphicsItem(ModuleGraphicsItem): for index, register_value in enumerate(self.module.registers): text = self.register_text_labels[index] name = "Gr" + str(index) - hex_length = math.ceil(self.state["bit_length"]/4) + hex_length = math.ceil(self.state["bit_length"] / 4) value_text = f"0x{register_value:0{hex_length}x}" full_text = name + ": " + value_text[2:] diff --git a/src/simudator/gui/module_graphics_item/mia/mia_ir_graphic.py b/src/simudator/gui/module_graphics_item/mia/mia_ir_graphic.py index f232639815e3bc8b5f36ee57b16455ba62e93fdc..2616d856d5e8bd245ac3d92a5c8a445c97e4158c 100644 --- a/src/simudator/gui/module_graphics_item/mia/mia_ir_graphic.py +++ b/src/simudator/gui/module_graphics_item/mia/mia_ir_graphic.py @@ -15,7 +15,7 @@ class IrGraphicsItem(ModuleGraphicsItem): SMALL_RECT_WIDTH = 70 BIG_RECT_HEIGHT = 60 - BIG_RECT_WIDTH = 180 + BIG_RECT_WIDTH = 180 TEXT_WIDTH_MARGIN = 5 TEXT_HEIGHT_MARGIN = 8 @@ -26,31 +26,27 @@ class IrGraphicsItem(ModuleGraphicsItem): self.draw_ports() def draw_graphics_item(self): - # Add small rect - QGraphicsRectItem(0, 0, - self.SMALL_RECT_WIDTH, - self.SMALL_RECT_HEIGHT, self) + QGraphicsRectItem(0, 0, self.SMALL_RECT_WIDTH, self.SMALL_RECT_HEIGHT, self) # Add text for small rect self.small_text = QGraphicsSimpleTextItem("", self) - self.small_text.setPos(self.TEXT_WIDTH_MARGIN, - self.TEXT_HEIGHT_MARGIN) + self.small_text.setPos(self.TEXT_WIDTH_MARGIN, self.TEXT_HEIGHT_MARGIN) # Add big rect - self.baserect = QGraphicsRectItem(0, self.SMALL_RECT_HEIGHT, - self.BIG_RECT_WIDTH, - self.BIG_RECT_HEIGHT, self) + self.baserect = QGraphicsRectItem( + 0, self.SMALL_RECT_HEIGHT, self.BIG_RECT_WIDTH, self.BIG_RECT_HEIGHT, self + ) # Add text verbose text for big rect self.big_text = QGraphicsSimpleTextItem("", self) - self.big_text.setPos(self.TEXT_WIDTH_MARGIN, - self.SMALL_RECT_HEIGHT + self.BIG_TEXT_HEIGHT_MARGIN) + self.big_text.setPos( + self.TEXT_WIDTH_MARGIN, self.SMALL_RECT_HEIGHT + self.BIG_TEXT_HEIGHT_MARGIN + ) self.update() def update(self): - # Update small text instr = self.module.instruction self.small_text.setText(f"IR: {instr:04x}") @@ -61,14 +57,16 @@ class IrGraphicsItem(ModuleGraphicsItem): op_field = instr >> 12 grx_field = (instr >> 10) & 0b11 m_field = (instr >> 8) & 0b11 - adr_field = instr & (2**8-1) - - instr_text = ("op: " + - "grx: " + - "m: " + - "adr:\n" + - f"{op_field:04b} " + - f"{grx_field:02b} " + - f"{m_field:02b} " + - f"{adr_field:08b}") + adr_field = instr & (2**8 - 1) + + instr_text = ( + "op: " + + "grx: " + + "m: " + + "adr:\n" + + f"{op_field:04b} " + + f"{grx_field:02b} " + + f"{m_field:02b} " + + f"{adr_field:08b}" + ) self.big_text.setText(instr_text) diff --git a/src/simudator/gui/module_graphics_item/mia/mia_memory_content_dialog.py b/src/simudator/gui/module_graphics_item/mia/mia_memory_content_dialog.py index 4b7bf2f41a643e05a102ebf4bb252087227c8aa7..28d7f88d6b10462bce19f509dc4adbaedcdb7aab 100644 --- a/src/simudator/gui/module_graphics_item/mia/mia_memory_content_dialog.py +++ b/src/simudator/gui/module_graphics_item/mia/mia_memory_content_dialog.py @@ -20,6 +20,7 @@ class HexValidator(QValidator): A QValidator for constricting user input to a hex number that lies within a specified (exclusive) range. """ + def __init__(self, bottom: int, top: int) -> None: super().__init__(None) self.bottom = bottom @@ -51,15 +52,16 @@ class HexValidator(QValidator): class MiaMemoryContentDialog(QDialog): accepted = pyqtSignal(str, str, str, name="accepted") - def __init__(self, - memory: Module, - parent: Optional['QWidget'] = None, - flags: Qt.WindowFlags | Qt.WindowType = Qt.WindowFlags() - ) -> None: + def __init__( + self, + memory: Module, + parent: Optional['QWidget'] = None, + flags: Qt.WindowFlags | Qt.WindowType = Qt.WindowFlags(), + ) -> None: super().__init__(parent, flags) self.memory = memory - self.memory_name =memory.get_state()['name'] + self.memory_name = memory.get_state()['name'] adresses = [hex(i) for i in range(len(memory.get_state()['memory']))] adressValidator = HexValidator(0, len(adresses)) @@ -93,7 +95,6 @@ class MiaMemoryContentDialog(QDialog): value = memory_content[selectedAdress] self.valuesWidget.setText(hex(value)) - def signalAccepted(self) -> None: adress = self.adressSelectWidget.currentText() enteredValues = self.valuesWidget.text() diff --git a/src/simudator/gui/module_graphics_item/mia/mia_memory_graphic.py b/src/simudator/gui/module_graphics_item/mia/mia_memory_graphic.py index d11b96320501071876347c963688ebf6024a920c..759df324b6453fa63202c789990cf1aa65b31a19 100644 --- a/src/simudator/gui/module_graphics_item/mia/mia_memory_graphic.py +++ b/src/simudator/gui/module_graphics_item/mia/mia_memory_graphic.py @@ -40,13 +40,12 @@ class MiaMemoryWindow(QWidget): def update(self): memory_str = "" for adress, value in enumerate(self.module.memory): - # Add adress and content to string # Make sure its unifrom lenght so rows are consistent memory_str += f"0x{adress:02x}" + ": " + f"0x{value:04x}" + " " + "\t" # Make new line when we reach end of row - if adress % self.ROW_LENGTH == self.ROW_LENGTH-1: + if adress % self.ROW_LENGTH == self.ROW_LENGTH - 1: memory_str += "\n" self.text.setText(memory_str) @@ -57,45 +56,45 @@ class MiaMemoryGraphicsItem(MemoryGraphicsItem): Graphics module for a Memory module. """ - RECT_WIDTH = 150 - RECT_HEIGHT= 200 - - def __init__(self, memory_module: Memory): - super().__init__(memory_module) + def __init__(self, memory_module: Memory, **kwargs): + super().__init__(memory_module, **kwargs) self.memory_window = None def draw_graphics_item(self) -> None: # Same as normal memory but no control signal # Create base backgrond rect - self.baserect = QGraphicsRectItem(0, 0, self.RECT_WIDTH, self.RECT_HEIGHT, self) + self.baserect = QGraphicsRectItem(0, 0, self._width, self._height, self) self.baserect.setPen(CS.ModuleOutline) self.baserect.setBrush(CS.ModuleBackground) # Make ports - self.input = PortGraphicsItem(self.module.get_input_signal(), - Orientation.RIGHT, self) - self.input.setPos(self.RECT_WIDTH, self.RECT_HEIGHT/8) + self.input = PortGraphicsItem( + self.module.get_input_signal(), Orientation.RIGHT, self + ) + self.input.setPos(self._width, self._height / 8) - self.output = PortGraphicsItem(self.module.get_output_signal(), - Orientation.RIGHT, self) - self.output.setPos(self.RECT_WIDTH, self.RECT_HEIGHT/4) + self.output = PortGraphicsItem( + self.module.get_output_signal(), Orientation.RIGHT, self + ) + self.output.setPos(self._width, self._height / 4) - self.adress = PortGraphicsItem(self.module.get_adress_signal(), - Orientation.DOWN, self) - self.adress.setPos(self.RECT_WIDTH/4, self.RECT_HEIGHT) + self.adress = PortGraphicsItem( + self.module.get_adress_signal(), Orientation.DOWN, self + ) + self.adress.setPos(self._width / 4, self._height) self.ports = [self.input, self.output, self.adress] # Create name lable name_text = QGraphicsSimpleTextItem(self.state["name"], self) - name_text.setPos(self.RECT_WIDTH/2 - - len(self.state["name"])*self.CHAR_LEN, 0) + name_text.setPos(self._width / 2 - len(self.state["name"]) * self._CHAR_LEN, 0) # Create adress lable - self.adress_text = QGraphicsSimpleTextItem("adress: " - + str(self.state["current_adress"]), self) - self.adress_text.setPos(self.RECT_WIDTH/20, self.RECT_HEIGHT/8) + self.adress_text = QGraphicsSimpleTextItem( + "adress: " + str(self.state["current_adress"]), self + ) + self.adress_text.setPos(self._width / 20, self._height / 8) def showMemoryContents(self) -> None: """ @@ -113,24 +112,24 @@ class MiaMemoryGraphicsItem(MemoryGraphicsItem): self.memory_br_dialog.accepted.connect(self.memoryBreakpointAccepted) @Slot(str, str, str) - def memoryBreakpointAccepted(self, module_name: str, - adress: str, - value: str) -> None: + def memoryBreakpointAccepted( + self, module_name: str, adress: str, value: str + ) -> None: """ Same as prent function but also prases data so it is hexadecimal. """ try: parsed_adress = int(adress, 16) parsed_value = ast.literal_eval(value) - self.new_memory_breakpoint_signal.emit(module_name, - str(parsed_adress), - str(parsed_value)) + self.new_memory_breakpoint_signal.emit( + module_name, str(parsed_adress), str(parsed_value) + ) except SyntaxError as e: self.errorMessageWidget.showMessage(str(e)) except ValueError: - self.errorMessageWidget.showMessage("You must enter a hexadecimal" - "number preceeded by '0x' (e.g." - "0xc3).") + self.errorMessageWidget.showMessage( + "You must enter a hexadecimal" "number preceeded by '0x' (e.g." "0xc3)." + ) @Slot(str, str, str) def editMemoryAccepted(self, module_name: str, adress: str, value: str) -> None: @@ -143,9 +142,9 @@ class MiaMemoryGraphicsItem(MemoryGraphicsItem): except SyntaxError as e: self.errorMessageWidget.showMessage(str(e)) except ValueError: - self.errorMessageWidget.showMessage("You must enter a hexadecimal" - "number preceeded by '0x' (e.g." - "0xc3).") + self.errorMessageWidget.showMessage( + "You must enter a hexadecimal" "number preceeded by '0x' (e.g." "0xc3)." + ) else: module_state = self.module.get_state() module_state['memory'][parsed_adress] = parsed_value diff --git a/src/simudator/gui/module_graphics_item/mia/mia_micro_memory_graphic.py b/src/simudator/gui/module_graphics_item/mia/mia_micro_memory_graphic.py index 060b978edcab09f3280ce935c3d52c489b9e6028..d0b4402f5bc30443770444677a3d47cb2e6c48dd 100644 --- a/src/simudator/gui/module_graphics_item/mia/mia_micro_memory_graphic.py +++ b/src/simudator/gui/module_graphics_item/mia/mia_micro_memory_graphic.py @@ -13,33 +13,29 @@ class MicroMemoryGraphicsItem(MiaMemoryGraphicsItem): Graphics module for mia's micro memory module. """ - RECT_WIDTH = 280 - RECT_HEIGHT= 50 - def __init__(self, memory_module: MicroMemory): - super().__init__(memory_module) + super().__init__(memory_module, width=280, height=50) self.update() def draw_graphics_item(self): - # Create base backgrond rect - self.baserect = QGraphicsRectItem(0, 0, self.RECT_WIDTH, self.RECT_HEIGHT, self) + self.baserect = QGraphicsRectItem(0, 0, self._width, self._height, self) # Create name lable name_text = QGraphicsSimpleTextItem(self.state["name"], self) - name_text.setPos(self.RECT_WIDTH/2-len(self.state["name"])*self.CHAR_LEN, 0) + name_text.setPos(self._width / 2 - len(self.state["name"]) * self._CHAR_LEN, 0) self.instr_label = QGraphicsSimpleTextItem("", self) - self.instr_label.setPos(self.RECT_WIDTH/20, self.RECT_HEIGHT/3) + self.instr_label.setPos(self._width / 20, self._height / 3) # Create ports - self.input = PortGraphicsItem(self.module.get_uPC_signal(), - Orientation.LEFT, self) - self.input.setPos(0, self.RECT_HEIGHT/2) + self.input = PortGraphicsItem( + self.module.get_uPC_signal(), Orientation.LEFT, self + ) + self.input.setPos(0, self._height / 2) self.ports.append(self.input) def update(self): - # get instruction field current_instr = self.module.curr_instr instr = self.module.memory[current_instr] @@ -60,22 +56,24 @@ class MicroMemoryGraphicsItem(MiaMemoryGraphicsItem): # The sapcing is done with spaces here # but could probably be done in a better way - instr_text = ("alu: " + - "tb: " + - "fb: " + - "s: " + - "p: " + - "lc: " + - "seq: " + - "uadr:\n" + - f"{alu_field:04b} " + - f"{tb_field:03b} " + - f"{fb_field:03b} " + - f"{s_field:01b} " + - f"{p_field:01b} " + - f"{lc_field:02b} " + - f"{seq_field:04b} " + - f"{uadr_field:07b} ") + instr_text = ( + "alu: " + + "tb: " + + "fb: " + + "s: " + + "p: " + + "lc: " + + "seq: " + + "uadr:\n" + + f"{alu_field:04b} " + + f"{tb_field:03b} " + + f"{fb_field:03b} " + + f"{s_field:01b} " + + f"{p_field:01b} " + + f"{lc_field:02b} " + + f"{seq_field:04b} " + + f"{uadr_field:07b} " + ) self.instr_label.setText(instr_text) diff --git a/src/simudator/gui/module_graphics_item/mia/pc_graphic.py b/src/simudator/gui/module_graphics_item/mia/pc_graphic.py index 534dc332c44aa536f3e844f9b734d1684b5cdb61..498de090b8af31e97563f590d28c193658d91440 100644 --- a/src/simudator/gui/module_graphics_item/mia/pc_graphic.py +++ b/src/simudator/gui/module_graphics_item/mia/pc_graphic.py @@ -22,18 +22,16 @@ class PcGraphicsItem(IntegerRegisterGraphicsItem): def draw_graphics_item(self): self.draw_rect() - width = len(self.text.text())*self.RECT_WIDTH_PER_CHAR + self.TEXT_WIDTH_MARGIN + width = ( + len(self.text.text()) * self._RECT_WIDTH_PER_CHAR + self._TEXT_WIDTH_MARGIN + ) # Draw to bus port - to_bus_port = PortGraphicsItem(self.module.bus_input_s, parent = self) - to_bus_port.setPos( - width, - self.RECT_HEIGHT/4) + to_bus_port = PortGraphicsItem(self.module.bus_input_s, parent=self) + to_bus_port.setPos(width, self._RECT_HEIGHT / 4) self.ports.append(to_bus_port) # Draw from bus port - from_bus_port = PortGraphicsItem(self.module.bus_output_s, parent = self) - from_bus_port.setPos( - width, - self.RECT_HEIGHT*3/4) + from_bus_port = PortGraphicsItem(self.module.bus_output_s, parent=self) + from_bus_port.setPos(width, self._RECT_HEIGHT * 3 / 4) self.ports.append(from_bus_port) diff --git a/src/simudator/gui/module_graphics_item/mia/supc_graphic.py b/src/simudator/gui/module_graphics_item/mia/supc_graphic.py index 149065ce49b9fe801408a475a860e4fbe6582623..730a1a292056febc7c76df298eba09ea3cd13edb 100644 --- a/src/simudator/gui/module_graphics_item/mia/supc_graphic.py +++ b/src/simudator/gui/module_graphics_item/mia/supc_graphic.py @@ -13,16 +13,18 @@ class SupcGraphicsItem(IntegerRegisterGraphicsItem): def draw_graphics_item(self): self.draw_rect() - width = len(self.text.text())*self.RECT_WIDTH_PER_CHAR + self.TEXT_WIDTH_MARGIN + width = ( + len(self.text.text()) * self._RECT_WIDTH_PER_CHAR + self._TEXT_WIDTH_MARGIN + ) # Draw to mPc port - to_upc_port = PortGraphicsItem(self.module.input_s, - Orientation.UP, parent = self) - to_upc_port.setPos(width*2/8, 0) + to_upc_port = PortGraphicsItem(self.module.input_s, Orientation.UP, parent=self) + to_upc_port.setPos(width * 2 / 8, 0) self.ports.append(to_upc_port) # Draw from uPc port - from_upc_port = PortGraphicsItem(self.module.output_s, - Orientation.UP, parent = self) - from_upc_port.setPos(width*6/8, 0) + from_upc_port = PortGraphicsItem( + self.module.output_s, Orientation.UP, parent=self + ) + from_upc_port.setPos(width * 6 / 8, 0) self.ports.append(from_upc_port) diff --git a/src/simudator/gui/module_graphics_item/mia/upc_graphic.py b/src/simudator/gui/module_graphics_item/mia/upc_graphic.py index 2498c39a48ce5a4f9c9e3eb7f3beb834d8699e70..180c318e322fa512eb7b3bb5cc91b3cc508d8126 100644 --- a/src/simudator/gui/module_graphics_item/mia/upc_graphic.py +++ b/src/simudator/gui/module_graphics_item/mia/upc_graphic.py @@ -22,30 +22,30 @@ class uPcGraphicsItem(IntegerRegisterGraphicsItem): def draw_graphics_item(self): self.draw_rect() - width = len(self.text.text())*self.RECT_WIDTH_PER_CHAR + self.TEXT_WIDTH_MARGIN + width = ( + len(self.text.text()) * self._RECT_WIDTH_PER_CHAR + self._TEXT_WIDTH_MARGIN + ) # Draw from K1 port - from_k1 = PortGraphicsItem(self.module.from_k1, Orientation.UP, parent = self) - from_k1.setPos(width*2/8, 0) + from_k1 = PortGraphicsItem(self.module.from_k1, Orientation.UP, parent=self) + from_k1.setPos(width * 2 / 8, 0) self.ports.append(from_k1) # Draw from K2 port - from_k2 = PortGraphicsItem(self.module.from_k2, Orientation.UP, parent = self) - from_k2.setPos(width*6/8, 0) + from_k2 = PortGraphicsItem(self.module.from_k2, Orientation.UP, parent=self) + from_k2.setPos(width * 6 / 8, 0) self.ports.append(from_k2) # Draw to SUPc port - to_supc_port = PortGraphicsItem(self.module.from_supc, - Orientation.DOWN, parent = self) - to_supc_port.setPos( - width*2/8, - self.RECT_HEIGHT) + to_supc_port = PortGraphicsItem( + self.module.from_supc, Orientation.DOWN, parent=self + ) + to_supc_port.setPos(width * 2 / 8, self._RECT_HEIGHT) self.ports.append(to_supc_port) # Draw from SuPc port - from_supc_port = PortGraphicsItem(self.module.to_supc, - Orientation.DOWN, parent = self) - from_supc_port.setPos( - width*6/8, - self.RECT_HEIGHT) + from_supc_port = PortGraphicsItem( + self.module.to_supc, Orientation.DOWN, parent=self + ) + from_supc_port.setPos(width * 6 / 8, self._RECT_HEIGHT) self.ports.append(from_supc_port) diff --git a/src/simudator/gui/module_graphics_item/module_graphics_item.py b/src/simudator/gui/module_graphics_item/module_graphics_item.py index acfd420f8a5fda8bbb48e02fee0e4986d1075bd1..30ae67a723d2f6c1fd73596e917d49a19c63b8d6 100644 --- a/src/simudator/gui/module_graphics_item/module_graphics_item.py +++ b/src/simudator/gui/module_graphics_item/module_graphics_item.py @@ -16,7 +16,7 @@ from qtpy.QtWidgets import ( ) from simudator.core.module import Module -from simudator.gui.color_scheme import ColorScheme as CS +from simudator.gui.color_scheme import ColorScheme from simudator.gui.dialogs.module_state_dialog import ModuleStateDialog from simudator.gui.orientation import Orientation from simudator.gui.port_graphics_item import PortGraphicsItem @@ -29,11 +29,11 @@ class ModuleGraphicsItem(QGraphicsObject, QGraphicsItem): Can not guarantee visual appeal or readabiliy. """ - SIGNAL_MARGIN = 20 - STATE_MEMBER_MARGIN = 12 - CHAR_LEN = 3 - CHAR_HEIGHT = 8 - WIDTH_MARGIN = 12 + _SIGNAL_MARGIN = 20 + _STATE_MEMBER_MARGIN = 12 + _CHAR_LEN = 3 + _CHAR_HEIGHT = 8 + _WIDTH_MARGIN = 12 moved = pyqtSignal(name="moved") new_state_breakpoint_signal = pyqtSignal(str, str, str) update_graphics_signal = pyqtSignal() @@ -94,34 +94,32 @@ class ModuleGraphicsItem(QGraphicsObject, QGraphicsItem): in_signals = len(self.input_signals) out_signals = len(self.output_signals) states = len(self.module.get_gui_state()) - inputs_height = (in_signals + 1) * self.SIGNAL_MARGIN - outputs_height = (out_signals + 1) * self.SIGNAL_MARGIN - members_height = states * self.CHAR_HEIGHT + self.STATE_MEMBER_MARGIN + inputs_height = (in_signals + 1) * self._SIGNAL_MARGIN + outputs_height = (out_signals + 1) * self._SIGNAL_MARGIN + members_height = states * self._CHAR_HEIGHT + self._STATE_MEMBER_MARGIN # Calculate width according to state character length longest_line_len = self.module.get_longest_line_len() - height = max(inputs_height, - outputs_height, - members_height) - width = longest_line_len * self.CHAR_LEN * 2 + 2 * self.WIDTH_MARGIN + height = max(inputs_height, outputs_height, members_height) + width = longest_line_len * self._CHAR_LEN * 2 + 2 * self._WIDTH_MARGIN # Shadow self.shadow = QGraphicsRectItem(0, 0, width, height, self) - self.shadow.setBrush(CS.ModuleShadow) - self.shadow.setPen(CS.ModuleShadow) + self.shadow.setBrush(ColorScheme.ModuleShadow) + self.shadow.setPen(ColorScheme.ModuleShadow) self.shadow.setPos(self.pos() + QtCore.QPointF(5, 10)) # Create base background rectangle self.baserect = QGraphicsRectItem(0, 0, width, height, self) - self.baserect.setBrush(CS.ModuleBackground) + self.baserect.setBrush(ColorScheme.ModuleBackground) # Add ports for input signals self.draw_ports() # Create name lable name_text = QGraphicsSimpleTextItem(self.state["name"], self) - name_text.setPos(width/2-len(self.state["name"])*self.CHAR_LEN, 0) - name_text.setBrush(CS.ModuleText) + name_text.setPos(width / 2 - len(self.state["name"]) * self._CHAR_LEN, 0) + name_text.setBrush(ColorScheme.ModuleText) # Create state lables self.state_lables = {} @@ -130,9 +128,8 @@ class ModuleGraphicsItem(QGraphicsObject, QGraphicsItem): if state_tuple[1] != "name": text = state_tuple[1] + ": " + str(gui_states[state_tuple[1]]) state_text = QGraphicsSimpleTextItem(text, self) - state_text.setPos(width/20, - state_tuple[0]*self.CHAR_HEIGHT) - state_text.setBrush(CS.ModuleText) + state_text.setPos(width / 20, state_tuple[0] * self._CHAR_HEIGHT) + state_text.setBrush(ColorScheme.ModuleText) self.state_lables[state_tuple[1]] = state_text def draw_ports(self) -> None: @@ -144,17 +141,16 @@ class ModuleGraphicsItem(QGraphicsObject, QGraphicsItem): height = self.baserect.rect().height() for signal_tuple in enumerate(self.input_signals): port = PortGraphicsItem(signal_tuple[1], Orientation.LEFT, self) - height_spacing = height/(len(self.input_signals)) - port.setPos(0, (signal_tuple[0] + 0.5)*height_spacing) + height_spacing = height / (len(self.input_signals)) + port.setPos(0, (signal_tuple[0] + 0.5) * height_spacing) self.ports.append(port) # Add ports for output signals for signal_tuple in enumerate(self.output_signals): port = PortGraphicsItem(signal_tuple[1], parent=self) - height_spacing = height/(len(self.output_signals)) - port.setPos( - width, (signal_tuple[0] + 0.5)*height_spacing) + height_spacing = height / (len(self.output_signals)) + port.setPos(width, (signal_tuple[0] + 0.5) * height_spacing) self.ports.append(port) def showPorts(self) -> None: diff --git a/src/simudator/gui/module_graphics_item/register_graphic.py b/src/simudator/gui/module_graphics_item/register_graphic.py index 52b6a944f9b6b121593ddc8acd69ae11d5cc3365..9cf63a23139d25d20dab120ca039a3e510231e9e 100644 --- a/src/simudator/gui/module_graphics_item/register_graphic.py +++ b/src/simudator/gui/module_graphics_item/register_graphic.py @@ -9,11 +9,12 @@ class RegisterGraphicsItem(ModuleGraphicsItem): """ A general graphics item for register modules. """ - RECT_HEIGHT = 40 - RECT_WIDTH_PER_CHAR = 8 - RECT_HEIGHT_PER_CHAR = 16 - TEXT_HEIGHT_MARGIN = 10 - TEXT_WIDTH_MARGIN = 5 + + _RECT_HEIGHT = 40 + _RECT_WIDTH_PER_CHAR = 8 + _RECT_HEIGHT_PER_CHAR = 16 + _TEXT_HEIGHT_MARGIN = 10 + _TEXT_WIDTH_MARGIN = 5 def draw_graphics_item(self) -> None: self.draw_rect() @@ -24,22 +25,21 @@ class RegisterGraphicsItem(ModuleGraphicsItem): Draw the register rect basd on contents of the register """ self.text = QGraphicsSimpleTextItem("", self) - self.text.setPos(self.TEXT_WIDTH_MARGIN, self.TEXT_HEIGHT_MARGIN) + self.text.setPos(self._TEXT_WIDTH_MARGIN, self._TEXT_HEIGHT_MARGIN) # Update here to fill text box, we can then use text box for lenght self.update() - self.baserect = QGraphicsRectItem(0, 0, - len(self.text.text())*self.RECT_WIDTH_PER_CHAR + self.TEXT_WIDTH_MARGIN, - self.RECT_HEIGHT, self) + self.baserect = QGraphicsRectItem( + 0, + 0, + len(self.text.text()) * self._RECT_WIDTH_PER_CHAR + self._TEXT_WIDTH_MARGIN, + self._RECT_HEIGHT, + self, + ) def update(self): - self.state = self.module.get_state() - name = self.state["name"] - value = self.state["value"] - - value_text = f"{value}" - full_text = name + ": " + value_text - + state = self.module.get_state() + full_text = f"{state['name']}: {state['value']}" self.text.setText(full_text) @@ -50,12 +50,7 @@ class IntegerRegisterGraphicsItem(RegisterGraphicsItem): """ def update(self): - self.state = self.module.get_state() - name = self.state["name"] - value = self.state["value"] - hex_length = math.ceil(self.state["bit_length"]/4) - - value_text = f"{value:0{hex_length}x}" - full_text = name + ": " + value_text - + state = self.module.get_state() + hex_length = math.ceil(state["bit_length"] / 4) + full_text = f"{state['name']}: {state['value']:0{hex_length}x}" self.text.setText(full_text) diff --git a/src/simudator/gui/orientation.py b/src/simudator/gui/orientation.py index fed1d171ca6d5c80d52dc585b35d46a7128cbd5a..6d3adfe80bf67dbc4b230128b90677109d7bd017 100644 --- a/src/simudator/gui/orientation.py +++ b/src/simudator/gui/orientation.py @@ -5,6 +5,7 @@ class Orientation(IntEnum): """ Used to give and track ports orientations. """ + UP = 0 LEFT = 1 DOWN = 2 diff --git a/src/simudator/gui/port_graphics_item.py b/src/simudator/gui/port_graphics_item.py index 7f81f760dc5d9b87880cc6258b507260f168682f..12da9fe8bc69e8038b9d02771bb911cd1044d1c2 100644 --- a/src/simudator/gui/port_graphics_item.py +++ b/src/simudator/gui/port_graphics_item.py @@ -24,6 +24,7 @@ class PyQtSignalHolder(QObject): from QObject or QGraphicsObject to have pyqtSignals like normally done for QGraphicsItem subclasses that need pyqtSignals. """ + moved = pyqtSignal(name="moved") toggled = pyqtSignal(name="toggled") @@ -45,11 +46,13 @@ class PortGraphicsItem(QGraphicsItemGroup): FONT_SIZE = 8 id_iter = itertools.count() - def __init__(self, - signal: Signal, - orientation: Orientation = Orientation.RIGHT, - parent = None, - display_name: str = None): + def __init__( + self, + signal: Signal, + orientation: Orientation = Orientation.RIGHT, + parent=None, + display_name: str = None, + ): super().__init__(parent) # Use the name of the signal if no display name is given if display_name is None: @@ -111,7 +114,6 @@ class PortGraphicsItem(QGraphicsItemGroup): """ self.orientation = orientation if orientation == Orientation.UP: - # Set right line visible self.vertical_line.setVisible(True) self.horizontal_line.setVisible(False) @@ -120,11 +122,11 @@ class PortGraphicsItem(QGraphicsItemGroup): self.vertical_line.setPos(0, -self.LINE_LENGTH) # set label position - self.name_label.setPos(-len(self.name)*self.CHAR_LEN/2, - -self.LINE_LENGTH*3.2) + self.name_label.setPos( + -len(self.name) * self.CHAR_LEN / 2, -self.LINE_LENGTH * 3.2 + ) elif orientation == Orientation.DOWN: - # Set right line visible self.vertical_line.setVisible(True) self.horizontal_line.setVisible(False) @@ -133,11 +135,11 @@ class PortGraphicsItem(QGraphicsItemGroup): self.vertical_line.setPos(0, 0) # set label position - self.name_label.setPos(-len(self.name)*self.CHAR_LEN/2, - self.LINE_LENGTH*1.2) + self.name_label.setPos( + -len(self.name) * self.CHAR_LEN / 2, self.LINE_LENGTH * 1.2 + ) elif orientation == Orientation.LEFT: - # Set right line visible self.vertical_line.setVisible(False) self.horizontal_line.setVisible(True) @@ -147,11 +149,11 @@ class PortGraphicsItem(QGraphicsItemGroup): # set label position self.name_label.setPos( - -self.LINE_LENGTH*1.5 - len(self.name)*self.CHAR_LEN, - -self.CHAR_HEIGHT) + -self.LINE_LENGTH * 1.5 - len(self.name) * self.CHAR_LEN, + -self.CHAR_HEIGHT, + ) elif orientation == Orientation.RIGHT: - # Set right line visible self.vertical_line.setVisible(False) self.horizontal_line.setVisible(True) @@ -160,8 +162,7 @@ class PortGraphicsItem(QGraphicsItemGroup): self.horizontal_line.setPos(0, 0) # set label position - self.name_label.setPos(self.LINE_LENGTH*1.5, -self.CHAR_HEIGHT) - + self.name_label.setPos(self.LINE_LENGTH * 1.5, -self.CHAR_HEIGHT) def getPoint(self) -> QPointF: """ @@ -218,10 +219,12 @@ class PortGraphicsItem(QGraphicsItemGroup): y_position = min(max(relative_mouse_pos_y, min_y), max_y) # Do special check for if mouse is inside and dont move if so - if (x_position < max_x and - x_position > min_x and - y_position < max_y and - y_position > min_y): + if ( + x_position < max_x + and x_position > min_x + and y_position < max_y + and y_position > min_y + ): return self.setX(x_position) diff --git a/src/simudator/gui/run_continuously_thread.py b/src/simudator/gui/run_continuously_thread.py index 47089a606908b9054bfc91cd40eb9e381c04e759..c974a3bf542c3f3bb0ca99abeb2456665fc6c3ec 100644 --- a/src/simudator/gui/run_continuously_thread.py +++ b/src/simudator/gui/run_continuously_thread.py @@ -17,7 +17,6 @@ class RunThread(QRunnable): self.steps = steps def run(self): - if self.run_continuously: self.cpu.run_continuously() diff --git a/src/simudator/gui/signal_graphics_item.py b/src/simudator/gui/signal_graphics_item.py index 130632126f26c058031890be8dd6b212accd441d..cbed8b9d2c449c878d802bf8a55e8130e452beb6 100644 --- a/src/simudator/gui/signal_graphics_item.py +++ b/src/simudator/gui/signal_graphics_item.py @@ -13,6 +13,7 @@ SHORTEST_LINE_LEN = 400 LONGEST_CLICK_DIST = 20 MERGE_THRESHOLD = 25 + class SignalGraphicsItem(QGraphicsItem): """ Class used to draw and update signals between modules, represented @@ -20,14 +21,16 @@ class SignalGraphicsItem(QGraphicsItem): The constituent line segments of the signal can both be moved orthogonally to the axis of the segments as well as be split into smaller segments. """ - id_iter = itertools.count() - def __init__(self, - start_port: PortGraphicsItem, - end_port: PortGraphicsItem, - parent = None, - display_name: str = None): + id_iter = itertools.count() + def __init__( + self, + start_port: PortGraphicsItem, + end_port: PortGraphicsItem, + parent=None, + display_name: str = None, + ): super().__init__(parent) self.start_port = start_port self.end_port = end_port @@ -47,7 +50,6 @@ class SignalGraphicsItem(QGraphicsItem): self.last_line_end_i = None self.setFlag(QGraphicsItem.GraphicsItemFlag.ItemIsMovable) - # Initialise the visual representation of the signal as a zig-zag # between the ports of the signal # |---- port @@ -56,14 +58,14 @@ class SignalGraphicsItem(QGraphicsItem): start_point = self.start_port.getPoint() end_point = self.end_port.getPoint() self.points.append(start_point) - half_x =(start_point.x() + end_point.x()) / 2 + half_x = (start_point.x() + end_point.x()) / 2 self.points.append(QPointF(half_x, start_point.y())) self.points.append(QPointF(half_x, end_point.y())) self.points.append(end_point) self.drawSignal() # Used to lock layout - self.is_locked= False + self.is_locked = False def reset(self) -> None: """ @@ -86,7 +88,7 @@ class SignalGraphicsItem(QGraphicsItem): start_point = self.start_port.getPoint() end_point = self.end_port.getPoint() self.points.append(start_point) - half_x =(start_point.x() + end_point.x()) / 2 + half_x = (start_point.x() + end_point.x()) / 2 self.points.append(QPointF(half_x, start_point.y())) self.points.append(QPointF(half_x, end_point.y())) self.points.append(end_point) @@ -94,7 +96,6 @@ class SignalGraphicsItem(QGraphicsItem): self.setVisible(True) self.drawSignal() - def drawSignal(self) -> None: """ Draws the signal as a collection of orthogonal line segments which @@ -133,7 +134,7 @@ class SignalGraphicsItem(QGraphicsItem): """ # Last line is not splittable, split next last instead - if end_point_index == len(self.points) -1: + if end_point_index == len(self.points) - 1: start_point_index -= 1 end_point_index -= 1 @@ -241,9 +242,9 @@ class SignalGraphicsItem(QGraphicsItem): self.drawSignal() def handleLeftClick(self) -> None: - self.align(self.last_line_start_i, - self.last_line_end_i, - self.last_line_vertical) + self.align( + self.last_line_start_i, self.last_line_end_i, self.last_line_vertical + ) self.drawSignal() # Reset cursor @@ -256,25 +257,27 @@ class SignalGraphicsItem(QGraphicsItem): merge small line segments. """ if ascending: - if start_index + 2 >= len(self.points) -1: + if start_index + 2 >= len(self.points) - 1: return start_index - distance_sq = self.distance_sq(self.points[start_index], - self.points[start_index+1]) + distance_sq = self.distance_sq( + self.points[start_index], self.points[start_index + 1] + ) if distance_sq < MERGE_THRESHOLD: - return self.align_search(start_index+2, ascending) + return self.align_search(start_index + 2, ascending) return start_index else: if start_index - 2 <= 0: return start_index - distance_sq = self.distance_sq(self.points[start_index], - self.points[start_index-1]) + distance_sq = self.distance_sq( + self.points[start_index], self.points[start_index - 1] + ) if distance_sq < MERGE_THRESHOLD: - return self.align_search(start_index-2, ascending) + return self.align_search(start_index - 2, ascending) return start_index def align(self, line_start, line_end, vertical=False): @@ -288,7 +291,7 @@ class SignalGraphicsItem(QGraphicsItem): self.points[new_line_end].setX(self.points[new_line_start].x()) else: self.points[new_line_end].setY(self.points[new_line_start].y()) - self.points = self.points[:new_line_start+1] + self.points[new_line_end:] + self.points = self.points[: new_line_start + 1] + self.points[new_line_end:] def handleRightClick(self) -> None: """ @@ -333,10 +336,10 @@ class SignalGraphicsItem(QGraphicsItem): closest_2_i = None for index, point in enumerate(self.points[1:]): distance = self.getLineDistance(prev_point, point, ref_point) - if distance < shortest_distance: # Found a closer line + if distance < shortest_distance: # Found a closer line shortest_distance = distance closest_1_i = index - closest_2_i = index+1 + closest_2_i = index + 1 prev_point = point @@ -355,7 +358,7 @@ class SignalGraphicsItem(QGraphicsItem): # Make sure the closest point is not outside the line upper_y = max(line_start.y(), line_end.y()) lower_y = min(line_start.y(), line_end.y()) - if lower_y <= ref_point.y() <= upper_y: + if lower_y <= ref_point.y() <= upper_y: distance = abs(ref_point.x() - line_end.x()) else: @@ -363,7 +366,6 @@ class SignalGraphicsItem(QGraphicsItem): # Horisontal line segment elif line_start.y() == line_end.y(): - # Make sure the closest point is not outside the line leftmost_x = min(line_start.x(), line_end.x()) rightmost_x = max(line_start.x(), line_end.x()) @@ -396,8 +398,7 @@ class SignalGraphicsItem(QGraphicsItem): neighbour_point = self.points[-2] new_end_point = new_end else: - return # Nothing moved - + return # Nothing moved # Move the points correctly knowing the end points is horizontal if end_point.y() == neighbour_point.y():