summaryrefslogtreecommitdiff
path: root/src/main/python/utils/undo.py
diff options
context:
space:
mode:
authorBlaine2020-05-18 19:58:32 +0530
committerBlaine2020-05-18 19:58:32 +0530
commitabbdeffa7260c97188e82ff4d727348aa844ce93 (patch)
treeea2bb143cda3584b36c71e6b7251e2e003246956 /src/main/python/utils/undo.py
parent051af95251fd0f87457322d9bd80ca99309c6c56 (diff)
downloadChemical-PFD-abbdeffa7260c97188e82ff4d727348aa844ce93.tar.gz
Chemical-PFD-abbdeffa7260c97188e82ff4d727348aa844ce93.tar.bz2
Chemical-PFD-abbdeffa7260c97188e82ff4d727348aa844ce93.zip
Undo Framework implemented
Diffstat (limited to 'src/main/python/utils/undo.py')
-rw-r--r--src/main/python/utils/undo.py63
1 files changed, 63 insertions, 0 deletions
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