From 2c0582c4bad2dcfc6264ab4e15461344baae2e1e Mon Sep 17 00:00:00 2001
From: Olle Hansson <olle.hansson@liu.se>
Date: Thu, 16 Feb 2023 11:48:50 +0100
Subject: [PATCH] Closes #136, can now drag to connect ports

---
 b_asic/GUI/main_window.py |  7 ++++--
 b_asic/GUI/port_button.py | 45 +++++++++++++++++++++++++++++++++++++--
 2 files changed, 48 insertions(+), 4 deletions(-)

diff --git a/b_asic/GUI/main_window.py b/b_asic/GUI/main_window.py
index a02130ab..18a32548 100644
--- a/b_asic/GUI/main_window.py
+++ b/b_asic/GUI/main_window.py
@@ -11,7 +11,7 @@ import sys
 from pprint import pprint
 
 from qtpy.QtCore import QFileInfo, QSize, Qt
-from qtpy.QtGui import QIcon, QKeySequence, QPainter
+from qtpy.QtGui import QCursor, QIcon, QKeySequence, QPainter
 from qtpy.QtWidgets import (
     QAction,
     QApplication,
@@ -67,7 +67,8 @@ class MainWindow(QMainWindow):
         self.operationItemSceneList = []
         self.signalList = []
         self.mouse_pressed = False
-        self.mouse_draggin = False
+        self.mouse_dragging = False
+        self.starting_port = []
         self.pressed_operations = []
         self.portDict = {}
         self.signalPortDict = {}
@@ -134,6 +135,8 @@ class MainWindow(QMainWindow):
             "section on the toolbar."
         )
 
+        self.cursor = QCursor()
+
     def init_ui(self):
         self.create_toolbar_view()
         self.create_graphics_view()
diff --git a/b_asic/GUI/port_button.py b/b_asic/GUI/port_button.py
index 487f6edc..27d3e2ca 100644
--- a/b_asic/GUI/port_button.py
+++ b/b_asic/GUI/port_button.py
@@ -1,7 +1,8 @@
 """
 B-ASIC port button module.
 """
-from qtpy.QtCore import Qt, Signal
+from qtpy.QtCore import QMimeData, Qt, Signal
+from qtpy.QtGui import QDrag
 from qtpy.QtWidgets import QMenu, QPushButton
 
 
@@ -30,6 +31,8 @@ class PortButton(QPushButton):
         self.clicked = 0
         self._m_drag = False
         self._m_press = False
+        self.setAcceptDrops(True)
+        self.setCursor(Qt.ArrowCursor)
 
         self.setStyleSheet("background-color: white")
         self.connectionRequested.connect(self._window._connect_callback)
@@ -41,13 +44,51 @@ class PortButton(QPushButton):
 
     def mousePressEvent(self, event):
         if event.button() == Qt.MouseButton.LeftButton:
+            self._window.mouse_pressed = True
             self.select_port(event.modifiers())
-
         super().mousePressEvent(event)
 
     def mouseReleaseEvent(self, event):
+        if (
+            event.button() == Qt.MouseButton.LeftButton
+            and self._window.mouse_pressed
+        ):
+            self._window.mouse_pressed = False
+            if self._window.mouse_dragging:
+                self._window.mouse_dragging = False
         super().mouseReleaseEvent(event)
 
+    def mouseMoveEvent(self, event):
+        if self._window.mouse_pressed:
+            self._window.mouse_draggin = True
+            self._window.starting_port = self
+            data = QMimeData()
+            drag = QDrag(self)
+            drag.setMimeData(data)
+            drag.exec()
+        super().mouseMoveEvent(event)
+
+    def dragEnterEvent(self, event):
+        event.acceptProposedAction()
+        self.update()
+        super().dragEnterEvent(event)
+
+    def dragLeaveEvent(self, event):
+        self.update()
+        super().dragLeaveEvent(event)
+
+    def dragMoveEvent(self, event):
+        event.acceptProposedAction()
+        super().dragMoveEvent(event)
+
+    def dropEvent(self, event):
+        event.acceptProposedAction()
+        if self != self._window.starting_port:
+            self.select_port(Qt.KeyboardModifier.ControlModifier)
+            self._window._connect_callback()
+        self.update()
+        super().dropEvent(event)
+
     def _toggle_port(self, pressed=False):
         self.pressed = not pressed
         self.setStyleSheet(
-- 
GitLab