class CommandStack:
def __init__(self):
self.call_before_action = []
+ self.stack_observers = []
self.clear()
def clear(self):
self.undo_stack = []
self.redo_stack = []
+ self.notify_stack_observers()
def do(self, command):
for f in self.call_before_action: f()
command.redo()
self.undo_stack.append(command)
self.reduce_undo_history()
+ self.notify_stack_observers()
def undo(self):
if not self.undo_stack: return
command = self.undo_stack.pop()
command.undo()
self.redo_stack.append(command)
+ self.notify_stack_observers()
return command
def redo(self):
command = self.redo_stack.pop()
command.redo()
self.undo_stack.append(command)
+ self.notify_stack_observers()
return command
def reduce_undo_history(self):
def get_last_command(self):
if not self.undo_stack: return None
return self.undo_stack[-1]
-
+
+ def notify_stack_observers(self):
+ for func in self.stack_observers:
+ func(self)
class Action:
'''Base class for all undo/redoable actions. Subclasses must implement the
self.stroke_observers = [] # callback arguments: stroke, brush (brush is a temporary read-only convenience object)
self.doc_observers = []
self.frame_observers = []
+ self.command_stack_observers = []
self.clear(True)
self._frame = [0, 0, 0, 0]
if not init:
bbox = self.get_bbox()
# throw everything away, including undo stack
+
self.command_stack = command.CommandStack()
+ self.command_stack.stack_observers = self.command_stack_observers
self.set_background((255, 255, 255))
self.layers = []
self.layer_idx = None