#!/usr/bin/env python # -*- coding: utf-8 -*- # Copyright (C) 2013 Agustin Zubiaga . # # 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 3 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. from gi.repository import Gtk from gi.repository import GObject from sugar3.activity import activity from sugar3.activity.widgets import StopButton from sugar3.graphics.toolbarbox import ToolbarBox from sugar3.activity.widgets import ToolbarButton from sugar3.graphics.toolbutton import ToolButton import time import os try: import json except ImportError: import simplejson as json class SimpleActivity(activity.Activity): """SimpleActivity: This class is extension of sugar3.activity.activity.Activiy This will create the Toolbar automatically with the ActivityToolbarButton or will put all the Activity Buttons in the toolbarbox if you set activitytoolbarbutton (__init__ parameter) to False. And you can add buttons to the main in a simple way just using the add_toolbutton method. Every "add-method" support the index parameter: self.add_toolbutton(..., index=2) And when you finish building you toolbar just call: self.add_stop_button() There is a reserved variable (self.data) where you can save something and when the activity gets closed, it is auto-saved to the jorunal. And then when the activity runs again, self.data will contain what was saved. You can connect to the "data-loaded" signal to know when the self.data loaded is ready to use (Have loaded data from file). There are also other very easy-to-use methods, see their docs to get more information about what you can do with this. """ __gtype_name__ = 'SugarSimpleActivity' __gsignals__ = { 'data-loaded': (GObject.SignalFlags.RUN_FIRST, None, [])} def __init__(self, handle, activitytoolbarbutton=True, sharing=False): activity.Activity.__init__(self, handle) if not sharing: self.max_participants = 1 self.data = {} self.activity_toolbar = None self.toolbarbox = ToolbarBox() if activitytoolbarbutton: from sugar3.activity.widgets import ActivityToolbarButton activity_button = ActivityToolbarButton(self) self.activity_toolbar = activity_button.props.page self.add_to_toolbar(activity_button) else: from sugar3.activity.widgets import ActivityButton from sugar3.activity.widgets import TitleEntry from sugar3.activity.widgets import ShareButton from sugar3.activity.widgets import DescriptionItem activity_button = ActivityButton(self) self.add_to_toolbar(activity_button) activity_button.show() title_entry = TitleEntry(self) self.add_to_toolbar(title_entry) title_entry.show() description_item = DescriptionItem(self) self.add_to_toolbar(description_item) description_item.show() share_button = ShareButton(self) self.add_to_toolbar(share_button) share_button.show() self.toolbarbox.show() self.set_toolbar_box(self.toolbarbox) def add_toolbar(self, item, icon, index=-1): """Add a ToolbarButton to the toolbarbox, and return the page (Gtk.Toolbar)""" toolbar_btn = ToolbarButton(icon_name=icon) toolbar_btn.props.page = Gtk.Toolbar() self.toolbarbox.toolbar.insert(toolbar_btn, index) return toolbar_btn.props.page def add_toolbutton(self, icon=None, cb=None, tooltip=None, index=-1, toolbar=None): """ Creates a ToolButton and add it to the main toolbar or another. To add a ToolButton just do this: self.add_toolbutton(icon, callback, tooltip, index, toolbar) icon: The name of the icon you want to use callback: The method you want to call when the button is clicked tooltip: The tooltip you want to show index: The place of the toolbar you want to take toolbar: The toolbar where you want to add this button If you do not give this parameter, it will use the main toolbar (self.toolbarbox) Do you want to do more with your button? Don't worry you can get it doing this: mybutton = self.add_toolbutton(... """ toolbutton = ToolButton(icon) if icon is not None else ToolButton() if cb is not None: toolbutton.connect('clicked', cb) if tooltip is not None: toolbutton.set_tooltip(tooltip) self.add_to_toolbar(toolbutton, index=index, toolbar=toolbar) return toolbutton def add_to_toolbar(self, item, index=-1, toolbar=None): """Add a ToolItem to the main toolbar (toolbarbox) or another If you give another Gtk.Widget (not an instance of Gtk.ToolItem) it will be added automatically into a one. Example: entry = Gtk.Entry() self.add_to_toolbar(entry) Want to add this to another toolbar just set the toolbar parameter: self.add_to_toolbar(button, toolbar=mytoolbar) """ toolbar = toolbar or self.toolbarbox.toolbar if not isinstance(item, Gtk.ToolItem): widget = item item = Gtk.ToolItem() item.add(widget) toolbar.insert(item, index) item.show() def add_to_activitytoolbar(self, item, index=-1): """Add an item to activity_toolbar""" if self.activity_toolbar: self.add_to_toolbar(item, index=index, toolbar=self.activity_toolbar) else: raise Exception("There isn't an activity toolbar") def add_separator(self, draw=True, expand=False, index=-1, toolbar=None): """Add separator to the toolbarbox or another self.add_separator(draw, expand) It also supports index and toolbar parameters. """ separator = Gtk.SeparatorToolItem() separator.set_draw(draw) separator.set_expand(expand) self.add_to_toolbar(separator, index=index, toolbar=toolbar) def add_stopbutton(self): """Appends the stop button to the end of the toolbarbox""" self.add_separator(False, True) stopbutton = StopButton(self) self.add_to_toolbar(stopbutton) def get_data_file(self, filename=None): """Returns a file in the data/ dir of the activity, where you can save whatever""" data_dir = os.path.join(activity.get_activity_root(), 'data') file_path = filename if filename else os.path.join(data_dir, str(time.time())) return file_path def save_data(self): """Override this function, in order to save whatever in self.data, this mean that sugar is call the write_file method""" return def write_file(self, file_name): self.save_data() if self.data is not None and self.data != {}: try: wfile = open(file_name, 'w') json.dump(self.data, wfile) finally: wfile.close() def read_file(self, file_name): try: rfile = open(file_name) self.data = json.load(rfile) self.emit('data-loaded') finally: rfile.close()