From f9f8d9d093adbc95be67278861a9b74db6c4ce24 Mon Sep 17 00:00:00 2001 From: Walter Bender Date: Fri, 18 Nov 2011 15:10:53 +0000 Subject: migrate to toolbar utils --- diff --git a/PathsActivity.py b/PathsActivity.py index 4e77714..faea56f 100644 --- a/PathsActivity.py +++ b/PathsActivity.py @@ -33,6 +33,9 @@ from sugar.graphics.menuitem import MenuItem from sugar.graphics.icon import Icon from sugar.datastore import datastore +from toolbar_utils import button_factory, image_factory, label_factory, \ + separator_factory + import telepathy from dbus.service import signal from dbus.gobject_service import ExportedGObject @@ -56,60 +59,6 @@ IFACE = SERVICE PATH = '/org/augarlabs/PathsActivity' -def _button_factory(icon_name, tooltip, callback, toolbar, cb_arg=None, - accelerator=None): - """Factory for making toolbar buttons""" - my_button = ToolButton(icon_name) - my_button.set_tooltip(tooltip) - my_button.props.sensitive = True - if accelerator is not None: - my_button.props.accelerator = accelerator - if cb_arg is not None: - my_button.connect('clicked', callback, cb_arg) - else: - my_button.connect('clicked', callback) - if hasattr(toolbar, 'insert'): # the main toolbar - toolbar.insert(my_button, -1) - else: # or a secondary toolbar - toolbar.props.page.insert(my_button, -1) - my_button.show() - return my_button - - -def _label_factory(label, toolbar): - """ Factory for adding a label to a toolbar """ - my_label = gtk.Label(label) - my_label.set_line_wrap(True) - my_label.show() - toolitem = gtk.ToolItem() - toolitem.add(my_label) - toolbar.insert(toolitem, -1) - toolitem.show() - return my_label - - -def _separator_factory(toolbar, visible=True, expand=False): - """ Factory for adding a separator to a toolbar """ - separator = gtk.SeparatorToolItem() - separator.props.draw = visible - separator.set_expand(expand) - toolbar.insert(separator, -1) - separator.show() - - -def _image_factory(image, toolbar, tooltip=None): - """ Add an image to the toolbar """ - img = gtk.Image() - img.set_from_pixbuf(image) - img_tool = gtk.ToolItem() - img_tool.add(img) - if tooltip is not None: - img.set_tooltip_text(tooltip) - toolbar.insert(img_tool, -1) - img_tool.show() - return img - - class PathsActivity(activity.Activity): """ Path puzzle game """ @@ -170,33 +119,33 @@ class PathsActivity(activity.Activity): toolbox.set_current_toolbar(1) self.toolbar = games_toolbar - self._new_game_button = _button_factory( - 'new-game', _('Start a new game.'), self._new_game_cb, - self.toolbar) + self._new_game_button = button_factory( + 'new-game', self.toolbar, self._new_game_cb, + tooltip=_('Start a new game.')) - self.robot_button = _button_factory( - 'robot-off', _('Play with the robot.'), self._robot_cb, - self.toolbar) + self.robot_button = button_factory( + 'robot-off', self.toolbar, self._robot_cb, + tooltip= _('Play with the robot.')) - self.player = _image_factory( + self.player = image_factory( svg_str_to_pixbuf(generate_xo(scale=0.8, colors=['#303030', '#303030'])), self.toolbar, tooltip=self.nick) - self.dialog_button = _button_factory('go-next', - _('Turn complete'), - self._dialog_cb, self.toolbar) + self.dialog_button = button_factory( + 'go-next', self.toolbar, self._dialog_cb, + tooltip=_('Turn complete')) - self.status = _label_factory('', self.toolbar) + self.status = label_factory(self.toolbar, '') - self.hint_button = _button_factory('help-toolbar', - _('Help'), - self._hint_cb, self.toolbar) + self.hint_button = button_factory( + 'help-toolbar', self.toolbar, self._hint_cb, + tooltip=_('Help')) - self.score = _label_factory(_('Score: ') + '0', self.toolbar) + self.score = label_factory(self.toolbar, _('Score: ') + '0') if _have_toolbox: - _separator_factory(toolbox.toolbar, False, True) + separator_factory(toolbox.toolbar, True, False) stop_button = StopButton(self) stop_button.props.accelerator = 'q' diff --git a/toolbar_utils.py b/toolbar_utils.py new file mode 100644 index 0000000..94e6883 --- /dev/null +++ b/toolbar_utils.py @@ -0,0 +1,164 @@ +# -*- coding: utf-8 -*- +# Copyright (c) 2011, Walter Bender + +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3 of the License, or +# (at your option) any later version. +# +# You should have received a copy of the GNU General Public License +# along with this library; if not, write to the Free Software +# Foundation, 51 Franklin Street, Suite 500 Boston, MA 02110-1335 USA + + +import gtk + +from sugar.graphics.radiotoolbutton import RadioToolButton +from sugar.graphics.toolbutton import ToolButton +from sugar.graphics.combobox import ComboBox +from sugar.graphics.toolcombobox import ToolComboBox + + +def combo_factory(combo_array, toolbar, callback, cb_arg=None, + tooltip=None, default=None): + '''Factory for making a toolbar combo box''' + combo = ComboBox() + if tooltip is not None and hasattr(combo, 'set_tooltip_text'): + combo.set_tooltip_text(tooltip) + if cb_arg is not None: + combo.connect('changed', callback, cb_arg) + else: + combo.connect('changed', callback) + for i, selection in enumerate(combo_array): + combo.append_item(i, selection, None) + combo.show() + toolitem = gtk.ToolItem() + toolitem.add(combo) + if hasattr(toolbar, 'insert'): # the main toolbar + toolbar.insert(toolitem, -1) + else: # or a secondary toolbar + toolbar.props.page.insert(toolitem, -1) + toolitem.show() + if default is not None: + combo.set_active(combo_array.index(default)) + return combo + + +def entry_factory(default_string, toolbar, tooltip=None, max=3): + ''' Factory for adding a text box to a toolbar ''' + entry = gtk.Entry() + entry.set_text(default_string) + if tooltip is not None and hasattr(entry, 'set_tooltip_text'): + entry.set_tooltip_text(tooltip) + entry.set_width_chars(max) + entry.show() + toolitem = gtk.ToolItem() + toolitem.add(entry) + if hasattr(toolbar, 'insert'): # the main toolbar + toolbar.insert(toolitem, -1) + else: # or a secondary toolbar + toolbar.props.page.insert(toolitem, -1) + toolitem.show() + return entry + + +def button_factory(icon_name, toolbar, callback, cb_arg=None, tooltip=None, + accelerator=None): + '''Factory for making tooplbar buttons''' + button = ToolButton(icon_name) + if tooltip is not None: + button.set_tooltip(tooltip) + button.props.sensitive = True + if accelerator is not None: + button.props.accelerator = accelerator + if cb_arg is not None: + button.connect('clicked', callback, cb_arg) + else: + button.connect('clicked', callback) + if hasattr(toolbar, 'insert'): # the main toolbar + toolbar.insert(button, -1) + else: # or a secondary toolbar + toolbar.props.page.insert(button, -1) + button.show() + return button + + +def radio_factory(name, toolbar, callback, cb_arg=None, tooltip=None, + group=None): + ''' Add a radio button to a toolbar ''' + button = RadioToolButton(group=group) + button.set_named_icon(name) + if callback is not None: + if cb_arg is None: + button.connect('clicked', callback) + else: + button.connect('clicked', callback, cb_arg) + if hasattr(toolbar, 'insert'): # Add button to the main toolbar... + toolbar.insert(button, -1) + else: # ...or a secondary toolbar. + toolbar.props.page.insert(button, -1) + button.show() + if tooltip is not None: + button.set_tooltip(tooltip) + return button + + +def label_factory(toolbar, label_text, width=None): + ''' Factory for adding a label to a toolbar ''' + label = gtk.Label(label_text) + label.set_line_wrap(True) + if width is not None: + label.set_size_request(width, -1) # doesn't work on XOs + label.show() + toolitem = gtk.ToolItem() + toolitem.add(label) + if hasattr(toolbar, 'insert'): # the main toolbar + toolbar.insert(toolitem, -1) + else: # or a secondary toolbar + toolbar.props.page.insert(toolitem, -1) + toolitem.show() + return label + + +def separator_factory(toolbar, expand=False, visible=True): + ''' add a separator to a toolbar ''' + separator = gtk.SeparatorToolItem() + separator.props.draw = visible + separator.set_expand(expand) + if hasattr(toolbar, 'insert'): # the main toolbar + toolbar.insert(separator, -1) + else: # or a secondary toolbar + toolbar.props.page.insert(separator, -1) + separator.show() + + +def image_factory(image, toolbar, tooltip=None): + ''' Add an image to the toolbar ''' + img = gtk.Image() + img.set_from_pixbuf(image) + img_tool = gtk.ToolItem() + img_tool.add(img) + if tooltip is not None: + img.set_tooltip_text(tooltip) + if hasattr(toolbar, 'insert'): # the main toolbar + toolbar.insert(img_tool, -1) + else: # or a secondary toolbar + toolbar.props.page.insert(img_tool, -1) + img_tool.show() + return img + + +def spin_factory(default, min, max, callback, toolbar): + spin_adj = gtk.Adjustment(default, min, max, 1, 32, 0) + spin = gtk.SpinButton(spin_adj, 0, 0) + spin_id = spin.connect('value-changed', callback) + spin.set_numeric(True) + spin.show() + toolitem = gtk.ToolItem() + toolitem.add(spin) + if hasattr(toolbar, 'insert'): # the main toolbar + toolbar.insert(toolitem, -1) + else: + toolbar.props.page.insert(toolitem, -1) + toolitem.show() + return spin -- cgit v0.9.1