From 8713e4293b7297e2f0d96c9439e870fbd934e975 Mon Sep 17 00:00:00 2001 From: Blaine Date: Wed, 22 Apr 2020 16:05:50 +0530 Subject: Add save and load feature --- src/main/python/utils/graphics.py | 1 + 1 file changed, 1 insertion(+) create mode 100644 src/main/python/utils/graphics.py (limited to 'src/main/python/utils/graphics.py') diff --git a/src/main/python/utils/graphics.py b/src/main/python/utils/graphics.py new file mode 100644 index 0000000..fc80254 --- /dev/null +++ b/src/main/python/utils/graphics.py @@ -0,0 +1 @@ +pass \ No newline at end of file -- cgit From 97b304ea746d3ad380fa1ac31409cadc4dbb139b Mon Sep 17 00:00:00 2001 From: Blaine Date: Wed, 6 May 2020 16:51:02 +0530 Subject: scroll wheel init --- src/main/python/utils/graphics.py | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) (limited to 'src/main/python/utils/graphics.py') diff --git a/src/main/python/utils/graphics.py b/src/main/python/utils/graphics.py index fc80254..9e1f690 100644 --- a/src/main/python/utils/graphics.py +++ b/src/main/python/utils/graphics.py @@ -1 +1,15 @@ -pass \ No newline at end of file +from PyQt5.QtCore import Qt +from PyQt5.QtWidgets import QGraphicsView +class customView(QGraphicsView): + + def __init__(self, scene, parent=None): + super(customView, self).__init__(scene, parent) + self.zoom = 1 + + def wheelEvent(self, QWheelEvent): + if Qt.ControlModifier: + self.zoom += QWheelEvent.angleDelta().y()/2880 + self.scale(self.zoom, self.zoom) + QWheelEvent.accept() + else: + return super().wheelEvent(self, QWheelEvent) \ No newline at end of file -- cgit From ecb94d6b121d0c3c9d79dc58140535432cfef973 Mon Sep 17 00:00:00 2001 From: Blaine Date: Wed, 6 May 2020 18:24:38 +0530 Subject: Fix zoom in zoom out --- src/main/python/utils/graphics.py | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) (limited to 'src/main/python/utils/graphics.py') diff --git a/src/main/python/utils/graphics.py b/src/main/python/utils/graphics.py index 9e1f690..aed7eed 100644 --- a/src/main/python/utils/graphics.py +++ b/src/main/python/utils/graphics.py @@ -1,15 +1,26 @@ from PyQt5.QtCore import Qt from PyQt5.QtWidgets import QGraphicsView + class customView(QGraphicsView): - def __init__(self, scene, parent=None): - super(customView, self).__init__(scene, parent) + def __init__(self, scene = None, parent=None): + if scene is not None: + super(customView, self).__init__(scene, parent) + else: + super(customView, self).__init__(parent) self.zoom = 1 + self.setDragMode(True) def wheelEvent(self, QWheelEvent): if Qt.ControlModifier: - self.zoom += QWheelEvent.angleDelta().y()/2880 - self.scale(self.zoom, self.zoom) + temp = self.zoom + if QWheelEvent.source() == Qt.MouseEventNotSynthesized: + if self.zoom + QWheelEvent.angleDelta().y()/2880 > 0.1: + self.zoom += QWheelEvent.angleDelta().y()/2880 + else: + if self.zoom + QWheelEvent.pixelDelta().y() > 0.1: + self.zoom += QWheelEvent.angleDelta().y() + self.scale(self.zoom / temp, self.zoom / temp) QWheelEvent.accept() else: return super().wheelEvent(self, QWheelEvent) \ No newline at end of file -- cgit From 6c5a39579c737d0054e29403ec0a045cc6a6d52a Mon Sep 17 00:00:00 2001 From: Blaine Date: Wed, 6 May 2020 20:39:07 +0530 Subject: revert close button move --- src/main/python/utils/graphics.py | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) (limited to 'src/main/python/utils/graphics.py') diff --git a/src/main/python/utils/graphics.py b/src/main/python/utils/graphics.py index aed7eed..8c87b96 100644 --- a/src/main/python/utils/graphics.py +++ b/src/main/python/utils/graphics.py @@ -8,19 +8,27 @@ class customView(QGraphicsView): super(customView, self).__init__(scene, parent) else: super(customView, self).__init__(parent) - self.zoom = 1 + self._zoom = 1 self.setDragMode(True) def wheelEvent(self, QWheelEvent): if Qt.ControlModifier: - temp = self.zoom if QWheelEvent.source() == Qt.MouseEventNotSynthesized: if self.zoom + QWheelEvent.angleDelta().y()/2880 > 0.1: self.zoom += QWheelEvent.angleDelta().y()/2880 else: if self.zoom + QWheelEvent.pixelDelta().y() > 0.1: self.zoom += QWheelEvent.angleDelta().y() - self.scale(self.zoom / temp, self.zoom / temp) QWheelEvent.accept() else: - return super().wheelEvent(self, QWheelEvent) \ No newline at end of file + return super().wheelEvent(self, QWheelEvent) + + @property + def zoom(self): + return self._zoom + + @zoom.setter + def zoom(self, value): + temp = self.zoom + self._zoom = value + self.scale(self.zoom / temp, self.zoom / temp) \ No newline at end of file -- cgit From c70c63d437bca2bb6fbc428281a6aac4f3a28bd8 Mon Sep 17 00:00:00 2001 From: Blaine Date: Sat, 9 May 2020 14:40:23 +0530 Subject: Week 3 update --- src/main/python/utils/graphics.py | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) (limited to 'src/main/python/utils/graphics.py') diff --git a/src/main/python/utils/graphics.py b/src/main/python/utils/graphics.py index 8c87b96..4d754b5 100644 --- a/src/main/python/utils/graphics.py +++ b/src/main/python/utils/graphics.py @@ -2,9 +2,11 @@ from PyQt5.QtCore import Qt from PyQt5.QtWidgets import QGraphicsView class customView(QGraphicsView): - + """ + Defines custom QGraphicsView with zoom features, overriding wheel event + """ def __init__(self, scene = None, parent=None): - if scene is not None: + if scene is not None: #overloaded constructor super(customView, self).__init__(scene, parent) else: super(customView, self).__init__(parent) @@ -12,23 +14,28 @@ class customView(QGraphicsView): self.setDragMode(True) def wheelEvent(self, QWheelEvent): - if Qt.ControlModifier: - if QWheelEvent.source() == Qt.MouseEventNotSynthesized: - if self.zoom + QWheelEvent.angleDelta().y()/2880 > 0.1: + #overload wheelevent, to zoom if control is pressed, else scroll normally + if Qt.ControlModifier: #check if control is pressed + if QWheelEvent.source() == Qt.MouseEventNotSynthesized: #check if precision mouse(mac) + # angle delta is 1/8th of a degree per scroll unit + if self.zoom + QWheelEvent.angleDelta().y()/2880 > 0.1: # hit and trial value (2880) self.zoom += QWheelEvent.angleDelta().y()/2880 else: + # precision delta is exactly equal to amount to scroll if self.zoom + QWheelEvent.pixelDelta().y() > 0.1: self.zoom += QWheelEvent.angleDelta().y() - QWheelEvent.accept() + QWheelEvent.accept() # accept event so that scrolling doesnt happen simultaneously else: - return super().wheelEvent(self, QWheelEvent) + return super().wheelEvent(self, QWheelEvent) # scroll if ctrl not pressed @property def zoom(self): + # property for zoom return self._zoom @zoom.setter def zoom(self, value): + # 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 -- cgit From aab2924c33eaa4de26203aee76a8e1f4a8ee559d Mon Sep 17 00:00:00 2001 From: Blaine Date: Thu, 14 May 2020 20:56:49 +0530 Subject: enable drag and drop --- src/main/python/utils/graphics.py | 43 +++++++++++++++++++++++++++++++++++++-- 1 file changed, 41 insertions(+), 2 deletions(-) (limited to 'src/main/python/utils/graphics.py') 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 -- cgit From e666d20c72138e1cb679b2877d90604c06130926 Mon Sep 17 00:00:00 2001 From: Blaine Date: Fri, 15 May 2020 11:56:00 +0530 Subject: working fix --- src/main/python/utils/graphics.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src/main/python/utils/graphics.py') diff --git a/src/main/python/utils/graphics.py b/src/main/python/utils/graphics.py index 243b086..48d031b 100644 --- a/src/main/python/utils/graphics.py +++ b/src/main/python/utils/graphics.py @@ -25,8 +25,8 @@ class customView(QGraphicsView): QDragMoveEvent.acceptProposedAction() def dropEvent(self, QDropEvent): - if QDropEvent.mimeData().hasText(): - graphic = getattr(QtWidgets, QDropEvent.mimeData().text())(QDropEvent.pos().x(), QDropEvent.pos().y(), 300, 300) + if QDropEvent.mimeData().hasText(): + graphic = getattr(QtWidgets, QDropEvent.mimeData().text())(QDropEvent.pos().x()-150, QDropEvent.pos().y()-150, 300, 300) graphic.setPen(QPen(Qt.black, 2)) graphic.setFlags(QGraphicsItem.ItemIsSelectable | QGraphicsItem.ItemIsMovable) self.scene().addItem(graphic) -- cgit From c6a60b1c69daaa819ba52d29c5f5dc8a83023892 Mon Sep 17 00:00:00 2001 From: Blaine Date: Fri, 15 May 2020 18:14:17 +0530 Subject: minor cleanup --- src/main/python/utils/graphics.py | 2 -- 1 file changed, 2 deletions(-) (limited to 'src/main/python/utils/graphics.py') diff --git a/src/main/python/utils/graphics.py b/src/main/python/utils/graphics.py index 48d031b..6b2066f 100644 --- a/src/main/python/utils/graphics.py +++ b/src/main/python/utils/graphics.py @@ -60,8 +60,6 @@ class customView(QGraphicsView): 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() -- cgit From 7df3f6ab2aadbc178173a26a299c9a809aaa35f4 Mon Sep 17 00:00:00 2001 From: Blaine Date: Sat, 16 May 2020 11:55:23 +0530 Subject: QDragLeaveEvent + Generator functions --- src/main/python/utils/graphics.py | 3 +++ 1 file changed, 3 insertions(+) (limited to 'src/main/python/utils/graphics.py') diff --git a/src/main/python/utils/graphics.py b/src/main/python/utils/graphics.py index 6b2066f..dce664a 100644 --- a/src/main/python/utils/graphics.py +++ b/src/main/python/utils/graphics.py @@ -23,6 +23,9 @@ class customView(QGraphicsView): def dragMoveEvent(self, QDragMoveEvent): if QDragMoveEvent.mimeData().hasText(): QDragMoveEvent.acceptProposedAction() + + def dragLeaveEvent(self, QDragLeaveEvent): + QDragLeaveEvent.accept() def dropEvent(self, QDropEvent): if QDropEvent.mimeData().hasText(): -- cgit From 051af95251fd0f87457322d9bd80ca99309c6c56 Mon Sep 17 00:00:00 2001 From: Blaine Date: Sat, 16 May 2020 18:24:07 +0530 Subject: minor clean up and comments --- src/main/python/utils/graphics.py | 32 ++++++++++++++------------------ 1 file changed, 14 insertions(+), 18 deletions(-) (limited to 'src/main/python/utils/graphics.py') diff --git a/src/main/python/utils/graphics.py b/src/main/python/utils/graphics.py index dce664a..0fe0030 100644 --- a/src/main/python/utils/graphics.py +++ b/src/main/python/utils/graphics.py @@ -5,7 +5,7 @@ from PyQt5 import QtWidgets class customView(QGraphicsView): """ - Defines custom QGraphicsView with zoom features, overriding wheel event + Defines custom QGraphicsView with zoom features and drag-drop accept event, overriding wheel event """ def __init__(self, scene = None, parent=None): if scene is not None: #overloaded constructor @@ -13,22 +13,28 @@ class customView(QGraphicsView): else: super(customView, self).__init__(parent) self._zoom = 1 - self.setDragMode(True) - self.setAcceptDrops(True) + self.setDragMode(True) #sets pannable using mouse + self.setAcceptDrops(True) #sets ability to accept drops + #following four functions are required to be overridden for drag-drop functionality def dragEnterEvent(self, QDragEnterEvent): + #defines acceptable drop items if QDragEnterEvent.mimeData().hasText(): QDragEnterEvent.acceptProposedAction() def dragMoveEvent(self, QDragMoveEvent): + #defines acceptable drop items if QDragMoveEvent.mimeData().hasText(): QDragMoveEvent.acceptProposedAction() def dragLeaveEvent(self, QDragLeaveEvent): + #accept any drag leave event, avoid unnecessary logging QDragLeaveEvent.accept() def dropEvent(self, QDropEvent): + #defines item drop, fetches text, creates corresponding QGraphicItem and adds it to scene if QDropEvent.mimeData().hasText(): + #QDropEvent.mimeData().text() defines intended drop item, the pos values define position graphic = getattr(QtWidgets, QDropEvent.mimeData().text())(QDropEvent.pos().x()-150, QDropEvent.pos().y()-150, 300, 300) graphic.setPen(QPen(Qt.black, 2)) graphic.setFlags(QGraphicsItem.ItemIsSelectable | QGraphicsItem.ItemIsMovable) @@ -63,19 +69,9 @@ class customView(QGraphicsView): self.scale(self.zoom / temp, self.zoom / temp) class customScene(QGraphicsScene): - 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() + """ + re-implement QGraphicsScene for future functionality + hint: QUndoFramework + """ + pass \ No newline at end of file -- cgit