[gnome-games/applygsoc2009] Not going bad...



commit f8580e74d1758c2b7dc7353bf66def8ed8671201
Author: Pablo Castellano <pablog src gnome org>
Date:   Wed Sep 8 19:40:07 2010 +0200

    Not going bad...

 gnome-sudoku/data/main.ui           |  195 +++++++++++++++++------------------
 gnome-sudoku/src/lib/main.py        |  120 +++++++++++----------
 gnome-sudoku/src/lib/printing.py    |    2 +-
 gnome-sudoku/src/lib/tracker_box.py |   12 +-
 gnome-sudoku/src/lib/view.py        |   41 ++++++++
 5 files changed, 205 insertions(+), 165 deletions(-)
---
diff --git a/gnome-sudoku/data/main.ui b/gnome-sudoku/data/main.ui
index f1d707b..fc2032f 100644
--- a/gnome-sudoku/data/main.ui
+++ b/gnome-sudoku/data/main.ui
@@ -2,94 +2,6 @@
 <interface>
   <requires lib="gtk+" version="2.16"/>
   <!-- interface-naming-policy project-wide -->
-  <object class="GtkToggleAction" id="ToggleHighlight">
-    <property name="label">_Highlighter</property>
-    <property name="tooltip">Highlight the current row, column and box</property>
-    <property name="stock_id">gtk-select-color</property>
-  </object>
-  <object class="GtkToggleAction" id="ToggleToolbar">
-    <property name="label">Show _Toolbar</property>
-    <property name="active">True</property>
-  </object>
-  <object class="GtkToggleAction" id="ToggleTracker">
-    <property name="label">_Track additions</property>
-    <property name="tooltip">Mark new additions in a separate color so you can keep track of them</property>
-    <property name="stock_id">tracks</property>
-  </object>
-  <object class="GtkToggleAction" id="ShowImpossibleImplications">
-    <property name="label">Warn about _unfillable squares</property>
-    <property name="tooltip">Warn about squares made unfillable by a move</property>
-    <property name="active">True</property>
-  </object>
-  <object class="GtkToggleAction" id="AlwaysShowPossible">
-    <property name="label">_Always show hint</property>
-    <property name="tooltip">Always show possible numbers in a square</property>
-  </object>
-  <object class="GtkAction" id="Redo">
-    <property name="label">_Redo</property>
-    <property name="tooltip">Redo last action</property>
-    <property name="stock_id">gtk-redo</property>
-  </object>
-  <object class="GtkAction" id="Undo">
-    <property name="label">_Undo</property>
-    <property name="tooltip">Undo last action</property>
-    <property name="stock_id">gtk-undo</property>
-  </object>
-  <object class="GtkAction" id="ShowHelp">
-    <property name="label">_Contents</property>
-    <property name="stock_id">gtk-help</property>
-  </object>
-  <object class="GtkAction" id="About">
-    <property name="label">_About</property>
-    <property name="stock_id">gtk-about</property>
-  </object>
-  <object class="GtkAction" id="PuzzleInfo">
-    <property name="label">Puzzle _Statistics</property>
-    <property name="tooltip">Show statistics about current puzzle</property>
-    <property name="stock_id">gtk-about</property>
-  </object>
-  <object class="GtkAction" id="FullScreen">
-    <property name="label">_FullScreen</property>
-    <property name="stock_id">gtk-fullscreen</property>
-  </object>
-  <object class="GtkAction" id="ClearBottomNotes">
-    <property name="label">Clear _Bottom Notes</property>
-    <property name="tooltip">Clear all of the bottom notes</property>
-  </object>
-  <object class="GtkAction" id="ClearTopNotes">
-    <property name="label">Clear _Top Notes</property>
-    <property name="tooltip">Clear all of the top notes</property>
-  </object>
-  <object class="GtkAction" id="ShowPossible">
-    <property name="label">_Hint</property>
-    <property name="tooltip">Show which numbers could go in the current square</property>
-    <property name="stock_id">gtk-dialog-info</property>
-  </object>
-  <object class="GtkAction" id="Close">
-    <property name="label">_Close</property>
-    <property name="tooltip">Close Sudoku</property>
-    <property name="stock_id">gtk-close</property>
-  </object>
-  <object class="GtkAction" id="PrintMany">
-    <property name="label">Print _Multiple Sudokus</property>
-    <property name="tooltip">Print more than one sudoku at a time</property>
-    <property name="stock_id">gtk-print</property>
-  </object>
-  <object class="GtkAction" id="Print">
-    <property name="label">_Print</property>
-    <property name="tooltip">Print current game</property>
-    <property name="stock_id">gtk-print</property>
-  </object>
-  <object class="GtkAction" id="Reset">
-    <property name="label">_Reset</property>
-    <property name="tooltip">Reset current grid(do-over)</property>
-    <property name="stock_id">gtk-clear</property>
-  </object>
-  <object class="GtkAction" id="New">
-    <property name="label">_New</property>
-    <property name="tooltip">New game</property>
-    <property name="stock_id">gtk-new</property>
-  </object>
   <object class="GtkWindow" id="window">
     <child>
       <object class="GtkVBox" id="vbox">
