forked from wxGlade/wxGlade
-
Notifications
You must be signed in to change notification settings - Fork 0
/
history.py
478 lines (411 loc) · 18.1 KB
/
history.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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
"""\
history for undo/redo/repeat
copyright: 2017-2021 Dietmar Schwertberger
license: MIT (see LICENSE.txt) - THIS PROGRAM COMES WITH NO WARRANTY
"""
import common, config, clipboard, misc
import wx
def copy_value(prop):
# make a copy for lists or sets
if hasattr(prop, "value_set"):
value = prop.value_set
else:
value = prop.value
if isinstance(value, list):
if value and isinstance(value[0], list):
return [v[:] for v in value]
else:
return value[:]
elif isinstance(value, set):
return set(value)
return value
class PropertyValue(object):
# used by HistoryPropertyItem to track old and new value and state of a property
def __init__(self, prop):
self.deactivated = prop.deactivated
self.value = copy_value(prop)
def set(self, p):
# apply value to a property instance
if hasattr(p, "value_set"):
p.value_set.clear()
p.value_set.update(self.value)
p.value = None
else:
value = p.value
# lists and sets are modified in place
if isinstance(value, list):
value[:] = self.value
elif isinstance(value, set): # not yet used
value.clear()
value.update(self.value)
else:
p.value = self.value
activate = self.deactivated!=p.deactivated
p.deactivated = self.deactivated
p.update_display()
if activate: p.activate_controls()
def __eq__(self, other):
return self.deactivated==other.deactivated and self.value==other.value
def __repr__(self):
return "(%r, %r)"%(self.deactivated, self.value)
class HistoryItem(object):
def __init__(self, prop):
self.path = prop.owner.get_path()
self.name = prop.name
def get_key(self):
return self.name
class HistoryPropertyItem(HistoryItem):
def __init__(self, prop):
HistoryItem.__init__(self, prop)
self._prop = prop # keep a reference until finalize is called
self.old = PropertyValue(prop)
self.new = None
self.dependent = []
def finalize(self, monitor):
self.new = PropertyValue(self._prop)
self._prop = None
# check whether other, depending properties were changed as well
for prop, old in monitor:
new = PropertyValue(prop)
if new!=old:
self.dependent.append( [prop.owner.get_path(), prop.name, old, new] )
def undo(self):
owner = common.root.find_widget_from_path(self.path)
p = owner.properties[self.name]
self.old.set(p)
changed = [self.name]
for path, name, old, new in self.dependent:
changed.append(name)
if path==self.path:
old.set( owner.properties[name] )
else:
owner_ = common.root.find_widget_from_path(self.path)
old.set( owner_.properties[name] )
owner.properties_changed(changed)
misc.set_focused_widget(owner)
def redo(self):
owner = common.root.find_widget_from_path(self.path)
p = owner.properties[self.name]
self.new.set(p)
changed = [self.name]
for path, name, old, new in self.dependent:
changed.append(name)
if path==self.path:
new.set( owner.properties[name] )
else:
owner_ = common.root.find_widget_from_path(self.path)
new.set( owner_.properties[name] )
owner.properties_changed(changed)
misc.set_focused_widget(owner)
def __repr__(self):
return "%s(%s, %r, %r, %r)"%(self.__class__.__name__, self.path, self.name, self.old, self.new)
class HistorySetPropertyItem(HistoryPropertyItem):
# same as before, but tracks the flag that was checked/unchecked (self.flag_value, self.checked)
def __init__(self, prop):
HistoryPropertyItem.__init__(self, prop)
self.flag_value = self.checked = None
def __repr__(self):
return "%s(%s, %s, %r, %r)"%(self.__class__.__name__, self.path, self.name, self.flag_value, self.checked)
def get_key(self):
return (self.name, self.flag_value)
class HistoryRemovedItem(HistoryItem):
def __init__(self, widget):
self.IS_SLOT = widget.IS_SLOT
self.index = widget.index
self.path = widget.get_path()
self.xml_data = clipboard.dump_widget(widget)
self.slot_path = self.slot_tab = None
parent = widget.parent
if widget.IS_SLOT and parent.IS_CONTAINER and parent.check_prop("tabs"):
# a slot / page is being removed from a notebook -> store tab name
self.slot_tab = parent.tabs[widget.index][0]
def finalize(self, slot=None):
if slot:
# a slot has been left
self.slot_path = slot.get_path()
def undo(self):
# identical to HistoryAddedItem.redo, except for the slot_tab part
path = self.slot_path or self.path.rsplit("/",1)[0] # slot or parent
widget = common.root.find_widget_from_path(path)
if widget.IS_ROOT:# or widget.IS_CONTAINER:
widget.clipboard_paste(self.xml_data, self.index)
elif self.IS_SLOT:
widget.insert_item(None, self.index) # placeholder
if self.slot_tab is not None:
tabs_p = widget.properties["tabs"]
tabs_p.value.insert(self.index, [self.slot_tab])
tabs_p.reset()
if widget.IS_SIZER:
from edit_sizers import SizerSlot as Slot
else:
from edit_base import Slot
slot = Slot(widget, index=self.index)
if widget.widget:
slot.create_widget()
widget.child_widget_created(slot, 0)
# update structure
misc.rebuild_tree( widget, focus=False )
# update following slots
for c in widget.children[self.index+1:]:
if c.IS_SLOT: common.app_tree.refresh(c)
misc.set_focused_widget(slot)
else:
widget.clipboard_paste(self.xml_data)
def redo(self):
# identical to HistoryAddedItem.undo
widget = common.root.find_widget_from_path(self.path)
slot = widget.remove(user=False)
if slot is not None and not self.slot_path:
# a slot has been left there, but should not be
slot.remove(user=False)
class HistoryAddedItem(HistoryItem):
def __init__(self, parent, xml_data=None):
self.slot_path = parent.IS_SLOT and parent.get_path() or None
self.xml_data = xml_data # could be set on undo
self.path = self.index = None
def finalize(self, item):
self.path = item.get_path()
self.index = item.index
self.IS_SLOT = item.IS_SLOT
def undo(self):
# identical to HistoryRemovedItem.redo
widget = common.root.find_widget_from_path(self.path)
if self.xml_data is None: self.xml_data = clipboard.dump_widget(widget)
slot = widget.remove(user=False)
if slot is not None and not self.slot_path:
# a slot has been left there, but should not be
slot.remove(user=False)
def redo(self):
# identical to HistoryRemovedItem.undo, except for the slot_tab part
path = self.slot_path or self.path.rsplit("/",1)[0] # slot or parent
widget = common.root.find_widget_from_path(path)
if widget.IS_ROOT:# or widget.IS_CONTAINER:
widget.clipboard_paste(self.xml_data, self.index)
elif self.IS_SLOT:
widget.insert_item(None, self.index) # placeholder
#if self.slot_tab is not None:
#tabs_p = widget.properties["tabs"]
#tabs_p.value.insert(self.index, [self.slot_tab])
#tabs_p.reset()
if widget.IS_SIZER:
from edit_sizers import SizerSlot as Slot
else:
from edit_base import Slot
slot = Slot(widget, index=self.index)
if widget.widget:
slot.create_widget()
widget.child_widget_created(slot, 0)
# update structure
misc.rebuild_tree( widget, focus=False )
# update following slots
for c in widget.children[self.index+1:]:
if c.IS_SLOT: common.app_tree.refresh(c)
misc.set_focused_widget(slot)
else:
widget.clipboard_paste(self.xml_data)
class HistorySizerSlots(HistoryItem):
# for added/inserted slots
def __init__(self, sizer, index, count=1):
self.path = sizer.get_path()
self.index = index
self.count = count
def undo(self):
sizer = common.root.find_widget_from_path(self.path)
for n in range(self.count):
sizer.children[self.index].remove(user=False)
def redo(self):
sizer = common.root.find_widget_from_path(self.path)
with sizer.window.frozen():
for n in range(self.count):
if self.index==-1:
sizer._add_slot()
else:
sizer._insert_slot(self.index+n)
if sizer.widget: sizer.layout()
misc.rebuild_tree( sizer, recursive=False, focus=False )
class HistoryGridSizerRowCol(HistoryItem):
# for added / removed rows / cols
def __init__(self, sizer, type, index, count=1, inserted_slots=None):
# count: negative if removed
self.path = sizer.get_path()
self.type = type # "row" or "col"
self.index = index # row or column index
self.count = count
self.inserted_slots = inserted_slots
def undo(self):
sizer = common.root.find_widget_from_path(self.path)
if self.type=="row" and self.count==1:
sizer.remove_row(self.index, user=False, remove_slots=self.inserted_slots)
elif self.type=="row" and self.count==-1:
sizer.insert_row(self.index, user=False)
elif self.type=="col" and self.count==1:
sizer.remove_col(self.index, user=False, remove_slots=self.inserted_slots)
elif self.type=="col" and self.count==-1:
sizer.insert_col(self.index, user=False)
def redo(self):
sizer = common.root.find_widget_from_path(self.path)
if self.type=="row" and self.count==1:
sizer.insert_row(self.index, user=False)
elif self.type=="row" and self.count==-1:
sizer.remove_row(self.index, user=False)
elif self.type=="col" and self.count==1:
sizer.insert_col(self.index, user=False)
elif self.type=="col" and self.count==-1:
sizer.remove_col(self.index, user=False)
class History(object):
def __init__(self, depth=20):
self.actions = []
self.actions_redo = [] # on undo, the action is moved from actions to actions_redo
self.depth = depth
self._buffer = None
self._redo_widget = None # the widget that originally was modified
self._redo_info = [] # name of properties
self._repeating = False
self.can_undo = self.can_redo = self.can_repeat = False
def reset(self):
del self.actions[:]
del self.actions_redo[:]
self.can_undo = False
self.can_redo = False
self.can_repeat = len(self._redo_info) > 1
def set_widget(self, widget):
# for enabling/disabling tools and menus
path = widget and widget.get_path() or []
if path==self._redo_widget or self._redo_widget is None:
self.can_repeat = self.can_redo = False
else:
self.can_redo = True
self.can_repeat = len(self._redo_info) > 1
self.can_undo = bool(self.actions)
def undo(self, focused_widget):
if not self.actions:
return wx.Bell()
action = self.actions.pop(0)
action.undo()
self.actions_redo.append(action)
def redo(self, focused_widget):
if not self.actions_redo:
# XXX check whether it's the same
repeated = self.repeat(focused_widget, multiple=False)
if not repeated: wx.Bell()
return
action = self.actions_redo.pop(-1)
action.redo()
self.actions.insert(0, action)
def repeat(self, focused_widget, multiple=True):
"apply action(s) to another widget"
if focused_widget is None: return False
if not self.actions or not isinstance(self.actions[0], HistoryPropertyItem): return False
if not self._redo_widget: return False
path = focused_widget.get_path()
if path==self._redo_widget: return False
# find all actions that could be repeated; they need to be HistoryPropertyItems from the _redo_widget
repeat_actions = []
repeat_actions_keys = set() # set of names, to avoid multiple changes of the same property
for i,action in enumerate(self.actions):
if not isinstance(action, HistoryPropertyItem):break
if repeat_actions and action.path!=self._redo_widget: break
if action.path==self._redo_widget:
action_key = action.get_key() # this may be a tuple for HistorySetPropertyItem
if action.name in focused_widget.properties and not action_key in repeat_actions_keys:
repeat_actions.append( action )
repeat_actions_keys.add( action_key )
if not multiple: break
repeat_actions.reverse()
# apply to the new widget
self._repeating = True # don't set self._redo_widget
for action in repeat_actions:
if config.debugging:
print("Repeating %s"%action)
prop = focused_widget.properties[action.name]
if isinstance(action, HistorySetPropertyItem):
prop._change_value(action.flag_value, action.checked)
elif isinstance(action, HistoryPropertyItem):
if prop.deactivated is None:
# a property that can not be deactivated
prop._check_for_user_modification(action.new.value)
else:
force = action.new.deactivated!=prop.deactivated
prop._check_for_user_modification(action.new.value, force=force, activate=not action.new.deactivated)
self._repeating = False
return True
def add_item(self, item):
self.actions.insert(0, item)
if len(self.actions)>self.depth:
del self.actions[-1]
if not self._repeating and isinstance(item, HistoryPropertyItem):
path = item.path
if path != self._redo_widget:
self._redo_widget = path
del self._redo_info[:]
key = item.get_key()
if not key in self._redo_info:
self._redo_info.append(key)
if self.actions_redo:
del self.actions_redo[:]
if config.debugging:
print("UndoBuffer:")
for entry in self.actions:
print(entry)
####################################################################################################################
# property changes: interface from Property instances
def property_changing(self, prop):
"to be called when property value is still the old one"
if config.debugging: print("property_changing", prop)
self._buffer = HistoryPropertyItem(prop)
self._monitor = [] # list of (property, PropertyValue)
def set_property_changing(self, prop):
# same as before, but this will track the clicked flag
if config.debugging: print("set_property_changing", prop)
self._buffer = HistorySetPropertyItem(prop)
self._monitor = [] # list of (property, PropertyValue)
def monitor_property(self, prop):
# monitor dependent properties; these will be un-/re-done together with the main property
if not self._buffer: return
self._monitor.append( (prop, PropertyValue(prop)))
def _finalize_item(self, stop=False):
# helper
item = self._buffer
self._buffer = None if not stop else False
item.finalize(self._monitor)
del self._monitor
return item
def property_changed(self, prop, user=True):
"argument user: True if set by the user, False if set in dependence to another change"
if config.debugging: print("property_changed", prop, user)
if self._buffer is False: # e.g. handled already by another item, e.g. HistoryNotebookTabsItem
self._buffer = None
return
item = self._finalize_item()
if item.new==item.old: return
self.add_item(item)
def set_property_changed(self, prop, value, checked, user=True):
if self._buffer:
# track which checkbox was checked
self._buffer.flag_value = value
self._buffer.checked = checked
self.property_changed(prop, user=True)
####################################################################################################################
# structural changes
def widget_adding(self, parent, xml_data=None):
# for pasting or single widget dropping
# parent can be a slot, which will be filled/replaced
# or e.g. a panel where a sizer will be added without having a slot before
self._adding_buffer = HistoryAddedItem(parent, xml_data)
def widget_added(self, widget):
self._adding_buffer.finalize(widget)
self.add_item( self._adding_buffer )
self._adding_buffer = None
def widget_removing(self, widget):
# store information and XML data before the widget is actually removed
self._buffer = HistoryRemovedItem(widget)
def widget_removed(self, slot=None):
self._buffer.finalize(slot)
self.add_item( self._buffer )
self._buffer = None
# sizers
def sizer_slots_added(self, sizer, index, count):
# called from SizerBase.insert_slot and add_slot
self.add_item( HistorySizerSlots(sizer, index, count) )
def gridsizer_row_col_changed(self, sizer, type, index, count, inserted_slots=None):
self.add_item( HistoryGridSizerRowCol(sizer, type, index, count, inserted_slots) )