Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/activity.py
blob: 1c51fdbd13b4280e844ddb6b03473401675584de (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
# -*- coding: utf-8 -*-
#
# 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 logging
logger = logging.getLogger('hnd')

import os
import subprocess

from gi.repository import Gtk
from gi.repository import GdkPixbuf
from gi.repository import WebKit
from sugar3.activity import activity
from sugar3.activity.widgets import StopButton
from sugar3.activity.widgets import ActivityToolbarButton
from sugar3.graphics.toolbarbox import ToolbarBox
from sugar3.graphics.toolbutton import ToolButton
from sugar3.graphics.combobox import ComboBox
from sugar3.graphics import style

RUNSERVER = os.path.join(os.environ['SUGAR_BUNDLE_PATH'], 'runserver.sh')
SERVERDIR = os.path.join(os.environ['SUGAR_BUNDLE_PATH'], 'hnd_webapp')
IMAGESDIR = os.path.join(SERVERDIR, 'images')
subprocess.Popen([RUNSERVER, SERVERDIR])

ACTIVITIES_URL = 'http://0.0.0.0:8000/lesson.html?name='

grades = [('Tercer grado',
           [('unidad2', 'Perpendicularidad'),
            ('unidad3', 'Adición'),
            ('unidad4', 'Sustracción'),
            ('unidad11', 'Longitud'),
            ('unidad12', 'Operaciones combinadas'),
            ('unidad13', 'Peso'),
            ('unidad14', 'Simetría'),
            ('unidad17', 'Moneda'),
            ('tiempo', 'Tiempo')]),
          ('Cuarto grado',
           [('cuadrilateros', 'Cuadriláteros'),
            ('M010407decimales_4g', 'Decimales'),
            ('M020407longitud_4grado', 'Longitud'),
            ('M080407elementos', 'Sólidos geométricos'),
            ('4_moneda', 'Moneda'),
            ('Hora_y_tiempo', 'Hora y tiempo'),
            ('4_peso', 'Peso'),
            ('M010408capacidad', 'La ruleta del saber')]),
          ('Quinto Grado',
           [('5u1', 'Potencias'),
            ('5u2', 'Ángulos'),
            ('5u3', 'Múltiplos'),
            ('5u4', 'Superficies'),
            ('U5', 'Fracciones'),
            ('5u6', 'Gráficas lineares'),
            ('5U7', 'Decimales'),
            ('5U9', 'Áreas'),
            ('5u10', 'Círculos y circunferencias')]),
          ('Sexto grado',
           [('U6', 'Sólidos geométricos'),
            ('U7', 'Multiplicación y división de fracciones'),
            ('M010607_volumen6', 'Volumen'),
            ('mayas', 'Sistema de numeración maya'),
            ('U10', 'Calendario maya')]),
          ('Otras',
           [('sintaxis', 'Sintáxis'),
            ('tom_sawlyer', 'Tom Sawlyer')])
          ]


class UnitBrowser(activity.Activity):

    buffers = {}

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

        self.max_participants = 1
        self._wait_for_complete = False

        # Toolbar
        toolbarbox = ToolbarBox()

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

        separator = Gtk.SeparatorToolItem()
        toolbarbox.toolbar.insert(separator, -1)

        self.combobox = ComboBox()
        self.combobox.connect('changed', self._combo_changed)
        self.combobox.show()
        item = Gtk.ToolItem()
        item.add(self.combobox)
        item.show()
        toolbarbox.toolbar.insert(item, -1)

        separator = Gtk.SeparatorToolItem()
        separator.props.draw = False
        toolbarbox.toolbar.insert(separator, -1)

        self._show_activities_btn = ToolButton()
        self._show_activities_btn.set_tooltip('Ver lista de Unidades')
        self._show_activities_btn.props.icon_name = 'activities-list'
        self._show_activities_btn.connect('clicked',
                                          self._show_activities_list)
        toolbarbox.toolbar.insert(self._show_activities_btn, -1)

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

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

        self.set_toolbar_box(toolbarbox)

        # Canvas
        self._notebook = Gtk.Notebook()
        self._notebook.set_show_tabs(False)

        self._iconview = Gtk.IconView()
        self.load_buffers()

        self.combobox.set_active(0)

        scrollwnd = Gtk.ScrolledWindow()
        scrollwnd.set_policy(Gtk.PolicyType.AUTOMATIC, Gtk.PolicyType.AUTOMATIC)

        self._iconview.set_model(self.buffers['Tercer grado'])
        self._iconview.set_markup_column(0)
        self._iconview.set_pixbuf_column(1)
        self._iconview.connect('selection-changed', self._select_fn)
        scrollwnd.add(self._iconview)
        self._notebook.append_page(scrollwnd, Gtk.Label())

        self._browser = WebKit.WebView()
        self._browser.connect('load-finished', self._load_finished_cb)
        self._browser.set_full_content_zoom(True)
        self._browser.set_zoom_level(
            self._browser.get_zoom_level() + 0.3)
        self._notebook.append_page(self._browser, Gtk.Label())

        self.set_canvas(self._notebook)
 
        self.show_all()
        self._show_activities_list()

    def _show_activities_list(self, *args):
        self._notebook.set_current_page(0)
        self._show_activities_btn.set_sensitive(False)
        self.combobox.set_sensitive(True)

    def _combo_changed(self, widget):
        active = widget.get_active_item()
        logger.warning(active[0])
        self._iconview.set_model(self.buffers[active[0]])

    def read_file(self, file_path):
        return

    def write_file(self, file_path):
        return

    def load_buffers(self):
        for i in grades:
            grade_buffer = Gtk.ListStore(str, GdkPixbuf.Pixbuf, str)
            for lesson in i[1]:
                imagepath = os.path.join(IMAGESDIR, lesson[0], 'portada.gif')
                if os.path.exists(imagepath):
                    image = GdkPixbuf.Pixbuf.new_from_file_at_size(
                                                  imagepath,
                                                  style.XLARGE_ICON_SIZE * 1.5,
                                                  style.XLARGE_ICON_SIZE * 1.5)
                else:
                    # We don't found the image
                    image = GdkPixbuf.Pixbuf.new_from_file_at_size(
                                                  'icons/activities-list.svg',
                                                  style.XLARGE_ICON_SIZE * 1.5,
                                                  style.XLARGE_ICON_SIZE * 1.5)
                name = lesson[1]
                grade_buffer.append(['<b>%s</b>' % name, image, lesson[0]])
            logger.debug(i[0])
            self.buffers[i[0]] = grade_buffer
            self.combobox.append_item(i[0], i[0])

    def _select_fn(self, widget):
        self.combobox.set_sensitive(False)
        self._show_activities_btn.set_sensitive(True)
        iter_ = widget.get_model().get_iter(widget.get_selected_items()[0])
        unit = widget.get_model().get(iter_, 2)[0]
        self._browser.open(ACTIVITIES_URL + unit)
        self._wait_for_complete = True

    def _load_finished_cb(self, widget, frame):
        if widget.props.uri.endswith('index.html'):
            self._show_activities_list()
        
        else:
            if self._wait_for_complete:
                self._notebook.set_current_page(1)
                self._wait_for_complete = False