OSDN Git Service

command stack: add an observer interface
authorAndrew Chadwick <andrewc-git@piffle.org>
Wed, 27 Jul 2011 22:47:43 +0000 (23:47 +0100)
committerAndrew Chadwick <andrewc-git@piffle.org>
Sun, 31 Jul 2011 14:16:51 +0000 (15:16 +0100)
Allow the state of the command stack to be observed.

lib/command.py
lib/document.py

index 02bb892..6cefde6 100644 (file)
@@ -12,11 +12,13 @@ import helpers
 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()
@@ -24,6 +26,7 @@ class CommandStack:
         command.redo()
         self.undo_stack.append(command)
         self.reduce_undo_history()
+        self.notify_stack_observers()
     
     def undo(self):
         if not self.undo_stack: return
@@ -31,6 +34,7 @@ class CommandStack:
         command = self.undo_stack.pop()
         command.undo()
         self.redo_stack.append(command)
+        self.notify_stack_observers()
         return command
         
     def redo(self):
@@ -39,6 +43,7 @@ class CommandStack:
         command = self.redo_stack.pop()
         command.redo()
         self.undo_stack.append(command)
+        self.notify_stack_observers()
         return command
 
     def reduce_undo_history(self):
@@ -55,7 +60,10 @@ class CommandStack:
     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
index 36dd3b2..bec8b32 100644 (file)
@@ -52,6 +52,7 @@ class Document():
         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]
@@ -117,7 +118,9 @@ class Document():
         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