@@ -438,41 +350,34 @@
                 </child>
               </object>
               <packing>
+                <property name="expand">True</property>
+                <property name="fill">True</property>
                 <property name="position">0</property>
               </packing>
             </child>
             <child>
               <object class="GtkVBox" id="side_bar">
                 <property name="visible">True</property>
-                <property name="orientation">vertical</property>
                 <child>
                   <object class="GtkVBox" id="tracker_ui_container">
                     <property name="visible">True</property>
-                    <property name="orientation">vertical</property>
                     <child>
                       <placeholder/>
                     </child>
                   </object>
                   <packing>
+                    <property name="expand">True</property>
+                    <property name="fill">True</property>
                     <property name="position">0</property>
                   </packing>
                 </child>
                 <child>
-                  <object class="GtkAlignment" id="side_grid_container">
-                    <property name="visible">True</property>
-                    <property name="yalign">1</property>
-                    <child>
-                      <placeholder/>
-                    </child>
-                  </object>
-                  <packing>
-                    <property name="expand">False</property>
-                    <property name="position">1</property>
-                  </packing>
+                  <placeholder/>
                 </child>
               </object>
               <packing>
                 <property name="expand">False</property>
+                <property name="fill">True</property>
                 <property name="position">1</property>
               </packing>
             </child>
@@ -497,4 +402,92 @@
       </object>
     </child>
   </object>
