Skip to content
Snippets Groups Projects
Commit 827071b0 authored by Oscar Gustafsson's avatar Oscar Gustafsson :bicyclist:
Browse files

Enable connecting multiple ports in a logical manner

parent 7c94e73b
No related branches found
No related tags found
1 merge request!187Enable connecting multiple ports in a logical manner
Pipeline #89660 passed
...@@ -33,13 +33,14 @@ from b_asic.GUI._preferences import GAP, GRID, MINBUTTONSIZE, PORTHEIGHT ...@@ -33,13 +33,14 @@ from b_asic.GUI._preferences import GAP, GRID, MINBUTTONSIZE, PORTHEIGHT
from b_asic.GUI.arrow import Arrow from b_asic.GUI.arrow import Arrow
from b_asic.GUI.drag_button import DragButton from b_asic.GUI.drag_button import DragButton
from b_asic.GUI.gui_interface import Ui_main_window from b_asic.GUI.gui_interface import Ui_main_window
from b_asic.GUI.port_button import PortButton
from b_asic.GUI.select_sfg_window import SelectSFGWindow from b_asic.GUI.select_sfg_window import SelectSFGWindow
from b_asic.GUI.show_pc_window import ShowPCWindow from b_asic.GUI.show_pc_window import ShowPCWindow
from b_asic.GUI.simulate_sfg_window import Plot, SimulateSFGWindow from b_asic.GUI.simulate_sfg_window import Plot, SimulateSFGWindow
from b_asic.GUI.util_dialogs import FaqWindow, KeybindsWindow from b_asic.GUI.util_dialogs import FaqWindow, KeybindsWindow
from b_asic.GUI.utils import decorate_class, handle_error from b_asic.GUI.utils import decorate_class, handle_error
from b_asic.gui_utils.about_window import AboutWindow from b_asic.gui_utils.about_window import AboutWindow
from b_asic.port import OutputPort from b_asic.port import InputPort, OutputPort
from b_asic.save_load_structure import python_to_sfg, sfg_to_python from b_asic.save_load_structure import python_to_sfg, sfg_to_python
from b_asic.signal_flow_graph import SFG from b_asic.signal_flow_graph import SFG
...@@ -125,7 +126,7 @@ class MainWindow(QMainWindow): ...@@ -125,7 +126,7 @@ class MainWindow(QMainWindow):
self.shortcut_help = QShortcut(QKeySequence("Ctrl+?"), self) self.shortcut_help = QShortcut(QKeySequence("Ctrl+?"), self)
self.shortcut_help.activated.connect(self.display_faq_page) self.shortcut_help.activated.connect(self.display_faq_page)
self.shortcut_signal = QShortcut(QKeySequence(Qt.Key_Space), self) self.shortcut_signal = QShortcut(QKeySequence(Qt.Key_Space), self)
self.shortcut_signal.activated.connect(self._connect_button) self.shortcut_signal.activated.connect(self._connect_callback)
self.logger.info("Finished setting up GUI") self.logger.info("Finished setting up GUI")
self.logger.info( self.logger.info(
...@@ -288,7 +289,7 @@ class MainWindow(QMainWindow): ...@@ -288,7 +289,7 @@ class MainWindow(QMainWindow):
] ]
if source and destination: if source and destination:
self.connect_button(source[0], destination[0]) self._connect_button(source[0], destination[0])
for port in self.pressed_ports: for port in self.pressed_ports:
port.select_port() port.select_port()
...@@ -494,7 +495,7 @@ class MainWindow(QMainWindow): ...@@ -494,7 +495,7 @@ class MainWindow(QMainWindow):
namespace, self.ui.custom_operations_list namespace, self.ui.custom_operations_list
) )
def create_operation(self, op, position=None, is_flipped=False): def create_operation(self, op, position=None, is_flipped: bool = False):
try: try:
if op in self.operationDragDict: if op in self.operationDragDict:
self.logger.warning( self.logger.warning(
...@@ -600,7 +601,7 @@ class MainWindow(QMainWindow): ...@@ -600,7 +601,7 @@ class MainWindow(QMainWindow):
self.pressed_operations.clear() self.pressed_operations.clear()
super().keyPressEvent(event) super().keyPressEvent(event)
def _connect_button(self, *event): def _connect_callback(self, *event):
if len(self.pressed_ports) < 2: if len(self.pressed_ports) < 2:
self.logger.warning( self.logger.warning(
"Cannot connect less than two ports. Please select at least" "Cannot connect less than two ports. Please select at least"
...@@ -608,37 +609,45 @@ class MainWindow(QMainWindow): ...@@ -608,37 +609,45 @@ class MainWindow(QMainWindow):
) )
return return
for i in range(len(self.pressed_ports) - 1): pressed_op_inports = [
source = ( pressed
self.pressed_ports[i] for pressed in self.pressed_ports
if isinstance(self.pressed_ports[i].port, OutputPort) if isinstance(pressed.port, InputPort)
else self.pressed_ports[i + 1] ]
) pressed_op_outports = [
destination = ( pressed
self.pressed_ports[i + 1] for pressed in self.pressed_ports
if source is not self.pressed_ports[i + 1] if isinstance(pressed.port, OutputPort)
else self.pressed_ports[i] ]
)
if source.port.operation is destination.port.operation:
self.logger.warning("Cannot connect to the same port")
continue
if isinstance(source.port, type(destination.port)): if len(pressed_op_outports) != 1:
self.logger.warning( raise ValueError("Exactly one output port must be selected!")
"Cannot connect port of type: %s to port of type: %s."
% (
type(source.port).__name__,
type(destination.port).__name__,
)
)
continue
self.connect_button(source, destination) pressed_op_outport = pressed_op_outports[0]
for pressed_op_inport in pressed_op_inports:
self._connect_button(pressed_op_outport, pressed_op_inport)
for port in self.pressed_ports: for port in self.pressed_ports:
port.select_port() port.select_port()
def connect_button(self, source, destination): def _connect_button(
self, source: PortButton, destination: PortButton
) -> None:
"""
Connect two PortButtons with an Arrow.
Parameters
----------
source : PortButton
The PortButton to start the signal at.
destination : PortButton
The PortButton to end the signal at.
Returns
-------
None.
"""
signal_exists = ( signal_exists = (
signal signal
for signal in source.port.signals for signal in source.port.signals
......
...@@ -225,7 +225,7 @@ def test_add_operation_and_create_sfg(qtbot, monkeypatch): ...@@ -225,7 +225,7 @@ def test_add_operation_and_create_sfg(qtbot, monkeypatch):
assert len(widget.pressed_ports) == 2 assert len(widget.pressed_ports) == 2
# Connect ports # Connect ports
widget._connect_button() widget._connect_callback()
# Not sure why this won't work # Not sure why this won't work
# qtbot.keyClick(widget, QtCore.Qt.Key.Key_Space, delay=10) # qtbot.keyClick(widget, QtCore.Qt.Key.Key_Space, delay=10)
# Still one selected!? # Still one selected!?
...@@ -245,7 +245,7 @@ def test_add_operation_and_create_sfg(qtbot, monkeypatch): ...@@ -245,7 +245,7 @@ def test_add_operation_and_create_sfg(qtbot, monkeypatch):
QtCore.Qt.KeyboardModifier.ControlModifier, QtCore.Qt.KeyboardModifier.ControlModifier,
) )
# Connect # Connect
widget._connect_button() widget._connect_callback()
assert len(widget.signalList) == 2 assert len(widget.signalList) == 2
# Select input op # Select input op
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment