summaryrefslogtreecommitdiff
path: root/src/main/python/utils/undo.py
blob: 78324837a3416f1cb21c98c9ae70a2726a162b32 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
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