[Sugar-devel] [PATCH sugar-toolkit-gtk3] Add DescriptionEntry to the activity sub-toolbar (v3)

Simon Schampijer simon at schampijer.de
Wed Feb 1 16:39:51 EST 2012


This is the implementation of the 'Write to Journal anytime'
feature. This patch is based on Walter's sugar-toolkit-gtk2
patch.

The patch itself adds a DescriptionEntry 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.

Signed-off-by: Simon Schampijer <simon at laptop.org>
Signed-off-by: Walter Bender <walter.bender at gmail.com>
---
 src/sugar3/activity/widgets.py |   73 ++++++++++++++++++++++++++++++++++++++++
 1 files changed, 73 insertions(+), 0 deletions(-)

diff --git a/src/sugar3/activity/widgets.py b/src/sugar3/activity/widgets.py
index 3f67300..0701230 100644
--- a/src/sugar3/activity/widgets.py
+++ b/src/sugar3/activity/widgets.py
@@ -1,4 +1,6 @@
 # Copyright (C) 2009, Aleksey Lim, Simon Schampijer
+# Copyright (C) 2012, Walter Bender
+# Copyright (C) 2012, One Laptop Per Child
 #
 # This library is free software; you can redistribute it and/or
 # modify it under the terms of the GNU Lesser General Public
@@ -29,6 +31,7 @@ from sugar3.graphics.toolbox import Toolbox
 from sugar3.graphics.xocolor import XoColor
 from sugar3.graphics.icon import Icon
 from sugar3.bundle.activitybundle import ActivityBundle
+from sugar3.graphics import style
 
 
 _ = lambda msg: gettext.dgettext('sugar-toolkit', msg)
@@ -213,6 +216,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('write-journal')
+        description_button.show()
+        description_button.set_tooltip(_('Descriptions'))
+        self._palette = description_button.get_palette()
+
+        description_box = Gtk.HBox()
+        sw = Gtk.ScrolledWindow()
+        sw.set_size_request(int(Gdk.Screen.width() / 2),
+                            2 * style.GRID_CELL_SIZE)
+        sw.set_policy(Gtk.PolicyType.AUTOMATIC, Gtk.PolicyType.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 and sharing button"""
 
@@ -234,6 +302,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)
-- 
1.7.7.6



More information about the Sugar-devel mailing list