Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/activity.py
blob: cca8b36829e340b3fa646c114426b825cd69e052 (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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# activity.py
#
# Copyright 2012 S. Daniel Francis <francis@sugarlabs.org>,
#                Agustin Zubiaga <aguz@sugarlabs.org>
#
# 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 Street, Fifth Floor, Boston,
# MA 02110-1301, USA.

import gtk

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

from sugar import mime
from gettext import gettext as _

from frames_list import FramesList
from animation import Animation


class AnimateActivity(activity.Activity):

    def __init__(self, handler):
        super(AnimateActivity, self).__init__(handler)

        self.max_participants = 1

        toolbarbox = ToolbarBox()

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

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

        add_image = ToolButton('insert-image')
        add_image.set_tooltip(_('Add a frame'))
        add_image.connect('clicked', self._add_frame)
        toolbarbox.toolbar.insert(add_image, -1)

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

        run_btn = ToolButton('media-playback-start')
        run_btn.set_tooltip(_('Run animation'))
        run_btn.connect('clicked', self._run_pause_animation)
        toolbarbox.toolbar.insert(run_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)
        toolbarbox.show_all()

        canvas = gtk.HBox()

        self._animation = None

        self._frames_list = FramesList()
        self._frames_list.show()
        canvas.pack_end(self._frames_list, False, True, 0)

        canvas.connect("expose-event", self.__canvas_expose_cb)
        canvas.show()
        self.set_canvas(canvas)

        self.show()

    def _add_frame(self, widget):
        chooser = ObjectChooser(parent=self,
                                what_filter=mime.GENERIC_TYPE_IMAGE)
        result = chooser.run()
        if result == gtk.RESPONSE_ACCEPT:
            jobject = chooser.get_selected_object()
            pixbuf = gtk.gdk.pixbuf_new_from_file(jobject.get_file_path())
            self._frames_list.add_frame(pixbuf)
            self._animation.add_image(jobject.get_file_path())

        else:
            return

    def _run_pause_animation(self, widget):
        if not self._animation._running:
            self._animation.run()
            widget.set_icon("media-playback-pause")
            widget.set_tooltip(_("Pause animation"))

        elif self._animation._running:
            self._animation.pause()
            widget.set_icon("media-playback-start")
            widget.set_tooltip(_("Run animation"))

    def __canvas_expose_cb(self, canvas, event):
        canvas_allocation = canvas.get_allocation()
        treeview_allocation = self._frames_list.get_allocation()

        width = canvas_allocation[-2] - treeview_allocation[-2]
        height = canvas_allocation[-1]

        if not self._animation:
            self._animation = Animation(width, height)
            self._animation.show_all()

        self._frames_list._animation = self._animation
        canvas.pack_start(self._animation, True, True, 0)