[gnome-games/applygsoc2009: 51/76] Change method to function



commit 1bbefa44e3dffe5e725b9478f2a4f3e21ab727ef
Author: Pablo Castellano <pablog src gnome org>
Date:   Wed Sep 1 04:11:30 2010 +0200

    Change method to function

 gnome-sudoku/src/lib/tracker_box.py |   85 +++++++++++++++++++++-------------
 1 files changed, 52 insertions(+), 33 deletions(-)
---
diff --git a/gnome-sudoku/src/lib/tracker_box.py b/gnome-sudoku/src/lib/tracker_box.py
index 36c6543..feeabe7 100644
--- a/gnome-sudoku/src/lib/tracker_box.py
+++ b/gnome-sudoku/src/lib/tracker_box.py
@@ -9,23 +9,59 @@ from gettext import gettext as _
 
 import defaults
 
-ICON_FACTORY = gtk.IconFactory()
 STOCK_PIXBUFS = {}
-for filename, stock_id in [('footprints.png', 'tracks'), ]:
+
+def _add_tracker_stock_icon():
+    icon_factory = gtk.IconFactory()
+    for filename, stock_id in [('footprints.png', 'tracks'), ]:
+        try:
+            pixbuf = gtk.gdk.pixbuf_new_from_file(os.path.join(defaults.IMAGE_DIR,
+                filename))
+        except gobject.GError, err:
+            print 'Failed to load pixbuf: %s' % err
+            continue
+        STOCK_PIXBUFS[stock_id] = pixbuf
+        iconset = gtk.IconSet(pixbuf)
+        icon_factory.add(stock_id, iconset)
+        icon_factory.add_default()
+
+    gtk.stock_add([('tracks',
+                    _('Track moves'),
+                    0, 0, ""), ])
+
+_add_tracker_stock_icon()
+
+TRACKER_COLORS = [
+    # Use tango colors recommended here:
+    # http://tango.freedesktop.org/Tango_Icon_Theme_Guidelines
+    tuple([c / 255.0 for c in cols]) for cols in
+    [(32, 74, 135), # Sky Blue 3
+     (78, 154, 6), # Chameleon 3
+     (206, 92, 0), # Orange 3
+     (143, 89, 2), # Chocolate 3
+     (92, 53, 102), # Plum 3
+     (85, 87, 83), # Aluminium 5
+     (196, 160, 0), # Butter 3
+     ]
+    ]
+
+def _get_tracker_color(identifier):
     try:
-        pb = gtk.gdk.pixbuf_new_from_file(os.path.join(defaults.IMAGE_DIR,
-            filename))
-    except gobject.GError, e:
-        print 'Failed to load pixbuf: %s' % e
-        continue
-    STOCK_PIXBUFS[stock_id] = pb
-    iconset = gtk.IconSet(pb)
-    ICON_FACTORY.add(stock_id, iconset)
-    ICON_FACTORY.add_default()
-
-gtk.stock_add([('tracks',
-                _('Track moves'),
-                0, 0, ""), ])
+        return TRACKER_COLORS[identifier]
+    except IndexError:
+        new_color = _generate_new_color()
+        TRACKER_COLORS.append(new_color)
+        return _get_tracker_color(identifier)
+
+def _generate_new_color():
+    random_color = TRACKER_COLORS[0]
+    while random_color in TRACKER_COLORS:
+        # If we have generated all possible colors, this will
+        # enter an infinite loop
+        random_color = (random.randint(0, 100)/100.0,
+                        random.randint(0, 100)/100.0,
+                        random.randint(0, 100)/100.0)
+    return random_color
 
 def _pixbuf_transform_color(pixbuf, color):
     """Return new pixbuf with color changed to color"""
@@ -321,7 +357,7 @@ class TrackerModel:
                 self._add_to_tracker(tracker_id, x, y)
 
     def _add_to_tracker(self, tracker_id, x, y):
-        color = self.get_tracker_color(tracker_id)
+        color = _get_tracker_color(tracker_id)
         self._sudoku_view.set_color(x, y, color)
         self._trackers[tracker_id].append((x, y))
 
@@ -336,23 +372,6 @@ class TrackerModel:
         self._trackers[identifier] = []
         return identifier
 
-    def get_tracker_color(self, identifier):
-        return self.TRACKER_COLORS[identifier]
-        if 0:
-            new_color = self._generate_new_color()
-            self.TRACKER_COLORS.append(new_color)
-            return self.get_tracker_color(identifier)
-
-    def _generate_new_color(self):
-        random_color = self.TRACKER_COLORS[0]
-        while random_color in self.TRACKER_COLORS:
-            # If we have generated all possible colors, this will
-            # enter an infinite loop
-            random_color = (random.randint(0, 100)/100.0,
-                            random.randint(0, 100)/100.0,
-                            random.randint(0, 100)/100.0)
-        return random_color
-
     def toggle_tracker(self, identifier, value):
         """Toggle tracking for tracker identified by identifier."""
         self.__trackers_tracking__[identifier] = value



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