Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/activity.py
blob: 5909573b918616790d21d84cf05c53124819abd9 (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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
# Copyright 2009 Simon Schampijer
#
# 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 2 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


# gst-launch-0.10 playbin2 uri=file:///.....

import os
import math

from gi.repository import GObject
from gi.repository import Gtk
from gi.repository import Gdk
from gi.repository import GdkPixbuf

import pygst
pygst.require('0.10')
import gst
import gst.interfaces

import logging

from gettext import gettext as _

from sugar3.activity import activity
from sugar3.graphics.toolbarbox import ToolbarBox
from sugar3.graphics.toolbutton import ToolButton
from sugar3.activity.widgets import ActivityToolbarButton
from sugar3.activity.widgets import StopButton
from sugar3.graphics import style
from sugar3 import profile
from sugar3.graphics.icon import EventIcon

from roundbox import RoundBox
from data import DAT_DIC

MAP_PAGE = 0
VIDEO_PAGE = 1
INFO_PAGE = 2


class MyEcoTvActivity(activity.Activity):

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

        # we do not have collaboration features
        self.max_participants = 1

        toolbar_box = ToolbarBox()

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

        toolbar_box.toolbar.insert(Gtk.SeparatorToolItem(), -1)

        self._world_bt = ToolButton('world-map-au')
        self._world_bt.set_tooltip(_('Map'))
        self._world_bt.connect('clicked',
                               self.__show_world_map_cb)
        toolbar_box.toolbar.insert(self._world_bt, -1)

        self._play_bt = ToolButton('player_play')
        self._play_bt.set_tooltip(_('Video'))
        self._play_bt.connect('clicked',
                              self.__play_video_cb)
        toolbar_box.toolbar.insert(self._play_bt, -1)

        toolbar_box.toolbar.insert(Gtk.SeparatorToolItem(), -1)

        self._text_level = 'text_easy'

        self._text_easy_bt = ToolButton('easy')
        self._text_easy_bt.set_tooltip(_('Easy text'))
        self._text_easy_bt.connect('clicked',
                                   self.__show_info_cb, 'text_easy')
        toolbar_box.toolbar.insert(self._text_easy_bt, -1)

        self._text_medium_bt = ToolButton('medium')
        self._text_medium_bt.set_tooltip(_('Medium text'))
        self._text_medium_bt.connect('clicked',
                                     self.__show_info_cb, 'text_medium')
        toolbar_box.toolbar.insert(self._text_medium_bt, -1)

        self._text_hard_bt = ToolButton('hard')
        self._text_hard_bt.set_tooltip(_('Hard text'))
        self._text_hard_bt.connect('clicked',
                                   self.__show_info_cb, 'text_hard')
        toolbar_box.toolbar.insert(self._text_hard_bt, -1)

        separator = Gtk.SeparatorToolItem()
        separator.props.draw = False
        separator.set_expand(True)
        toolbar_box.toolbar.insert(separator, -1)

        stop_button = StopButton(self)
        toolbar_box.toolbar.insert(stop_button, -1)

        self.set_toolbar_box(toolbar_box)
        toolbar_box.show_all()

        self._notebook = Gtk.Notebook()
        self._notebook.set_show_tabs(False)
        worldmap = WorldMap()
        worldmap.connect('icon-clicked', self.__icon_clicked_cb)

        self._notebook.append_page(worldmap, None)

        self._videoplayer = VideoPlayer()
        self._notebook.append_page(self._videoplayer, None)
        self._notebook.show_all()
        self.set_canvas(self._notebook)
        self._icon_selected = None
        self.set_current_page(MAP_PAGE)

    def get_data_icon(self, icon_name):
        for data in DAT_DIC:
            if icon_name == data['name']:
                return data
        return None

    def __icon_clicked_cb(self, worldmap, icon_name):
        logging.error('Icon %s clicked', icon_name)
        self._icon_selected = icon_name
        data = self.get_data_icon(icon_name)
        logging.error(data)
        if data is not None and data['video'] is not None:
            self.show_video(data['video'])

    def show_video(self, video):
        # the information box resizes the notebook
        # need remove it to have the video desplayed in
        # the correct pvertical position
        if self._notebook.get_n_pages() > 2:
            self._notebook.remove_page(-1)

        self.set_current_page(VIDEO_PAGE)
        video_path = os.path.join(activity.get_bundle_path(), 'video',
                                  video)
        self._videoplayer.play(video_path)

    def __show_world_map_cb(self, button):
        self._icon_selected = None
        self.set_current_page(MAP_PAGE)

    def __play_video_cb(self, button):
        data = self.get_data_icon(self._icon_selected)
        if data is not None and data['video'] is not None:
            self.show_video(data['video'])

    def __show_info_cb(self, button, text_level):
        self._text_level = text_level
        if self._icon_selected is None:
            return
        data = self.get_data_icon(self._icon_selected)
        if data is not None:

            if self._notebook.get_n_pages() > 2:
                self._notebook.remove_page(-1)

            info_box = InformationBox(data['icon_name'], _('Did you know?'),
                                      data[text_level])
            self._notebook.append_page(info_box, None)
            self.set_current_page(INFO_PAGE)

    def write_file(self, file_name):
        pass

    def read_file(self, file_name):
        pass

    def set_current_page(self, page):
        self._notebook.set_current_page(page)
        self._world_bt.set_sensitive(page != MAP_PAGE)
        self._text_easy_bt.set_sensitive(page in (VIDEO_PAGE, INFO_PAGE))
        self._text_medium_bt.set_sensitive(page in (VIDEO_PAGE, INFO_PAGE))
        self._text_hard_bt.set_sensitive(page in (VIDEO_PAGE, INFO_PAGE))
        self._play_bt.set_sensitive(page == INFO_PAGE)
        if page != VIDEO_PAGE:
            self._videoplayer.stop()


class VideoPlayer(Gtk.EventBox):
    def __init__(self):
        super(VideoPlayer, self).__init__()

        self._width = Gdk.Screen.width()
        self._height = Gdk.Screen.height() - style.GRID_CELL_SIZE
        self.set_size_request(self._width, self._height)

        self.set_double_buffered(False)
        self.set_app_paintable(True)

        self._sink = None
        self._xid = None
        self.connect('realize', self.__realize)
        self.playing = False
        self.paused = False

        self._vpipeline = gst.element_factory_make('playbin2', 'vplayer')

        bus = self._vpipeline.get_bus()
        bus.enable_sync_message_emission()
        bus.add_signal_watch()
        bus.connect('sync-message::element', self.__on_sync_message)
        bus.connect('message', self.__on_vmessage)

    def __on_sync_message(self, bus, message):
        if message.structure is None:
            return
        if message.structure.get_name() == 'prepare-xwindow-id':
            message.src.set_property('force-aspect-ratio', True)
            self._sink = message.src
            self._sink.set_xwindow_id(self._xid)

    def __on_vmessage(self, bus, message):
        t = message.type
        if t == gst.MESSAGE_EOS:
            self._vpipeline.seek_simple(
                gst.FORMAT_TIME, gst.SEEK_FLAG_FLUSH, 0)
            self.playing = False

    def __realize(self, widget):
        self._xid = self.get_window().get_xid()

    def do_expose_event(self):
        if self._sink:
            self._sink.expose()
            return False
        else:
            return True

    def play(self, filename):
        if filename:
            path = os.path.join(
                activity.get_bundle_path(), 'video', filename)
            self._vpipeline.set_property('uri', "file://" + path)

        self._vpipeline.set_state(gst.STATE_PLAYING)
        self.playing = True

    def stop(self):
        self._vpipeline.set_state(gst.STATE_NULL)
        self.playing = False

    def pause(self):
        self._vpipeline.set_state(gst.STATE_PAUSED)
        self.paused = True

    def unpause(self):
        self._vpipeline.set_state(gst.STATE_PLAYING)
        self.paused = False


class InformationBox(Gtk.EventBox):

    def __init__(self, icon_name, title, text):
        """
        icon_name: str
        title: str
        text: array of str
        """
        Gtk.EventBox.__init__(self)
        self._width = Gdk.Screen.width()
        self._height = Gdk.Screen.height() - style.GRID_CELL_SIZE
        self.set_size_request(self._width, self._height)
        self.modify_bg(Gtk.StateType.NORMAL,
                       style.COLOR_WHITE.get_gdk_color())
        self._icon_name = icon_name

        roundbox = RoundBox()
        roundbox.background_color = style.Color('#179820')
        roundbox.border_color = style.Color('#179820')
        roundbox.props.margin = style.GRID_CELL_SIZE

        vbox = Gtk.VBox()
        vbox.props.margin = style.GRID_CELL_SIZE * 2

        if style.zoom(100) == 100:
            # xo
            self._font_size = 12
        else:
            # desktop
            self._font_size = 25

        self._order = 0
        self._text = text

        title_label = Gtk.Label()
        title_label.set_markup(
            '<span foreground="white" font="Sans %d"><b>%s\n</b></span>' %
            (self._font_size, title))
        title_label.set_alignment(0.0, 0.0)
        vbox.pack_start(title_label, False, False, 10)
        self.text_label = Gtk.Label()
        self.text_label.set_markup(
            '<span foreground="white" font="Sans %d">%s</span>' %
            (self._font_size, text[self._order]))
        self.text_label.set_alignment(0.0, 0.5)
        self.text_label.set_line_wrap(True)
        vbox.pack_start(self.text_label, False, False, 10)
        roundbox.add(vbox)

        self._xo_color = profile.get_color()

        overlay = Gtk.Overlay()
        self.add(overlay)
        overlay.add(roundbox)

        if len(text) > 0:
            text_changer = TextChanger(self._xo_color, len(text))
            text_changer.connect('change-text', self.__change_text)
            text_changer.props.halign = Gtk.Align.CENTER
            text_changer.props.valign = Gtk.Align.START
            text_changer.props.margin_top = self._height - \
                (style.GRID_CELL_SIZE * 3.5)
            overlay.add_overlay(text_changer)

        icon = EventIcon(icon_name='globe', xo_color=self._xo_color)
        icon.props.halign = Gtk.Align.START
        icon.props.valign = Gtk.Align.START
        icon.props.margin_top = 0
        icon.props.margin_left = int(style.GRID_CELL_SIZE * 2.6)
        overlay.add_overlay(icon)
        icon.set_size(style.zoom(266))

        icon = EventIcon(icon_name=self._icon_name, xo_color=self._xo_color)
        icon.props.halign = Gtk.Align.START
        icon.props.valign = Gtk.Align.START
        icon.props.margin_top = style.GRID_CELL_SIZE / 2
        icon.props.margin_left = style.GRID_CELL_SIZE * 3
        overlay.add_overlay(icon)
        icon.set_size(style.zoom(200))

        self.show_all()

    def __change_text(self, widget, order):
        self._order = order
        self.text_label.set_markup(
            '<span foreground="white" font="Sans %d">%s</span>' %
            (self._font_size, self._text[self._order]))


class TextChanger(Gtk.HBox):

    __gsignals__ = {
        'change-text': (GObject.SignalFlags.RUN_FIRST, GObject.TYPE_NONE,
                        ([int])), }

    def __init__(self, xo_color, array_size):
        Gtk.HBox.__init__(self)
        self._xo_color = xo_color
        self._array_size = array_size
        self._order = 0

        prev_bt = EventIcon(icon_name='go-previous-paired-grey',
                            xo_color=self._xo_color)
        prev_bt.connect('button-press-event', self.__prev_clicked_cb)
        self.pack_start(prev_bt, False, False, 5)

        self.sequence_view = SequenceView(array_size)
        self.pack_start(self.sequence_view, False, False, 5)

        next_bt = EventIcon(icon_name='go-next-paired-grey',
                            xo_color=self._xo_color)
        next_bt.connect('button-press-event', self.__next_clicked_cb)
        self.pack_start(next_bt, False, False, 5)

    def __prev_clicked_cb(self, icon, event):
        if self._order == 0:
            self._order = self._array_size - 1
        else:
            self._order = self._order - 1
        self.sequence_view.set_value(self._order)
        self.emit('change-text', self._order)

    def __next_clicked_cb(self, icon, event):
        if self._order < self._array_size - 1:
            self._order = self._order + 1
        else:
            self._order = 0
        self.sequence_view.set_value(self._order)
        self.emit('change-text', self._order)


class SequenceView(Gtk.DrawingArea):

    def __init__(self, cant, point_size=10):
        Gtk.DrawingArea.__init__(self)
        self._cant = cant
        self._value = 0
        self._point_size = point_size
        logging.error('init SequenceView cant= %d', self._cant)
        self.set_size_request(self._point_size * self._cant * 2,
                              self._point_size)
        self.connect('draw', self.__draw_cb)

        def size_allocate_cb(widget, allocation):
            self._width, self._height = allocation.width, allocation.height
            self.disconnect(self._setup_handle)

        self._setup_handle = self.connect('size_allocate',
                                          size_allocate_cb)

    def set_value(self, value):
        self._value = value
        self.queue_draw()

    def __draw_cb(self, widget, ctx):
        if self._cant == 0:
            return

        ctx.save()
        radio = self._point_size / 2.0
        ctx.translate(0, 0)
        #ctx.rectangle(0, 0, self._width, self._height)
        #ctx.set_source_rgb(1.0, 1.0, 1.0)
        #ctx.fill()
        ctx.translate(radio, self._height / 2 - radio)
        for n in range(self._cant):
            if n == self._value:
                ctx.set_source_rgb(0.913, 0.733, 0.0)  # eebb00
            else:
                ctx.set_source_rgb(0.33, 0.33, 0.33)  # grey

            ctx.arc(radio, radio, radio, 0., 2 * math.pi)
            ctx.fill()

            ctx.translate(self._point_size * 2, 0)
        ctx.restore()


class WorldMap(Gtk.EventBox):

    __gsignals__ = {
        'icon-clicked': (GObject.SignalFlags.RUN_FIRST, GObject.TYPE_NONE,
                         ([str])), }

    def __init__(self):
        Gtk.EventBox.__init__(self)
        self._width = Gdk.Screen.width()
        self._height = Gdk.Screen.height() - style.GRID_CELL_SIZE
        self.set_size_request(self._width, self._height)
        self._background_pixbuf = GdkPixbuf.Pixbuf.new_from_file_at_size(
            './MAP.svg', self._width, self._height)
        self._xo_color = profile.get_color()
        self._fixed = Gtk.Fixed()
        self.connect('draw', self.__draw_cb)

        for data in DAT_DIC:
            icon = EventIcon(icon_name=data['icon_name'],
                             xo_color=self._xo_color)
            icon.set_size(100)
            x = int((data['icon_position'][0] + 1) * self._width / 2)
            y = int((1 - data['icon_position'][1]) * self._height / 2)
            self._fixed.put(icon, x, y)
            icon.show()
            icon.connect('button-press-event',
                         self.__icon_button_press_cb, data['name'])
        self._fixed.show()
        self.add(self._fixed)

    def __draw_cb(self, widget, ctx):
        ctx.rectangle(0, 0, self._width, self._height)
        ctx.set_source_rgba(1.0, 1.0, 1.0, 1.0)
        ctx.paint()
        ctx.save()
        ctx.translate(0, 0)
        scale_x = float(self._width) / self._background_pixbuf.get_width()
        scale_y = float(self._height) / self._background_pixbuf.get_height()
        ctx.scale(scale_x, scale_y)
        Gdk.cairo_set_source_pixbuf(ctx, self._background_pixbuf, 0, 0)
        ctx.paint()
        ctx.restore()

    def __icon_button_press_cb(self, icon, event, data_name):
        self.emit('icon-clicked', data_name)