+  <object class="GtkAction" id="New">
+    <property name="label">_New</property>
+    <property name="tooltip">New game</property>
+    <property name="stock_id">gtk-new</property>
+  </object>
+  <object class="GtkAction" id="Reset">
+    <property name="label">_Reset</property>
+    <property name="tooltip">Reset current grid(do-over)</property>
+    <property name="stock_id">gtk-clear</property>
+  </object>
+  <object class="GtkAction" id="Print">
+    <property name="label">_Print</property>
+    <property name="tooltip">Print current game</property>
+    <property name="stock_id">gtk-print</property>
+  </object>
+  <object class="GtkAction" id="PrintMany">
+    <property name="label">Print _Multiple Sudokus</property>
+    <property name="tooltip">Print more than one sudoku at a time</property>
+    <property name="stock_id">gtk-print</property>
+  </object>
+  <object class="GtkAction" id="Close">
+    <property name="label">_Close</property>
+    <property name="tooltip">Close Sudoku</property>
+    <property name="stock_id">gtk-close</property>
+  </object>
+  <object class="GtkAction" id="ShowPossible">
+    <property name="label">_Hint</property>
+    <property name="tooltip">Show which numbers could go in the current square</property>
+    <property name="stock_id">gtk-dialog-info</property>
+  </object>
+  <object class="GtkAction" id="ClearTopNotes">
+    <property name="label">Clear _Top Notes</property>
+    <property name="tooltip">Clear all of the top notes</property>
+  </object>
+  <object class="GtkAction" id="ClearBottomNotes">
+    <property name="label">Clear _Bottom Notes</property>
+    <property name="tooltip">Clear all of the bottom notes</property>
+  </object>
+  <object class="GtkAction" id="FullScreen">
+    <property name="label">_FullScreen</property>
+    <property name="stock_id">gtk-fullscreen</property>
+  </object>
+  <object class="GtkAction" id="PuzzleInfo">
+    <property name="label">Puzzle _Statistics</property>
+    <property name="tooltip">Show statistics about current puzzle</property>
+    <property name="stock_id">gtk-about</property>
+  </object>
+  <object class="GtkAction" id="About">
+    <property name="label">_About</property>
+    <property name="stock_id">gtk-about</property>
+  </object>
+  <object class="GtkAction" id="ShowHelp">
+    <property name="label">_Contents</property>
+    <property name="stock_id">gtk-help</property>
+  </object>
+  <object class="GtkAction" id="Undo">
+    <property name="label">_Undo</property>
+    <property name="tooltip">Undo last action</property>
+    <property name="stock_id">gtk-undo</property>
+  </object>
+  <object class="GtkAction" id="Redo">
+    <property name="label">_Redo</property>
+    <property name="tooltip">Redo last action</property>
+    <property name="stock_id">gtk-redo</property>
+  </object>
+  <object class="GtkToggleAction" id="AlwaysShowPossible">
+    <property name="label">_Always show hint</property>
+    <property name="tooltip">Always show possible numbers in a square</property>
+  </object>
+  <object class="GtkToggleAction" id="ShowImpossibleImplications">
+    <property name="label">Warn about _unfillable squares</property>
+    <property name="tooltip">Warn about squares made unfillable by a move</property>
+    <property name="active">True</property>
+  </object>
+  <object class="GtkToggleAction" id="ToggleTracker">
+    <property name="label">_Track additions</property>
+    <property name="tooltip">Mark new additions in a separate color so you can keep track of them</property>
+    <property name="stock_id">tracks</property>
+  </object>
+  <object class="GtkToggleAction" id="ToggleToolbar">
+    <property name="label">Show _Toolbar</property>
+    <property name="active">True</property>
+  </object>
+  <object class="GtkToggleAction" id="ToggleHighlight">
+    <property name="label">_Highlighter</property>
+    <property name="tooltip">Highlight the current row, column and box</property>
+    <property name="stock_id">gtk-select-color</property>
+  </object>
 </interface>
diff --git a/gnome-sudoku/src/lib/main.py b/gnome-sudoku/src/lib/main.py
index a9012fe..09b5e9d 100644
--- a/gnome-sudoku/src/lib/main.py
+++ b/gnome-sudoku/src/lib/main.py
@@ -62,13 +62,16 @@ class SudokuGame (gconf_wrapper.GConfWrapper):
                                             )
         self.sudoku_maker = sudoku_maker.SudokuMaker()
         self.sudoku_tracker = saver.SudokuTracker()
+        
         self._main_model = None
         self._notes_model = None
         self._main_grid_vew = None
+        
         self._history_manager = None
+        
         self.setup_gui()
+        
         self.timer = timer.ActiveTimer(self.w)
-        self.gsd.set_timer(self.timer)
         self.won = False
         self.worker_connections = []
         self.is_fullscreen = False
@@ -134,6 +137,10 @@ class SudokuGame (gconf_wrapper.GConfWrapper):
         self._main_model = model.SudokuModel(virgin, 9, in_prog)
         self._main_model.set_autofill_count(jar["gsd.auto_fills"])
 
+        # Do the connection here; it will setup the readonly appearance.
+        # And it must be before the following tracker-setup
+        self._main_grid_vew.connect_to_model(self._main_model)
+
         self._notes_model = model.NotesModel(self._main_model, group_size=9)
         self._notes_model.set_hints_count(jar["gsd.hints"])
         for (x, y, top, bottom) in jar["notes"]:
@@ -142,7 +149,6 @@ class SudokuGame (gconf_wrapper.GConfWrapper):
         self.timer.__absolute_start_time__ = jar["timer.__absolute_start_time__"]
         self.timer.active_time = jar["timer.active_time"]
         self.timer.total_time = jar["timer.total_time"]
