Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/src/jarabe/journal/thumbsview.py
blob: 4a59ff213113a73a53fc33e030a30bac8dbecd7c (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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
# 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 sys
import gobject
import hippo
import pango

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.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_HORIZONTAL,
                padding_left=style.DEFAULT_SPACING,
                padding_top=style.DEFAULT_SPACING * 2,
                spacing=style.DEFAULT_PADDING)

        self.connect('button-release-event', self.__button_release_event_cb)

        # tools column

        tools_box = hippo.CanvasBox(
                spacing=style.DEFAULT_PADDING,
                orientation=hippo.ORIENTATION_VERTICAL,
                box_width=STAR_WIDTH)
        self.append(tools_box)

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

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

        # main column

        main_box = hippo.CanvasBox(
                orientation=hippo.ORIENTATION_VERTICAL)
        self.append(main_box, hippo.PACK_EXPAND)

        self.allocation_box = hippo.CanvasBox()
        main_box.append(self.allocation_box, hippo.PACK_EXPAND)

        self.activity_box = hippo.CanvasBox(
                border=style.LINE_WIDTH,
                border_color=style.COLOR_BUTTON_GREY.get_int())
        self.allocation_box.append(self.activity_box, hippo.PACK_FIXED)

        self.thumb = ThumbCanvas(self,
                xalign=hippo.ALIGNMENT_START,
                yalign=hippo.ALIGNMENT_START)
        self.thumb.connect('detail-clicked', self.__detail_clicked_cb)
        self.activity_box.append(self.thumb, hippo.PACK_FIXED)

        self.activity_icon = ActivityIcon(self,
                xalign=hippo.ALIGNMENT_START,
                yalign=hippo.ALIGNMENT_START)
        self.activity_icon.connect('detail-clicked', self.__detail_clicked_cb)
        self.activity_box.append(self.activity_icon, hippo.PACK_EXPAND)

        self.title = hippo.CanvasText(
                padding_top=style.DEFAULT_PADDING,
                xalign=hippo.ALIGNMENT_START,
                size_mode=hippo.CANVAS_SIZE_ELLIPSIZE_END)
        main_box.append(self.title)

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

    def do_fill_in(self):
        title_weight = pango.AttrWeight(pango.WEIGHT_BOLD)
        title_weight.start_index = 0
        title_weight.end_index = sys.maxint
        title_attributes = pango.AttrList()
        title_attributes.insert(title_weight)

        self.title.props.attributes = title_attributes
        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.table.thumb_size
        self.activity_box.props.box_width = w
        self.activity_box.props.box_height = h

        thumb = self.row[Source.FIELD_THUMB]

        if self._last_uid == self.row[Source.FIELD_UID] and \
                not ObjectModel.FIELD_FETCHED_FLAG in self.row:
            # 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.palette = None
            self.activity_icon.update_icon()
        else:
            self.activity_icon.set_visible(False)
            self.thumb.set_visible(True)
            self.thumb.props.scale_width = w - style.LINE_WIDTH * 2
            self.thumb.props.scale_height = h - style.LINE_WIDTH * 2
            self.thumb.props.image = thumb
            self.thumb.palette = None
            self.thumb.allocate(w, h, True)
            self.activity_box.set_position(self.thumb,
                    style.LINE_WIDTH, style.LINE_WIDTH)

    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.table.emit('detail-clicked', self.row[Source.FIELD_UID])

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

    def __button_release_event_cb(self, sender, event):
        if not self.table.props.hover_selection:
            return False
        uid = self.row[Source.FIELD_UID]
        self.table.emit('entry-activated', uid)
        return False


class ActivityCanvas(object):

    def __init__(self, cell):
        self._cell = cell
        self.connect_after('button-release-event',
                self.__button_release_event_cb)
        self.palette = None

    def create_palette(self):
        if self._cell.table.props.hover_selection:
            return
        palette = ObjectPalette(self._cell.row.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, event):
        misc.resume(self._cell.row.metadata)
        return True


class ActivityIcon(ActivityCanvas, CanvasIcon):

    __gsignals__ = {
            'detail-clicked': (gobject.SIGNAL_RUN_FIRST, gobject.TYPE_NONE,
                              ([str])),
            }

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

    def update_icon(self):
        metadata = self._cell.row.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 'icon-color' in metadata and metadata['icon-color']:
                self.props.xo_color = XoColor(metadata['icon-color'])


class ThumbCanvas(ActivityCanvas, hippo.CanvasImage):

    __gsignals__ = {
            'detail-clicked': (gobject.SIGNAL_RUN_FIRST, gobject.TYPE_NONE,
                              ([str])),
            }

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

        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.stroke_color = style.COLOR_TRANSPARENT.get_svg()
        self.connect('motion-notify-event', self.__motion_notify_event_cb)
        self.connect('activated', self.__on_leave_cb)
        self.__on_leave_cb(None)

    def __on_leave_cb(self, button):
        self.props.fill_color = style.COLOR_BUTTON_GREY.get_svg()

    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:
            self.__on_leave_cb(None)


class ThumbsView(TableView):

    __gsignals__ = {
            'detail-clicked': (gobject.SIGNAL_RUN_FIRST,
                              gobject.TYPE_NONE,
                              ([object])),
            'entry-activated': (gobject.SIGNAL_RUN_FIRST,
                                gobject.TYPE_NONE,
                                ([str])),
            }

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

    def do_size_allocate(self, allocation):
        text_layout = self.create_pango_layout('W')
        w = allocation.width / COLUMNS - STAR_WIDTH - style.DEFAULT_SPACING - \
                style.DEFAULT_PADDING - style.LINE_WIDTH * 2
        h = allocation.height / ROWS - text_layout.get_pixel_size()[1] * 2 - \
                style.DEFAULT_SPACING * 2 - style.DEFAULT_PADDING - \
                style.LINE_WIDTH * 2

        # keep thumb size 4:3
        if w / 4. * 3. > h:
            w = int(h / 3. * 4.)
        else:
            h = int(w / 4. * 3.)

        self.thumb_size = (max(0, w), max(0, h))

        TableView.do_size_allocate(self, allocation)