Update Metadata Viewer gramplet to use a tree view
svn: r17892
This commit is contained in:
parent
59f4dc3fe9
commit
e305834f5a
@ -1,3 +1,5 @@
|
|||||||
|
#!/usr/bin/env python
|
||||||
|
# -*- coding: utf-8 -*-
|
||||||
# Gramps - a GTK+/GNOME based genealogy program
|
# Gramps - a GTK+/GNOME based genealogy program
|
||||||
#
|
#
|
||||||
# Copyright (C) 2011 Nick Hall
|
# Copyright (C) 2011 Nick Hall
|
||||||
@ -20,7 +22,7 @@
|
|||||||
# $Id$
|
# $Id$
|
||||||
#
|
#
|
||||||
|
|
||||||
from ListModel import ListModel, NOSORT
|
from ListModel import ListModel
|
||||||
from gen.plug import Gramplet
|
from gen.plug import Gramplet
|
||||||
from gen.ggettext import gettext as _
|
from gen.ggettext import gettext as _
|
||||||
import gen.lib
|
import gen.lib
|
||||||
@ -28,15 +30,60 @@ import DateHandler
|
|||||||
import datetime
|
import datetime
|
||||||
import gtk
|
import gtk
|
||||||
import Utils
|
import Utils
|
||||||
import sys
|
|
||||||
import pyexiv2
|
import pyexiv2
|
||||||
|
|
||||||
# v0.1 has a different API to v0.2 and above
|
# v0.1 has a different API to v0.2 and above
|
||||||
if hasattr(pyexiv2, 'version_info'):
|
if hasattr(pyexiv2, 'version_info'):
|
||||||
LesserVersion = False
|
OLD_API = False
|
||||||
else:
|
else:
|
||||||
# version_info attribute does not exist prior to v0.2.0
|
# version_info attribute does not exist prior to v0.2.0
|
||||||
LesserVersion = True
|
OLD_API = True
|
||||||
|
|
||||||
|
def format_datetime(exif_dt):
|
||||||
|
"""
|
||||||
|
Convert a python datetime object into a string for display, using the
|
||||||
|
standard Gramps date format.
|
||||||
|
"""
|
||||||
|
if type(exif_dt) != datetime.datetime:
|
||||||
|
return ''
|
||||||
|
date_part = gen.lib.Date()
|
||||||
|
date_part.set_yr_mon_day(exif_dt.year, exif_dt.month, exif_dt.day)
|
||||||
|
date_str = DateHandler.displayer.display(date_part)
|
||||||
|
time_str = _('%(hr)02d:%(min)02d:%(sec)02d') % {'hr': exif_dt.hour,
|
||||||
|
'min': exif_dt.minute,
|
||||||
|
'sec': exif_dt.second}
|
||||||
|
return _('%(date)s %(time)s') % {'date': date_str, 'time': time_str}
|
||||||
|
|
||||||
|
def format_gps(tag_value):
|
||||||
|
"""
|
||||||
|
Convert a (degrees, minutes, seconds) tuple into a string for display.
|
||||||
|
"""
|
||||||
|
return "%d°%02d'%05.2f\"" % (tag_value[0], tag_value[1], tag_value[2])
|
||||||
|
|
||||||
|
IMAGE = _('Image')
|
||||||
|
CAMERA = _('Camera')
|
||||||
|
GPS = _('GPS')
|
||||||
|
|
||||||
|
TAGS = [(IMAGE, 'Exif.Image.ImageDescription', None, None),
|
||||||
|
(IMAGE, 'Exif.Image.Rating', None, None),
|
||||||
|
(IMAGE, 'Exif.Photo.DateTimeOriginal', None, format_datetime),
|
||||||
|
(IMAGE, 'Exif.Image.Artist', None, None),
|
||||||
|
(IMAGE, 'Exif.Image.Copyright', None, None),
|
||||||
|
(IMAGE, 'Exif.Photo.PixelXDimension', None, None),
|
||||||
|
(IMAGE, 'Exif.Photo.PixelYDimension', None, None),
|
||||||
|
(CAMERA, 'Exif.Image.Make', None, None),
|
||||||
|
(CAMERA, 'Exif.Image.Model', None, None),
|
||||||
|
(CAMERA, 'Exif.Photo.FNumber', None, None),
|
||||||
|
(CAMERA, 'Exif.Photo.ExposureTime', None, None),
|
||||||
|
(CAMERA, 'Exif.Photo.ISOSpeedRatings', None, None),
|
||||||
|
(CAMERA, 'Exif.Photo.FocalLength', None, None),
|
||||||
|
(CAMERA, 'Exif.Photo.MeteringMode', None, None),
|
||||||
|
(CAMERA, 'Exif.Photo.ExposureProgram', None, None),
|
||||||
|
(CAMERA, 'Exif.Photo.Flash', None, None),
|
||||||
|
(GPS, 'Exif.GPSInfo.GPSLatitude',
|
||||||
|
'Exif.GPSInfo.GPSLatitudeRef', format_gps),
|
||||||
|
(GPS, 'Exif.GPSInfo.GPSLongitude',
|
||||||
|
'Exif.GPSInfo.GPSLongitudeRef', format_gps)]
|
||||||
|
|
||||||
class MetadataViewer(Gramplet):
|
class MetadataViewer(Gramplet):
|
||||||
"""
|
"""
|
||||||
@ -56,13 +103,14 @@ class MetadataViewer(Gramplet):
|
|||||||
top = gtk.TreeView()
|
top = gtk.TreeView()
|
||||||
titles = [(_('Key'), 1, 250),
|
titles = [(_('Key'), 1, 250),
|
||||||
(_('Value'), 2, 350)]
|
(_('Value'), 2, 350)]
|
||||||
self.model = ListModel(top, titles)
|
self.model = ListModel(top, titles, list_mode="tree")
|
||||||
return top
|
return top
|
||||||
|
|
||||||
def main(self):
|
def main(self):
|
||||||
active_handle = self.get_active('Media')
|
active_handle = self.get_active('Media')
|
||||||
media = self.dbstate.db.get_object_from_handle(active_handle)
|
media = self.dbstate.db.get_object_from_handle(active_handle)
|
||||||
|
|
||||||
|
self.sections = {}
|
||||||
self.model.clear()
|
self.model.clear()
|
||||||
if media:
|
if media:
|
||||||
self.display_exif_tags(media)
|
self.display_exif_tags(media)
|
||||||
@ -78,12 +126,13 @@ class MetadataViewer(Gramplet):
|
|||||||
"""
|
"""
|
||||||
Return True if the gramplet has data, else return False.
|
Return True if the gramplet has data, else return False.
|
||||||
"""
|
"""
|
||||||
|
# pylint: disable-msg=E1101
|
||||||
if media is None:
|
if media is None:
|
||||||
return False
|
return False
|
||||||
|
|
||||||
full_path = Utils.media_path_full(self.dbstate.db, media.get_path())
|
full_path = Utils.media_path_full(self.dbstate.db, media.get_path())
|
||||||
|
|
||||||
if LesserVersion: # prior to v0.2.0
|
if OLD_API: # prior to v0.2.0
|
||||||
try:
|
try:
|
||||||
metadata = pyexiv2.Image(full_path)
|
metadata = pyexiv2.Image(full_path)
|
||||||
except IOError:
|
except IOError:
|
||||||
@ -107,24 +156,32 @@ class MetadataViewer(Gramplet):
|
|||||||
"""
|
"""
|
||||||
Display the exif tags.
|
Display the exif tags.
|
||||||
"""
|
"""
|
||||||
|
# pylint: disable-msg=E1101
|
||||||
full_path = Utils.media_path_full(self.dbstate.db, media.get_path())
|
full_path = Utils.media_path_full(self.dbstate.db, media.get_path())
|
||||||
|
|
||||||
if LesserVersion: # prior to v0.2.0
|
if OLD_API: # prior to v0.2.0
|
||||||
try:
|
try:
|
||||||
metadata = pyexiv2.Image(full_path)
|
metadata = pyexiv2.Image(full_path)
|
||||||
except IOError:
|
except IOError:
|
||||||
self.set_has_data(False)
|
self.set_has_data(False)
|
||||||
return
|
return
|
||||||
metadata.readMetadata()
|
metadata.readMetadata()
|
||||||
for key in metadata.exifKeys():
|
for section, key, key2, func in TAGS:
|
||||||
label = metadata.tagDetails(key)[0]
|
if key in metadata.exifKeys():
|
||||||
if key in ("Exif.Image.DateTime",
|
if section not in self.sections:
|
||||||
"Exif.Photo.DateTimeOriginal",
|
node = self.model.add([section, ''])
|
||||||
"Exif.Photo.DateTimeDigitized"):
|
self.sections[section] = node
|
||||||
human_value = format_datetime(metadata[key])
|
else:
|
||||||
else:
|
node = self.sections[section]
|
||||||
human_value = metadata.interpretedExifValue(key)
|
label = metadata.tagDetails(key)[0]
|
||||||
self.model.add((label, human_value))
|
if func:
|
||||||
|
human_value = func(metadata[key])
|
||||||
|
else:
|
||||||
|
human_value = metadata.interpretedExifValue(key)
|
||||||
|
if key2:
|
||||||
|
human_value += ' ' + metadata.interpretedExifValue(key2)
|
||||||
|
self.model.add((label, human_value), node=node)
|
||||||
|
self.model.tree.expand_all()
|
||||||
|
|
||||||
else: # v0.2.0 and above
|
else: # v0.2.0 and above
|
||||||
metadata = pyexiv2.ImageMetadata(full_path)
|
metadata = pyexiv2.ImageMetadata(full_path)
|
||||||
@ -133,29 +190,21 @@ class MetadataViewer(Gramplet):
|
|||||||
except IOError:
|
except IOError:
|
||||||
self.set_has_data(False)
|
self.set_has_data(False)
|
||||||
return
|
return
|
||||||
for key in metadata.exif_keys:
|
for section, key, key2, func in TAGS:
|
||||||
tag = metadata[key]
|
if key in metadata.exif_keys:
|
||||||
if key in ("Exif.Image.DateTime",
|
if section not in self.sections:
|
||||||
"Exif.Photo.DateTimeOriginal",
|
node = self.model.add([section, ''])
|
||||||
"Exif.Photo.DateTimeDigitized"):
|
self.sections[section] = node
|
||||||
human_value = format_datetime(tag.value)
|
else:
|
||||||
else:
|
node = self.sections[section]
|
||||||
human_value = tag.human_value
|
tag = metadata[key]
|
||||||
self.model.add((tag.label, human_value))
|
if func:
|
||||||
|
human_value = func(tag.value)
|
||||||
self.set_has_data(self.model.count > 0)
|
else:
|
||||||
|
human_value = tag.human_value
|
||||||
|
if key2:
|
||||||
|
human_value += ' ' + metadata[key2].human_value
|
||||||
|
self.model.add((tag.label, human_value), node=node)
|
||||||
|
self.model.tree.expand_all()
|
||||||
|
|
||||||
def format_datetime(exif_dt):
|
self.set_has_data(self.model.count > 0)
|
||||||
"""
|
|
||||||
Convert a python datetime object into a string for display, using the
|
|
||||||
standard Gramps date format.
|
|
||||||
"""
|
|
||||||
if type(exif_dt) != datetime.datetime:
|
|
||||||
return ''
|
|
||||||
date_part = gen.lib.Date()
|
|
||||||
date_part.set_yr_mon_day(exif_dt.year, exif_dt.month, exif_dt.day)
|
|
||||||
date_str = DateHandler.displayer.display(date_part)
|
|
||||||
time_str = _('%(hr)02d:%(min)02d:%(sec)02d') % {'hr': exif_dt.hour,
|
|
||||||
'min': exif_dt.minute,
|
|
||||||
'sec': exif_dt.second}
|
|
||||||
return _('%(date)s %(time)s') % {'date': date_str, 'time': time_str}
|
|
||||||
|
Loading…
Reference in New Issue
Block a user