[gnome-games/sudoku-tube] Split tracker-box into separate file
- From: Zhang Sen <zhangsen src gnome org>
- To: svn-commits-list gnome org
- Subject: [gnome-games/sudoku-tube] Split tracker-box into separate file
- Date: Sat, 4 Jul 2009 00:45:17 +0000 (UTC)
commit d529aa35ec187e49816e2333e15be958a738df00
Author: Zhang Sen <zh jesse gmail com>
Date: Sat Jul 4 08:44:20 2009 +0800
Split tracker-box into separate file
gnome-sudoku/src/lib/Makefile.am | 1 +
gnome-sudoku/src/lib/main.py | 154 +--------------------------------
gnome-sudoku/src/lib/tracker_box.py | 163 +++++++++++++++++++++++++++++++++++
3 files changed, 166 insertions(+), 152 deletions(-)
---
diff --git a/gnome-sudoku/src/lib/Makefile.am b/gnome-sudoku/src/lib/Makefile.am
index aa1aaa6..7650d3b 100644
--- a/gnome-sudoku/src/lib/Makefile.am
+++ b/gnome-sudoku/src/lib/Makefile.am
@@ -27,4 +27,5 @@ gnomesudoku_PYTHON = \
sudoku_thumber.py \
timer.py \
tp_tube.py \
+ tracker_box.py \
tube_handler.py
diff --git a/gnome-sudoku/src/lib/main.py b/gnome-sudoku/src/lib/main.py
index ccd5d85..a5b3baa 100644
--- a/gnome-sudoku/src/lib/main.py
+++ b/gnome-sudoku/src/lib/main.py
@@ -21,6 +21,7 @@ import saver
import sudoku_generator_gui
import sudoku_maker
import timer
+import tracker_box
from defaults import (APPNAME, APPNAME_SHORT, AUTHORS, COPYRIGHT, DESCRIPTION,
IMAGE_DIR, LICENSE, MIN_NEW_PUZZLES, UI_DIR, VERSION, WEBSITE, WEBSITE_LABEL)
from gtk_goodies import gconf_wrapper, Undo, dialog_extras
@@ -33,23 +34,6 @@ import tube_handler
logging.basicConfig()
logger = logging.getLogger("main")
-ICON_FACTORY = gtk.IconFactory()
-STOCK_PIXBUFS = {}
-for filename, stock_id in [('footprints.png', 'tracks'), ]:
- try:
- pb = gtk.gdk.pixbuf_new_from_file(os.path.join(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, ""), ])
-
class UI (gconf_wrapper.GConfWrapper):
@@ -508,7 +492,7 @@ class UI (gconf_wrapper.GConfWrapper):
self.gsd.auto_fill_current_entry()
def setup_tracker_interface (self):
- self.tracker_ui = TrackerBox(self)
+ self.tracker_ui = tracker_box.TrackerBox(self)
self.tracker_ui.show_all()
self.tracker_ui.hide()
self.main_area.pack_start(self.tracker_ui, expand=False)
@@ -625,140 +609,6 @@ class UI (gconf_wrapper.GConfWrapper):
def generate_puzzle_gui (self, *args):
sudoku_generator_gui.GameGenerator(self, self.gconf)
-class TrackerBox (gtk.VBox):
-
- def __init__ (self, main_ui):
-
- gtk.VBox.__init__(self)
- self.builder = gtk.Builder()
- self.builder.add_from_file(os.path.join(UI_DIR, 'tracker.ui'))
- self.main_ui = main_ui
- vbox = self.builder.get_object('vbox1')
- vbox.unparent()
- self.pack_start(vbox, expand = True, fill = True)
- self._setup_actions()
- self._setup_tree()
- self.show_all()
-
- def reset (self):
-
- for tree in self.tracker_model:
- if tree[0] > -1:
- self.tracker_model.remove(tree.iter)
-
- def _setup_tree (self):
- self.tracker_tree = self.builder.get_object('treeview1')
- self.tracker_model = gtk.ListStore(int, gtk.gdk.Pixbuf, str)
- self.tracker_tree.set_model(self.tracker_model)
- col1 = gtk.TreeViewColumn("", gtk.CellRendererPixbuf(), pixbuf = 1)
- col2 = gtk.TreeViewColumn("", gtk.CellRendererText(), text = 2)
- self.tracker_tree.append_column(col2)
- self.tracker_tree.append_column(col1)
- # Our initial row...
- self.tracker_model.append([-1, None, _('No Tracker')])
- self.tracker_tree.get_selection().connect('changed', self._selection_changed_cb)
-
- def _setup_actions (self):
- self.tracker_actions = gtk.ActionGroup('tracker_actions')
- self.tracker_actions.add_actions(
- [('Clear',
- gtk.STOCK_CLEAR,
- _('_Clear Tracker'),
- None, _('Clear all moves tracked by selected tracker.'),
- self._clear_cb
- ),
- ('Keep', None,
- _('_Clear Others'),
- None,
- _('Clear all moves not tracked by selected tracker.'),
- self._keep_cb),
- ]
- )
- for action, widget_name in [('Clear', 'ClearTrackerButton'),
- ('Keep', 'KeepTrackerButton'),
- ]:
- a = self.tracker_actions.get_action(action)
- a.connect_proxy(self.builder.get_object(widget_name))
- self.builder.get_object('AddTrackerButton').connect('clicked',
- lambda button: self.add_tracker())
- # Default to insensitive (they only become sensitive once a tracker is added)
- self.tracker_actions.set_sensitive(False)
-
- def add_tracker (self):
- tracker_id = self.main_ui.gsd.create_tracker()
- pixbuf = self._pixbuf_transform_color(
- STOCK_PIXBUFS['tracks'],
- self.main_ui.gsd.get_tracker_color(tracker_id),
- )
- # select our new tracker
- self.tracker_tree.get_selection().select_iter(
- self.tracker_model.append([tracker_id,
- pixbuf,
- _("Tracker %s") % (tracker_id + 1)]
- )
- )
-
- def _pixbuf_transform_color (self, pixbuf, color):
- """Return new pixbuf with color changed to color"""
- pixbuf_str = pixbuf.get_pixels()
- pixbuf_str_new = ""
-
- for alpha in pixbuf_str[3::4]:
- pixbuf_str_new += chr(int(color[0]*255))
- pixbuf_str_new += chr(int(color[1]*255))
- pixbuf_str_new += chr(int(color[2]*255))
- pixbuf_str_new += alpha
-
- return gtk.gdk.pixbuf_new_from_data(pixbuf_str_new, gtk.gdk.COLORSPACE_RGB, True, 8,
- pixbuf.get_width(), pixbuf.get_height(), pixbuf.get_rowstride())
-
- def select_tracker (self, tracker_id):
- for row in self.tracker_model:
- if row[0] == tracker_id:
- self.tracker_tree.get_selection().select_iter(row.iter)
-
- def _selection_changed_cb (self, selection):
- mod, itr = selection.get_selected()
- if itr:
- selected_tracker_id = mod.get_value(itr, 0)
- else:
- selected_tracker_id = -1
- # This should be cheap since we don't expect many trackers...
- # We cycle through each row and toggle it off if it's not
- # selected; on if it is selected
- for row in self.tracker_model:
- tid = row[0]
- if tid != -1: # -1 == no tracker
- self.main_ui.gsd.toggle_tracker(tid, tid == selected_tracker_id)
- self.tracker_actions.set_sensitive(selected_tracker_id != -1)
-
- def _clear_cb (self, action):
- mod, itr = self.tracker_tree.get_selection().get_selected()
- # This should only be called if there is an itr, but we'll
- # double-check just in case.
- if itr:
- selected_tracker_id = mod.get_value(itr, 0)
- self._tracker_delete_tracks(selected_tracker_id)
-
- def _keep_cb (self, action):
- mod, itr = self.tracker_tree.get_selection().get_selected()
- selected_tracker_id = mod.get_value(itr, 0)
- self._tracker_keep_tracks(selected_tracker_id)
-
- def _tracker_delete_tracks (self, tracker_id):
- clearer = Undo.UndoableObject(
- lambda *args: self.main_ui.cleared.append(self.main_ui.gsd.delete_by_tracker(tracker_id)),
- lambda *args: [self.main_ui.gsd.add_value(*entry) for entry in self.main_ui.cleared.pop()],
- self.main_ui.history)
- clearer.perform()
-
- def _tracker_keep_tracks (self, tracker_id):
- clearer = Undo.UndoableObject(
- lambda *args: self.main_ui.cleared.append(self.main_ui.gsd.delete_except_for_tracker(tracker_id)),
- lambda *args: [self.main_ui.gsd.add_value(*entry) for entry in self.main_ui.cleared.pop()],
- self.main_ui.history)
- clearer.perform()
-
def start_game(debug=False):
if debug:
diff --git a/gnome-sudoku/src/lib/tracker_box.py b/gnome-sudoku/src/lib/tracker_box.py
new file mode 100644
index 0000000..43d7a10
--- /dev/null
+++ b/gnome-sudoku/src/lib/tracker_box.py
@@ -0,0 +1,163 @@
+# -*- coding: utf-8 -*-
+
+import os.path
+
+import gtk
+import gobject
+from gettext import gettext as _
+
+import defaults
+from gtk_goodies import Undo
+
+ICON_FACTORY = gtk.IconFactory()
+STOCK_PIXBUFS = {}
+for filename, stock_id in [('footprints.png', 'tracks'), ]:
+ 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, ""), ])
+
+class TrackerBox (gtk.VBox):
+
+ def __init__ (self, main_ui):
+
+ gtk.VBox.__init__(self)
+ self.builder = gtk.Builder()
+ self.builder.add_from_file(os.path.join(defaults.UI_DIR,
+ 'tracker.ui'))
+ self.main_ui = main_ui
+ vbox = self.builder.get_object('vbox1')
+ vbox.unparent()
+ self.pack_start(vbox, expand = True, fill = True)
+ self._setup_actions()
+ self._setup_tree()
+ self.show_all()
+
+ def reset (self):
+
+ for tree in self.tracker_model:
+ if tree[0] > -1:
+ self.tracker_model.remove(tree.iter)
+
+ def _setup_tree (self):
+ self.tracker_tree = self.builder.get_object('treeview1')
+ self.tracker_model = gtk.ListStore(int, gtk.gdk.Pixbuf, str)
+ self.tracker_tree.set_model(self.tracker_model)
+ col1 = gtk.TreeViewColumn("", gtk.CellRendererPixbuf(), pixbuf = 1)
+ col2 = gtk.TreeViewColumn("", gtk.CellRendererText(), text = 2)
+ self.tracker_tree.append_column(col2)
+ self.tracker_tree.append_column(col1)
+ # Our initial row...
+ self.tracker_model.append([-1, None, _('No Tracker')])
+ self.tracker_tree.get_selection().connect('changed', self._selection_changed_cb)
+
+ def _setup_actions (self):
+ self.tracker_actions = gtk.ActionGroup('tracker_actions')
+ self.tracker_actions.add_actions(
+ [('Clear',
+ gtk.STOCK_CLEAR,
+ _('_Clear Tracker'),
+ None, _('Clear all moves tracked by selected tracker.'),
+ self._clear_cb
+ ),
+ ('Keep', None,
+ _('_Clear Others'),
+ None,
+ _('Clear all moves not tracked by selected tracker.'),
+ self._keep_cb),
+ ]
+ )
+ for action, widget_name in [('Clear', 'ClearTrackerButton'),
+ ('Keep', 'KeepTrackerButton'),
+ ]:
+ a = self.tracker_actions.get_action(action)
+ a.connect_proxy(self.builder.get_object(widget_name))
+ self.builder.get_object('AddTrackerButton').connect('clicked',
+ lambda button: self.add_tracker())
+ # Default to insensitive (they only become sensitive once a tracker is added)
+ self.tracker_actions.set_sensitive(False)
+
+ def add_tracker (self):
+ tracker_id = self.main_ui.gsd.create_tracker()
+ pixbuf = self._pixbuf_transform_color(
+ STOCK_PIXBUFS['tracks'],
+ self.main_ui.gsd.get_tracker_color(tracker_id),
+ )
+ # select our new tracker
+ self.tracker_tree.get_selection().select_iter(
+ self.tracker_model.append([tracker_id,
+ pixbuf,
+ _("Tracker %s") % (tracker_id + 1)]
+ )
+ )
+
+ def _pixbuf_transform_color (self, pixbuf, color):
+ """Return new pixbuf with color changed to color"""
+ pixbuf_str = pixbuf.get_pixels()
+ pixbuf_str_new = ""
+
+ for alpha in pixbuf_str[3::4]:
+ pixbuf_str_new += chr(int(color[0]*255))
+ pixbuf_str_new += chr(int(color[1]*255))
+ pixbuf_str_new += chr(int(color[2]*255))
+ pixbuf_str_new += alpha
+
+ return gtk.gdk.pixbuf_new_from_data(pixbuf_str_new, gtk.gdk.COLORSPACE_RGB, True, 8,
+ pixbuf.get_width(), pixbuf.get_height(), pixbuf.get_rowstride())
+
+ def select_tracker (self, tracker_id):
+ for row in self.tracker_model:
+ if row[0] == tracker_id:
+ self.tracker_tree.get_selection().select_iter(row.iter)
+
+ def _selection_changed_cb (self, selection):
+ mod, itr = selection.get_selected()
+ if itr:
+ selected_tracker_id = mod.get_value(itr, 0)
+ else:
+ selected_tracker_id = -1
+ # This should be cheap since we don't expect many trackers...
+ # We cycle through each row and toggle it off if it's not
+ # selected; on if it is selected
+ for row in self.tracker_model:
+ tid = row[0]
+ if tid != -1: # -1 == no tracker
+ self.main_ui.gsd.toggle_tracker(tid, tid == selected_tracker_id)
+ self.tracker_actions.set_sensitive(selected_tracker_id != -1)
+
+ def _clear_cb (self, action):
+ mod, itr = self.tracker_tree.get_selection().get_selected()
+ # This should only be called if there is an itr, but we'll
+ # double-check just in case.
+ if itr:
+ selected_tracker_id = mod.get_value(itr, 0)
+ self._tracker_delete_tracks(selected_tracker_id)
+
+ def _keep_cb (self, action):
+ mod, itr = self.tracker_tree.get_selection().get_selected()
+ selected_tracker_id = mod.get_value(itr, 0)
+ self._tracker_keep_tracks(selected_tracker_id)
+
+ def _tracker_delete_tracks (self, tracker_id):
+ clearer = Undo.UndoableObject(
+ lambda *args: self.main_ui.cleared.append(self.main_ui.gsd.delete_by_tracker(tracker_id)),
+ lambda *args: [self.main_ui.gsd.add_value(*entry) for entry in self.main_ui.cleared.pop()],
+ self.main_ui.history)
+ clearer.perform()
+
+ def _tracker_keep_tracks (self, tracker_id):
+ clearer = Undo.UndoableObject(
+ lambda *args: self.main_ui.cleared.append(self.main_ui.gsd.delete_except_for_tracker(tracker_id)),
+ lambda *args: [self.main_ui.gsd.add_value(*entry) for entry in self.main_ui.cleared.pop()],
+ self.main_ui.history)
+ clearer.perform()
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]