Newer
Older
Angus Lothian
committed
import sys
from qtpy.QtWidgets import QPushButton, QMenu
from qtpy.QtCore import Qt, Signal
Angus Lothian
committed
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
class PortButton(QPushButton):
connectionRequested = Signal(QPushButton)
moved = Signal()
def __init__(self, name, operation, port, window, parent=None):
super(PortButton, self).__init__(name, operation, parent)
self.pressed = False
self._window = window
self.port = port
self.operation = operation
self.clicked = 0
self._m_drag = False
self._m_press = False
self.setStyleSheet("background-color: white")
self.connectionRequested.connect(self._window._connect_button)
def contextMenuEvent(self, event):
menu = QMenu()
menu.addAction("Connect", lambda: self.connectionRequested.emit(self))
menu.exec_(self.cursor().pos())
def mousePressEvent(self, event):
if event.button() == Qt.LeftButton:
self.select_port(event.modifiers())
super(PortButton, self).mousePressEvent(event)
def mouseReleaseEvent(self, event):
super(PortButton, self).mouseReleaseEvent(event)
def _toggle_port(self, pressed=False):
self.pressed = not pressed
self.setStyleSheet(f"background-color: {'white' if not self.pressed else 'grey'}")
def select_port(self, modifiers=None):
if modifiers != Qt.ControlModifier:
for port in self._window.pressed_ports:
port._toggle_port(port.pressed)
self._toggle_port(self.pressed)
self._window.pressed_ports = [self]
else:
self._toggle_port(self.pressed)
if self in self._window.pressed_ports:
self._window.pressed_ports.remove(self)
else:
self._window.pressed_ports.append(self)
for signal in self._window.signalList:
signal.update()