-        return
 
         # this is a bit easily breakable... we take advantage of the fact
         # that we create tracker IDs sequentially and that {}.items()
@@ -151,14 +157,10 @@ class SudokuGame (gconf_wrapper.GConfWrapper):
             # add 1 tracker per existing tracker...
             self.tracker_ui.add_tracker()
             for x, y, val in tracked:
-                self.gsd.add_tracker(x, y, tracker, val = val)
+                self._main_model.set_value(x, y, val)
         for tracker, tracking in jar.get('tracking', {}).items():
             if tracking:
                 ui.tracker_ui.select_tracker(tracker)
-        set_value_from_jar(ui, jar)
-        if jar.has_key('notes') and jar['notes']:
-            for x, y, top, bot in jar['notes']:
-                ui.gsd.__entries__[(x, y)].set_note_text(top, bot)
 
     def _post_open_setup(self):
         """Properly do the setup after entering game
@@ -166,10 +168,15 @@ class SudokuGame (gconf_wrapper.GConfWrapper):
         Like show highlight, update hints, start timer, etc."""
         self._main_grid_vew.connect_to_notes_model(self._notes_model)
         self._main_model.connect('puzzle-finished', self._puzzle_finished_cb)
-        self.timer.start_timing()
+        self._calc_difficulty()
+        self.setup_toggles()
 #        self.update_statusbar() #FIXME
-        if self.gconf['always_show_hints']:
-            self.gsd.update_all_hints()
+
+        self.timer.start_timing()
+
+    def _calc_difficulty(self):
+        puzzle = self._main_model.grid.virgin.to_string()
+        self._difficulty = self.sudoku_maker.get_difficulty(puzzle)
 
     def _run_background_generator (self):
         """Generate puzzles in background"""
@@ -190,18 +197,9 @@ class SudokuGame (gconf_wrapper.GConfWrapper):
         selector = game_selector.NewOrSavedGameSelector()
         choice = selector.run_dialog()
         selector.dialog.destroy()
-
-        if self.gconf['show_toolbar']:
-            self.toolbar.show()
-        if self.gconf['always_show_hints']:
-            self.gsd.update_all_hints()
-        if self.gconf['highlight']:
-            self.gsd.toggle_highlight(True)
-
         return choice
 
     def show (self):
-        self.gsd.show()
         self.w.show()
 
     def setup_gui (self):
@@ -210,8 +208,6 @@ class SudokuGame (gconf_wrapper.GConfWrapper):
         self.setup_undo()
         self._setup_main_boxes()
 
-        self.setup_tracker_interface() #?
-
         self.setup_color()
         self._setup_actions()
         self.setup_autosave()
@@ -239,7 +235,6 @@ class SudokuGame (gconf_wrapper.GConfWrapper):
 
     def _setup_main_boxes(self):
         main_grid_container = self.builder.get_object("main_grid_container")
-        side_grid_container = self.builder.get_object("side_grid_container")
         tracker_ui_container = self.builder.get_object("tracker_ui_container")
 
         self._main_grid_vew = view.SudokuView(9, self._history_manager)
@@ -247,7 +242,6 @@ class SudokuGame (gconf_wrapper.GConfWrapper):
 
         self.tracker_ui = tracker_box.TrackerBox(self._main_grid_vew)
         self.tracker_ui.hide()
-        side_grid_container.add(self._side_grid_vew)
 
     def setup_color (self):
         # setup background colors
@@ -306,6 +300,23 @@ class SudokuGame (gconf_wrapper.GConfWrapper):
         gobject.timeout_add_seconds(self.gconf['auto_save_interval'] or 60, # in seconds...
                             self.autosave)
 
