#!/usr/bin/env python # -*- coding: utf-8 -*- # activity.py by: # Agustin Zubiaga # Daniel Francis # 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 import json from gettext import gettext as _ import gtk from sugar import profile from sugar import mime from sugar.activity import activity from sugar.activity.widgets import StopButton from sugar.activity.widgets import ActivityToolbarButton from sugar.activity.widgets import ToolbarButton from sugar.graphics.icon import Icon from sugar.graphics.colorbutton import ColorToolButton from sugar.graphics.toolbarbox import ToolbarBox from sugar.graphics.toolbutton import ToolButton from sugar.graphics.objectchooser import ObjectChooser from sugar.graphics.alert import NotifyAlert from helpbutton import HelpButton from dialogs import ExampleDialog def rgb2html(color): """Returns a html string from a Gdk color""" red = "%x" % int(color.red / 65535.0 * 255) if len(red) == 1: red = "0%s" % red green = "%x" % int(color.green / 65535.0 * 255) if len(green) == 1: green = "0%s" % green blue = "%x" % int(color.blue / 65535.0 * 255) if len(blue) == 1: blue = "0%s" % blue new_color = "#%s%s%s" % (red, green, blue) return new_color class IllustrateActivity(activity.Activity): def __init__(self, handle): activity.Activity.__init__(self, handle, True) toolbarbox = ToolbarBox() activity_button = ActivityToolbarButton(self) open_from_journal_button = ToolButton("open-from-journal") open_from_journal_button.connect("clicked", self.load_objectchooser) open_from_journal_button.props.tooltip = \ _("Load text from Sugar Journal") open_from_journal_button.show() activity_button.page.insert(open_from_journal_button, -1) toolbarbox.toolbar.insert(activity_button, 0) separator = gtk.SeparatorToolItem() separator.set_expand(True) separator.set_draw(False) toolbarbox.toolbar.insert(separator, -1) dialog_btn = ToolButton("dialog-icon") dialog_btn.set_tooltip(_("Show an example of a dialog")) dialog_btn.connect("clicked", self._show_example_dialog) toolbarbox.toolbar.insert(dialog_btn, -1) options_button = ToolbarButton(icon_name='preferences-system') options_toolbar = gtk.Toolbar() toolitem = gtk.ToolItem() toolitem.add(gtk.Label(_("Color Button example: "))) options_toolbar.insert(toolitem, -1) profile_color = profile.get_color() self.fill_color_btn = ColorToolButton() self.fill_color_btn.set_color(gtk.gdk.Color( profile_color.get_fill_color())) self.fill_color_btn.set_title(_("Fill Color")) self.fill_color_btn.connect('notify::color', self._set_fill_color) options_toolbar.insert(self.fill_color_btn, -1) self.stroke_color_btn = ColorToolButton() self.stroke_color_btn.set_color(gtk.gdk.Color( profile_color.get_stroke_color())) self.stroke_color_btn.set_title(_("Stroke Color")) self.stroke_color_btn.connect('notify::color', self._set_stroke_color) options_toolbar.insert(self.stroke_color_btn, -1) options_button.props.page = options_toolbar options_toolbar.show_all() toolbarbox.toolbar.insert(options_button, -1) separator = gtk.SeparatorToolItem() separator.set_expand(True) separator.set_draw(False) toolbarbox.toolbar.insert(separator, -1) self._helpbutton(toolbarbox) stopbtn = StopButton(self) toolbarbox.toolbar.insert(stopbtn, -1) self.set_toolbar_box(toolbarbox) canvas = gtk.EventBox() canvas.modify_bg(gtk.STATE_NORMAL, gtk.gdk.color_parse("white")) self.set_canvas(canvas) self.show_all() self._create_canvas(canvas) def load_objectchooser(self, widget): chooser = ObjectChooser(parent=self, what_filter=mime.GENERIC_TYPE_TEXT) result = chooser.run() if result == gtk.RESPONSE_ACCEPT: jobject = chooser.get_selected_object() _file = open(str(jobject.get_file_path()), "r") text = _file.read() _file.close() alert = NotifyAlert(10) alert.props.title = _('Text loaded') alert.props.msg = text alert.connect('response', lambda w, i: self.remove_alert(w)) self.add_alert(alert) else: return def _show_example_dialog(self, widget): ExampleDialog() def _set_fill_color(self, widget, pspec): self._icon.props.fill_color = rgb2html(widget.get_color()) def _set_stroke_color(self, widget, pspec): self._icon.props.stroke_color = rgb2html(widget.get_color()) def _create_canvas(self, canvas): x, y, w, h = canvas.get_allocation() pixel_size = w - h - 50 self._icon = Icon(pixel_size=pixel_size) self._icon.props.icon_name = 'sugar-integration' self._icon.props.pixel_size = pixel_size canvas.add(self._icon) self._icon.show() self._set_fill_color(self.fill_color_btn, None) self._set_stroke_color(self.stroke_color_btn, None) def _helpbutton(self, toolbarbox): helpbutton = HelpButton() helpbutton.add_section(_("A section")) helpbutton.add_paragraph(_("A paragraph with an icon"), "help-icon") helpbutton.add_paragraph( _("You can add as many paragraphs you'd like.")) helpbutton.add_section( _("Why should your activity be well integrated?")) helpbutton.add_paragraph(_("There are many Sugar activities;\ if they all work in a similar way,\ the children and teachers who use Sugar will be more comfortable.")) helpbutton.add_section(_("About")) helpbutton.add_paragraph( _("This Sugar Activity integrates examples.")) helpbutton.add_paragraph(_("Thanks for using our activity!")) toolbarbox.toolbar.insert(helpbutton, -1) def write_file(self, file_path): jfile = open(file_path, "w") colors = {"fill": self._icon.props.fill_color, "stroke": self._icon.props.stroke_color} try: json.dump(colors, jfile) finally: jfile.close() def read_file(self, file_path): jfile = open(file_path, "r") try: colors = json.load(jfile) finally: jfile.close() self.fill_color_btn.set_color(gtk.gdk.Color(colors["fill"])) self.stroke_color_btn.set_color(gtk.gdk.Color(colors["stroke"]))