Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/illustrate.py
blob: ceee39e723321441dbd47f1524f97713a27657c0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
#!/usr/bin/env python
# -*- coding: utf-8 -*-

# activity.py by:
#    Agustin Zubiaga <aguz@sugarlabs.org>
#    Daniel Francis <francis@sugarlabs.org>

# 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 Alert, 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)

        # Toolbar(s)
        toolbarbox = ToolbarBox()

        # The Activity Button:
        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()

        # Insert a button in the activity button toolbar
        activity_button.page.insert(open_from_journal_button, -1)

        # Insert the Activity Toolbar Button in the toolbarbox
        toolbarbox.toolbar.insert(activity_button, 0)

        separator = gtk.SeparatorToolItem()
        separator.set_expand(False)
        separator.set_draw(True)
        toolbarbox.toolbar.insert(separator, -1)

        dialog_btn = ToolButton("dialog-icon")
        dialog_btn.set_tooltip(_("Dialog example"))
        dialog_btn.connect("clicked", self._show_example_dialog)
        toolbarbox.toolbar.insert(dialog_btn, -1)

        alert_btn = ToolButton("alert-icon")
        alert_btn.set_tooltip(_("Alert example"))
        alert_btn.connect("clicked", self._show_alert_btn_pallete)
        toolbarbox.toolbar.insert(alert_btn, -1)

        # Preferences toolbar
        options_button = ToolbarButton(icon_name='preferences-system')
        options_toolbar = gtk.Toolbar()

        # Add a gtk.Label in a toolbar using gtk.ToolItem
        toolitem = gtk.ToolItem()
        toolitem.add(gtk.Label(_("Color Button example:  ")))
        options_toolbar.insert(toolitem, -1)

        # Get the profile color
        profile_color = profile.get_color()

        # Color Button
        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)

        # For a white background:
        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)
        self._create_alert_btn_palette(alert_btn)

    def _show_alert_btn_pallete(self, widget):
        widget.props.palette.popup(immediate=True, state=1)

    def _create_alert_btn_palette(self, button):
        palette = button.get_palette()
        hbox = gtk.HBox()

        simple = ToolButton("alert-icon")
        simple.connect("clicked", self._show_example_alert, False)

        notify = ToolButton("notify-alert-icon")
        notify.connect("clicked", self._show_example_alert, True)

        hbox.pack_start(simple, False, True, 0)
        hbox.pack_end(notify, False, True, 0)

        hbox.show_all()

        palette.set_content(hbox)

    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 _show_example_alert(self, widget, notify=False):
        if notify:
            alert = NotifyAlert(10)
            alert.props.title = _('Notify Alert')

        elif not notify:
            alert = Alert()
            ok_icon = Icon(icon_name='dialog-ok')
            alert.add_button(gtk.RESPONSE_OK, _('Ok'), ok_icon)
            ok_icon.show()
            alert.props.title = _('Simple Alert')

        alert.props.msg = _('Message')

        alert.connect('response', lambda a, r: self.remove_alert(a))

        self.add_alert(alert)

        alert.show()

    def _set_fill_color(self, widget, pspec):
        # Set fill color
        self._icon.props.fill_color = rgb2html(widget.get_color())

    def _set_stroke_color(self, widget, pspec):
        # Set stroke color
        self._icon.props.stroke_color = rgb2html(widget.get_color())

    def _create_canvas(self, canvas):
        # Get canvas allocation
        x, y, w, h = canvas.get_allocation()

        # Resize for all the screen sizes
        pixel_size = w - h - 50

        # A sugar icon for set stroke and fill color
        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"]))