summaryrefslogtreecommitdiff
path: root/trunk/dimbola/plugins/phototags_plugin.py
blob: e2fc1d2fdf8996e40d65f2bb0d200b1a8d36b37e (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
# 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 gtk

import dimbola


class PhotoTags(dimbola.Plugin):

    '''Show/edit selected photo's tags.'''

    def __init__(self, mwc):
        self.mwc = mwc
        mwc.connect('setup-widgets', self.setup_widgets)
        
    def setup_widgets(self, mwc):
        self.textview = mwc.widgets['tags_textview']
        self.popup = mwc.widgets['tags_textview_popup']
        self.remove_menuitem = mwc.widgets['remove_photo_tag_menuitem']
        self.taglist = dimbola.Taglist(self.textview, self.popup, 
                                       self.prepare_popup)
        self.photoid = None
        
    def enable(self):
        self.enable_signal(self.mwc.grid.model, 'selection-changed', 
                           self.selection_changed)
        self.enable_signal(self.mwc, 'tagtree-changed', 
                           lambda *args: self.refresh_tags())
        self.mwc.add_to_sidebar('right_sidebar', 'photo_tags_expander')

    def disable(self):
        self.disable_signals()
        self.mwc.remove_from_sidebar('right_sidebar', 'photo_tags_expander')
    
    def refresh_tags(self):
        with self.mwc.db:
            tags = []
            for tagid in self.mwc.db.get_tagids(self.photoid):
                tagname = self.mwc.db.get_tagname(tagid)
                if tagname is not None:
                    tags.append((tagid, tagname))
        tags.sort()
        self.taglist.set_tags(self.photoid, tags)

    def selection_changed(self, model):
        if self.mwc.db:
            if len(model.selected) == 1:
                self.photoid = model.selected[0]
                self.refresh_tags()
            else:
                self.taglist.clear()
                self.photoid = None
        else:
            self.photoid = None
    
    def tags_textview_is_sensitive(self):
        return len(self.mwc.grid.model.selected) == 1
        
    def prepare_popup(self):
        self.remove_menuitem.set_sensitive(
            self.taglist.buf.get_has_selection())
        
    def on_tags_textview_button_press_event(self, widget, event):
        return self.taglist.button_press_event(widget, event)
            
    def on_remove_photo_tag_menuitem_activate(self, *args):
        photoid, tagid = self.taglist.selected_tag()
        if photoid is not None:
            with self.mwc.db:
                self.mwc.db.remove_tagid(photoid, tagid)
            self.refresh_tags()

    def on_tags_textview_drag_motion(self, *args):
        return self.taglist.drag_motion(*args)

    def on_tags_textview_drag_leave(self, *args):
        return self.taglist.drag_leave(*args)

    def on_tags_textview_drag_data_received(self, *args):
        if self.photoid is None:
            return

        tagids = self.taglist.drag_data_received(*args)
        with self.mwc.db:
            for tagid in tagids:
                self.mwc.db.add_tagid(self.photoid, tagid)
        self.refresh_tags()