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

import logging
logger = logging.getLogger()

import gtk
import gobject

from sugar.activity import activity
from sugar.graphics.objectchooser import ObjectChooser
from sugar import mime

from gettext import gettext as _
from StringIO import StringIO

import animation
from frames_tray import FramesTray
from toolbars import AnimateToolbarBox
#from toolbars import FramesToolbar


class AnimateActivity(activity.Activity):

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

        self.max_participants = 1

        self._toolbarbox = AnimateToolbarBox(self)

        self.set_toolbar_box(self._toolbarbox)
        self._toolbarbox.show_all()

        canvas = gtk.EventBox()
        canvas.modify_bg(gtk.STATE_NORMAL, gtk.gdk.color_parse('white'))

        self._animation = None
        self._animation_frames = []
        self.animation_mode = animation.MODE_RETURN
        self.modes_buttons = self._toolbarbox.modes_buttons

        self._frames_tray = FramesTray()
        self._frames_tray.connect("current-frame-changed",
                                  self._current_frame_changed_cb)
#        self._frames_tray.connect("move", self._move_cb)
#        self._frames_tray.connect('get-current-frame',
#                                lambda w: self._animation.get_current_frame())
        self._frames_tray.show()
        self.set_tray(self._frames_tray, gtk.POS_BOTTOM)

#        self._frames_toolbar = FramesToolbar()
#        self._frames_toolbar.connect('go-up', self._frames_list.move_up)
#        self._frames_toolbar.connect('go-down', self._frames_list.move_down)
#        vbox.pack_end(self._frames_toolbar, False, True, 0)
#        canvas.pack_end(vbox, False, True, 0)

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

        self.show()

    def _move_cb(self, widget, index, new_index):
        self._animation.move(index, new_index)

    def _current_frame_changed_cb(self, widget, index):
        logger.debug('%d' % index)
        self._animation.set_current_frame(index)

    def write_file(self, file_path):
        self.metadata["mime_type"] = "application/x-animate-activity"
        zfile = zipfile.ZipFile(file_path, 'w')
        temp_file_path = tempfile.mktemp()
        temp_file = open(temp_file_path, "w")
        temp_file_content = {"mode": self._animation.get_mode(),
                             "framerate": self._toolbarbox.get_fps()}
        try:
            json.dump(temp_file_content, temp_file)
        finally:
            temp_file.close()
        zfile.write(temp_file_path, 'config')
        frames_path = tempfile.mktemp()
        count = 0
        for i in self._animation.frames:
            i.save(frames_path, 'png')
            zfile.write(frames_path, 'frames/%d.png' % count)
            count += 1
        zfile.close()

        os.remove(temp_file_path)

    def read_file(self, file_path):
        zfile = zipfile.ZipFile(file_path, 'r')
        frames = zfile.namelist()
        del(frames[frames.index('config')])
        temp_frames_path = tempfile.mktemp()
        for frame in frames:
            frame_content = zfile.read(frame)
            frame_file = open(temp_frames_path, 'w')
            frame_file.write(frame_content)
            frame_file.close()
            pixbuf = gtk.gdk.pixbuf_new_from_file(temp_frames_path)
            self._animation_frames.append(pixbuf)

        str_io = StringIO(zfile.read('config'))
        zfile.close()

        try:
            options = json.load(str_io)
        finally:
            str_io.close()
            mode = options['mode']
            self._animation_mode = mode
            self.modes_buttons[mode].set_active(True)
            self._toolbarbox.set_fps(options['framerate'])

    def _remove_frame(self, widget):
        self._frames_tray.remove_selected_frame()
        self._animation.remove_current_frame()

    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._animation.add_frame(pixbuf)
            self._frames_tray.add_frame(pixbuf)
        else:
            return

    def _fps_changed_cb(self, widget):
        fps = widget.get_adjustment().get_value()
        self._animation.set_fps(fps)

    def _set_return_mode(self, widget):
        self._animation.set_mode(animation.MODE_RETURN)

    def _set_normal_mode(self, widget):
        self._animation.set_mode(animation.MODE_NORMAL)

    def _set_pingpong_mode(self, widget):
        self._animation.set_mode(animation.MODE_PING_PONG)

    def _previous_frame(self, widget):
        self._animation.back()

    def _next_frame(self, widget):
        self._animation.next()

    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 _get_animation_size(self):
        canvas_allocation = self.get_canvas().get_allocation()
        tray_allocation = self._frames_tray.get_allocation()

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

        return width, height

    def __canvas_expose_cb(self, canvas, event):
        x, y, width, height = event.area

        if not self._animation:
            self._animation = animation.Animation(width, height)
            if self._animation_mode:
                self._animation.set_mode(self._animation_mode)
            #self._animation.connect('running',
            #                        self._frames_toolbar.set_buttons_sensitive)
            for pixbuf in self._animation_frames:
                self._animation.add_frame(pixbuf)
                self._frames_tray.add_frame(pixbuf)
            self._animation.set_fps(self._toolbarbox.get_fps())
            self._animation.connect("current-frame-updated",
                                    self._current_frame_updated_cb)
            self._animation.show_all()

            self._animation_mode = None

            canvas.add(self._animation)

        self._animation.set_size(width,
                                 height)

    def _current_frame_updated_cb(self, widget, index):
        if widget.frames_reverse:
            index = len(widget.frames) - 1 - index
        self._frames_tray.select_frame(index)
        return index

    def _view_fullscreen(self, widget):
        self._frames_tray.hide()
        self._animation.set_size(gtk.gdk.screen_width(),
                                 gtk.gdk.screen_height())
        activity.Activity.fullscreen(self)

    def unfullscreen(self):
        self._frames_tray.show()
        width, height = self._get_animation_size()
        self._animation.set_size(width, height)

        gobject.idle_add(lambda: activity.Activity.unfullscreen(self))