+    def setup_toggles (self):
+        logger.debug("sync up toggles with gconf values")
+        [self.gconf_wrap_toggle(conf, action) for (conf, action) in [
+            ('always_show_hints',
+                self.builder.get_object('AlwaysShowPossible')),
+            ('show_impossible_implications',
+                self.builder.get_object('ShowImpossibleImplications')),
+#FIXME:
+#            ('generate_puzzles_in_background',
+#                self.builder.get_object('BackgroundGenerator')),
+            ('show_toolbar',
+                self.builder.get_object('ToggleToolbar')),
+            ('highlight',
+                self.builder.get_object('ToggleHighlight')),
+            ('show_tracker',
+                self.builder.get_object('ToggleTracker'))]]
+                
     def start_worker_thread (self):
         n_new_puzzles = self.sudoku_maker.n_puzzles(new = True)
         try:
@@ -344,7 +355,9 @@ class SudokuGame (gconf_wrapper.GConfWrapper):
         else:
             sublabel = _("You completed the puzzle in %(totalTime)s.") % {'totalTime': self.timer.total_time_string()}
         sublabel += "\n"
-        sublabel += ngettext("You got %(n)s hint.", "You got %(n)s hints.", self.gsd.hints) % {'n':self.gsd.hints}
+        sublabel += ngettext("You got %(n)s hint", "You got %(n)s hints",
+                self._notes_model.get_hints_count()) % {
+                        'n': self._notes_model.get_hints_count()}
         sublabel += "\n"
         impossible_hints = self._main_model.get_impossible_hints()
         if impossible_hints:
@@ -352,9 +365,9 @@ class SudokuGame (gconf_wrapper.GConfWrapper):
                                  "You had %(n)s impossibilities pointed out.",
                                  impossible_hints) % {'n': impossible_hints}
             sublabel += "\n"
-        self.start_dancer()
+        self.start_dancer() #FIXME
         dialog_extras.show_message(_("You win!"), label = _("You win!"),
-                                   sublabel=label)
+                                   sublabel=label) #FIXME
 
         return sublabel
 
@@ -456,21 +469,25 @@ class SudokuGame (gconf_wrapper.GConfWrapper):
         clearer.perform()
 
     def do_game_reset (self, *args):
-        self.gsd.cover_track()
-        self.cleared.append(self.tinfo.save())
-        self.cleared.append(self.gsd.reset_grid())
-        self.cleared_notes.append((tracker_info.NO_TRACKER, self.gsd.clear_notes('All')))
-        self.tinfo.reset()
-        self.stop_dancer()
+        pass
+#FIXME:
+#        self.gsd.cover_track()
+#        self.cleared.append(self.tinfo.save())
+#        self.cleared.append(self.gsd.reset_grid())
+#        self.cleared_notes.append((tracker_info.NO_TRACKER, self.gsd.clear_notes('All')))
+#        self.tinfo.reset()
+#        self.stop_dancer()
 
     def undo_game_reset (self, *args):
-        self.tracker_ui.select_tracker(tracker_info.NO_TRACKER)
-        for entry in self.cleared.pop():
-            self.gsd.add_value(*entry)
-        self.tinfo.load(self.cleared.pop())
-        self.tracker_ui.select_tracker(self.tinfo.current_tracker)
-        self.gsd.show_track()
-        self.undo_clear_notes()
+        pass
+#FIXME:
+#        self.tracker_ui.select_tracker(tracker_info.NO_TRACKER)
+#        for entry in self.cleared.pop():
+#            self.gsd.add_value(*entry)
+#        self.tinfo.load(self.cleared.pop())
+#        self.tracker_ui.select_tracker(self.tinfo.current_tracker)
+#        self.gsd.show_track()
+#        self.undo_clear_notes()
 
     def clear_top_notes_cb (self, *args):
         #FIXME:
@@ -499,14 +516,14 @@ class SudokuGame (gconf_wrapper.GConfWrapper):
         'Top' - Clear only the top notes
         'Bottom' - Clear only the bottom notes
         '''
-        self.cleared_notes.append((self.tinfo.current_tracker, self.gsd.clear_notes(side)))
+        self.cleared_notes.append((self.tinfo.current_tracker, self.gsd.clear_notes(side))) #FIXME
         # Turn off auto-hint if the player clears the bottom notes
         if side == 'Bottom' and self.gconf['always_show_hints']:
             always_show_hint_wdgt = self.main_actions.get_action('AlwaysShowPossible')
             always_show_hint_wdgt.activate()
         # Update the hints...in case we're redoing a clear of them
         if self.gconf['always_show_hints']:
-            self.gsd.update_all_hints()
+            self.gsd.update_all_hints() #FIXME
 
     def undo_clear_notes(self):
         ''' Undo previously cleared notes
