Newer
Older

Felix Goding
committed
"""@package docstring
Drag button class.
This class creates a dragbutton which can be clicked, dragged and dropped.
"""
import os.path
from properties_window import PropertiesWindow
from PySide2.QtWidgets import QPushButton, QMenu, QAction
from PySide2.QtCore import Qt, QSize, Signal
from PySide2.QtGui import QIcon

Felix Goding
committed
from utils import decorate_class, handle_error

Felix Goding
committed

Felix Goding
committed
class DragButton(QPushButton):
connectionRequested = Signal(QPushButton)
moved = Signal()
def __init__(self, name, operation, operation_path_name, is_show_name, window, parent = None):

Felix Goding
committed
self.name = name
self.is_show_name = is_show_name

Felix Goding
committed
self.operation = operation
self.operation_path_name = operation_path_name
self.clicked = 0
self.pressed = False
self._mouse_press_pos = None
self._mouse_move_pos = None
super(DragButton, self).__init__(self._window)

Felix Goding
committed
def contextMenuEvent(self, event):
menu = QMenu()
properties = QAction("Properties")
menu.addAction(properties)
properties.triggered.connect(self.show_properties_window)
menu.exec_(self.cursor().pos())
def show_properties_window(self, event):
self.properties_window = PropertiesWindow(self, self._window)
self.properties_window.show()
def add_label(self, label):
self.label = label

Felix Goding
committed
def mousePressEvent(self, event):
if event.button() == Qt.LeftButton:
self._mouse_press_pos = event.pos()
self._mouse_move_pos = event.pos()

Felix Goding
committed
for signal in self._window.signalList:

Felix Goding
committed
signal.update()
self.clicked += 1
if self.clicked == 1:
self.pressed = True
self.setStyleSheet("background-color: grey; border-style: solid;\
border-color: black; border-width: 2px")

Felix Goding
committed
path_to_image = os.path.join('operation_icons', self.operation_path_name + '_grey.png')
self.setIcon(QIcon(path_to_image))
self.setIconSize(QSize(55, 55))
self._window.pressed_button.append(self)

Felix Goding
committed
elif self.clicked == 2:
self.clicked = 0
self.pressed = False
self.setStyleSheet("background-color: white; border-style: solid;\
border-color: black; border-width: 2px")

Felix Goding
committed
path_to_image = os.path.join('operation_icons', self.operation_path_name + '.png')
self.setIcon(QIcon(path_to_image))
self.setIconSize(QSize(55, 55))
self._window.pressed_button.remove(self)

Felix Goding
committed
super(DragButton, self).mousePressEvent(event)
def mouseMoveEvent(self, event):
if event.buttons() == Qt.LeftButton:
self.move(self.mapToParent(event.pos() - self._mouse_press_pos))

Felix Goding
committed
super(DragButton, self).mouseMoveEvent(event)
def mouseReleaseEvent(self, event):
if self._mouse_press_pos is not None:

Felix Goding
committed
if moved.manhattanLength() > 3:
event.ignore()
return
super(DragButton, self).mouseReleaseEvent(event)
def remove(self):
self.deleteLater()