Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/ImageViewerActivity.py
blob: cdb23a1a1fcc728d4d004be5348c5c940a2a6505 (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
# Copyright (C) 2008, One Laptop Per Child
# Author: Sayamindu Dasgupta <sayamindu@laptop.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 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


from sugar.activity import activity
import logging

from gettext import gettext as _

import sys, os
import gtk

import ImageView
import ImageViewerToolbar


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

        self.zoom = None
        
        self.view = ImageView.ImageViewer()

        toolbox = activity.ActivityToolbox(self)
        self._view_toolbar = ImageViewerToolbar.ViewToolbar(self.view)
        toolbox.add_toolbar(_('View'), self._view_toolbar)
        self._view_toolbar.show()
        self.set_toolbox(toolbox)
        toolbox.show()

        self._view_toolbar.connect('go-fullscreen',
                self.__view_toolbar_go_fullscreen_cb)

        self.connect('window-state-event', 
                self.__window_state_event_cb)

        vadj = gtk.Adjustment()
        hadj = gtk.Adjustment()
        self.sw = gtk.ScrolledWindow(hadj, vadj)

        self.sw.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
        self.sw.add_with_viewport(self.view)

        self.set_canvas(self.sw)
        self.sw.show_all()


    def read_file(self, file_path):
        self.view.set_file_location(file_path)

        #self.zoom = int(self.metadata.get('zoom', '0'))
        #if self.zoom == 0:
        #    self.zoom = self.view.get_property('zoom')
        #else:
        #    self.view.set_zoom(self.zoom)

    #def write_file(self, file_path):
        #try:
        #    self.metadata['zoom'] = str(self.zoom)
        #except Exception, e:
        #    logging.error('write_file(): %s', e)

    def __view_toolbar_go_fullscreen_cb(self, view_toolbar):
        self._old_zoom = self.view.get_property('zoom') #XXX: Hack
        # Zoom to fit screen if possible
        screen = self.get_screen()
        zoom = self.view.calculate_optimal_zoom(screen.get_width(), screen.get_height())
        self.view.set_zoom(zoom)
        
        self.fullscreen()

    def __window_state_event_cb(self, window, event):
        if event.changed_mask & gtk.gdk.WINDOW_STATE_FULLSCREEN:
            if not self.window.get_state() & gtk.gdk.WINDOW_STATE_FULLSCREEN:
                self.view.set_zoom(self._old_zoom)