@@ -517,12 +534,12 @@ class SudokuGame (gconf_wrapper.GConfWrapper):
         # Change the tracker selection if it was tracking during the clear
         if cleared_tracker != tracker_info.NO_TRACKER:
             self.tracker_ui.select_tracker(cleared_tracker)
-        self.gsd.apply_notelist(cleared_notes)
+        self.gsd.apply_notelist(cleared_notes) #FIXME
         # Update the hints...in case we're undoing over top of them
         if self.gconf['always_show_hints']:
-            self.gsd.update_all_hints()
+            self.gsd.update_all_hints() #FIXME
         # Redraw the notes
-        self.gsd.update_all_notes()
+        self.gsd.update_all_notes() #FIXME
         # Make sure we're still dancing if we undo after win
         if self._main_model.grid.check_for_completeness():
             self.start_dancer()
@@ -537,14 +554,6 @@ class SudokuGame (gconf_wrapper.GConfWrapper):
     def impossible_implication_cb (self, action):
         self._main_model.toggle_impossible_implications(True)
 
-    def setup_tracker_interface (self):
-        self.tracker_ui = tracker_box.TrackerBox(self)
-        self.tracker_ui.show_all()
-        self.tracker_ui.hide()
-        self.tinfo = tracker_info.TrackerInfo()
-        self.old_tracker_view = None
-        self.main_area.pack(self.tracker_ui, expand=False)
-
     def tracker_toggle_cb (self, widg):
         if widg.get_active():
             if self.old_tracker_view:
@@ -564,10 +573,7 @@ class SudokuGame (gconf_wrapper.GConfWrapper):
             self.toolbar.hide()
 
     def toggle_highlight_cb (self, widg):
-        if widg.get_active():
-            self.gsd.toggle_highlight(True)
-        else:
-            self.gsd.toggle_highlight(False)
+        self._main_grid_vew.toggle_highlight(widg.get_active())
 
     def show_info_cb (self, action):
         if not self._main_model.grid:
@@ -615,7 +621,7 @@ class SudokuGame (gconf_wrapper.GConfWrapper):
             print _('Unable to display help: %s') % str(error)
 
     def print_game (self, action):
-        printing.print_sudokus([self.gsd], self.w)
+        printing.print_sudokus([self._main_model], self.w)
 
     def print_multiple_games (self, action):
         gp = printing.GamePrinter(self.sudoku_maker, self.gconf)
diff --git a/gnome-sudoku/src/lib/printing.py b/gnome-sudoku/src/lib/printing.py
index 806a512..98b6848 100644
--- a/gnome-sudoku/src/lib/printing.py
+++ b/gnome-sudoku/src/lib/printing.py
@@ -85,7 +85,7 @@ class SudokuPrinter:
             cr.move_to(left, top - height / 2)
             cr.show_text(label)
 
-            if isinstance(sudoku, model.SudokuGameDisplay):
+            if isinstance(sudoku, model.SudokuModel):
                 sudoku = sudoku.grid
 
             sudoku_thumber.draw_sudoku (cr, sudoku.grid, None, best_square_size, left, top, for_printing = True)
diff --git a/gnome-sudoku/src/lib/tracker_box.py b/gnome-sudoku/src/lib/tracker_box.py
index f392132..50e9199 100644
--- a/gnome-sudoku/src/lib/tracker_box.py
+++ b/gnome-sudoku/src/lib/tracker_box.py
@@ -54,11 +54,11 @@ class TrackerBox (gtk.VBox):
 
         gtk.VBox.__init__(self)
         self.builder = gtk.Builder()
