[gnome-games/applygsoc2009: 59/76] XXX: Let the model emit the puzzle-finished signal



commit 5458796c31d63acf38fa3c119045c84eebba1201
Author: Pablo Castellano <pablog src gnome org>
Date:   Wed Sep 1 04:59:03 2010 +0200

    XXX: Let the model emit the puzzle-finished signal

 gnome-sudoku/src/lib/main.py  |   25 +++++++++++++++----------
 gnome-sudoku/src/lib/model.py |   16 ++++++++++------
 gnome-sudoku/src/lib/view.py  |   12 +-----------
 3 files changed, 26 insertions(+), 27 deletions(-)
---
diff --git a/gnome-sudoku/src/lib/main.py b/gnome-sudoku/src/lib/main.py
index 19189ec..33bd1a9 100644
--- a/gnome-sudoku/src/lib/main.py
+++ b/gnome-sudoku/src/lib/main.py
@@ -15,6 +15,7 @@ import pango
 from gettext import gettext as _
 from gettext import ngettext
 
+import dancer
 import game_selector
 import model
 import view
@@ -163,6 +164,7 @@ class UI (gconf_wrapper.GConfWrapper):
         """Properly do the setup after entering game
 
         Like show highlight, update hints, start timer, etc."""
+        self._main_model.connect('puzzle-finished', self.you_win_callback)
         self.timer.start_timing()
         self.update_statusbar()
         if self.gconf['always_show_hints']:
@@ -350,14 +352,7 @@ class UI (gconf_wrapper.GConfWrapper):
         self.dancer = dancer.GridDancer(self._main_grid_vew, self._main_model)
         self.dancer.start_dancing()
 
-    def you_win_callback (self, grid):
-        if hasattr(self, 'dancer'):
-            return
-        self.won = True
-        # increase difficulty for next time.
-        self.gconf['difficulty'] = self.gconf['difficulty'] + 0.1
-        self.timer.finish_timing()
-        self.sudoku_tracker.finish_game(self._jar_game())
+    def _build_report_label(self):
         if self.timer.active_time != self.timer.total_time:
             sublabel = _("You completed the puzzle in %(totalTime)s (%(activeTime)s active).") % {'totalTime': self.timer.total_time_string(),
             'activeTime': self.timer.active_time_string()
@@ -375,8 +370,18 @@ class UI (gconf_wrapper.GConfWrapper):
             sublabel += "\n"
         self.start_dancer()
         dialog_extras.show_message(_("You win!"), label = _("You win!"),
-                                   sublabel = sublabel
-                                   )
+                                   sublabel=self._build_report_label())
+
+        return sublabel
+
+    def you_win_callback(self, model):
+        if hasattr(self, 'dancer'):
+            return
+        self.won = True
+        # increase difficulty for next time.
+        self.gconf['difficulty'] = self.gconf['difficulty'] + 0.1
+        self.timer.finish_timing()
+        self.sudoku_tracker.finish_game(self._jar_game())
 
     def new_cb (self, *args):
         if self._close_current_game():
diff --git a/gnome-sudoku/src/lib/model.py b/gnome-sudoku/src/lib/model.py
index ecd27d6..5664526 100644
--- a/gnome-sudoku/src/lib/model.py
+++ b/gnome-sudoku/src/lib/model.py
@@ -1,4 +1,6 @@
 # -*- coding: utf-8 -*-
+import gobject
+
 import sudoku
 
 class NumberBoxModel:
@@ -90,9 +92,15 @@ class NotesModel:
             self.set_notes(x, y, bottom_note="X")
 
 
-class SudokuModel:
+class SudokuModel(gobject.GObject):
+
+    __gsignals__ = {
+            # atm. only used by dancer
+            "puzzle-finished": (gobject.SIGNAL_RUN_LAST, gobject.TYPE_NONE, ())
+            }
 
 	def __init__ (self, virgin_grid, group_size, initial_grid=None):
+		gobject.GObject.__init__(self)
         self.hints = 0
         self.autofill_count = 0
         self.show_impossible_implications = False
@@ -148,10 +156,6 @@ class SudokuModel:
             self.animate_hint()
             self.hint_timer = gobject.timeout_add(150, self.animate_hint)
 
-    def _signal_completeness(self):
-        for observer in self._observers:
-            observer.puzzle_finished_cb()
-
     def reset_grid (self):
         '''Remove all untracked values from the grid
 
@@ -298,7 +302,7 @@ class SudokuModel:
 
     def _check_for_completeness(self):
         if self.grid.check_for_completeness():
-            self._signal_completeness()
+            self.emit("puzzle-finished")
 
     def set_autofill_count(self, count):
         self.autofill_count = count
diff --git a/gnome-sudoku/src/lib/view.py b/gnome-sudoku/src/lib/view.py
index 9a9057c..6ee5190 100644
--- a/gnome-sudoku/src/lib/view.py
+++ b/gnome-sudoku/src/lib/view.py
@@ -2,7 +2,6 @@
 import math
 
 import gtk
-import gobject
 
 import colors
 import number_box
@@ -170,18 +169,12 @@ class SudokuNumberGrid (gtk.AspectFrame):
         self.__entries__[(x, y)].set_value(value)
 
 
-class SudokuView (SudokuNumberGrid, gobject.GObject):
-
-    # some signals to give notice about change of the View
-    __gsignals__ = {
-            "puzzle-finished": (gobject.SIGNAL_RUN_LAST, gobject.TYPE_NONE, ())
-            }
+class SudokuView(SudokuNumberGrid):
 
     def __init__(self, group_size, controller=None):
         SudokuNumberGrid.__init__(self, group_size)
 #        self.hint_square = None
 #        self.tinfo = tracker_info.TrackerInfo()
-        gobject.GObject.__init__(self)
         SudokuNumberGrid.__init__(self, group_size = group_size)
 
         self._model = None
@@ -263,9 +256,6 @@ class SudokuView (SudokuNumberGrid, gobject.GObject):
 
         # default black color
         return None
-
-    def puzzle_finished_cb(self):
-        self.emit("puzzle-finished")
         
     def update_notes(self, notes_list):
         for notes in notes_list:



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