Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/activity.py
blob: 44ea803907885a215d1448b058f78e451b8f1de4 (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
# Copyright 2012 One Laptop per Child
#
# 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

import os
import logging
from gettext import gettext as _

import gtk
import gobject

from sugar.activity import activity
from sugar.graphics.toolbarbox import ToolbarBox
from sugar.activity.widgets import ActivityToolbarButton
from sugar.activity.widgets import StopButton
from sugar.graphics import style
from sugar.graphics.toolbutton import ToolButton
from sugar.graphics.icon import Icon

DEFAULT_CHANGE_IMAGE_TIME = 10


class WelcomeActivity(activity.Activity):
    """WelcomeActivity class as specified in activity.info"""

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

        # we do not have collaboration features
        # make the share option insensitive
        self.max_participants = 1

        # toolbar with the new toolbar redesign
        toolbar_box = ToolbarBox()

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

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

        self.image_viewer = ImageCollectionViewer(False)

        prev_bt = ToolButton("go-previous-paired")
        prev_bt.connect("clicked", self.image_viewer.prev_image_clicked_cb)
        toolbar_box.toolbar.insert(prev_bt, -1)

        next_bt = ToolButton("go-next-paired")
        next_bt.connect("clicked", self.image_viewer.next_image_clicked_cb)
        toolbar_box.toolbar.insert(next_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.modify_bg(gtk.STATE_NORMAL, style.COLOR_WHITE.get_gdk_color())
        self.set_canvas(self.image_viewer)


class CustomButton(gtk.EventBox):

    def __init__(self, icon, size):
        super(gtk.EventBox, self).__init__()
        image = gtk.Image()
        path = os.path.expanduser('~/Activities/Welcome.activity/icons/')
        pxb = gtk.gdk.pixbuf_new_from_file_at_size('%s/%s.svg' % (path, icon),
                size, size)
        image.set_from_pixbuf(pxb)
        self.add(image)
        self.modify_bg(gtk.STATE_NORMAL, style.COLOR_WHITE.get_gdk_color())


class ImageCollectionViewer(gtk.VBox):

    __gtype_name__ = 'WelcomeDialog'

    def __init__(self, start_window=True):
        super(gtk.VBox, self).__init__()

        self.image = gtk.Image()
        self.pack_start(self.image, True, True, padding=0)

        # images management
        images_path = \
                os.path.expanduser('~/Activities/Welcome.activity/images/')

        self.image_order = 0
        self.image_files_list = []
        for fname in os.listdir(images_path):
            self.image_files_list.append(images_path + fname)
            logging.error('Image file: %s', fname)

        if self.image_files_list:
            self.image.set_from_file(self.image_files_list[self.image_order])

        if start_window:
            # Create bottom controls

            bottom_toolbar = gtk.HBox()
            self.pack_start(bottom_toolbar, False, padding=style.zoom(30))

            left_box = gtk.HBox()
            bottom_toolbar.pack_start(left_box, False, padding=0)

            center_align = gtk.Alignment(0.5, 0, 0, 0)
            center_box = gtk.HBox()
            center_align.add(center_box)
            bottom_toolbar.pack_start(center_align, True, True, padding=0)

            right_box = gtk.HBox()
            bottom_toolbar.pack_start(right_box, False, padding=0)

            _next_button = gtk.Button()
            _next_button.set_label(gtk.STOCK_GO_FORWARD)
            _next_button.set_use_stock(True)
            _next_button.connect('clicked', self.__next_clicked_cb)
            right_box.pack_end(_next_button, False, False,
                    padding=style.zoom(30))
            bt_width, bt_height = _next_button.size_request()

            prev_bt = CustomButton('go-previous-paired-grey', bt_height)
            center_box.pack_start(prev_bt, False, False, 5)
            prev_bt.connect('button-press-event', self.prev_image_clicked_cb)

            next_bt = CustomButton('go-next-paired-grey', bt_height)
            center_box.pack_start(next_bt, False, False, 5)
            next_bt.connect('button-press-event', self.next_image_clicked_cb)

            # do the right_box and left_box have the same size
            width = int(gtk.gdk.screen_width() / 4)
            right_box.set_size_request(width, -1)
            left_box.set_size_request(width, -1)

        self.show_all()

        self.timer_id = gobject.timeout_add_seconds(DEFAULT_CHANGE_IMAGE_TIME,
                self.auto_change_image)

        # calculate space available for images
        #   (only to tell to the designers)
        height_av = gtk.gdk.screen_height() - style.GRID_CELL_SIZE * 2
        width_av = gtk.gdk.screen_width()
        print 'Size available for image: %d x %d' % (width_av, height_av)

    def __next_clicked_cb(self, button):
        gtk.main_quit()

    def auto_change_image(self):
        self.next_image_clicked_cb(None)
        return True

    def next_image_clicked_cb(self, button, event):
        gobject.source_remove(self.timer_id)
        self.timer_id = gobject.timeout_add_seconds(DEFAULT_CHANGE_IMAGE_TIME,
                self.auto_change_image)
        self.image_order += 1
        if self.image_order == len(self.image_files_list):
            self.image_order = 0
        self.image.set_from_file(self.image_files_list[self.image_order])

    def prev_image_clicked_cb(self, button, event):
        self.image_order -= 1
        if self.image_order < 0:
            self.image_order = len(self.image_files_list) - 1
        self.image.set_from_file(self.image_files_list[self.image_order])


def main():
    win = gtk.Window()
    image_viewer = ImageCollectionViewer()
    win.add(image_viewer)
    win.set_size_request(gtk.gdk.screen_width(), gtk.gdk.screen_height())
    win.set_position(gtk.WIN_POS_CENTER_ALWAYS)
    win.set_decorated(False)
    win.modify_bg(gtk.STATE_NORMAL, style.COLOR_WHITE.get_gdk_color())

    win.show_all()
    win.connect("destroy", gtk.main_quit)
    gtk.main()

if __name__ == "__main__":
    main()