Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/simpleactivity.py
blob: 46e11813a050a2bd88a4bf5a000f7d70eb6dce78 (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
#!/usr/bin/env python
# -*- coding: utf-8 -*-

# Copyright (C) 2013 Agustin Zubiaga <aguz@sugarlabs.org>.
#
# 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()