Skip to content
Snippets Groups Projects
port_button.py 1.81 KiB
Newer Older
  • Learn to ignore specific revisions
  • from qtpy.QtCore import Qt, Signal
    
    Oscar Gustafsson's avatar
    Oscar Gustafsson committed
    from qtpy.QtWidgets import QMenu, QPushButton
    
    
    
    class PortButton(QPushButton):
        connectionRequested = Signal(QPushButton)
        moved = Signal()
    
    Oscar Gustafsson's avatar
    Oscar Gustafsson committed
    
    
        def __init__(self, name, operation, port, window, parent=None):
    
            super().__init__(name, parent=operation)
    
            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().mousePressEvent(event)
    
            super().mouseReleaseEvent(event)
    
    
        def _toggle_port(self, pressed=False):
            self.pressed = not pressed
    
    Oscar Gustafsson's avatar
    Oscar Gustafsson committed
            self.setStyleSheet(
    
    Oscar Gustafsson's avatar
    Oscar Gustafsson committed
                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()