[gnome-games/applygsoc2009: 56/76] XXX: Can undo auto-fill



commit e59b97e07f4e4bb7576d430d8b53c2e310c08548
Author: Pablo Castellano <pablog src gnome org>
Date:   Wed Sep 1 04:41:32 2010 +0200

    XXX: Can undo auto-fill

 gnome-sudoku/src/lib/model.py |   28 +++++++++++++++++++++++++++-
 gnome-sudoku/src/lib/undo.py  |    5 -----
 gnome-sudoku/src/lib/view.py  |    4 ++--
 3 files changed, 29 insertions(+), 8 deletions(-)
---
diff --git a/gnome-sudoku/src/lib/model.py b/gnome-sudoku/src/lib/model.py
index 115e789..7ece385 100644
--- a/gnome-sudoku/src/lib/model.py
+++ b/gnome-sudoku/src/lib/model.py
@@ -2,6 +2,7 @@
 import sudoku
 
 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
@@ -369,7 +370,6 @@ class SudokuModel:
     def get_impossible_hints(self):
         return len(self.impossibilities)
 
-
     def delete_by_tracker (self):
         '''Delete all cells tracked by the current tracker
 
@@ -428,3 +428,29 @@ class SudokuModel:
         # Update all hints if we need to
         if self.always_show_hints and not self.doing_initial_setup:
             self.update_all_hints()
+
+
+#
+# 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 d9b1f26..489dadf 100644
--- a/gnome-sudoku/src/lib/view.py
+++ b/gnome-sudoku/src/lib/view.py
@@ -6,7 +6,7 @@ import gobject
 
 import colors
 import number_box
-import undo
+import model
 
 TRACKER_COLORS = [
     # Use tango colors recommended here:
@@ -255,7 +255,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 _notes_changed_cb(self, widget, top_note, bottom_note):



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