Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/activity.py
blob: 5e5afaa27815f447d55d330fd0af34e0b5b11568 (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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
#!/usr/bin/env python
# -*- coding: UTF-8 -*-

# activity.py by:
#    Agustin Zubiaga <aguzubiaga97@gmail.com>
#    Gonzalo Odiard <godiard@gmail.com>

# 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 gtk
import gobject

from sugar.activity import activity
from sugar.activity.widgets import ActivityToolbarButton
from sugar.activity.widgets import StopButton
from sugar.graphics.toolbarbox import ToolbarBox
from sugar.graphics.toolbutton import ToolButton

from charts import BarChart


class SimpleGraph(activity.Activity):

    def __init__(self, handle):

        activity.Activity.__init__(self, handle, True)

        self.max_participiants = 1

        # CHART_OPTIONS

        self.x_label = None
        self.y_label = None
        self.chart_title = None
        self.chart_color = None
        self.chart_line_color = None
        self.current_chart = None
        self.chart_data = []

        # TOOLBARS
        self.toolbarbox = ToolbarBox()

        self.activity_button = ActivityToolbarButton(self)
        self.toolbarbox.toolbar.insert(self.activity_button, 0)

        self.add_v = ToolButton("gtk-add")
        self.add_v.connect("clicked", self.add_value)
        self.add_v.set_tooltip("Add a value")

        self.toolbarbox.toolbar.insert(self.add_v, -1)

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

        self.add_vbar_chart = ToolButton("vbar")
        self.add_vbar_chart.connect("clicked", self.add_chart_cb, "vbar")
        self.add_vbar_chart.set_tooltip("Create a Vertical bar chart")
        self.toolbarbox.toolbar.insert(self.add_vbar_chart, -1)

        self.add_hbar_chart = ToolButton("hbar")
        self.add_hbar_chart.connect("clicked", self.add_chart_cb, "hbar")
        self.add_hbar_chart.set_tooltip("Create a horizontal bar chart")
        self.toolbarbox.toolbar.insert(self.add_hbar_chart, -1)

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

        self.stopbtn = StopButton(self)
        self.toolbarbox.toolbar.insert(self.stopbtn, -1)

        self.set_toolbar_box(self.toolbarbox)

        # CANVAS
        self.paned = gtk.HPaned()
        self.box = gtk.VBox()

        self.labels_and_values = TreeView()

        self.labels_and_values.connect("label-changed", self.label_changed)
        self.labels_and_values.connect("value-changed", self.value_changed)

        self.box.pack_start(self.labels_and_values, True, True, 0)

        self.options = Options()

        self.options.connect("title-changed", self.set_chart_title)
        self.options.connect("hlabel-changed", self.set_h_label)
        self.options.connect("vlabel-changed", self.set_v_label)
        self.options.connect("chart-color-changed", self.set_chart_color)
        self.options.connect("line-color-changed", self.set_chart_line_color)

        self.box.pack_end(self.options, False, True, 10)

        self.paned.add1(self.box)

        # CHARTS AREA
        self.charts_area = gtk.Image()
        self.paned.add2(self.charts_area)

        self.set_canvas(self.paned)

        self.show_all()

    def add_value(self, widget):
        self.labels_and_values.add_value("Unknown", "0")
        self.chart_data.append(("Unknown", "0"))

    def add_chart_cb(self, widget, type="vbar"):
        if type == "vbar":
            self.current_chart = BarChart(type="vertical")

        elif type == "hbar":
            self.current_chart = BarChart(type="horizontal")

        print self.chart_data

        self.current_chart.data_set(self.chart_data)
        self.current_chart.set_title(self.chart_title)
        self.current_chart.set_x_label(self.x_label)
        self.current_chart.set_y_label(self.y_label)
        self.current_chart.set_chart_color(self.chart_color)
        self.current_chart.set_line_color(self.chart_line_color)
        self.current_chart.connect("ready", lambda w, f:
                                              self.charts_area.set_from_file(f))
        self.current_chart.render()

    def label_changed(self, tw, path, new_label):
        path = int(path)
        self.chart_data[path] = (new_label, self.chart_data[path][1])

    def value_changed(self, tw, path, new_value):
        path = int(path)
        self.chart_data[path] = (self.chart_data[path][0], int(new_value))

    def set_h_label(self, options, label):
        self.x_label = label

    def set_v_label(self, options, label):
        self.y_label = label

    def set_chart_title(self, options, title):
        self.chart_title = title

    def set_chart_color(self, options, color):
        self.chart_color = color

    def set_chart_line_color(self, options, color):
        self.chart_line_color = color


class TreeView(gtk.TreeView):

    __gsignals__ = {
             'label-changed': (gobject.SIGNAL_RUN_FIRST, None, [str, str], ),
             'value-changed': (gobject.SIGNAL_RUN_FIRST, None, [str, str], ), }

    def __init__(self):

        gtk.TreeView.__init__(self)

        self.model = gtk.ListStore(str, str)
        self.set_model(self.model)

        # Label column

        column = gtk.TreeViewColumn("Label")
        label = gtk.CellRendererText()
        label.set_property('editable', True)
        label.connect("edited", self.label_changed, self.model)

        column.pack_start(label)
        column.set_attributes(label, text=0)
        self.append_column(column)

        # Value column

        column = gtk.TreeViewColumn("Value")
        value = gtk.CellRendererText()
        value.set_property('editable', True)
        value.connect("edited", self.value_changed, self.model)

        column.pack_start(value)
        column.set_attributes(value, text=1)

        self.append_column(column)

        self.show_all()

    def add_value(self, label, value):
        self.model.append([label, value])
        print "Added: %s, Value: %s" % (label, value)

    def label_changed(self, cell, path, new_text, model):
        print "Change '%s' to '%s'" % (model[path][0], new_text)
        model[path][0] = new_text

        self.emit("label-changed", str(path), new_text)

    def value_changed(self, cell, path, new_text, model):
        print "Change '%s' to '%s'" % (model[path][1], new_text)
        is_number = True
        try:
            float(new_text)
        except:
            is_number = False

        if is_number:
            model[path][1] = new_text

            self.emit("value-changed", str(path), new_text)

        elif not is_number:
            error = gtk.MessageDialog(None,
                              gtk.DIALOG_MODAL,
                              gtk.MESSAGE_ERROR,
                              gtk.BUTTONS_OK, \
                              'The value must be a number (integer or float)')

            response = error.run()
            if response == gtk.RESPONSE_OK:
                error.destroy()


class Options(gtk.VBox):

    __gsignals__ = {
             'title-changed': (gobject.SIGNAL_RUN_FIRST, None, [str]),
             'hlabel-changed': (gobject.SIGNAL_RUN_FIRST, None, [str]),
             'vlabel-changed': (gobject.SIGNAL_RUN_FIRST, None, [str]),
             'chart-color-changed': (gobject.SIGNAL_RUN_FIRST, None, [object]),
             'line-color-changed': (gobject.SIGNAL_RUN_FIRST, None, [object])}

    def __init__(self):
        gtk.VBox.__init__(self)

        hbox = gtk.HBox()
        title = gtk.Label("Title:")
        hbox.pack_start(title, False, True, 0)

        entry = gtk.Entry(max=0)
        entry.connect("changed", lambda w: self.emit("title-changed",
                                                                  w.get_text()))
        hbox.pack_end(entry, False, True, 5)

        self.pack_start(hbox, False, True, 3)

        hbox = gtk.HBox()
        title = gtk.Label("Horizontal label:")
        hbox.pack_start(title, False, True, 0)

        entry = gtk.Entry(max=0)
        entry.connect("changed", lambda w: self.emit("hlabel-changed",
                                                                  w.get_text()))
        hbox.pack_end(entry, False, True, 5)

        self.pack_start(hbox, False, True, 3)

        hbox = gtk.HBox()
        title = gtk.Label("Vertical label:")
        hbox.pack_start(title, False, True, 0)

        entry = gtk.Entry(max=0)
        entry.connect("changed", lambda w: self.emit("vlabel-changed",
                                                                  w.get_text()))
        hbox.pack_end(entry, False, True, 5)

        self.pack_start(hbox, False, True, 3)

        hbox = gtk.HBox()
        title = gtk.Label("Background Color:")
        hbox.pack_start(title, False, True, 0)

        btn = gtk.Button()
        btn.id = 0
        label = gtk.Label("Color")
        label.modify_fg(gtk.STATE_NORMAL, gtk.gdk.Color("#000000"))
        btn.add(label)
        btn.modify_bg(gtk.STATE_NORMAL, gtk.gdk.Color("#F3F9FB"))
        btn.modify_bg(gtk.STATE_PRELIGHT, gtk.gdk.Color("#F3F9FB"))
        btn.connect("clicked", self.color_selector)
        hbox.pack_end(btn, False, True, 5)

        self.pack_start(hbox, False, True, 3)

        hbox = gtk.HBox()
        title = gtk.Label("Lines Color:")
        hbox.pack_start(title, False, True, 0)

        btn = gtk.Button()
        btn.id = 1
        label = gtk.Label("Color")
        label.modify_fg(gtk.STATE_NORMAL, gtk.gdk.Color("#000000"))
        btn.add(label)
        btn.connect("clicked", self.color_selector)
        btn.modify_bg(gtk.STATE_NORMAL, gtk.gdk.Color("#D1E5EC"))
        btn.modify_bg(gtk.STATE_PRELIGHT, gtk.gdk.Color("#D1E5EC"))
        hbox.pack_end(btn, False, True, 5)

        self.pack_start(hbox, False, True, 3)

        self.show_all()

    def color_selector(self, widget):
        selector = gtk.ColorSelectionDialog("Color Selector")
        selector.get_color_selection().connect("color-changed",
                                                     self.color_changed, widget)
        selector.ok_button.connect("clicked", lambda w: selector.destroy())
        selector.cancel_button.destroy()
        selector.help_button.destroy()
        selector.show_all()

    def color_changed(self, widget, btn):
        color = widget.get_current_color()
        btn.modify_bg(gtk.STATE_NORMAL, color)
        btn.modify_bg(gtk.STATE_PRELIGHT, 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)

        if not btn.id:
            self.emit("chart-color-changed", new_color)

        elif btn.id:
            self.emit("line-color-changed", new_color)