diff options
author | Walter Bender <walter.bender@gmail.com> | 2011-08-04 15:07:55 (GMT) |
---|---|---|
committer | Rafael Ortiz <rafael@activitycentral.com> | 2011-08-04 15:23:46 (GMT) |
commit | 6dc96718fe80b1c98d604f1e1799a86683c238b9 (patch) | |
tree | c5244f5ac2c7a470f45840acb82a99c5c2186807 | |
parent | 4fc831eb56ef6ca70f87271bd1f548c3e5c7ecee (diff) |
added custom distance metric toolbar; some minor clean up
This patch adds a toolbar with a combo box for specifying units of measure. The default is meters, but other choices include inches, yards, feet, cm. Also, the current measurement can be used to define a new metric, e.g., Smoots.
A few misc. clean ups in activity.py as well introduced while I was trying to sort out issues caused by the spam bug and the launching problem.
Signed-off-by: Rafael Ortiz <rafael@activitycentral.com>
-rw-r--r-- | activity.py | 89 | ||||
-rw-r--r-- | smoot_toolbar.py | 127 |
2 files changed, 182 insertions, 34 deletions
diff --git a/activity.py b/activity.py index e102e83..daa25b1 100644 --- a/activity.py +++ b/activity.py @@ -1,3 +1,4 @@ +# Copyright 2007-9 Benjamin M. Schwartz # Copyright 2007 Collabora Ltd. # # This program is free software; you can redistribute it and/or modify @@ -34,11 +35,11 @@ from dbus.gobject_service import ExportedGObject # named after our pid, to inhibit suspend. POWERD_INHIBIT_DIR = '/var/run/powerd-inhibit-suspend' -import sugar.activity.activity -from sugar.activity.activity import Activity, ActivityToolbox +import sugar +from sugar.activity import activity from sugar.presence import presenceservice -from gettext import gettext +from gettext import gettext as _ # will eventually be imported from sugar from sugar.presence.tubeconn import TubeConnection @@ -55,6 +56,7 @@ import dbus #import socket_test as arange import arange import atm_toolbars +import smoot_toolbar SERVICE = "org.laptop.AcousticMeasure" IFACE = SERVICE @@ -71,7 +73,7 @@ def gobject_idle_do(func, *args): ev.wait() return retval[0] -class AcousticMeasureActivity(Activity): +class AcousticMeasureActivity(activity.Activity): """AcousticMeasure Activity as specified in activity.info""" _message_dict = {} @@ -79,11 +81,15 @@ class AcousticMeasureActivity(Activity): def __init__(self, handle): """Set up the Acoustic Tape Measure activity.""" - Activity.__init__(self, handle) - gobject.threads_init() - #self.set_title(gettext('Acoustic Tape Measure Activity')) + super(AcousticMeasureActivity, self).__init__(handle) + + #self.set_title(_('Acoustic Tape Measure Activity')) self._logger = logging.getLogger('acousticmeasure-activity') + self._logger.debug("Here we go...") + + gobject.threads_init() + try: self._logger.debug("locale: " + locale.setlocale(locale.LC_ALL, '')) except locale.Error: @@ -100,6 +106,18 @@ class AcousticMeasureActivity(Activity): toolbar_box.toolbar.insert(activity_button, 0) activity_button.show() + title_entry = TitleEntry(self) + toolbar_box.toolbar.insert(title_entry, -1) + title_entry.show() + + share_button = ShareButton(self) + toolbar_box.toolbar.insert(share_button, -1) + share_button.show() + + separator = gtk.SeparatorToolItem() + toolbar_box.toolbar.insert(separator, -1) + separator.show() + self._t_h_bar = atm_toolbars.TempToolbar() tb = gtk.HBox() self._t_h_bar.bigbox.reparent(tb) @@ -108,18 +126,13 @@ class AcousticMeasureActivity(Activity): icon_name='preferences-system') toolbar_box.toolbar.insert(adj_button, -1) adj_button.show() - - separator = gtk.SeparatorToolItem() - toolbar_box.toolbar.insert(separator, -1) - separator.show() - - title_entry = TitleEntry(self) - toolbar_box.toolbar.insert(title_entry, -1) - title_entry.show() - share_button = ShareButton(self) - toolbar_box.toolbar.insert(share_button, -1) - share_button.show() + self._smoot_bar = smoot_toolbar.SmootToolbar(self) + self._smoot_bar.show_all() + custom_button = ToolbarButton(page=self._smoot_bar, + icon_name='view-source') + toolbar_box.toolbar.insert(custom_button, -1) + custom_button.show() separator = gtk.SeparatorToolItem() separator.props.draw = False @@ -141,7 +154,10 @@ class AcousticMeasureActivity(Activity): toolbox.show() self._t_h_bar = atm_toolbars.TempToolbar() - toolbox.add_toolbar(gettext("Atmosphere"), self._t_h_bar) + toolbox.add_toolbar(_("Atmosphere"), self._t_h_bar) + + self._smoot_bar = smoot_toolbar.SmootToolbar() + toolbox.add_toolbar(_("Custom metric"), self._smoot_bar) if not self.powerd_running(): try: @@ -154,23 +170,26 @@ class AcousticMeasureActivity(Activity): self._logger.warning("Error setting OHM inhibit: %s" % e) self.ohm_keystore = None + #distance in meters + self.current_distance = 0.0 + #worker thread self._button_event = threading.Event() thread.start_new_thread(self._helper_thread, ()) # Main Panel GUI self.main_panel = gtk.VBox() - self._message_dict['unshared'] = gettext("To measure the distance between two laptops, you must first share this Activity.") - self._message_dict['ready'] = gettext("Press the button to measure the distance to another laptop") - self._message_dict['preparing'] = gettext("Preparing to measure distance") - self._message_dict['waiting'] = gettext("Ready to make a measurement. Waiting for partner to be ready.") - self._message_dict['playing'] = gettext("Recording sound from each laptop.") - self._message_dict['processing'] = gettext("Processing recorded audio.") + self._message_dict['unshared'] = _("To measure the distance between two laptops, you must first share this Activity.") + self._message_dict['ready'] = _("Press the button to measure the distance to another laptop") + self._message_dict['preparing'] = _("Preparing to measure distance") + self._message_dict['waiting'] = _("Ready to make a measurement. Waiting for partner to be ready.") + self._message_dict['playing'] = _("Recording sound from each laptop.") + self._message_dict['processing'] = _("Processing recorded audio.") self._message_dict['done'] = self._message_dict['ready'] - self._message_dict['full'] = gettext("This activity already has two participants, so you cannot join.") + self._message_dict['full'] = _("This activity already has two participants, so you cannot join.") - self._button_dict['waiting'] = gettext("Begin Measuring Distance") - self._button_dict['going'] = gettext("Stop Measuring Distance") + self._button_dict['waiting'] = _("Begin Measuring Distance") + self._button_dict['going'] = _("Stop Measuring Distance") self.button = gtk.ToggleButton(label=self._button_dict['waiting']) self.button.connect('clicked',self._button_clicked) @@ -203,14 +222,14 @@ class AcousticMeasureActivity(Activity): eb.add(self.value) eb.modify_bg(gtk.STATE_NORMAL, gtk.gdk.color_parse("white")) - fr = gtk.Frame(gettext("Measured Distance in Meters")) - fr.set_label_align(0.5,0.5) - fr.add(eb) + self.fr = gtk.Frame(_('Measured distance in %s') % (_('meters'))) + self.fr.set_label_align(0.5,0.5) + self.fr.add(eb) self.main_panel.pack_start(self.button, expand=False, padding=6) self.main_panel.pack_start(self.message, expand=False) self.main_panel.pack_start(img, expand=True, fill=False) - self.main_panel.pack_start(fr, expand=True, fill=False, padding=10) + self.main_panel.pack_start(self.fr, expand=True, fill=False, padding=10) self.set_canvas(self.main_panel) self.show_all() @@ -230,7 +249,7 @@ class AcousticMeasureActivity(Activity): self.connect('shared', self._shared_cb) self.connect('joined', self._joined_cb) - + self.connect('key-press-event', self._keypress_cb) def powerd_running(self): @@ -290,10 +309,12 @@ class AcousticMeasureActivity(Activity): self._logger.debug("initiating measurement") dt = arange.measure_dt_seq(self.main_socket, self.initiating, self._change_message) x = dt * self._t_h_bar.get_speed() - arange.OLPC_OFFSET + self.current_distance = x self._update_distance(x) def _update_distance(self, x): - mes = locale.format("%.2f", x) + scale = self._smoot_bar.get_scale() + mes = locale.format("%.2f", x * scale) gobject_idle_do(self.value.set_text, mes) def read_file(self, file_path): diff --git a/smoot_toolbar.py b/smoot_toolbar.py new file mode 100644 index 0000000..c130169 --- /dev/null +++ b/smoot_toolbar.py @@ -0,0 +1,127 @@ +# Copyright 2007 Benjamin M. Schwartz +# Copyright 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 2 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + +import gtk +from gettext import gettext as _ + +from sugar.graphics.combobox import ComboBox +from sugar.graphics.toolcombobox import ToolComboBox + +METERS = 0 +CENTIMETERS = 1 +INCHES = 2 +FEET = 3 +YARDS = 4 +CUSTOM = 5 +UNITS = [_('meters'), _('centimeters'), + # TRANS: English units of measure + _('inches'), _('feet'), _('yards'), + _('custom units')] +UNIT_DICTIONARY = {METERS: (_('meters'), 1.0), + CENTIMETERS: (_('centimeters'), 10.0), + INCHES: (_('inches'), 39.37), + FEET: (_('feet'), 3.28), + YARDS: (_('yards'), 1.09), + CUSTOM: (_('custom units'), None)} + + +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 _combo_factory(combo_array, default, tooltip, callback, toolbar): + '''Factory for making a toolbar combo box''' + my_combo = ComboBox() + if hasattr(my_combo, 'set_tooltip_text'): + my_combo.set_tooltip_text(tooltip) + + my_combo.connect('changed', callback) + + for i, s in enumerate(combo_array): + my_combo.append_item(i, s, None) + + toolbar.insert(ToolComboBox(my_combo), -1) + + my_combo.set_active(default) + + return my_combo + + +class SmootToolbar(gtk.Toolbar): + ''' Defines a toolbar for specifying units of measure ''' + + def __init__(self, parent): + gtk.Toolbar.__init__(self) + + self._parent = parent + self._unit_name = _('meters') + # Conversion factor between meters and custom units + self._unit_scale = 1.0 + + label = _label_factory(_('Choose a unit of measure:'), self) + label.show() + + self._unit_combo = _combo_factory(UNITS, METERS, _('select units'), + self._unit_combo_cb, self) + self._unit_combo.show() + + self._factor_label = _label_factory(' ', self) + self._factor_label.show() + + def get_name(self): + return self._unit_name + + def set_name(self, name): + self._unit_name = name + if hasattr(self._parent, 'fr'): + self._parent.fr.set_label( + _('Measured distance in %s') % (self._unit_name)) + if name == _('meters'): + self._factor_label.set_label(' ') + else: + self._factor_label.set_label(_('%0.2f %s per meter') % ( + self._unit_scale, name)) + + def get_scale(self): + return self._unit_scale + + def set_scale(self, scale): + if scale is None: + if self._parent.current_distance > 0: + self._unit_scale = (1.0 / self._parent.current_distance) + else: + self._unit_scale = 1.0 + else: + self._unit_scale = scale + + def _unit_combo_cb(self, arg=None): + ''' Read value of predefined conversion factors from combo box ''' + try: + self.set_scale( + UNIT_DICTIONARY[self._unit_combo.get_active()][1]) + self.set_name( + UNIT_DICTIONARY[self._unit_combo.get_active()][0]) + except KeyError: + pass |