From 07c94da97bb4f0ef70b2e00b47553821eccf51bb Mon Sep 17 00:00:00 2001
From: Oscar Gustafsson <oscar.gustafsson@gmail.com>
Date: Wed, 25 Jan 2023 19:28:47 +0100
Subject: [PATCH] Move add_ports logic to drag_button

---
 b_asic/GUI/drag_button.py | 49 ++++++++++++++++++++++++++++++++++-----
 b_asic/GUI/main_window.py | 37 ++---------------------------
 2 files changed, 45 insertions(+), 41 deletions(-)

diff --git a/b_asic/GUI/drag_button.py b/b_asic/GUI/drag_button.py
index bbdcf8dd..b676b51f 100644
--- a/b_asic/GUI/drag_button.py
+++ b/b_asic/GUI/drag_button.py
@@ -12,7 +12,13 @@ from qtpy.QtWidgets import QAction, QMenu, QPushButton
 
 from b_asic.GUI.properties_window import PropertiesWindow
 from b_asic.GUI.utils import decorate_class, handle_error
-from b_asic.GUI._preferences import GRID, MINBUTTONSIZE, PORTWIDTH
+from b_asic.GUI._preferences import (
+    GAP,
+    GRID,
+    MINBUTTONSIZE,
+    PORTHEIGHT,
+    PORTWIDTH,
+)
 from b_asic.port import InputPort
 
 
@@ -67,8 +73,7 @@ class DragButton(QPushButton):
         menu.exec_(self.cursor().pos())
 
     def show_properties_window(self, event):
-        self.properties_window = PropertiesWindow(self, self._window)
-        self.properties_window.show()
+        PropertiesWindow(self, self._window).show()
 
     def add_label(self, label):
         self.label = label
@@ -146,9 +151,7 @@ class DragButton(QPushButton):
         path_to_image = os.path.join(
             os.path.dirname(__file__),
             "operation_icons",
-            (
-                f"{self.operation_path_name}{'_grey.png' if self.pressed else '.png'}"
-            ),
+            f"{self.operation_path_name}{'_grey.png' if self.pressed else '.png'}",
         )
         self.setIcon(QIcon(path_to_image))
         self.setIconSize(QSize(MINBUTTONSIZE, MINBUTTONSIZE))
@@ -227,3 +230,37 @@ class DragButton(QPushButton):
 
         if self.operation in self._window.operationDragDict:
             del self._window.operationDragDict[self.operation]
+
+    def add_ports(self):
+        def _determine_port_distance(height, ports):
+            """Determine the distance between each port on the side of an operation.
+            The method returns the distance that each port should have from 0.
+            """
+            return (
+                [(height - PORTHEIGHT) // 2]
+                if ports == 1
+                else [(PORTHEIGHT + GAP) * i for i in range(ports)]
+            )
+
+        def _get_button_height(op):
+            max_ports = max(op.input_count, op.output_count)
+            return max(
+                MINBUTTONSIZE, max_ports * PORTHEIGHT + (max_ports - 1) * GAP
+            )
+
+        op = self.operation
+        # TODO: get height from operation
+        height = _get_button_height(op)
+        _output_ports_dist = _determine_port_distance(height, op.output_count)
+        _input_ports_dist = _determine_port_distance(height, op.input_count)
+        for i, dist in enumerate(_input_ports_dist):
+            port = PortButton(">", self, op.input(i), self._window)
+            self.ports.append(port)
+            port.move(0, dist)
+            port.show()
+
+        for i, dist in enumerate(_output_ports_dist):
+            port = PortButton(">", self, op.output(i), self._window)
+            self.ports.append(port)
+            port.move(MINBUTTONSIZE - PORTWIDTH, dist)
+            port.show()
diff --git a/b_asic/GUI/main_window.py b/b_asic/GUI/main_window.py
index 5420bc01..3851f026 100644
--- a/b_asic/GUI/main_window.py
+++ b/b_asic/GUI/main_window.py
@@ -452,40 +452,6 @@ class MainWindow(QMainWindow):
         self.dialog.add_sfg_to_dialog()
         self.dialog.show()
 
-    def _determine_port_distance(self, height, ports):
-        """Determine the distance between each port on the side of an operation.
-        The method returns the distance that each port should have from 0.
-        """
-        return (
-            [(height - PORTHEIGHT) // 2]
-            if ports == 1
-            else [(PORTHEIGHT + GAP) * i for i in range(ports)]
-        )
-
-    def add_ports(self, operation):
-        op = operation.operation
-        height = self._get_button_height(op)
-        _output_ports_dist = self._determine_port_distance(
-            height, op.output_count
-        )
-        _input_ports_dist = self._determine_port_distance(
-            height, op.input_count
-        )
-        self.portDict[operation] = []
-        for i, dist in enumerate(_input_ports_dist):
-            port = PortButton(">", operation, op.input(i), self)
-            self.portDict[operation].append(port)
-            operation.ports.append(port)
-            port.move(0, dist)
-            port.show()
-
-        for i, dist in enumerate(_output_ports_dist):
-            port = PortButton(">", operation, op.output(i), self)
-            self.portDict[operation].append(port)
-            operation.ports.append(port)
-            port.move(MINBUTTONSIZE - PORTWIDTH, dist)
-            port.show()
-
     def get_operations_from_namespace(self, namespace):
         self.logger.info(
             f"Fetching operations from namespace: {namespace.__name__}."
@@ -549,7 +515,8 @@ class MainWindow(QMainWindow):
                 "background-color: white; border-style: solid;"
                 "border-color: black; border-width: 2px"
             )
-            self.add_ports(attr_button)
+            attr_button.add_ports()
+            self.portDict[attr_button] = attr_button.ports
 
             icon_path = os.path.join(
                 os.path.dirname(__file__),
-- 
GitLab