summaryrefslogtreecommitdiff
path: root/trunk/dimbola/grid.py
blob: 49916af3cdc88c17a826f7cdad7c7ec8d413ebed (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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
# Copyright (C) 2009  Lars Wirzenius <liw@liw.fi>
# 
# 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, see <http://www.gnu.org/licenses/>.


# This is necessary for running under Python 2.5, which we need to
# do on Debian, for now.
from __future__ import with_statement


import gobject
import gtk

import dimbola


class GridModel(gobject.GObject):

    '''This is the MVC model of the thumbnail grid.
    
    This class takes care of maintaining data about the grid: the current
    list of photoids to be shown in the grid (photoids property), and the 
    list of photoids that are currently selected (selected property).
    It also takes care of computing the vertical size in pixels of the
    thumbnail grid (the whole grid, not just the visible part that gets
    painted onto a gtk.DrawingArea).
    
    The .selected property is a bit special. Its first element, if any,
    is the focus for moving selection around with the keyboard.
    
    '''
    
    __gsignals__ = {
        'photoids-changed': (gobject.SIGNAL_RUN_FIRST, gobject.TYPE_NONE, []),
        'selection-changed': (gobject.SIGNAL_RUN_FIRST, gobject.TYPE_NONE, []),
    }
    
    def __init__(self):
        gobject.GObject.__init__(self)
        self._photoids = []
        self._selected = []
        self.thumbnails = dict()
        self.angles = dict()
        self.padding = 20
        self.scale_value = None
        self.widget_width = None
        self.widget_height = None

    def get_photoids(self):
        return self._photoids
    def set_photoids(self, photoids):
        self._photoids = photoids
        del self.selected[:]
        self.thumbnails.clear()
        self.angles.clear()
        self.photoids_changed()
    photoids = property(get_photoids, set_photoids)

    def photoids_changed(self):
        '''Emit the photoids-changed signal.'''
        self.emit('photoids-changed')
    
    def get_selected(self):
        return self._selected
    def set_selected(self, selected):
        assert set(self._photoids).issuperset(set(selected))
        self._selected = selected
        self.selection_changed()
    selected = property(get_selected, set_selected)

    def selection_changed(self):
        '''Emit the selection-changed signal.'''
        self.emit('selection-changed')

    def set_thumbnail(self, photoid, thumbnail):
        '''Set the thumbnail for a photo.'''
        assert photoid in self.photoids
        self.thumbnails[photoid] = thumbnail
        self.photoids_changed()

    def set_angle(self, photoid, angle):
        '''Set the angle for a photo.'''
        assert photoid in self.photoids
        self.angles[photoid] = angle
        self.photoids_changed()

    @property
    def maxdim(self):
        '''Maximum dimension of a thumbnail on the grid.'''
        return min(int(self.scale_value), self.widget_width - self.padding)

    @property
    def distance(self):
        '''Compute the distance between thumbnails in grid.
        
        This includes padding between them.
        
        '''
        return self.maxdim + self.padding

    @property
    def maxcols(self):
        '''Maximum number of columns.'''
        return self.widget_width / self.distance

    @property
    def vertical_pixels(self):
        '''Height of thumbnail grid (not just visible part) in pixels.
        
        We get the dimension of the visible grid (the gtk.DrawingArea
        widget) in pixels, and the size of the thumbnails also in pixels 
        from the slider. We also get the number of photos. We need to 
        compute the height of grid (not just visible part), in pixels.
        
        '''
    
        total_rows = (len(self.photoids) + self.maxcols - 1) / self.maxcols
        return total_rows * self.distance

    def thumbnail_pos(self, i):
        '''Compute x and y for thumbnail of ith photo in grid.'''
        
        maxcols = self.widget_width / self.distance

        colno = i % self.maxcols
        x = colno * self.distance
        
        rowno = i / self.maxcols
        y = rowno * self.distance
        
        return x, y

    def select_next(self):
        '''Select next photo.'''
        if self.selected:
            i = self.photoids.index(self.selected[0])
            self.selected = [self.photoids[min(i+1, len(self.photoids) - 1)]]
        else:
            self.selected = [self.photoids[0]]
        
    def select_previous(self):
        '''Select previous photo.'''
        if self.selected:
            i = self.photoids.index(self.selected[0])
            self.selected = [self.photoids[max(i-1, 0)]]
        else:
            self.selected = [self.photoids[0]]

        
class GridView(object): # pragma: no cover

    '''This is the MVC view of the thumbnail grid.
    
    This class takes care of drawing things on the grid.
    
    '''

    def __init__(self, model, widget, scrollbar):
        self.model = model
        self.widget = widget
        self.scrollbar = scrollbar

        # Set up the widget as a drag target.        
        self.widget.drag_dest_set(gtk.DEST_DEFAULT_ALL,
                                  [(dimbola.TAGIDS_TYPE, 
                                    gtk.TARGET_SAME_APP, 0)],
                                  gtk.gdk.ACTION_COPY)

        # Which photoids are currently drawn as selected?
        self.drawn_selected = list()
        
        # Connect to the model's change signals so we automatically
        # redraw the grid.
        self.model.connect('photoids-changed', self.refresh_thumbnails)
        self.model.connect('selection-changed', self.refresh_selection)

    def resize_scrollbar(self):
        '''Change the scrollbar's adjustment so it matches the model.'''
        adj = self.scrollbar.get_adjustment()
        
        lower = 0
        upper = max(0, self.model.vertical_pixels - self.model.widget_height)
        step = self.model.distance
        page = self.model.widget_height
        
        adj.set_all(lower=lower,
                    upper=upper,
                    step_increment=step,
                    page_increment=page,
                    page_size=page)

    def coords_to_photoid(self, x, y):
        '''Convert from widget's x,y to photoid.'''
        y += int(self.scrollbar.get_value())
        for i, photoid in enumerate(self.model.photoids):
            x1, y1 = self.model.thumbnail_pos(i)
            if (x >= x1 and x < x1 + self.model.distance and
                y >= y1 and y < y1 + self.model.distance):
                return photoid
        return None

    def refresh_selection(self, *args):
        '''Update the selection on screen.'''
        for photoid in set(self.drawn_selected + self.model.selected):
            self.draw_thumbnail(photoid)
        self.drawn_selected = self.model.selected[:]

    def refresh_thumbnails(self, *args):
        '''Update all thumbnails on screen.'''
        if self.widget.window:
            # We only do this if we're mapped and can draw.
            self.widget.window.clear()
            for photoid in self.model.photoids:
                self.draw_thumbnail(photoid)
            self.draw_focus_indicator()

    def draw_focus_indicator(self):
        '''Draw a visual focus indicator, if we have focus.'''
        
        if self.widget.flags() & gtk.HAS_FOCUS:
            width, height = self.widget.window.get_size()
            self.widget.get_style().paint_focus(self.widget.window,
                                                self.widget.state,
                                                None,
                                                None,
                                                None,
                                                0, 0,
                                                width, height)

    def highlight_thumbnail(self, photoid):
        '''Drag thumbnail with a drag destination highlight.'''
        self.draw_thumbnail(photoid, highlight=True)

    def draw_thumbnail(self, photoid, highlight=False):
        '''Draw thumbnail onto grid view.'''
        
        thumb = self.model.thumbnails.get(photoid)
        if not thumb:
            # We don't have the thumbnail yet. Can't draw it.
            return

        w = self.widget.window

        style = self.widget.get_style()
        if highlight:
            bg = style.bg_gc[gtk.STATE_PRELIGHT]
            fg = style.fg_gc[gtk.STATE_PRELIGHT]
        else:
            bg = style.bg_gc[gtk.STATE_NORMAL]
            fg = style.fg_gc[gtk.STATE_NORMAL]

        thumb = dimbola.scale_pixbuf(thumb, self.model.maxdim, 
                                     self.model.maxdim)
        thumb = dimbola.rotate_pixbuf(thumb, self.model.angles.get(photoid, 0))
        i = self.model.photoids.index(photoid)
        x, y = self.model.thumbnail_pos(i)

        y0 = int(self.scrollbar.get_value())
        if y + self.model.distance < y0:
            return
        if y >= y0 + self.model.widget_height:
            return

        if photoid in self.model.selected:
            gc = style.bg_gc[gtk.STATE_SELECTED]
        else:
            gc = bg
        w.draw_rectangle(gc, True, x, y - y0, self.model.distance, 
                         self.model.distance)

        xdelta = (self.model.distance - thumb.get_width()) / 2
        ydelta = (self.model.distance - thumb.get_height()) / 2
        w.draw_pixbuf(fg, thumb, 0, 0, x + xdelta, y + ydelta - y0)
        if highlight:
            w.draw_rectangle(fg, False, x, y - y0, self.model.distance - 1,
                             self.model.distance - 1)


class Grid(object): # pragma: no cover

    '''This is the MVC controller of the thumbnail grid.
    
    This class takes care of responding to events and signals related
    to the grid. The rest of the world will interface with the grid
    via this class.
    
    '''

    def __init__(self, mwc):
        mwc.connect('setup-widgets', self.init)
        mwc.connect('photo-meta-changed', self.on_photo_meta_changed)

    def init(self, mwc):
        '''Initialize this object after mwc's setup-widgets signal emitted.'''
        
        self.mwc = mwc
        
        self.model = GridModel()

        self.box = mwc.widgets['grid_vbox']
        drawingarea = mwc.widgets['thumbnail_drawingarea']
        scrollbar = mwc.widgets['thumbnail_vscrollbar']
        self.view = GridView(self.model, drawingarea, scrollbar)

        self.scale = mwc.widgets['thumbnail_scale']
        self.scale.set_range(50, 300)
        self.scale.set_increments(10, 25)
        self.scale.set_value(200)
        self.model.scale_value = self.scale.get_value()

        self.drag_dest = None

    def on_photo_meta_changed(self, mwc, photoid):
        with mwc.db:
            a, b, c, rotate = mwc.db.get_basic_photo_metadata(photoid)
            thumbnail = mwc.db.get_thumbnail(photoid)
        self.model.set_angle(photoid, rotate)

    def on_thumbnail_drawingarea_configure_event(self, widget, event):
        self.model.widget_width = event.width
        self.model.widget_height = event.height
    
    def on_thumbnail_drawingarea_expose_event(self, *args):
        if self.model.widget_height is not None:
            self.view.refresh_thumbnails()
            self.view.resize_scrollbar()

    def on_thumbnail_scale_value_changed(self, *args):
        self.model.scale_value = self.scale.get_value()
        if self.model.widget_height is not None:
            self.view.refresh_thumbnails()
            self.view.resize_scrollbar()
        
    def on_thumbnail_vscrollbar_value_changed(self, vscrollbar):
        self.view.refresh_thumbnails()

    def on_thumbnail_drawingarea_scroll_event(self, widget, event):
        adj = self.view.scrollbar.get_adjustment()
        value = adj.get_value()
        step = adj.get_step_increment()
        if event.direction == gtk.gdk.SCROLL_UP:
            value = max(0, value - step)
        else:
            value = min(adj.get_upper(), value + step)
        adj.set_value(value)

    def on_thumbnail_drawingarea_button_press_event(self, widget, event):
        '''Let user change thumbnail selection with mouse.'''

        widget.grab_focus()

        if event.button != 1:
            return False
        
        shift = (event.state & gtk.gdk.SHIFT_MASK) == gtk.gdk.SHIFT_MASK
        ctrl = (event.state & gtk.gdk.CONTROL_MASK) == gtk.gdk.CONTROL_MASK
        photoid = self.view.coords_to_photoid(event.x, event.y)
        photoids = self.model.photoids
        selected = self.model.selected

        if event.type == gtk.gdk._2BUTTON_PRESS:
            self.mwc.widgets['view_photo_menuitem'].set_active(True)
            return False
        
        if shift and photoid is not None:
            # Extend current selection by selecting everything from oldest
            # selection to the current one, inclusive, and only those.
            if selected:
                del selected[1:]
                index0 = photoids.index(selected[0])
                index = photoids.index(photoid)
                if index < index0:
                    for i in range(index, index0):
                        selected.append(photoids[i])
                else:
                    for i in range(index0 + 1, index + 1):
                        selected.append(photoids[i])
            else:
                # No current selection, just select current.
                selected.append(photoid)
        elif ctrl and photoid is not None:
            # Add or remove current photo to selection.
            if photoid in selected:
                selected.remove(photoid)
            else:
                selected.append(photoid)
        elif not shift and not ctrl:
            # Just select current one.
            del selected[:]
            if photoid is not None:
                selected.append(photoid)

        self.model.selected = selected

    def on_thumbnail_drawingarea_drag_leave(self, w, dc, timestamp):
        if self.drag_dest is not None:
            self.view.draw_thumbnail(self.drag_dest)
            self.drag_dest = None

    def on_thumbnail_drawingarea_drag_motion(self, w, dc, x, y, timestamp):
        if self.drag_dest is not None:
            self.view.draw_thumbnail(self.drag_dest)
            self.drag_dest = None
        self.drag_dest = self.view.coords_to_photoid(x, y)
        if self.drag_dest is None:
            return False
        else:
            dc.drag_status(gtk.gdk.ACTION_COPY, timestamp)
            self.view.highlight_thumbnail(self.drag_dest)
            return True
            
    def on_thumbnail_drawingarea_drag_data_received(self, *args):
        w, dc, x, y, data, info, timestamp = args
        photoid = self.view.coords_to_photoid(x, y)
        if photoid is not None:
            tagids = dimbola.decode_dnd_tagids(data.data)
            with self.mwc.db:
                old_tagids = set(self.mwc.db.get_tagids(photoid))
                for tagid in tagids:
                    if tagid not in old_tagids:
                        self.mwc.db.add_tagid(photoid, tagid)
            dc.finish(True, False, timestamp)
            self.model.selection_changed()

    def request_rating(self, stars):
        self.mwc.emit('photo-rating-requested', stars)

    def on_thumbnail_drawingarea_key_press_event(self, widget, event):
        if event.type == gtk.gdk.KEY_PRESS:
            bindings = {
                gtk.keysyms.Left: self.model.select_previous,
                gtk.keysyms.Right: self.model.select_next,
                '0': lambda *args: self.request_rating(0),
                '1': lambda *args: self.request_rating(1),
                '2': lambda *args: self.request_rating(2),
                '3': lambda *args: self.request_rating(3),
                '4': lambda *args: self.request_rating(4),
                '5': lambda *args: self.request_rating(5),
            }
            if event.keyval in bindings:
                bindings[event.keyval]()
                return True
            elif event.string in bindings:
                bindings[event.string]()
                return True
        return False

    def on_view_grid_menuitem_activate(self, radio):
        if radio.get_active():
            self.box.show()
        else:
            self.box.hide()