From 69d7ba3bf324228f916c657a4fd4ec9f83782f9d Mon Sep 17 00:00:00 2001 From: Walter Bender Date: Fri, 23 Mar 2012 19:18:17 +0000 Subject: new help system --- diff --git a/TurtleArt/tapalette.py b/TurtleArt/tapalette.py index be9d999..276e830 100644 --- a/TurtleArt/tapalette.py +++ b/TurtleArt/tapalette.py @@ -1,5 +1,5 @@ #!/usr/bin/env python -#Copyright (c) 2011 Walter Bender +#Copyright (c) 2011,12 Walter Bender #Permission is hereby granted, free of charge, to any person obtaining a copy #of this software and associated documentation files (the "Software"), to deal @@ -19,6 +19,8 @@ #OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN #THE SOFTWARE. +help_palettes = {} +help_windows = {} palette_names = [] palette_blocks = [] block_colors = [] @@ -75,6 +77,12 @@ block_styles = {'basic-style': [], 'portfolio-style-2x1': [], 'portfolio-style-1x2': []} + +import gtk + +from sugar.graphics.icon import Icon +from sugar.graphics import style + from taconstants import EXPANDABLE_STYLE from tautils import debug_output @@ -94,6 +102,25 @@ class Palette(): self._special_name = _(name) self._colors = colors self._help = None + self._max_text_width = int(gtk.gdk.screen_width() / 3) - 20 + + # Prepare a vbox for the help palette + if not (self._name in help_palettes): + self._help_box = gtk.VBox() + self._help_box.set_homogeneous(False) + help_palettes[self._name] = self._help_box + else: + self._help_box = help_palettes[self._name] + + help_windows[self._name] = gtk.ScrolledWindow() + help_windows[self._name].set_size_request( + int(gtk.gdk.screen_width() / 3), + gtk.gdk.screen_height() - style.GRID_CELL_SIZE * 3) + help_windows[self._name].set_policy( + gtk.POLICY_NEVER, gtk.POLICY_AUTOMATIC) + help_windows[self._name].add_with_viewport( + help_palettes[self._name]) + help_palettes[self._name].show() ''' self._fd = open('/home/walter/Desktop/turtleblocks/doc/%s-palette.page' % (name), 'a') @@ -150,8 +177,48 @@ class Palette(): else: help_strings[self._name] = '' + def add_section(self, section_text, icon=None): + ''' Add a section to the help palette. From helpbutton.py by + Gonzalo Odiard ''' + hbox = gtk.HBox() + label = gtk.Label() + label.set_use_markup(True) + label.set_markup('%s' % section_text) + label.set_line_wrap(True) + label.set_size_request(self._max_text_width, -1) + hbox.add(label) + if icon is not None: + _icon = Icon(icon_name=icon) + hbox.add(_icon) + label.set_size_request(self._max_text_width - 20, -1) + else: + label.set_size_request(self._max_text_width, -1) + + hbox.show_all() + self._help_box.pack_start(hbox, False, False, padding=5) + + def add_paragraph(self, text, icon=None): + ''' Add an entry to the help palette. From helpbutton.py by + Gonzalo Odiard ''' + hbox = gtk.HBox() + label = gtk.Label(text) + label.set_justify(gtk.JUSTIFY_LEFT) + label.set_line_wrap(True) + hbox.add(label) + if icon is not None: + _icon = Icon(icon_name=icon) + hbox.add(_icon) + label.set_size_request(self._max_text_width - 20, -1) + else: + label.set_size_request(self._max_text_width, -1) + + hbox.show_all() + self._help_box.pack_start(hbox, False, False, padding=5) + def set_help(self, help): self._help = help + if hasattr(self, '_help_box'): + self.add_section(self._help, icon=self._name + 'off') def set_special_name(self, name): self._special_name = name @@ -176,8 +243,15 @@ class Palette(): block.set_prim_name(prim_name) if logo_command is not None: block.set_logo_command(logo_command) - if help_string is not None: + if not hidden and help_string is not None: block.set_help(help_string) + if special_name is None: + if type(label) == list: + self.add_paragraph('%s: %s' % (label[0], help_string)) + else: + self.add_paragraph('%s: %s' % (label, help_string)) + else: + self.add_paragraph('%s: %s' % (special_name, help_string)) if colors is not None: block.set_colors(colors) block.set_value_block(value_block) diff --git a/TurtleArt/tawindow.py b/TurtleArt/tawindow.py index b324c7b..3771484 100644 --- a/TurtleArt/tawindow.py +++ b/TurtleArt/tawindow.py @@ -124,6 +124,8 @@ class TurtleArtWindow(): self.height = gtk.gdk.screen_height() self.rect = gtk.gdk.Rectangle(0, 0, 0, 0) + self.no_help = False + self.keypress = '' self.keyvalue = 0 self.dead_key = '' @@ -1729,6 +1731,8 @@ class TurtleArtWindow(): def _do_show_popup(self, block_name): """ Fetch the help text and display it. """ + if self.no_help: + return 0 if block_name in special_names: special_block_name = special_names[block_name] elif block_name in block_names: @@ -1738,18 +1742,16 @@ class TurtleArtWindow(): else: special_block_name = _(block_name) if block_name in help_strings: + label = help_strings[block_name] + ''' if special_block_name == '': label = help_strings[block_name] else: label = special_block_name + ": " + help_strings[block_name] + ''' else: label = special_block_name - if self.running_sugar: - self.activity.hover_help_label.set_text(label) - self.activity.hover_help_label.show() - else: - if self.interactive_mode: - self.parent.set_title(_("Turtle Art") + " — " + label) + self.showlabel('help', label=label) return 0 def _buttonrelease_cb(self, win, event): @@ -3073,7 +3075,9 @@ class TurtleArtWindow(): if shp == 'info': self.status_spr.move((PALETTE_WIDTH, self.height - 400)) else: - self.status_spr.move((PALETTE_WIDTH, self.height - 200)) + # Adjust vertical position based on scrolled window adjustment + self.status_spr.move((0, self.height - 200 + \ + self.activity.sw.get_vadjustment().get_value())) def calc_position(self, template): """ Relative placement of portfolio objects (deprecated) """ diff --git a/TurtleArtActivity.py b/TurtleArtActivity.py index fb32fd1..f532f47 100644 --- a/TurtleArtActivity.py +++ b/TurtleArtActivity.py @@ -58,6 +58,9 @@ from TurtleArt.tautils import data_to_file, data_to_string, data_from_string, \ from TurtleArt.tawindow import TurtleArtWindow from TurtleArt.tacollaboration import Collaboration +if HAS_TOOLBARBOX: + from util.helpbutton import HelpButton + class TurtleArtActivity(activity.Activity): ''' Activity subclass for Turtle Art ''' @@ -87,7 +90,8 @@ class TurtleArtActivity(activity.Activity): _logger.debug('_setup_palette_toolbar') self._setup_palette_toolbar() self._setup_extra_controls() - self._setup_help_toolbar() + if not self.has_toolbarbox: + self._setup_help_toolbar() _logger.debug('_setup_sharing') self._setup_sharing() @@ -258,9 +262,26 @@ class TurtleArtActivity(activity.Activity): return if not self.has_toolbarbox: self.palette_buttons[i].set_icon(palette_names[i] + 'on') + else: + _logger.debug('setting help button palette to %s' % (palette_names[i])) + self._help_button.set_current_palette(palette_names[i]) self.tw.show_palette(n=i) self.do_showpalette() + def _do_hover_help_toggle(self, button): + ''' Toggle hover help ''' + if self.tw.no_help: + _logger.debug('turning hover help on') + self.tw.no_help = False + self._hover_help_toggle.set_icon('help-off') + self._hover_help_toggle.set_tooltip(_('Turn off hover help')) + else: + _logger.debug('turning hover help off') + self.tw.no_help = True + self.tw.status_spr.hide() + self._hover_help_toggle.set_icon('help-on') + self._hover_help_toggle.set_tooltip(_('Turn on hover help')) + # These methods are called both from toolbar buttons and blocks. def do_hidepalette(self): @@ -484,10 +505,9 @@ class TurtleArtActivity(activity.Activity): self._palette_toolbar = gtk.Toolbar() self._palette_toolbar_button = ToolbarButton( page=self._palette_toolbar, icon_name='palette') - self._help_toolbar = gtk.Toolbar() - self._help_toolbar_button = ToolbarButton(label=_('Help'), - page=self._help_toolbar, - icon_name='help-toolbar') + + + self._help_button = HelpButton() self._make_load_save_buttons(activity_toolbar_button) @@ -515,8 +535,6 @@ class TurtleArtActivity(activity.Activity): self._toolbox.add_toolbar(_('Edit'), edit_toolbar) journal_toolbar = gtk.Toolbar() self._toolbox.add_toolbar(_('Save/Load'), journal_toolbar) - self._help_toolbar = gtk.Toolbar() - self._toolbox.add_toolbar(_('Help'), self._help_toolbar) self._make_palette_buttons(self._project_toolbar, palette_button=True) @@ -549,10 +567,13 @@ class TurtleArtActivity(activity.Activity): self.resize_down_button = self._add_button( 'resize-', _('Shrink blocks'), self.do_shrink_blocks_cb, view_toolbar) + self._hover_help_toggle = self._add_button( + 'help-off', _('Turn off hover help'), self._do_hover_help_toggle, + view_toolbar) + edit_toolbar.show() view_toolbar.show() - self._help_toolbar.show() self._toolbox.show() if not self.has_toolbarbox: @@ -574,28 +595,14 @@ class TurtleArtActivity(activity.Activity): 'ta-open', _('Load example'), self.do_samples_cb, self._toolbox.toolbar) - self._help_toolbar_button.show() - self._toolbox.toolbar.insert(self._help_toolbar_button, -1) + self._toolbox.toolbar.insert(self._help_button, -1) + self._help_button.show() stop_button = StopButton(self) stop_button.props.accelerator = 'Q' self._toolbox.toolbar.insert(stop_button, -1) stop_button.show() - def _setup_help_toolbar(self): - ''' The help toolbar must be setup we determine what hardware - is in use. ''' - # FIXME: Temporary work-around gtk problem with XO175 - if get_hardware() not in [XO1, XO15, XO175, XO30] and \ - (gtk.gtk_version[0] > 2 or gtk.gtk_version[1] > 16): - self.hover_help_label = self._add_label( - _('Move the cursor over the orange palette for help.'), - self._help_toolbar, gtk.gdk.screen_width() - 2 * ICON_SIZE) - else: - self.hover_help_label = self._add_label( - _('Move the cursor over the orange palette for help.'), - self._help_toolbar) - def _setup_palette_toolbar(self): ''' The palette toolbar must be setup *after* plugins are loaded. ''' if self.has_toolbarbox: diff --git a/icons/help-off.svg b/icons/help-off.svg new file mode 100644 index 0000000..7a038b6 --- /dev/null +++ b/icons/help-off.svg @@ -0,0 +1,54 @@ + + + + + + + + image/svg+xml + + + + + + + + + + + + + + + diff --git a/icons/help-on.svg b/icons/help-on.svg new file mode 100644 index 0000000..c818f84 --- /dev/null +++ b/icons/help-on.svg @@ -0,0 +1,57 @@ + + + + + + + + image/svg+xml + + + + + + + + + + + + + + + + diff --git a/images/help.svg b/images/help.svg new file mode 100644 index 0000000..3281351 --- /dev/null +++ b/images/help.svg @@ -0,0 +1,50 @@ + + + + + + + + image/svg+xml + + + + + + + + + + + X + + + diff --git a/util/helpbutton.py b/util/helpbutton.py new file mode 100644 index 0000000..879320e --- /dev/null +++ b/util/helpbutton.py @@ -0,0 +1,61 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +# Copyright (C) 2012, Gonzalo Odiard +# Copyright (C) 2012, 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. +# +# 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 + +# HelpButton widget + +from gettext import gettext as _ + +import gtk + +from sugar.graphics.toolbutton import ToolButton + +from TurtleArt.tapalette import palette_names, help_windows + +import logging +_logger = logging.getLogger('turtleart-activity') + + +class HelpButton(gtk.ToolItem): + + def __init__(self, **kwargs): + self._current_palette = 'turtle' + + gtk.ToolItem.__init__(self) + + help_button = ToolButton('help-toolbar') + help_button.set_tooltip(_('Help')) + self.add(help_button) + help_button.show() + + self._palette = help_button.get_palette() + + help_button.connect('clicked', self.__help_button_clicked_cb) + + def set_current_palette(self, name): + _logger.debug(name) + self._current_palette = name + + def __help_button_clicked_cb(self, button): + if not (self._current_palette in help_windows): + _logger.debug('name %s not found' % (self._current_palette)) + return + self._palette.set_content(help_windows[self._current_palette]) + help_windows[self._current_palette].show_all() + + self._palette.popup(immediate=True, state=1) -- cgit v0.9.1