-        self.builder.set_translation_domain(DOMAIN)
-        self.builder.add_from_file(os.path.join(UI_DIR, 'tracker.ui'))
-        self._tracker_model = TrackerModel(main_ui) ##???
-        self.tinfo = tracker_info.TrackerInfo()
-        self.tinfo.ui = self
+        self.builder.set_translation_domain(defaults.DOMAIN)
+        self.builder.add_from_file(os.path.join(defaults.UI_DIR, 'tracker.ui'))
+        #FIXME:
+#        self.tinfo = tracker_info.TrackerInfo()
+#        self.tinfo.ui = self
         self._view_controller = sudoku_view.get_tracker_controller()
         self.vb = self.builder.get_object('vbox1')
         self.vb.unparent()
@@ -91,7 +91,7 @@ class TrackerBox (gtk.VBox):
         self.tracker_tree.append_column(col2)
         self.tracker_tree.append_column(col1)
         # Our initial row...
-        pixbuf = self._pixbuf_transform_color(
+        pixbuf = _pixbuf_transform_color(
             STOCK_PIXBUFS['tracks'],
             (0, 0, 0)
             )
diff --git a/gnome-sudoku/src/lib/view.py b/gnome-sudoku/src/lib/view.py
index aa4bc79..627b09b 100644
--- a/gnome-sudoku/src/lib/view.py
+++ b/gnome-sudoku/src/lib/view.py
@@ -108,12 +108,28 @@ def gtkcolor_to_rgb (color):
             color.green / float(2**16),
             color.blue  / float(2**16))
 
+SPACING_FACTOR = 40 # The size of a box compared (roughly) to the size
+                    # of padding -- the larger this is, the smaller
+                    # the spaces
+SMALL_TO_BIG_FACTOR = 3.5 # The number of times wider than a small line a big line is.
+
 class SudokuNumberGrid (gtk.AspectFrame):
 
     def __init__ (self, group_size = 9):
         self.table = gtk.Table(rows = group_size, columns = group_size, homogeneous = True)
         self.group_size = group_size
         self.__entries__ = {}
+        self._setup_table ()
+        
+        gtk.AspectFrame.__init__(self, obey_child = False)
+        self.set_shadow_type(gtk.SHADOW_NONE)
+        self.eb = gtk.EventBox()
+        self.eb.add(self.table)
+        self.add(self.eb)
+        self.connect('size-allocate', self._allocate_cb)
+        self.show_all()
+    
+    def _setup_table (self):
         for x in range(self.group_size):
             for y in range(self.group_size):
                 e = number_box.SudokuNumberBox(upper = self.group_size)
@@ -123,6 +139,30 @@ class SudokuNumberGrid (gtk.AspectFrame):
                 self.__entries__[(x, y)] = e
                 e.connect('key-press-event', self._key_press_cb)
 
+    def _allocate_cb (self, widget, rect):
+        if rect.width > rect.height:
+            side = rect.height
+        else: side = rect.width
+        # we want our small spacing to be 1/15th the size of a box
+        spacing = float(side) / (self.group_size * SPACING_FACTOR)
+        if spacing == 0:
+            spacing = 1
+        if hasattr(self, 'small_spacing') and spacing == self.small_spacing:
+            return
+        else:
+            self._change_spacing(spacing)
+
+    def _change_spacing (self, small_spacing):
+        self.small_spacing = small_spacing
+        self.big_spacing = int(small_spacing*SMALL_TO_BIG_FACTOR)
+        self.table.set_row_spacings(int(small_spacing))
+        self.table.set_col_spacings(int(small_spacing))
+        box_side = int(math.sqrt(self.group_size))
+        for n in range(1, box_side):
+            self.table.set_row_spacing(box_side*n-1, self.big_spacing)
+            self.table.set_col_spacing(box_side*n-1, self.big_spacing)
+        self.table.set_border_width(self.big_spacing)
+                
     def set_color(self, x, y, color):
         self.__entries__[(x, y)].set_color(color)
 
@@ -194,6 +234,7 @@ class SudokuView(SudokuNumberGrid):
             user_input -> controller -> model -> view
         """
         SudokuNumberGrid.__init__(self, group_size)
+#FIXME:        
 #        self.hint_square = None
 #        self.tinfo = tracker_info.TrackerInfo()
 



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