diff options
-rw-r--r-- | src/main/python/utils/canvas.py | 7 | ||||
-rw-r--r-- | src/main/python/utils/graphics.py | 43 | ||||
-rw-r--r-- | src/main/python/utils/toolbar.py | 32 |
3 files changed, 71 insertions, 11 deletions
diff --git a/src/main/python/utils/canvas.py b/src/main/python/utils/canvas.py index d393e36..2332087 100644 --- a/src/main/python/utils/canvas.py +++ b/src/main/python/utils/canvas.py @@ -2,12 +2,11 @@ import pickle from PyQt5.QtCore import Qt from PyQt5.QtGui import QBrush, QPalette -from PyQt5.QtWidgets import (QFileDialog, QApplication, - QGraphicsScene, QGraphicsView, QHBoxLayout, QMenu, +from PyQt5.QtWidgets import (QFileDialog, QApplication, QHBoxLayout, QMenu, QTabWidget, QWidget, QSpacerItem, QStyle) from . import dialogs -from .graphics import customView +from .graphics import customView, customScene from .data import paperSizes, ppiList, sheetDimensionList class canvas(QWidget): @@ -28,7 +27,7 @@ class canvas(QWidget): # when we will draw items on this, this might be changed if QGraphicScene is subclassed. #set layout and background color - self.painter = QGraphicsScene() + self.painter = customScene() self.painter.setBackgroundBrush(QBrush(Qt.white)) #set white background self.view = customView(self.painter, self) #create a viewport for the canvas board diff --git a/src/main/python/utils/graphics.py b/src/main/python/utils/graphics.py index 4d754b5..243b086 100644 --- a/src/main/python/utils/graphics.py +++ b/src/main/python/utils/graphics.py @@ -1,5 +1,7 @@ from PyQt5.QtCore import Qt -from PyQt5.QtWidgets import QGraphicsView +from PyQt5.QtGui import QPen +from PyQt5.QtWidgets import QGraphicsView, QGraphicsScene, QGraphicsProxyWidget, QGraphicsItem +from PyQt5 import QtWidgets class customView(QGraphicsView): """ @@ -12,7 +14,24 @@ class customView(QGraphicsView): super(customView, self).__init__(parent) self._zoom = 1 self.setDragMode(True) + self.setAcceptDrops(True) + + def dragEnterEvent(self, QDragEnterEvent): + if QDragEnterEvent.mimeData().hasText(): + QDragEnterEvent.acceptProposedAction() + def dragMoveEvent(self, QDragMoveEvent): + if QDragMoveEvent.mimeData().hasText(): + QDragMoveEvent.acceptProposedAction() + + def dropEvent(self, QDropEvent): + if QDropEvent.mimeData().hasText(): + graphic = getattr(QtWidgets, QDropEvent.mimeData().text())(QDropEvent.pos().x(), QDropEvent.pos().y(), 300, 300) + graphic.setPen(QPen(Qt.black, 2)) + graphic.setFlags(QGraphicsItem.ItemIsSelectable | QGraphicsItem.ItemIsMovable) + self.scene().addItem(graphic) + QDropEvent.acceptProposedAction() + def wheelEvent(self, QWheelEvent): #overload wheelevent, to zoom if control is pressed, else scroll normally if Qt.ControlModifier: #check if control is pressed @@ -38,4 +57,24 @@ class customView(QGraphicsView): # set scale according to zoom value being set temp = self.zoom self._zoom = value - self.scale(self.zoom / temp, self.zoom / temp)
\ No newline at end of file + self.scale(self.zoom / temp, self.zoom / temp) + +class customScene(QGraphicsScene): + # def __init__(self, parent = None): + # super(customScene, self).__init__(parent) + def dragEnterEvent(self, e): + e.acceptProposedAction() + + def dropEvent(self, e): + # find item at these coordinates + item = self.itemAt(e.scenePos()) + if item.setAcceptDrops == True: + # pass on event to item at the coordinates + try: + item.dropEvent(e) + except RuntimeError: + pass #This will supress a Runtime Error generated when dropping into a widget with no MyProxy + + def dragMoveEvent(self, e): + e.acceptProposedAction() +
\ No newline at end of file diff --git a/src/main/python/utils/toolbar.py b/src/main/python/utils/toolbar.py index b870259..1c0a492 100644 --- a/src/main/python/utils/toolbar.py +++ b/src/main/python/utils/toolbar.py @@ -1,8 +1,8 @@ from fbs_runtime.application_context.PyQt5 import ApplicationContext -from PyQt5.QtCore import QSize, Qt, pyqtSignal -from PyQt5.QtGui import QIcon +from PyQt5.QtCore import QSize, Qt, pyqtSignal, QMimeData +from PyQt5.QtGui import QIcon, QDrag from PyQt5.QtWidgets import (QBoxLayout, QDockWidget, QGridLayout, QLineEdit, - QScrollArea, QToolButton, QWidget) + QScrollArea, QToolButton, QWidget, QApplication) from .data import defaultToolbarItems, toolbarItems from .funcs import grouper @@ -100,9 +100,31 @@ class toolbarButton(QToolButton): super(toolbarButton, self).__init__(parent) self.setIcon(QIcon(resourceManager.get_resource(f'toolbar/{item["icon"]}'))) self.setIconSize(QSize(40, 40)) - self.setText(f'item["name"]') - # self.setFixedSize(30, 30) + self.dragStartPosition = 0 + self.itemObject = item['object'] + self.setText(item["name"]) + self.setToolTip(item["name"]) + def mousePressEvent(self, event): + if event.button() == Qt.LeftButton: + # if event.wasHeld: + self.dragStartPosition = event.pos() + # else: + # # super(toolbarButton, self).mousePressEvent(event) + # self.clicked.emit() + + def mouseMoveEvent(self, event): + if not (event.buttons() and Qt.LeftButton): + return + if (event.pos() - self.dragStartPosition).manhattanLength() < QApplication.startDragDistance(): + return + + drag = QDrag(self) + mimeData = QMimeData() + mimeData.setText(self.itemObject) + drag.setMimeData(mimeData) + drag.exec(Qt.CopyAction) + def sizeHint(self): return self.minimumSizeHint() |