Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/activity.py
blob: d618546d44c94019616caf20544da82a74a79015 (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
# 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

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

DAT_DIC = ({'name': 'Butterfly', 'icon_name': 'butterflies',
            'video': 'Butterflies.mp4', 'icon_position': (-0.7, 0.1),
            'text': _('The monarch butterfly is famous for its southward '
                    'migration and northward return in summer from '
                    'Canada to Mexico and Baja California which spans '
                    'the life of three to four generations of the '
                    'butterfly. \n\n'
                    'The journey is 3000 miles and take 6 months')},
           {'name': 'Chimps', 'icon_name': 'chimps',
            'video': 'Chimps.mp4', 'icon_position': (-0.1, -0.1),
            'text': _('Chimpanzees are members of the Hominidae family, '
                    'along gorilas, humans and orangutans. '
                    'Chimpanzees split from the human branch of the '
                    'family about four to six millions years ago.')},
           {'name': 'Polar bears', 'icon_name': 'polar-bears',
            'video': 'PolarBears.mp4', 'icon_position': (0.0, 0.95),
            'text': _('Polar bears have two types of fur- a top layer and '
                    'an under layer. The top layer is made up of long '
                    'oily hairs and the under layer is soft and fine.\n\n'
                    'When the bears swim the top layer keeps the under '
                    'layer from getting wet and afterwards they can '
                    'shake off all of the water just like a dog.')},
           {'name': 'Pandas', 'icon_name': 'pandas',
            'video': 'Pandas.mp4', 'icon_position': (0.5, 0.3),
            'text': _('The giant panda lives in the mountain ranges in '
                    'central China. The giant panda has a black and '
                    'white coat. Adults measure around 1.2 to 1.8 meters '
                    'in length. \n\n'
                    'Pandas eat any of 25 bamboos species in the wild')},
           {'name': 'Whales', 'icon_name': 'whales',
            'video': 'Whales.mp4', 'icon_position': (0.1, -0.8),
            'text': _('Humpback Whales are giants amongst sea voyagers, '
                    'they spend their year roaming the oceans between '
                    'feeding and breading grounds.\n\n '
                    'They follow the coast of Mexico and Nort America all '
                    'the way to the Arctic, travelling day and night '
                    'without pause covering over 500 miles a week')},
           {'name': 'Kangaroos', 'icon_name': 'kangaroos',
            'video': None, 'icon_position': (0.6, -0.3),
            'text': ''}
           )

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.connect('clicked',
                               self.__show_world_map_cb)
        toolbar_box.toolbar.insert(self._world_bt, -1)

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

        self._info_bt = ToolButton('toolbar-help')
        self._info_bt.connect('clicked',
                              self.__show_info_cb)
        toolbar_box.toolbar.insert(self._info_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):
        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'])
            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._info_bt.set_sensitive(page == VIDEO_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):
        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
            font_size = 12
        else:
            # desktop
            font_size = 25

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

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

        self._xo_color = profile.get_color()
        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()


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)