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
|
"""
QUndoCommand class for generic undoable user actions performed to a BaseComponent
See `../life.py` for an example of a component that uses a custom QUndoCommand
"""
from PyQt6.QtGui import QUndoCommand
from copy import copy
import logging
log = logging.getLogger("AVP.ComponentHandler")
class ComponentUpdate(QUndoCommand):
"""Command object for making a component action undoable"""
def __init__(self, parent, oldWidgetVals, modifiedVals):
super().__init__("change %s component #%s" % (parent.name, parent.compPos))
self.undone = False
self.res = (int(parent.width), int(parent.height))
self.parent = parent
self.oldWidgetVals = {
attr: (
copy(val)
if attr not in self.parent._relativeWidgets
else self.parent.floatValForAttr(attr, val, axis=self.res)
)
for attr, val in oldWidgetVals.items()
if attr in modifiedVals
}
self.modifiedVals = {
attr: (
val
if attr not in self.parent._relativeWidgets
else self.parent.floatValForAttr(attr, val, axis=self.res)
)
for attr, val in modifiedVals.items()
}
# Because relative widgets change themselves every update based on
# their previous value, we must store ALL their values in case of undo
self.relativeWidgetValsAfterUndo = {
attr: copy(getattr(self.parent, attr))
for attr in self.parent._relativeWidgets
}
# Determine if this update is mergeable
self.id_ = -1
if self.parent.mergeUndo:
if len(self.modifiedVals) == 1:
attr, val = self.modifiedVals.popitem()
self.id_ = sum([ord(letter) for letter in attr[-14:]])
self.modifiedVals[attr] = val
return
log.warning(
"%s component settings changed at once. (%s)",
len(self.modifiedVals),
repr(self.modifiedVals),
)
def id(self):
"""If 2 consecutive updates have same id, Qt will call mergeWith()"""
return self.id_
def mergeWith(self, other):
self.modifiedVals.update(other.modifiedVals)
return True
def setWidgetValues(self, attrDict):
"""
Mask the component's usual method to handle our
relative widgets in case the resolution has changed.
"""
newAttrDict = {
attr: (
val
if attr not in self.parent._relativeWidgets
else self.parent.pixelValForAttr(attr, val)
)
for attr, val in attrDict.items()
}
self.parent.setWidgetValues(newAttrDict)
def redo(self):
if self.undone:
log.info("Redoing component update")
self.parent.oldAttrs = self.relativeWidgetValsAfterUndo
self.setWidgetValues(self.modifiedVals)
self.parent.update(auto=True)
self.parent.oldAttrs = None
if not self.undone:
self.relativeWidgetValsAfterRedo = {
attr: copy(getattr(self.parent, attr))
for attr in self.parent._relativeWidgets
}
self.parent._sendUpdateSignal()
def undo(self):
log.info("Undoing component update")
self.undone = True
self.parent.oldAttrs = self.relativeWidgetValsAfterRedo
self.setWidgetValues(self.oldWidgetVals)
self.parent.update(auto=True)
self.parent.oldAttrs = None
|