diff options
author | pravindalve | 2020-05-19 12:22:05 +0530 |
---|---|---|
committer | GitHub | 2020-05-19 12:22:05 +0530 |
commit | f1a75684e63a1afecb1e16a00fc776a1a5e91036 (patch) | |
tree | c4f4e72e54fb11e9ad511a66a439d56c5317fbc5 /src/main/python/utils | |
parent | 151ffe398218f8d46b07f9ea12b4af897b4ed81b (diff) | |
parent | 955175aea39b4ed1d3bf3309b63cfc74e7e91ae5 (diff) | |
download | Chemical-PFD-f1a75684e63a1afecb1e16a00fc776a1a5e91036.tar.gz Chemical-PFD-f1a75684e63a1afecb1e16a00fc776a1a5e91036.tar.bz2 Chemical-PFD-f1a75684e63a1afecb1e16a00fc776a1a5e91036.zip |
Merge pull request #2 from Blakeinstein/master
[Important] Helper commit to help merge shape branch with master
Diffstat (limited to 'src/main/python/utils')
-rw-r--r-- | src/main/python/utils/dialogs.py | 10 | ||||
-rw-r--r-- | src/main/python/utils/fileWindow.py | 2 | ||||
-rw-r--r-- | src/main/python/utils/graphics.py | 75 | ||||
-rw-r--r-- | src/main/python/utils/undo.py | 63 |
4 files changed, 141 insertions, 9 deletions
diff --git a/src/main/python/utils/dialogs.py b/src/main/python/utils/dialogs.py index e008aa5..3791599 100644 --- a/src/main/python/utils/dialogs.py +++ b/src/main/python/utils/dialogs.py @@ -99,4 +99,12 @@ def saveEvent(parent = None): if alert == QMessageBox.Save: if not parent.saveProject(): #the parent's saveProject method is called which returns false if saving was cancelled by the user return False - return True
\ No newline at end of file + return True + +def showUndoDialog(undoView, parent): + dialogBox = QDialog(parent) + dialogBox.resize(400, 400) + layout = QFormLayout(dialogBox) + layout.addWidget(undoView) + dialogBox.setWindowTitle("Undo Stack") + dialogBox.show()
\ No newline at end of file diff --git a/src/main/python/utils/fileWindow.py b/src/main/python/utils/fileWindow.py index e549568..8f2fd32 100644 --- a/src/main/python/utils/fileWindow.py +++ b/src/main/python/utils/fileWindow.py @@ -18,6 +18,7 @@ class fileWindow(QMdiSubWindow): canvases. Pre-Defined so that a file can be instantly created without defining the structure again. """ fileCloseEvent = pyqtSignal(int) + tabChangeEvent = pyqtSignal() def __init__(self, parent = None, title = 'New Project', size = 'A4', ppi = '72'): super(fileWindow, self).__init__(parent) @@ -203,6 +204,7 @@ class fileWindow(QMdiSubWindow): def changeTab(self, currentIndex): #placeholder function to detect tab change self.resizeHandler() + self.tabChangeEvent.emit() def closeTab(self, currentIndex): #show save alert on tab close diff --git a/src/main/python/utils/graphics.py b/src/main/python/utils/graphics.py index 0fe0030..62c62f7 100644 --- a/src/main/python/utils/graphics.py +++ b/src/main/python/utils/graphics.py @@ -1,7 +1,11 @@ -from PyQt5.QtCore import Qt -from PyQt5.QtGui import QPen -from PyQt5.QtWidgets import QGraphicsView, QGraphicsScene, QGraphicsProxyWidget, QGraphicsItem -from PyQt5 import QtWidgets +from PyQt5.QtCore import Qt, QPointF +from PyQt5.QtGui import QPen, QKeySequence +from PyQt5.QtWidgets import QGraphicsView, QGraphicsScene, QGraphicsProxyWidget, QGraphicsItem, QUndoStack, QAction, QUndoView + +from .undo import * +from .dialogs import showUndoDialog + +import shapes class customView(QGraphicsView): """ @@ -15,6 +19,10 @@ class customView(QGraphicsView): self._zoom = 1 self.setDragMode(True) #sets pannable using mouse self.setAcceptDrops(True) #sets ability to accept drops + if scene: + self.addAction(scene.undoAction) + self.addAction(scene.redoAction) + self.addAction(scene.deleteAction) #following four functions are required to be overridden for drag-drop functionality def dragEnterEvent(self, QDragEnterEvent): @@ -35,10 +43,10 @@ class customView(QGraphicsView): #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 = getattr(shapes, 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) + self.scene().addItemPlus(graphic) QDropEvent.acceptProposedAction() def wheelEvent(self, QWheelEvent): @@ -73,5 +81,56 @@ class customScene(QGraphicsScene): re-implement QGraphicsScene for future functionality hint: QUndoFramework """ - pass -
\ No newline at end of file + def __init__(self, *args, parent=None): + super(customScene, self).__init__(*args, parent=parent) + + self.undoStack = QUndoStack(self) + self.createActions() + + + def createActions(self): + self.deleteAction = QAction("Delete Item", self) + self.deleteAction.setShortcut(Qt.Key_Delete) + self.deleteAction.triggered.connect(self.deleteItem) + + self.undoAction = self.undoStack.createUndoAction(self, "Undo") + self.undoAction.setShortcut(QKeySequence.Undo) + self.redoAction = self.undoStack.createRedoAction(self, "Redo") + self.redoAction.setShortcut(QKeySequence.Redo) + + def createUndoView(self, parent): + undoView = QUndoView(self.undoStack, parent) + # undoView.resize(400, 400) + # undoView.show() + # undoView.setAttribute(Qt.WA_QuitOnClose, False) + showUndoDialog(undoView, parent) + + def deleteItem(self): + if self.selectedItems(): + for item in self.selectedItems(): + self.undoStack.push(deleteCommand(item, self)) + + def itemMoved(self, movedItem, lastPos): + self.undoStack.push(moveCommand(movedItem, lastPos)) + + def addItemPlus(self, item): + # returnVal = self.addItem(item) + self.undoStack.push(addCommand(item, self)) + # return returnVal + + def mousePressEvent(self, event): + bdsp = event.buttonDownScenePos(Qt.LeftButton) + point = QPointF(bdsp.x(), bdsp.y()) + itemList = self.items(point) + self.movingItem = itemList[0] if itemList else None + if self.movingItem and event.button() == Qt.LeftButton: + self.oldPos = self.movingItem.pos() + self.clearSelection() + return super(customScene, self).mousePressEvent(event) + + def mouseReleaseEvent(self, event): + if self.movingItem and event.button() == Qt.LeftButton: + if self.oldPos != self.movingItem.pos(): + self.itemMoved(self.movingItem, self.oldPos) + self.movingItem = None + return super(customScene, self).mouseReleaseEvent(event) diff --git a/src/main/python/utils/undo.py b/src/main/python/utils/undo.py new file mode 100644 index 0000000..7832483 --- /dev/null +++ b/src/main/python/utils/undo.py @@ -0,0 +1,63 @@ +from PyQt5.QtWidgets import QUndoCommand + +class addCommand(QUndoCommand): + + def __init__(self, addItem, scene, parent = None): + super(addCommand, self).__init__(parent) + self.scene = scene + self.diagramItem = addItem + self.itemPos = addItem.pos() + self.setText(f"Add {self.diagramItem} {self.itemPos}") + + def undo(self): + self.scene.removeItem(self.diagramItem) + self.scene.update() + + def redo(self): + self.scene.addItem(self.diagramItem) + self.diagramItem.setPos(self.itemPos) + self.scene.clearSelection() + self.scene.update() + +class deleteCommand(QUndoCommand): + + def __init__(self, item, scene, parent = None): + super(deleteCommand, self).__init__(parent) + self.scene = scene + item.setSelected(False) + self.diagramItem = item + self.setText(f"Delete {self.diagramItem} {self.diagramItem.pos()}") + + def undo(self): + self.scene.addItem(self.diagramItem) + self.scene.update() + + def redo(self): + self.scene.removeItem(self.diagramItem) + +class moveCommand(QUndoCommand): + + def __init__(self, item, lastPos, parent = None): + super(moveCommand, self).__init__(parent) + self.diagramItem = item + self.lastPos = lastPos + self.newPos = item.pos() + + def undo(self): + self.diagramItem.setPos(self.lastPos) + self.diagramItem.scene().update() + self.setText(f"Move {self.diagramItem} {self.newPos}") + + def redo(self): + self.diagramItem.setPos(self.newPos) + self.setText(f"Move {self.diagramItem} {self.newPos}") + + def mergeWith(self, move): + item = move.diagramItem + + if self.diagramItem != item: + return False + + self.newPos = item.pos() + self.setText(f"Move {self.diagramItem} {self.newPos}") + return True
\ No newline at end of file |