summaryrefslogtreecommitdiff
path: root/trunk/dimbola/plugins/photoinfo_plugin.py
blob: e5bb4fba23ec45b38a7f417b4cc4fae607c5d9cd (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
# 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 os

import gtk
import pyexiv2

import dimbola


class PhotoInfoViewer(dimbola.Plugin):

    '''Show information about the selected photo in the right sidebar.'''

    def __init__(self, mwc):
        self.mwc = mwc
        self.photoid = None
        
    def enable(self):
        self.enable_signal(self.mwc.grid.model, 'selection-changed', 
                           self.remember_photo)
        self.enable_signal(self.mwc, 'photo-meta-changed', 
                           self.remember_photo)
        self.mwc.add_to_sidebar('right_sidebar', 'basic_info_expander')
        
    def disable(self):
        self.disable_signals()
        self.mwc.remove_from_sidebar('right_sidebar', 'basic_info_expander')

    def exifvalue(self, exifname):
        if self.photoid is None:
            return ''
        else:
            return self.mwc.db.get_exif(self.photoid, exifname) or ''

    @property
    def foldername(self):
        if self.photoid is None:
            return ''
        else:
            folderid, basename, rating, rotate = \
                self.mwc.db.get_basic_photo_metadata(self.photoid)
            foldername = self.mwc.db.get_folder_name(folderid)
            if foldername:
                foldername = os.path.basename(foldername)
            return foldername or ''

    @property
    def filename(self):
        if self.photoid is None:
            return ''
        else:
            folderid, basename, rating, rotate = \
                self.mwc.db.get_basic_photo_metadata(self.photoid)
            return basename

    @property
    def rating(self):
        if self.photoid is None:
            return ''
        else:
            folderid, basename, rating, rotate = \
                self.mwc.db.get_basic_photo_metadata(self.photoid)
            return '*' * rating

    def draw_rating_stars(self):
        w = self.mwc.widgets['photo_info_rating']
        gc = w.get_style().fg_gc[gtk.STATE_NORMAL]
        x = 0
        y = 0
        width, dim = w.window.get_size()
        with self.mwc.db:
            dimbola.draw_stars(len(self.rating), w.window, gc, x, y, dim)

    def on_photo_info_rating_expose_event(self, w, event):
        self.draw_rating_stars()

    def remember_photo(self, *args):
        if self.mwc.db is None:
            return

        if len(self.mwc.grid.model.selected) == 1:
            self.photoid = self.mwc.grid.model.selected[0]
        else:
            self.photoid = None
        with self.mwc.db:
            exiftable = {
                'photo_info_camera': 'Exif.Image.Model',
                'photo_info_datetime': 'Exif.Image.DateTime',
                'photo_info_shutter': 'Exif.Photo.ExposureTime',
                'photo_info_aperture': 'Exif.Photo.ApertureValue',
                'photo_info_iso': 'Exif.Photo.ISOSpeedRatings',
                'photo_info_focal_length': 'Exif.Photo.FocalLength',
            }
            for widget_name, exifname in exiftable.iteritems():
                value = self.exifvalue(exifname)
                self.mwc.widgets[widget_name].set_text(value)

            misctable = {
                'photo_info_photoid': '%s' % (self.photoid or ''),
                'photo_info_folder': self.foldername,
                'photo_info_filename': self.filename,
            }
            for widget_name, value in misctable.iteritems():
                self.mwc.widgets[widget_name].set_text(value)
        self.draw_rating_stars()