Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/src/jarabe/journal/thumbsview.py
blob: ac553bd11b9034dbf3e0852384c6b5faa5b64dc4 (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
# Copyright (C) 2009, Aleksey Lim
#
# 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 gtk
import gobject
import logging
import hippo

from sugar.graphics import style
from sugar.graphics.icon import CanvasIcon
from sugar.graphics.xocolor import XoColor
from sugar.graphics.palette import CanvasInvoker

from jarabe.journal.keepicon import KeepIcon
from jarabe.journal.source import Source
from jarabe.journal.objectmodel import ObjectModel
from jarabe.journal.browse.tableview import TableView, TableCell
from jarabe.journal.palettes import ObjectPalette
from jarabe.journal import misc
from jarabe.journal import model

ROWS = 4
COLUMNS = 5
STAR_WIDTH = 30

class ThumbsCell(TableCell, hippo.CanvasBox):
    def __init__(self):
        TableCell.__init__(self)

        self._last_uid = None

        hippo.CanvasBox.__init__(self,
                orientation=hippo.ORIENTATION_VERTICAL,
                padding_left=style.DEFAULT_SPACING,
                padding_top=style.DEFAULT_SPACING,
                spacing=style.DEFAULT_PADDING)

        self.preview_box = hippo.CanvasBox(
                spacing=style.DEFAULT_PADDING,
                orientation=hippo.ORIENTATION_HORIZONTAL)
        self.append(self.preview_box, hippo.PACK_EXPAND)

        star_box = hippo.CanvasBox(
                orientation=hippo.ORIENTATION_VERTICAL,
                box_width=STAR_WIDTH)
        self.preview_box.append(star_box)

        self.keep = KeepIcon(False)
        self.keep.props.size = style.SMALL_ICON_SIZE
        self.keep.connect('activated', self.__star_activated_cb)
        star_box.append(self.keep)

        self.activity_box = hippo.CanvasBox()
        self.preview_box.append(self.activity_box, hippo.PACK_EXPAND)

        self.thumb = ThumbCanvas(
                border=style.LINE_WIDTH,
                border_color=style.COLOR_BUTTON_GREY.get_int(),
                xalign=hippo.ALIGNMENT_CENTER,
                yalign=hippo.ALIGNMENT_CENTER)
        self.activity_box.append(self.thumb, hippo.PACK_EXPAND)

        self.activity_icon = ActivityIcon(
                border=style.LINE_WIDTH,
                border_color=style.COLOR_BUTTON_GREY.get_int(),
                xalign=hippo.ALIGNMENT_CENTER,
                yalign=hippo.ALIGNMENT_CENTER)
        self.activity_box.append(self.activity_icon, hippo.PACK_EXPAND)

        title_box = hippo.CanvasBox(
                orientation=hippo.ORIENTATION_HORIZONTAL)
        self.append(title_box)

        tool_box = hippo.CanvasBox(
                orientation=hippo.ORIENTATION_VERTICAL,
                box_width=STAR_WIDTH)
        title_box.append(tool_box)

        details = DetailsIcon(
                size=style.SMALL_ICON_SIZE)
        details.connect('activated', self.__detail_activated_cb)
        tool_box.append(details)

        text_box = hippo.CanvasBox(
                orientation=hippo.ORIENTATION_VERTICAL)
        title_box.append(text_box)

        self.title = hippo.CanvasText(
                xalign=hippo.ALIGNMENT_START,
                size_mode=hippo.CANVAS_SIZE_ELLIPSIZE_END)
        text_box.append(self.title)

        self.date = hippo.CanvasText(
                xalign=hippo.ALIGNMENT_START,
                size_mode=hippo.CANVAS_SIZE_ELLIPSIZE_END)
        text_box.append(self.date)

    def do_fill_in(self):
        self.title.props.text = self.row[Source.FIELD_TITLE] or ''
        self.date.props.text = self.row[Source.FIELD_MODIFY_TIME] or ''
        self.keep.props.keep = int(self.row[Source.FIELD_KEEP] or 0) == 1

        w, h = self.activity_box.get_allocation()
        if w / 4. * 3. > h:
            w = int(h / 3. * 4.)
        else:
            h = int(w / 4. * 3.)
        thumb = self.row[Source.FIELD_THUMB]


        logging.error('> %r %r %r %r' % (self, self._last_uid, self.row[Source.FIELD_UID], self.row.has_key(ObjectModel.FIELD_FETCHED_FLAG)))

        if self._last_uid == self.row[Source.FIELD_UID] and \
                not self.row.has_key(ObjectModel.FIELD_FETCHED_FLAG):
            # do not blink by preview while re-reading entries
            return
        else:
            self._last_uid = self.row[Source.FIELD_UID]

        if thumb is None:
            self.thumb.set_visible(False)
            self.activity_icon.set_visible(True)
            self.activity_icon.set_metadata(self.row.metadata)
            self.activity_icon.allocate(w, h, True)
        else:
            self.activity_icon.set_visible(False)
            self.thumb.set_visible(True)
            self.thumb.props.scale_width = w
            self.thumb.props.scale_height = h
            self.thumb.props.image = thumb
            self.thumb.set_metadata(self.row.metadata)
            self.thumb.allocate(w, h, True)

    def __star_activated_cb(self, keep_button):
        self.row.metadata['keep'] = not keep_button.props.keep and 1 or 0
        model.write(self.row.metadata, update_mtime=False)

    def __detail_activated_cb(self, button):
        self.tree.emit('detail-clicked', self.row[Source.FIELD_UID])

class ActivityCanvas:
    def __init__(self):
        self._metadata = None
        self.connect_after('button-release-event',
                self.__button_release_event_cb)

    def set_metadata(self, metadata):
        self.palette = None
        self._metadata = metadata

    def create_palette(self):
        if self._metadata is None:
            return None

        palette = ObjectPalette(self._metadata, detail=True)
        palette.connect('detail-clicked',
                        self.__detail_clicked_cb)
        return palette

    def __detail_clicked_cb(self, palette, uid):
        self.emit('detail-clicked', uid)

    def __button_release_event_cb(self, button, foo):
        misc.resume(self._metadata)
        return True

class ActivityIcon(ActivityCanvas, CanvasIcon):
    def __init__(self, **kwargs):
        CanvasIcon.__init__(self, **kwargs)
        ActivityCanvas.__init__(self)

    def set_metadata(self, metadata):
        ActivityCanvas.set_metadata(self, metadata)

        self.props.file_name = misc.get_icon_name(metadata)

        if misc.is_activity_bundle(metadata):
            self.props.fill_color = style.COLOR_TRANSPARENT.get_svg()
            self.props.stroke_color = style.COLOR_BUTTON_GREY.get_svg()
        else:
            if metadata.has_key('icon-color') and metadata['icon-color']:
                self.props.xo_color = XoColor(self._metadata['icon-color'])

class ThumbCanvas(ActivityCanvas, hippo.CanvasImage):
    def __init__(self, **kwargs):
        hippo.CanvasImage.__init__(self, **kwargs)
        ActivityCanvas.__init__(self)

        self._palette_invoker = CanvasInvoker()
        self._palette_invoker.attach(self)
        self.connect('destroy', self.__destroy_cb)

    def __destroy_cb(self, icon):
        if self._palette_invoker is not None:
            self._palette_invoker.detach()

class DetailsIcon(CanvasIcon):
    def __init__(self, **kwargs):
        CanvasIcon.__init__(self, **kwargs)
        self.props.icon_name = 'go-right'
        self.props.fill_color = style.COLOR_BUTTON_GREY.get_svg()
        self.props.stroke_color = style.COLOR_TRANSPARENT.get_svg()
        self.connect('motion-notify-event', self.__motion_notify_event_cb)

    def __motion_notify_event_cb(self, icon, event):
        if event.detail == hippo.MOTION_DETAIL_ENTER:
            icon.props.fill_color = style.COLOR_BLACK.get_svg()
        elif event.detail == hippo.MOTION_DETAIL_LEAVE:
            icon.props.fill_color = style.COLOR_BUTTON_GREY.get_svg()

class ThumbsView(TableView):
    __gsignals__ = {
            'detail-clicked': (gobject.SIGNAL_RUN_FIRST,
                              gobject.TYPE_NONE,
                              ([object])),
            }

    def __init__(self):
        TableView.__init__(self, ThumbsCell, ROWS, COLUMNS)