From b43aa43114e329a467207dd3207c4c84f3aa8a26 Mon Sep 17 00:00:00 2001 From: Aleksey Lim Date: Thu, 30 Jul 2009 15:08:55 +0000 Subject: [PATCH] Move activity related widgets to sugar.activity.widgets --- src/sugar/activity/activity.py | 336 +-------------------------------- src/sugar/activity/widgets.py | 306 ++++++++++++++++++++++++++++++ 2 files changed, 309 insertions(+), 333 deletions(-) create mode 100644 src/sugar/activity/widgets.py diff --git a/src/sugar/activity/activity.py b/src/sugar/activity/activity.py index 6e19c51e..f20d402b 100644 --- a/src/sugar/activity/activity.py +++ b/src/sugar/activity/activity.py @@ -67,20 +67,15 @@ from sugar.activity.activityservice import ActivityService from sugar.activity.namingalert import NamingAlert from sugar.graphics import style from sugar.graphics.window import Window -from sugar.graphics.toolbox import Toolbox -from sugar.graphics.toolbutton import ToolButton -from sugar.graphics.toolcombobox import ToolComboBox from sugar.graphics.alert import Alert from sugar.graphics.icon import Icon -from sugar.graphics.xocolor import XoColor -from sugar.graphics.toolbar import Toolbar, ToolbarButton -from sugar.graphics.radiopalette import RadioPalette, RadioMenuButton -from sugar.graphics.radiotoolbutton import RadioToolButton -from sugar.bundle.activitybundle import ActivityBundle from sugar.datastore import datastore from sugar.session import XSMPClient from sugar import wm +# support deprecated imports +from sugar.activity.widgets import ActivityToolbar, EditToolbar, ActivityToolbox + _ = lambda msg: gettext.dgettext('sugar-toolkit', msg) SCOPE_PRIVATE = "private" @@ -91,226 +86,6 @@ J_DBUS_SERVICE = 'org.laptop.Journal' J_DBUS_PATH = '/org/laptop/Journal' J_DBUS_INTERFACE = 'org.laptop.Journal' -class ActivityToolbar(gtk.Toolbar): - """The Activity toolbar with the Journal entry title, sharing, - Keep and Stop buttons - - All activities should have this toolbar. It is easiest to add it to your - Activity by using the ActivityToolbox. - """ - def __init__(self, activity): - gtk.Toolbar.__init__(self) - - self._activity = activity - self._updating_share = False - - activity.connect('shared', self.__activity_shared_cb) - activity.connect('joined', self.__activity_shared_cb) - activity.connect('notify::max_participants', - self.__max_participants_changed_cb) - - if activity.metadata: - self.title = gtk.Entry() - self.title.set_size_request(int(gtk.gdk.screen_width() / 3), -1) - self.title.set_text(activity.metadata['title']) - self.title.connect('changed', self.__title_changed_cb) - self._add_widget(self.title) - - activity.metadata.connect('updated', self.__jobject_updated_cb) - - separator = gtk.SeparatorToolItem() - separator.props.draw = False - separator.set_expand(True) - self.insert(separator, -1) - separator.show() - - self.share = ToolComboBox(label_text=_('Share with:')) - self.share.combo.connect('changed', self.__share_changed_cb) - self.share.combo.append_item(SCOPE_PRIVATE, _('Private'), 'zoom-home') - self.share.combo.append_item(SCOPE_NEIGHBORHOOD, _('My Neighborhood'), - 'zoom-neighborhood') - self.insert(self.share, -1) - self.share.show() - - self._update_share() - - self.keep = ToolButton(tooltip=_('Keep')) - client = gconf.client_get_default() - color = XoColor(client.get_string('/desktop/sugar/user/color')) - keep_icon = Icon(icon_name='document-save', xo_color=color) - self.keep.set_icon_widget(keep_icon) - keep_icon.show() - self.keep.props.accelerator = 'S' - self.keep.connect('clicked', self.__keep_clicked_cb) - self.insert(self.keep, -1) - self.keep.show() - - self.stop = ToolButton('activity-stop', tooltip=_('Stop')) - self.stop.props.accelerator = 'Q' - self.stop.connect('clicked', self.__stop_clicked_cb) - self.insert(self.stop, -1) - self.stop.show() - - self._update_title_sid = None - - def _update_share(self): - self._updating_share = True - - if self._activity.props.max_participants == 1: - self.share.hide() - - if self._activity.get_shared(): - self.share.set_sensitive(False) - self.share.combo.set_active(1) - else: - self.share.set_sensitive(True) - self.share.combo.set_active(0) - - self._updating_share = False - - def __share_changed_cb(self, combo): - if self._updating_share: - return - - model = self.share.combo.get_model() - it = self.share.combo.get_active_iter() - (scope, ) = model.get(it, 0) - if scope == SCOPE_NEIGHBORHOOD: - self._activity.share() - - def __keep_clicked_cb(self, button): - self._activity.copy() - - def __stop_clicked_cb(self, button): - self._activity.close() - - def __jobject_updated_cb(self, jobject): - self.title.set_text(jobject['title']) - - def __title_changed_cb(self, entry): - if not self._update_title_sid: - self._update_title_sid = gobject.timeout_add_seconds( - 1, self.__update_title_cb) - - def __update_title_cb(self): - title = self.title.get_text() - - self._activity.metadata['title'] = title - self._activity.metadata['title_set_by_user'] = '1' - self._activity.save() - - shared_activity = self._activity.get_shared_activity() - if shared_activity: - shared_activity.props.name = title - - self._update_title_sid = None - return False - - def _add_widget(self, widget, expand=False): - tool_item = gtk.ToolItem() - tool_item.set_expand(expand) - - tool_item.add(widget) - widget.show() - - self.insert(tool_item, -1) - tool_item.show() - - def __activity_shared_cb(self, activity): - self._update_share() - - def __max_participants_changed_cb(self, activity, pspec): - self._update_share() - -class EditToolbar(gtk.Toolbar): - """Provides the standard edit toolbar for Activities. - - Members: - undo -- the undo button - redo -- the redo button - copy -- the copy button - paste -- the paste button - separator -- A separator between undo/redo and copy/paste - - This class only provides the 'edit' buttons in a standard layout, - your activity will need to either hide buttons which make no sense for your - Activity, or you need to connect the button events to your own callbacks: - - ## Example from Read.activity: - # Create the edit toolbar: - self._edit_toolbar = EditToolbar(self._view) - # Hide undo and redo, they're not needed - self._edit_toolbar.undo.props.visible = False - self._edit_toolbar.redo.props.visible = False - # Hide the separator too: - self._edit_toolbar.separator.props.visible = False - - # As long as nothing is selected, copy needs to be insensitive: - self._edit_toolbar.copy.set_sensitive(False) - # When the user clicks the button, call _edit_toolbar_copy_cb() - self._edit_toolbar.copy.connect('clicked', self._edit_toolbar_copy_cb) - - # Add the edit toolbar: - toolbox.add_toolbar(_('Edit'), self._edit_toolbar) - # And make it visible: - self._edit_toolbar.show() - """ - def __init__(self): - gtk.Toolbar.__init__(self) - - self.undo = ToolButton('edit-undo') - self.undo.set_tooltip(_('Undo')) - self.insert(self.undo, -1) - self.undo.show() - - self.redo = ToolButton('edit-redo') - self.redo.set_tooltip(_('Redo')) - self.insert(self.redo, -1) - self.redo.show() - - self.separator = gtk.SeparatorToolItem() - self.separator.set_draw(True) - self.insert(self.separator, -1) - self.separator.show() - - self.copy = ToolButton('edit-copy') - self.copy.set_tooltip(_('Copy')) - self.insert(self.copy, -1) - self.copy.show() - - self.paste = ToolButton('edit-paste') - self.paste.set_tooltip(_('Paste')) - self.insert(self.paste, -1) - self.paste.show() - -class ActivityToolbox(Toolbox): - """Creates the Toolbox for the Activity - - By default, the toolbox contains only the ActivityToolbar. After creating - the toolbox, you can add your activity specific toolbars, for example the - EditToolbar. - - To add the ActivityToolbox to your Activity in MyActivity.__init__() do: - - # Create the Toolbar with the ActivityToolbar: - toolbox = activity.ActivityToolbox(self) - ... your code, inserting all other toolbars you need, like EditToolbar - - # Add the toolbox to the activity frame: - self.set_toolbox(toolbox) - # And make it visible: - toolbox.show() - """ - def __init__(self, activity): - Toolbox.__init__(self) - - self._activity_toolbar = ActivityToolbar(activity) - self.add_toolbar(_('Activity'), self._activity_toolbar) - self._activity_toolbar.show() - - def get_activity_toolbar(self): - return self._activity_toolbar - class _ActivitySession(gobject.GObject): __gsignals__ = { 'quit-requested': (gobject.SIGNAL_RUN_FIRST, gobject.TYPE_NONE, ([])), @@ -1034,111 +809,6 @@ class Activity(Window, gtk.Container): # DEPRECATED _shared_activity = property(lambda self: self.shared_activity, None) -class ActivityToolbarButton(ToolbarButton): - def __init__(self, activity, **kwargs): - toolbar = ActivityToolbar(activity) - toolbar.stop.hide() - - ToolbarButton.__init__(self, page=toolbar, **kwargs) - self.activity = activity - - bundle = ActivityBundle(get_bundle_path()) - client = gconf.client_get_default() - color = XoColor(client.get_string('/desktop/sugar/user/color')) - icon = Icon(file=bundle.get_icon(), xo_color=color) - icon.show() - self.set_icon_widget(icon) - - def stop_button(self, **kwargs): - stop = ToolButton('activity-stop', tooltip=_('Stop'), **kwargs) - stop.props.accelerator = 'Q' - stop.connect('clicked', self.__stop_button_clicked_cb) - stop.show() - return stop - - def __stop_button_clicked_cb(self, button): - self.activity.close() - - def undo_button(self, **kwargs): - undo = ToolButton('edit-undo', **kwargs) - undo.set_tooltip(_('Undo')) - undo.show() - return undo - - def redo_button(self, **kwargs): - redo = ToolButton('edit-redo', **kwargs) - redo.set_tooltip(_('Redo')) - redo.show() - return redo - - def copy_button(self, **kwargs): - copy = ToolButton('edit-copy', **kwargs) - copy.set_tooltip(_('Copy')) - copy.show() - return copy - - def paste_button(self, **kwargs): - paste = ToolButton('edit-paste', **kwargs) - paste.set_tooltip(_('Paste')) - paste.show() - return paste - - def share_button(self): - palette = RadioPalette() - - private = RadioToolButton( - icon_name='zoom-home') - palette.append(private, _('Private')) - - neighborhood = RadioToolButton( - icon_name='zoom-neighborhood', - group=private) - neighborhood_handle = neighborhood.connect('clicked', - self.__neighborhood_clicked_cb) - palette.append(neighborhood, _('My Neighborhood')) - - self.activity.connect('shared', self.__update_share) - self.activity.connect('joined', self.__update_share) - - share = RadioMenuButton(palette=palette) - share.show() - - return share - - def __neighborhood_clicked_cb(self, button): - self.activity.share() - - def __update_share(self, activity): - neighborhood.handler_block(neighborhood_handle) - try: - if self.activity.get_shared(): - private.props.sensitive = False - neighborhood.props.sensitive = False - neighborhood.props.active = True - else: - private.props.sensitive = True - neighborhood.props.sensitive = True - private.props.active = True - finally: - neighborhood.handler_unblock(neighborhood_handle) - - def keep_button(self, **kwargs): - client = gconf.client_get_default() - color = XoColor(client.get_string('/desktop/sugar/user/color')) - keep_icon = Icon(icon_name='document-save', xo_color=color) - keep_icon.show() - - keep = ToolButton(tooltip=_('Keep'), **kwargs) - keep.set_icon_widget(keep_icon) - keep.props.accelerator = 'S' - keep.connect('clicked', self.__keep_button_clicked) - keep.show() - - return keep - - def __keep_button_clicked(self, button): - self.activity.copy() - _session = None def _get_session(): diff --git a/src/sugar/activity/widgets.py b/src/sugar/activity/widgets.py new file mode 100644 index 00000000..ca920aa6 --- /dev/null +++ b/src/sugar/activity/widgets.py @@ -0,0 +1,306 @@ +# Copyright (C) 2009, Aleksey Lim +# +# This library is free software; you can redistribute it and/or +# modify it under the terms of the GNU Lesser General Public +# License as published by the Free Software Foundation; either +# version 2 of the License, or (at your option) any later version. +# +# This library 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 +# Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public +# License along with this library; if not, write to the +# Free Software Foundation, Inc., 59 Temple Place - Suite 330, +# Boston, MA 02111-1307, USA. + +import gtk +import gobject +import logging +import gettext +import gconf + +from sugar.graphics.toolbutton import ToolButton +from sugar.graphics.toolbar import Toolbar, ToolbarButton +from sugar.graphics.radiopalette import RadioPalette, RadioMenuButton +from sugar.graphics.radiotoolbutton import RadioToolButton +from sugar.graphics.toolbox import Toolbox +from sugar.graphics.xocolor import XoColor +from sugar.graphics.icon import Icon +from sugar.bundle.activitybundle import ActivityBundle + +_ = lambda msg: gettext.dgettext('sugar-toolkit', msg) + +class ActivityToolbarButton(ToolbarButton): + def __init__(self, activity, **kwargs): + toolbar = ActivityToolbar(activity) + toolbar.stop.hide() + + ToolbarButton.__init__(self, page=toolbar, **kwargs) + + from sugar.activity.activity import get_bundle_path + bundle = ActivityBundle(get_bundle_path()) + + client = gconf.client_get_default() + color = XoColor(client.get_string('/desktop/sugar/user/color')) + icon = Icon(file=bundle.get_icon(), xo_color=color) + icon.show() + self.set_icon_widget(icon) + +class StopButton(ToolButton): + def __init__(self, activity, **kwargs): + ToolButton.__init__(self, 'activity-stop', + tooltip=_('Stop'), + accelerator='Q', + **kwargs) + self.connect('clicked', self.__stop_button_clicked_cb, activity) + + def __stop_button_clicked_cb(self, button, activity): + activity.close() + +class UndoButton(ToolButton): + def __init__(self, **kwargs): + ToolButton.__init__(self, 'edit-undo', + tooltip=_('Undo'), + accelerator='Q', + **kwargs) + +class RedoButton(ToolButton): + def __init__(self, **kwargs): + ToolButton.__init__(self, 'edit-redo', + tooltip=_('Redo'), + **kwargs) + +class CopyButton(ToolButton): + def __init__(self, **kwargs): + ToolButton.__init__(self, 'edit-copy', + tooltip=_('Copy'), + **kwargs) + +class PasteButton(ToolButton): + def __init__(self, **kwargs): + ToolButton.__init__(self, 'edit-paste', + tooltip=_('Paste'), + **kwargs) + +class ShareButton(RadioMenuButton): + def __init__(self, activity, **kwargs): + palette = RadioPalette() + + self.__private = RadioToolButton( + icon_name='zoom-home') + palette.append(self.__private, _('Private')) + + self.__neighborhood = RadioToolButton( + icon_name='zoom-neighborhood', + group=self.__private) + self.__neighborhood_handle = self.__neighborhood.connect( + 'clicked', self.__neighborhood_clicked_cb, activity) + palette.append(self.__neighborhood, _('My Neighborhood')) + + activity.connect('shared', self.__update_share) + activity.connect('joined', self.__update_share) + + RadioMenuButton.__init__(self, palette=palette, **kwargs) + + def __neighborhood_clicked_cb(self, button, activity): + activity.share() + + def __update_share(self, activity): + self.__neighborhood.handler_block(self.__neighborhood_handle) + try: + if activity.get_shared(): + self.__private.props.sensitive = False + self.__neighborhood.props.sensitive = False + self.__neighborhood.props.active = True + else: + self.__private.props.sensitive = True + self.__neighborhood.props.sensitive = True + self.__private.props.active = True + finally: + self.__neighborhood.handler_unblock(self.__neighborhood_handle) + +class KeepButton(ToolButton): + def __init__(self, activity, **kwargs): + ToolButton.__init__(self, + tooltip=_('Keep'), + accelerator='S', + **kwargs) + + client = gconf.client_get_default() + color = XoColor(client.get_string('/desktop/sugar/user/color')) + keep_icon = Icon(icon_name='document-save', xo_color=color) + keep_icon.show() + + self.set_icon_widget(keep_icon) + self.connect('clicked', self.__keep_button_clicked, activity) + + def __keep_button_clicked(self, button, activity): + activity.copy() + +class TitleEntry(gtk.Entry): + def __init__(self, activity, **kwargs): + gtk.Entry.__init__(self, **kwargs) + self.__update_title_sid = None + + self.set_size_request(int(gtk.gdk.screen_width() / 3), -1) + self.set_text(activity.metadata['title']) + self.connect('changed', self.__title_changed_cb, activity) + + activity.metadata.connect('updated', self.__jobject_updated_cb) + + def __jobject_updated_cb(self, jobject): + self.set_text(jobject['title']) + + def __title_changed_cb(self, entry, activity): + if not self.__update_title_sid: + self.__update_title_sid = gobject.timeout_add_seconds( + 1, self.__update_title_cb, activity) + + def __update_title_cb(self, activity): + title = self.get_text() + + activity.metadata['title'] = title + activity.metadata['title_set_by_user'] = '1' + activity.save() + + shared_activity = activity.get_shared_activity() + if shared_activity: + shared_activity.props.name = title + + self.__update_title_sid = None + return False + +class ActivityToolbar(gtk.Toolbar): + """The Activity toolbar with the Journal entry title, sharing, + Keep and Stop buttons + + All activities should have this toolbar. It is easiest to add it to your + Activity by using the ActivityToolbox. + """ + def __init__(self, activity): + gtk.Toolbar.__init__(self) + + self._activity = activity + + if activity.metadata: + self.title = TitleEntry(activity) + self._add_widget(self.title) + + separator = gtk.SeparatorToolItem() + separator.props.draw = False + separator.set_expand(True) + self.insert(separator, -1) + separator.show() + + self.share = ShareButton(activity) + self.share.show() + self.insert(self.share, -1) + + self.keep = KeepButton(activity) + self.insert(self.keep, -1) + self.keep.show() + + self.stop = StopButton(activity) + self.insert(self.stop, -1) + self.stop.show() + + def _add_widget(self, widget, expand=False): + tool_item = gtk.ToolItem() + tool_item.set_expand(expand) + + tool_item.add(widget) + widget.show() + + self.insert(tool_item, -1) + tool_item.show() + +class EditToolbar(gtk.Toolbar): + """Provides the standard edit toolbar for Activities. + + Members: + undo -- the undo button + redo -- the redo button + copy -- the copy button + paste -- the paste button + separator -- A separator between undo/redo and copy/paste + + This class only provides the 'edit' buttons in a standard layout, + your activity will need to either hide buttons which make no sense for your + Activity, or you need to connect the button events to your own callbacks: + + ## Example from Read.activity: + # Create the edit toolbar: + self._edit_toolbar = EditToolbar(self._view) + # Hide undo and redo, they're not needed + self._edit_toolbar.undo.props.visible = False + self._edit_toolbar.redo.props.visible = False + # Hide the separator too: + self._edit_toolbar.separator.props.visible = False + + # As long as nothing is selected, copy needs to be insensitive: + self._edit_toolbar.copy.set_sensitive(False) + # When the user clicks the button, call _edit_toolbar_copy_cb() + self._edit_toolbar.copy.connect('clicked', self._edit_toolbar_copy_cb) + + # Add the edit toolbar: + toolbox.add_toolbar(_('Edit'), self._edit_toolbar) + # And make it visible: + self._edit_toolbar.show() + """ + def __init__(self): + gtk.Toolbar.__init__(self) + + self.undo = ToolButton('edit-undo') + self.undo.set_tooltip(_('Undo')) + self.insert(self.undo, -1) + self.undo.show() + + self.redo = ToolButton('edit-redo') + self.redo.set_tooltip(_('Redo')) + self.insert(self.redo, -1) + self.redo.show() + + self.separator = gtk.SeparatorToolItem() + self.separator.set_draw(True) + self.insert(self.separator, -1) + self.separator.show() + + self.copy = ToolButton('edit-copy') + self.copy.set_tooltip(_('Copy')) + self.insert(self.copy, -1) + self.copy.show() + + self.paste = ToolButton('edit-paste') + self.paste.set_tooltip(_('Paste')) + self.insert(self.paste, -1) + self.paste.show() + +class ActivityToolbox(Toolbox): + """Creates the Toolbox for the Activity + + By default, the toolbox contains only the ActivityToolbar. After creating + the toolbox, you can add your activity specific toolbars, for example the + EditToolbar. + + To add the ActivityToolbox to your Activity in MyActivity.__init__() do: + + # Create the Toolbar with the ActivityToolbar: + toolbox = activity.ActivityToolbox(self) + ... your code, inserting all other toolbars you need, like EditToolbar + + # Add the toolbox to the activity frame: + self.set_toolbox(toolbox) + # And make it visible: + toolbox.show() + """ + def __init__(self, activity): + Toolbox.__init__(self) + + self._activity_toolbar = ActivityToolbar(activity) + self.add_toolbar(_('Activity'), self._activity_toolbar) + self._activity_toolbar.show() + + def get_activity_toolbar(self): + return self._activity_toolbar