From aaea3591e27adfafa6a5b47a63a309b15bbbff20 Mon Sep 17 00:00:00 2001 From: Gonzalo Odiard Date: Thu, 19 Apr 2012 19:21:56 +0000 Subject: Add DescriptionEntry to the activity sub-toolbar This is the implementation of the 'Write to Journal anytime' feature [1]. The patch itself adds a DescriptionItem to the activity sub-toolbar to make editing a Journal entry description from within the activity possible. The code has the same error handling as the TitleEntry. The patch is an adaptation from the one that landed in sugar-toolkit-gtk3 and has been ported to the sugar-toolkit. After talking with teachers and the learning team, I found out that they are very excited about this feature. Doing this change not only in sugar-toolkit-gtk3 and limit it to the activities that have been ported to the new toolkit we will have a more consistent experience across the activities in the 0.96 release. The original patch was done by Simon Schampijer and Walter Bender Signed-off-by: Gonzalo Odiard Acked-by: Simon Schampijer [1] http://wiki.sugarlabs.org/go/Features/Write_to_journal_anytime --- (limited to 'src/sugar/activity/widgets.py') diff --git a/src/sugar/activity/widgets.py b/src/sugar/activity/widgets.py index e5c4063..ab75cd6 100644 --- a/src/sugar/activity/widgets.py +++ b/src/sugar/activity/widgets.py @@ -28,6 +28,7 @@ from sugar.graphics.toolbox import Toolbox from sugar.graphics.xocolor import XoColor from sugar.graphics.icon import Icon from sugar.bundle.activitybundle import ActivityBundle +from sugar.graphics import style _ = lambda msg: gettext.dgettext('sugar-toolkit', msg) @@ -235,6 +236,71 @@ class TitleEntry(gtk.ToolItem): shared_activity.props.name = title +class DescriptionItem(gtk.ToolItem): + + def __init__(self, activity, **kwargs): + gtk.ToolItem.__init__(self) + + description_button = ToolButton('edit-description') + description_button.show() + description_button.set_tooltip(_('Description')) + self._palette = description_button.get_palette() + + description_box = gtk.HBox() + sw = gtk.ScrolledWindow() + sw.set_size_request(int(gtk.gdk.screen_width() / 2), + 2 * style.GRID_CELL_SIZE) + sw.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC) + self._text_view = gtk.TextView() + self._text_view.set_left_margin(style.DEFAULT_PADDING) + self._text_view.set_right_margin(style.DEFAULT_PADDING) + text_buffer = gtk.TextBuffer() + if 'description' in activity.metadata: + text_buffer.set_text(activity.metadata['description']) + self._text_view.set_buffer(text_buffer) + self._text_view.connect('focus-out-event', + self.__description_changed_cb, activity) + sw.add(self._text_view) + description_box.pack_start(sw, False, True, 0) + self._palette.set_content(description_box) + description_box.show_all() + + self.add(description_button) + description_button.connect('clicked', + self.__description_button_clicked_cb) + + activity.metadata.connect('updated', self.__jobject_updated_cb) + + def _get_text_from_buffer(self): + buf = self._text_view.get_buffer() + start_iter = buf.get_start_iter() + end_iter = buf.get_end_iter() + return buf.get_text(start_iter, end_iter, False) + + def __jobject_updated_cb(self, jobject): + if self._text_view.has_focus(): + return + if 'description' not in jobject: + return + if self._get_text_from_buffer() == jobject['description']: + return + buf = self._text_view.get_buffer() + buf.set_text(jobject['description']) + + def __description_button_clicked_cb(self, button): + self._palette.popup(immediate=True, state=1) + + def __description_changed_cb(self, widget, event, activity): + description = self._get_text_from_buffer() + if 'description' in activity.metadata and \ + description == activity.metadata['description']: + return + + activity.metadata['description'] = description + activity.save() + return False + + class ActivityToolbar(gtk.Toolbar): """The Activity toolbar with the Journal entry title, sharing and Stop buttons @@ -261,6 +327,11 @@ class ActivityToolbar(gtk.Toolbar): self.insert(separator, -1) separator.show() + if activity.metadata: + description_item = DescriptionItem(activity) + description_item.show() + self.insert(description_item, -1) + self.share = ShareButton(activity) self.share.show() self.insert(self.share, -1) -- cgit v0.9.1