[gnome-games/applygsoc2009: 55/76] Forgot to add the file



commit 74a52fb3b3af0d95a534d33be576d72465effd0b
Author: Pablo Castellano <pablog src gnome org>
Date:   Wed Sep 1 04:38:36 2010 +0200

    Forgot to add the file

 gnome-sudoku/src/lib/undo.py |   61 ++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 61 insertions(+), 0 deletions(-)
---
diff --git a/gnome-sudoku/src/lib/undo.py b/gnome-sudoku/src/lib/undo.py
new file mode 100644
index 0000000..6721225
--- /dev/null
+++ b/gnome-sudoku/src/lib/undo.py
@@ -0,0 +1,61 @@
+# -*- coding: utf-8 -*-
+
+class ChangeValueCmd:
+    def __init__(self, model, x, y, old, new):
+        self.execute = lambda: model.set_value(x, y, new)
+        self.undo = lambda: model.set_value(x, y, old)
+
+class HistoryManager:
+    def __init__(self, undo_action, redo_action):
+        self._history = []
+        self._next_undo = -1
+        self._undo_action = undo_action
+        self._redo_action = redo_action
+        self._update_gui()
+
+    def execute_command(self, cmd):
+        # first clear any undone items from the history list
+        self._trim_history_list()
+        cmd.execute()
+        self._history.append(cmd)
+        self._next_undo += 1
+        self._update_gui()
+
+    def undo(self):
+        # nothing to undo
+        if self._next_undo < 0:
+            return
+
+        command = self._history[self._next_undo]
+        command.undo()
+        self._next_undo -= 1
+        self._update_gui()
+
+    def redo(self):
+        # nothing to redo
+        if self._next_undo == len(self._history) - 1:
+            return
+
+        cmd = self._history[self._next_undo + 1]
+        cmd.execute()
+
+        self._next_undo += 1
+        self._update_gui()
+
+    def _trim_history_list(self):
+        """Purge all undone commands from the history list
+        """
+        del self._history[(self._next_undo + 1):]
+        self._update_gui()
+
+    def _update_gui(self):
+        undo, redo = True, True
+        if not self._history:
+            undo, redo = False, False
+        elif self._next_undo < 0:
+            undo = False
+        if self._next_undo == len(self._history) - 1:
+            redo = False
+
+        self._undo_action.set_sensitive(undo)
+        self._redo_action.set_sensitive(redo)



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]