[gnome-games/sudoku-tube] Can undo auto-fill



commit cdcd9fd630411177a43aefb977d48760fee62365
Author: Zhang Sen <zh jesse gmail com>
Date:   Sat Jul 25 09:07:59 2009 +0800

    Can undo auto-fill

 gnome-sudoku/src/lib/main.py  |    5 +++--
 gnome-sudoku/src/lib/model.py |   28 ++++++++++++++++++++++++++++
 gnome-sudoku/src/lib/undo.py  |    5 -----
 gnome-sudoku/src/lib/view.py  |    4 ++--
 4 files changed, 33 insertions(+), 9 deletions(-)
---
diff --git a/gnome-sudoku/src/lib/main.py b/gnome-sudoku/src/lib/main.py
index bf78065..9952061 100644
--- a/gnome-sudoku/src/lib/main.py
+++ b/gnome-sudoku/src/lib/main.py
@@ -500,7 +500,8 @@ class UI (gconf_wrapper.GConfWrapper):
         self._main_model.toggle_impossible_implications(True)
 
     def auto_fill_cb (self, action):
-        self._main_model.auto_fill()
+        cmd = model.AutoFillCmd(self._main_model)
+        self._history_manager.execute_command(cmd)
 
     def auto_fill_current_square_cb (self, *args):
         current = self._main_grid_vew.get_focus()
@@ -509,7 +510,7 @@ class UI (gconf_wrapper.GConfWrapper):
             new = self._main_model.calc_solution(x, y)
             if new:
                 old = self._main_grid_vew.get_value(x, y)
-                cmd = undo.ChangeValueCmd(self._main_model, x, y, old, new)
+                cmd = model.ChangeValueCmd(self._main_model, x, y, old, new)
                 self._history_manager.execute_command(cmd)
 
     def tracker_toggle_cb (self, widg):
diff --git a/gnome-sudoku/src/lib/model.py b/gnome-sudoku/src/lib/model.py
index fb27a3b..78f70f4 100644
--- a/gnome-sudoku/src/lib/model.py
+++ b/gnome-sudoku/src/lib/model.py
@@ -49,6 +49,7 @@ class ParallelDict (dict):
 
 
 class NumberBoxModel:
+    """Used to notify observers for the change of SudokuModel"""
     def __init__(self, x, y, value=None, conflict=None):
         self.x = x
         self.y = y
@@ -295,6 +296,7 @@ class SudokuModel:
             result.append(NumberBoxModel(x, y, value=val))
 
         self._signal_observers(result)
+        return changed
 
     def calc_solution(self, x, y):
         return self.grid.calc_solution(x, y)
@@ -327,3 +329,29 @@ class SudokuModel:
 
     def get_impossible_hints(self):
         return len(self.impossibilities)
+
+#
+# Commands that can undo themselves
+#
+
+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 AutoFillCmd:
+    def __init__(self, model):
+        self._model = model
+        self._filled = None
+
+    def execute(self):
+        self._filled = self._model.auto_fill()
+
+    def undo(self):
+        for coord, value in self._filled:
+            # Auto-fill should only fill in black boxes, so its undo action
+            # should fill them in 0.
+            # But, conflicting values don't go into the grid :( So we can't
+            # go back for a conflicting box.
+            self._model.set_value(coord[0], coord[1], 0)
+
diff --git a/gnome-sudoku/src/lib/undo.py b/gnome-sudoku/src/lib/undo.py
index 6721225..fa9ec0c 100644
--- a/gnome-sudoku/src/lib/undo.py
+++ b/gnome-sudoku/src/lib/undo.py
@@ -1,10 +1,5 @@
 # -*- 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 = []
diff --git a/gnome-sudoku/src/lib/view.py b/gnome-sudoku/src/lib/view.py
index 375c260..c581555 100644
--- a/gnome-sudoku/src/lib/view.py
+++ b/gnome-sudoku/src/lib/view.py
@@ -8,7 +8,7 @@ import gobject
 
 import colors
 import number_box
-import undo
+import model
 
 
 TRACKER_COLORS = [
@@ -290,7 +290,7 @@ class SudokuView(SudokuNumberGrid, gobject.GObject):
         # Use the displayed value, instead of that from the grid;
         # because conflicting value doesn't get into the grid
         old = self.get_value(x, y)
-        cmd = undo.ChangeValueCmd(self._model, x, y, old, new_number)
+        cmd = model.ChangeValueCmd(self._model, x, y, old, new_number)
         self._controller.execute_command(cmd)
 
     def update_model(self, x, y, value):



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