-
Notifications
You must be signed in to change notification settings - Fork 0
/
undoRedo.py
237 lines (185 loc) · 7.38 KB
/
undoRedo.py
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
from __future__ import annotations
import copy
from abc import ABC, abstractmethod
from dataclasses import dataclass, field
from typing import Generic, Optional, TypeVar, Protocol, AnyStr
from PyQt5.QtGui import QColor, QPixmap
from .GUI import addWidgetDrawer, PythonGUI, SizePolicy
from .utils.collections_ import Stack
_TTarget = TypeVar("_TTarget")
_TDocument = TypeVar("_TDocument")
class MementoABC(Generic[_TTarget], ABC):
@abstractmethod
def restore(self, currentState: _TTarget, lastRecordedState: _TTarget, doDeepCopy: bool) -> tuple[MementoABC[_TTarget], _TTarget, _TTarget]:
"""
:param currentState:
:param lastRecordedState:
:param doDeepCopy:
:return: (redoMemento, newState, lastRecordeState / newStateCopy)
"""
pass
class MakeMementoIfDiffFunc(Protocol[_TTarget]):
def __call__(self, oldState: Optional[_TTarget], newState: _TTarget, doDeepCopy: bool) -> tuple[Optional[MementoABC[_TTarget]], _TTarget]:
...
@dataclass
class SnapshotMemento(MementoABC[_TTarget]):
_oldState: _TTarget
def restore(self, currentState: _TTarget, lastRecordedState: _TTarget, doDeepCopy: bool) -> tuple[MementoABC[_TTarget], _TTarget, _TTarget]:
lastRecordeState = copy.deepcopy(self._oldState) if doDeepCopy else copy.copy(self._oldState)
# no deepcopy for currentState required here, because it will be replaced with oldState
return SnapshotMemento(currentState), self._oldState, lastRecordeState
@dataclass
class AnyStrMemento(MementoABC[AnyStr]):
_oldState: AnyStr
def restore(self, currentState: AnyStr, lastRecordedState: AnyStr, doDeepCopy: bool) -> tuple[AnyStrMemento[AnyStr], AnyStr, AnyStr]:
return AnyStrMemento(currentState), self._oldState, self._oldState
def makesSnapshotMementoIfDiff(oldState: Optional[_TTarget], newState: _TTarget, doDeepCopy: bool) -> tuple[Optional[SnapshotMemento[_TTarget]], _TTarget]:
if oldState == newState:
memento, newStateCopy = None, oldState
else:
memento = SnapshotMemento(copy.deepcopy(oldState)) if oldState is not None else None
newStateCopy = copy.deepcopy(newState) if doDeepCopy else copy.copy(newState)
return memento, newStateCopy
def makesAnyStrMementoIfDiff(oldState: Optional[AnyStr], newState: AnyStr, doDeepCopy: bool) -> tuple[Optional[AnyStrMemento[AnyStr]], AnyStr]:
if oldState is None or oldState == newState:
return None, newState
else:
return AnyStrMemento(oldState), newState
@dataclass
class UndoRedoStack2(Generic[_TDocument, _TTarget]):
_document: _TDocument
_dataProperty: str
_makeMementoIfDiff: MakeMementoIfDiffFunc[_TTarget]
doDeepCopy: bool = field(kw_only=True)
_undoStack: Stack[MementoABC[_TTarget]] = field(default_factory=Stack, init=False)
_redoStack: Stack[MementoABC[_TTarget]] = field(default_factory=Stack, init=False)
_isUndoingOrRedoing: bool = field(default=False, init=False)
lastRecordedState: Optional[_TTarget] = field(default=None, init=False)
def takeSnapshot(self):
currentState = self._getCurrentState()
memento, newStateCopy = self._makeMementoIfDiff(self.lastRecordedState, currentState, self.doDeepCopy)
if memento is not None:
self._undoStack.push(memento)
self.clearRedoStack()
self.lastRecordedState = newStateCopy
def takeSnapshotIfChanged(self):
return self.takeSnapshot()
@property
def isUndoingOrRedoing(self) -> bool:
return self._isUndoingOrRedoing
@property
def canUndo(self) -> bool:
return bool(self._undoStack)
@property
def canRedo(self) -> bool:
return bool(self._redoStack)
def canUndoMultiple(self, n: int) -> bool:
return len(self._undoStack) >= n
def canRedoMultiple(self, n: int) -> bool:
return len(self._redoStack) >= n
@property
def undoCount(self) -> int:
return len(self._undoStack)
@property
def redoCount(self) -> int:
return len(self._redoStack)
@property
def undoStack(self) -> Stack[MementoABC[_TTarget]]:
return self._undoStack
@property
def redoStack(self) -> Stack[MementoABC[_TTarget]]:
return self._redoStack
def undoOnce(self):
if not self.canUndo:
return
memento: MementoABC[_TTarget] = self._undoStack.pop()
redoMemento, newState, self.lastRecordedState = memento.restore(self._getCurrentState(), self.lastRecordedState, self.doDeepCopy)
self._redoStack.push(redoMemento)
self._setCurrentState(newState)
def redoOnce(self):
if not self.canRedo:
return
memento: MementoABC[_TTarget] = self._redoStack.pop()
undoMemento, newState, self.lastRecordedState = memento.restore(self._getCurrentState(), self.lastRecordedState, self.doDeepCopy)
self._undoStack.push(undoMemento)
self._setCurrentState(newState)
def undoMultiple(self, n: int):
n = min(self.undoCount, n)
if n <= 0:
return
newState = self._getCurrentState()
lastRecordedState = self.lastRecordedState
doDeepCopy = self.doDeepCopy
for i in range(n):
memento: MementoABC[_TTarget] = self._undoStack.pop()
redoMemento, newState, lastRecordedState = memento.restore(newState, lastRecordedState, doDeepCopy)
self._redoStack.push(redoMemento)
self.lastRecordedState = lastRecordedState
self._setCurrentState(newState)
def redoMultiple(self, n: int):
n = min(self.redoCount, n)
if n <= 0:
return
newState = self._getCurrentState()
lastRecordedState = self.lastRecordedState
doDeepCopy = self.doDeepCopy
for i in range(n):
memento: MementoABC[_TTarget] = self._redoStack.pop()
undoMemento, newState, lastRecordedState = memento.restore(newState, lastRecordedState, doDeepCopy)
self._undoStack.push(undoMemento)
self.lastRecordedState = lastRecordedState
self._setCurrentState(newState)
def _getCurrentState(self) -> _TTarget:
assert self._document is not None
assert self._dataProperty is not None
return getattr(self._document, self._dataProperty)
def _setCurrentState(self, state: _TTarget):
assert self._document is not None
assert self._dataProperty is not None
# self.lastRecordedState = state
self._isUndoingOrRedoing = True
try:
setattr(self._document, self._dataProperty, state)
finally:
self._isUndoingOrRedoing = False
def clearRedoStack(self):
self._redoStack.clear()
def colorLabel(gui: PythonGUI, v: QColor):
pixmap = QPixmap(16, 16)
pixmap.fill(v)
gui.label(pixmap)
def undoEntry(gui: PythonGUI, label: Optional[str], text: str, color: QColor):
with gui.hLayout(label):
if text:
gui.textField('Bla')
else:
gui.textField('')
colorLabel(gui, color)
def drawUndoRedoStack2(gui: PythonGUI, v: UndoRedoStack2, **kwargs) -> UndoRedoStack2:
undoStack: Stack = v.undoStack
redoStack: Stack = v.redoStack
with gui.hLayout():
if gui.button('Clear All'):
undoStack.clear()
redoStack.clear()
if gui.button('Clear Undo'):
undoStack.clear()
if gui.button('Clear Redo'):
redoStack.clear()
gui.addHSpacer(50, SizePolicy.Expanding)
with gui.scrollBox(preventVStretch=True):
for i, u in enumerate(undoStack):
if gui.button('o', isPrefix=True):
undoCount = len(undoStack) - i
v.undoMultiple(undoCount)
undoEntry(gui, label=None, text=' ', color=QColor(64, 64, 192, 255))
hasChanged = False # v.lastRecordedState != snapShot
color = QColor(192, 64, 64, 255) if hasChanged else QColor(64, 192, 64, 255)
undoEntry(gui, label='-->', text=v.lastRecordedState, color=color)
for i, u in enumerate(reversed(redoStack), 1):
if gui.button('o', isPrefix=True):
redoCount = i
v.redoMultiple(redoCount)
undoEntry(gui, label=None, text=' ', color=QColor(224, 192, 64, 255))
return v
addWidgetDrawer(UndoRedoStack2, drawUndoRedoStack2)