gramps/src/MediaView.py

415 lines
15 KiB
Python
Raw Normal View History

2002-10-20 19:55:16 +05:30
#
# Gramps - a GTK+/GNOME based genealogy program
#
# Copyright (C) 2001-2004 Donald N. Allingham
2002-10-20 19:55:16 +05:30
#
# 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#
# $Id$
2002-10-20 19:55:16 +05:30
#-------------------------------------------------------------------------
#
# standard python modules
#
#-------------------------------------------------------------------------
import string
import os
2002-10-20 19:55:16 +05:30
#-------------------------------------------------------------------------
#
# GTK/Gnome modules
#
#-------------------------------------------------------------------------
import gtk
import gtk.gdk
#-------------------------------------------------------------------------
#
# gramps modules
#
#-------------------------------------------------------------------------
import RelLib
2002-10-20 19:55:16 +05:30
import Utils
import GrampsCfg
import const
import ImageSelect
2002-10-20 19:55:16 +05:30
import RelImage
import DisplayModels
import GrampsMime
2002-10-20 19:55:16 +05:30
from QuestionDialog import QuestionDialog, ErrorDialog, WarningDialog
2002-10-20 19:55:16 +05:30
#-------------------------------------------------------------------------
#
# internationalization
#
#-------------------------------------------------------------------------
2003-08-17 07:44:33 +05:30
from gettext import gettext as _
2002-10-20 19:55:16 +05:30
column_names = [
_('Title'),
_('ID'),
_('Type'),
_('Path'),
_('Last Changed'),
]
2002-10-20 19:55:16 +05:30
#-------------------------------------------------------------------------
#
# MediaView
#
#-------------------------------------------------------------------------
class MediaView:
def __init__(self,parent,db,glade,update):
self.parent = parent
2002-10-20 19:55:16 +05:30
self.db = db
self.list = glade.get_widget("media_list")
self.mid = glade.get_widget("mid")
self.mtype = glade.get_widget("mtype")
self.mdesc = glade.get_widget("mdesc")
self.mpath = glade.get_widget("mpath")
self.mdetails = glade.get_widget("mdetails")
self.preview = glade.get_widget("preview")
self.topWindow = glade.get_widget("gramps")
self.renderer = gtk.CellRendererText()
2002-10-20 19:55:16 +05:30
# self.model = gtk.TreeModelSort(DisplayModels.MediaModel(self.db))
self.model = DisplayModels.MediaModel(self.db)
2002-10-20 19:55:16 +05:30
self.selection = self.list.get_selection()
self.list.set_model(self.model)
t = [ ('STRING', 0, 0),
('text/plain',0,0),
('text/uri-list',0,2),
('application/x-rootwin-drop',0,1)]
self.list.drag_source_set(gtk.gdk.BUTTON1_MASK|gtk.gdk.BUTTON3_MASK,
t,gtk.gdk.ACTION_COPY)
self.list.drag_dest_set(gtk.DEST_DEFAULT_ALL,
t,gtk.gdk.ACTION_COPY|gtk.gdk.ACTION_MOVE)
2003-05-17 08:50:50 +05:30
self.list.connect("drag_data_received", self.on_drag_data_received)
self.list.connect("drag_data_get", self.on_drag_data_get)
self.list.connect("drag_begin", self.on_drag_begin)
2002-10-20 19:55:16 +05:30
self.update = update
self.list.connect('button-press-event',self.on_button_press_event)
self.list.connect('key-press-event',self.key_press)
2002-10-20 19:55:16 +05:30
self.selection.connect('changed',self.on_select_row)
if not RelImage.is_cnv():
WarningDialog(_("Thumbnails not available")
,_("There is no suitable tool to generate thumbnails for the images. "
"If you would like to enable this feature, "
"install ImageMagick, available at http://www.imagemagick.org/"))
self.columns = []
self.build_columns()
self.build_tree()
def build_columns(self):
for column in self.columns:
self.list.remove_column(column)
column = gtk.TreeViewColumn(_('Title'), self.renderer,text=0)
column.set_resizable(gtk.TRUE)
column.set_min_width(225)
self.list.append_column(column)
self.columns = [column]
index = 1
for pair in self.parent.db.get_media_column_order():
if not pair[0]:
continue
name = column_names[pair[1]]
column = gtk.TreeViewColumn(name, self.renderer, text=pair[1])
column.set_resizable(gtk.TRUE)
column.set_min_width(75)
self.columns.append(column)
self.list.append_column(column)
index += 1
def change_db(self,db):
self.db = db
self.build_columns()
self.build_tree()
2003-04-04 11:18:25 +05:30
def build_tree(self):
self.model = gtk.TreeModelSort(DisplayModels.MediaModel(self.parent.db))
# self.model = DisplayModels.MediaModel(self.parent.db)
self.list.set_model(self.model)
self.selection = self.list.get_selection()
2002-10-20 19:55:16 +05:30
def on_select_row(self,obj):
fexists = 1
store,node = self.selection.get_selected()
if not node:
return
handle = store.get_value(node,5)
2002-10-20 19:55:16 +05:30
mobj = self.db.get_object_from_handle(handle)
mtype = mobj.get_mime_type()
type_name = Utils.get_mime_description(mtype)
path = mobj.get_path()
thumb_path = Utils.thumb_path(self.db.get_save_path(),mobj)
2002-10-20 19:55:16 +05:30
pexists = os.path.exists(path)
if pexists and os.path.exists(thumb_path):
self.preview.set_from_pixbuf(gtk.gdk.pixbuf_new_from_file(thumb_path))
else:
icon_image = gtk.gdk.pixbuf_new_from_file(Utils.find_icon(mtype))
2003-02-05 19:23:00 +05:30
self.preview.set_from_pixbuf(icon_image)
2002-10-20 19:55:16 +05:30
if not pexists:
fexists = 0
self.mid.set_text(mobj.get_gramps_id())
2002-10-20 19:55:16 +05:30
self.mtype.set_text(type_name)
self.mdesc.set_text(mobj.get_description())
2002-10-20 19:55:16 +05:30
if len(path) == 0 or fexists == 0:
self.mpath.set_text(_("The file no longer exists"))
elif path[0] == "/":
self.mpath.set_text(path)
else:
self.mpath.set_text(_("<local>"))
2002-10-20 19:55:16 +05:30
self.mdetails.set_text(Utils.get_detail_text(mobj,0))
def on_button_press_event(self,obj,event):
if event.type == gtk.gdk._2BUTTON_PRESS and event.button == 1:
self.on_edit_clicked(obj)
return 1
2002-10-20 19:55:16 +05:30
elif event.button == 3:
self.build_context_menu(event)
return 1
return 0
def key_press(self,obj,event):
if event.keyval == gtk.gdk.keyval_from_name("Return") \
and not event.state:
self.on_edit_clicked(obj)
return 1
return 0
def build_context_menu(self,event):
menu = gtk.Menu()
menu.set_title(_("Media Object"))
store,node = self.selection.get_selected()
if node:
handle = store.get_value(node,5)
obj = self.db.get_object_from_handle(handle)
self.obj = obj
mime_type = obj.get_mime_type()
Utils.add_menuitem(menu,_("View in the default viewer"),None,
self.popup_view_photo)
if mime_type[0:5] == "image":
Utils.add_menuitem(menu,_("Edit with the GIMP"),
2002-10-20 19:55:16 +05:30
None,self.popup_edit_photo)
item = gtk.MenuItem()
item.show()
menu.append(item)
sel_sensitivity = 1
else:
sel_sensitivity = 0
entries = [
(gtk.STOCK_ADD, self.on_add_clicked,1),
(gtk.STOCK_REMOVE, self.on_delete_clicked,sel_sensitivity),
(_("Edit properties"), self.on_edit_clicked,sel_sensitivity),
]
for stock_id,callback,sensitivity in entries:
item = gtk.ImageMenuItem(stock_id)
if callback:
item.connect("activate",callback)
item.set_sensitive(sensitivity)
item.show()
menu.append(item)
menu.popup(None,None,None,event.button,event.time)
2002-10-20 19:55:16 +05:30
def popup_view_photo(self, obj):
Utils.view_photo(self.obj)
def popup_edit_photo(self, obj):
if os.fork() == 0:
os.execvp(const.editor,[const.editor, self.obj.get_path()])
2002-10-20 19:55:16 +05:30
def popup_convert_to_private(self, obj):
path = self.db.get_save_path()
handle = self.obj.get_handle()
name = RelImage.import_media_object(self.obj.get_path(),path,handle)
2002-10-20 19:55:16 +05:30
if name:
self.obj.set_path(name)
self.load_media()
2002-10-20 19:55:16 +05:30
def popup_change_description(self, obj):
ImageSelect.GlobalMediaProperties(self.db,self.obj,self.load_media,
self,self.topWindow)
2002-10-20 19:55:16 +05:30
def load_media(self):
pass
2002-10-20 19:55:16 +05:30
def on_add_clicked(self,obj):
2002-10-20 19:55:16 +05:30
"""Add a new media object to the media list"""
import AddMedia
am = AddMedia.AddMediaObject(self.db,self.build_tree)
am.run()
2002-10-20 19:55:16 +05:30
def on_edit_clicked(self,obj):
2002-10-20 19:55:16 +05:30
"""Edit the properties of an existing media object in the media list"""
list_store, node = self.selection.get_selected()
if node:
handle = list_store.get_value(node,5)
obj = self.db.get_object_from_handle(handle)
ImageSelect.GlobalMediaProperties(self.db,obj,self.load_media,
self,self.topWindow)
2002-10-20 19:55:16 +05:30
def on_delete_clicked(self,obj):
store,node = self.selection.get_selected()
if not node:
2002-10-20 19:55:16 +05:30
return
handle = store.get_value(node,5)
mobj = self.db.get_object_from_handle(handle)
2002-10-20 19:55:16 +05:30
if self.is_object_used(mobj):
ans = ImageSelect.DeleteMediaQuery(mobj,self.db,self.build_tree)
2003-02-24 10:21:57 +05:30
QuestionDialog(_('Delete Media Object?'),
_('This media object is currently being used. '
'If you delete this object, it will be removed '
'from the database and from all records that '
'reference it.'),
_('_Delete Media Object'),
ans.query_response)
2002-10-20 19:55:16 +05:30
else:
trans = self.db.transaction_begin()
self.db.remove_object(mobj.get_handle(),trans)
self.db.transaction_commit(trans,_("Remove Media Object"))
self.build_tree()
2002-10-20 19:55:16 +05:30
def is_object_used(self,mobj):
for family_handle in self.db.get_family_handles():
* PeopleModel.py: simplify model interface * EditPerson.py: get_family_from_handle fixes * EditSource.py: get_family_from_handle fixes * GraphLayout.py: get_family_from_handle fixes * ImageSelect.py: get_family_from_handle fixes * MediaView.py: get_family_from_handle fixes * MergeData.py: get_family_from_handle fixes * PlaceView.py: get_family_from_handle fixes * ReadXML.py: get_family_from_handle fixes * RelLib.py: get_family_from_handle fixes * Relationship.py: get_family_from_handle fixes * SelectChild.py: get_family_from_handle fixes * SourceView.py: get_family_from_handle fixes * SubstKeywords.py: get_family_from_handle fixes * WriteXML.py: get_family_from_handle fixes * gramps_main.py: get_family_from_handle fixes * plugins/AncestorChart.py: get_family_from_handle fixes * plugins/AncestorChart2.py: get_family_from_handle fixes * plugins/AncestorReport.py: get_family_from_handle fixes * plugins/Ancestors.py: get_family_from_handle fixes * plugins/Check.py: get_family_from_handle fixes * plugins/CountAncestors.py: get_family_from_handle fixes * plugins/Desbrowser.py: get_family_from_handle fixes * plugins/DescendReport.py: get_family_from_handle fixes * plugins/DetAncestralReport.py: get_family_from_handle fixes * plugins/DetDescendantReport.py: get_family_from_handle fixes * plugins/FamilyGroup.py: get_family_from_handle fixes * plugins/FanChart.py: get_family_from_handle fixes * plugins/FtmStyleAncestors.py: get_family_from_handle fixes * plugins/FtmStyleDescendants.py: get_family_from_handle fixes * plugins/GraphViz.py: get_family_from_handle fixes * plugins/IndivComplete.py: get_family_from_handle fixes * plugins/IndivSummary.py: get_family_from_handle fixes * plugins/Merge.py: get_family_from_handle fixes * plugins/RelGraph.py: get_family_from_handle fixes * plugins/Verify.py: get_family_from_handle fixes * plugins/WebPage.py: get_family_from_handle fixes * plugins/WriteCD.py: get_family_from_handle fixes * plugins/WritePkg.py: get_family_from_handle fixes * plugins/rel_de.py: get_family_from_handle fixes * plugins/rel_hu.py: get_family_from_handle fixes * plugins/rel_ru.py: get_family_from_handle fixes svn: r3443
2004-08-20 03:05:16 +05:30
p = self.db.get_family_from_handle(family_handle)
2004-02-21 11:41:59 +05:30
for o in p.get_media_list():
if o.get_reference_handle() == mobj.get_handle():
2002-10-20 19:55:16 +05:30
return 1
for key in self.db.get_person_handles(sort_handles=False):
p = self.db.get_person_from_handle(key)
2004-02-21 11:41:59 +05:30
for o in p.get_media_list():
if o.get_reference_handle() == mobj.get_handle():
2002-10-20 19:55:16 +05:30
return 1
for key in self.db.get_source_handles():
p = self.db.get_source_from_handle(key)
2004-02-21 11:41:59 +05:30
for o in p.get_media_list():
if o.get_reference_handle() == mobj.get_handle():
2002-10-20 19:55:16 +05:30
return 1
for key in self.db.get_place_handles():
p = self.db.get_place_handle(key)
2004-02-21 11:41:59 +05:30
for o in p.get_media_list():
if o.get_reference_handle() == mobj.get_handle():
2002-10-20 19:55:16 +05:30
return 1
return 0
2003-05-17 08:50:50 +05:30
def on_drag_begin(self,obj,context):
store,node = self.selection.get_selected()
if not node:
2003-05-17 08:50:50 +05:30
return
if (const.dnd_images):
obj = self.db.get_object_from_handle(store.get_value(node,1))
name = Utils.thumb_path(self.db.get_save_path(),obj)
2003-05-17 08:50:50 +05:30
pix = gtk.gdk.pixbuf_new_from_file(name)
context.set_icon_pixbuf(pix,0,0)
2002-10-20 19:55:16 +05:30
def on_drag_data_get(self,w, context, selection_data, info, time):
if info == 1:
return
2003-05-17 08:50:50 +05:30
store,node = self.selection.get_selected()
if not node:
2003-05-17 08:50:50 +05:30
return
handle = store.get_value(node,1)
selection_data.set(selection_data.target, 8, handle)
2002-10-20 19:55:16 +05:30
def on_drag_data_received(self,w, context, x, y, data, info, time):
2004-05-14 04:15:51 +05:30
print "on_drag_data_received"
2002-10-20 19:55:16 +05:30
import urlparse
if data and data.format == 8:
d = string.strip(string.replace(data.data,'\0',' '))
protocol,site,name, j,k,l = urlparse.urlparse(d)
2002-10-20 19:55:16 +05:30
if protocol == "file":
mime = GrampsMime.get_type(name)
2004-02-21 11:41:59 +05:30
photo = RelLib.MediaObject()
photo.set_path(name)
photo.set_mime_type(mime)
2002-10-20 19:55:16 +05:30
description = os.path.basename(name)
photo.set_description(description)
trans = self.db.transaction_begin()
self.db.add_object(photo,trans)
2002-10-20 19:55:16 +05:30
self.load_media()
* src/data/gramps.schemas: Cleanup. * src/data/Makefile.am: Clen up install rule. Add uninstall rule. * src/StartupDialog.py: Correct use of keys. * src/GrampsCfg.py: Correct usage of gconf. * src/DbPrompter.py: Remove unused module. * src/SelectChild.py: Remove unused module. * src/SelectObject.py: Remove unused module. * src/WriteXML.py: Remove unused module. * src/gramps_main.py: Convert to new gconf usage. * src/FamilyView.py: Convert to new gconf usage. * src/AddSpouse.py: Convert to new gconf usage. * src/ChooseParents.py: Convert to new gconf usage. * src/EditPerson.py: Convert to new gconf usage. * src/EditPlace.py: Convert to new gconf usage. * src/EditSource.py: Convert to new gconf usage. * src/EventEdit.py: Convert to new gconf usage. * src/ImageSelect.py: Convert to new gconf usage. * src/Marriage.py: Convert to new gconf usage. * src/MediaView.py: Convert to new gconf usage. * src/MergeData.py: Convert to new gconf usage. * src/PedView.py: Convert to new gconf usage. * src/Plugins.py: Convert to new gconf usage. * src/RelLib.py: Convert to new gconf usage. * src/TipOfDay.py: Convert to new gconf usage. * src/plugins/AncestorChart2.py: Remove unused module. * src/plugins/AncestorChart.py: Remove unused module. * src/plugins/BookReport.py: Remove unused module. * src/plugins/FanChart.py: Remove unused module. * src/plugins/Partition.py: Remove unused module. * src/plugins/Desbrowser.py: Convert to new gconf usage. * src/plugins/Merge.py: Convert to new gconf usage. * src/plugins/RelCalc.py: Convert to new gconf usage. * src/plugins/WebPage.py: Convert to new gconf usage. svn: r3274
2004-07-15 08:24:04 +05:30
if GrampsCfg.get_media_reference() == 0:
2002-10-20 19:55:16 +05:30
name = RelImage.import_media_object(name,
self.db.get_save_path(),
photo.get_handle())
2002-10-20 19:55:16 +05:30
if name:
photo.set_path(name)
self.db.commit_media_object(photo,trans)
self.db.transaction_commit(trans,_("Add Media Object"))
* src/data/gramps.schemas: Cleanup. * src/data/Makefile.am: Clen up install rule. Add uninstall rule. * src/StartupDialog.py: Correct use of keys. * src/GrampsCfg.py: Correct usage of gconf. * src/DbPrompter.py: Remove unused module. * src/SelectChild.py: Remove unused module. * src/SelectObject.py: Remove unused module. * src/WriteXML.py: Remove unused module. * src/gramps_main.py: Convert to new gconf usage. * src/FamilyView.py: Convert to new gconf usage. * src/AddSpouse.py: Convert to new gconf usage. * src/ChooseParents.py: Convert to new gconf usage. * src/EditPerson.py: Convert to new gconf usage. * src/EditPlace.py: Convert to new gconf usage. * src/EditSource.py: Convert to new gconf usage. * src/EventEdit.py: Convert to new gconf usage. * src/ImageSelect.py: Convert to new gconf usage. * src/Marriage.py: Convert to new gconf usage. * src/MediaView.py: Convert to new gconf usage. * src/MergeData.py: Convert to new gconf usage. * src/PedView.py: Convert to new gconf usage. * src/Plugins.py: Convert to new gconf usage. * src/RelLib.py: Convert to new gconf usage. * src/TipOfDay.py: Convert to new gconf usage. * src/plugins/AncestorChart2.py: Remove unused module. * src/plugins/AncestorChart.py: Remove unused module. * src/plugins/BookReport.py: Remove unused module. * src/plugins/FanChart.py: Remove unused module. * src/plugins/Partition.py: Remove unused module. * src/plugins/Desbrowser.py: Convert to new gconf usage. * src/plugins/Merge.py: Convert to new gconf usage. * src/plugins/RelCalc.py: Convert to new gconf usage. * src/plugins/WebPage.py: Convert to new gconf usage. svn: r3274
2004-07-15 08:24:04 +05:30
if GrampsCfg.get_media_global():
ImageSelect.GlobalMediaProperties(self.db,photo,self.load_media,
self,self.topWindow)
2002-10-20 19:55:16 +05:30
elif protocol != "":
import urllib
u = urllib.URLopener()
try:
tfile,headers = u.retrieve(d)
except IOError, msg:
2003-03-21 10:12:12 +05:30
ErrorDialog(_('Image import failed'),str(msg))
2002-10-20 19:55:16 +05:30
return
mime = GrampsMime.get_type(tfile)
2004-02-21 11:41:59 +05:30
photo = RelLib.MediaObject()
photo.set_mime_type(mime)
photo.set_description(d)
photo.set_path(tfile)
trans = self.db.transaction_begin()
self.db.add_object(photo,trans)
2004-02-21 11:41:59 +05:30
oref = RelLib.MediaRef()
oref.set_reference_handle(photo.get_handle())
2002-10-20 19:55:16 +05:30
try:
handle = photo.get_handle()
path = self.db.get_save_path()
name = RelImage.import_media_object(tfile,path,handle)
2002-10-20 19:55:16 +05:30
if name:
photo.set_path(name)
2002-10-20 19:55:16 +05:30
except:
photo.set_path(tfile)
2002-10-20 19:55:16 +05:30
return
self.db.commit_media_object(photo,trans)
self.db.transaction_commit(trans,_("Add Media Object"))
* src/data/gramps.schemas: Cleanup. * src/data/Makefile.am: Clen up install rule. Add uninstall rule. * src/StartupDialog.py: Correct use of keys. * src/GrampsCfg.py: Correct usage of gconf. * src/DbPrompter.py: Remove unused module. * src/SelectChild.py: Remove unused module. * src/SelectObject.py: Remove unused module. * src/WriteXML.py: Remove unused module. * src/gramps_main.py: Convert to new gconf usage. * src/FamilyView.py: Convert to new gconf usage. * src/AddSpouse.py: Convert to new gconf usage. * src/ChooseParents.py: Convert to new gconf usage. * src/EditPerson.py: Convert to new gconf usage. * src/EditPlace.py: Convert to new gconf usage. * src/EditSource.py: Convert to new gconf usage. * src/EventEdit.py: Convert to new gconf usage. * src/ImageSelect.py: Convert to new gconf usage. * src/Marriage.py: Convert to new gconf usage. * src/MediaView.py: Convert to new gconf usage. * src/MergeData.py: Convert to new gconf usage. * src/PedView.py: Convert to new gconf usage. * src/Plugins.py: Convert to new gconf usage. * src/RelLib.py: Convert to new gconf usage. * src/TipOfDay.py: Convert to new gconf usage. * src/plugins/AncestorChart2.py: Remove unused module. * src/plugins/AncestorChart.py: Remove unused module. * src/plugins/BookReport.py: Remove unused module. * src/plugins/FanChart.py: Remove unused module. * src/plugins/Partition.py: Remove unused module. * src/plugins/Desbrowser.py: Convert to new gconf usage. * src/plugins/Merge.py: Convert to new gconf usage. * src/plugins/RelCalc.py: Convert to new gconf usage. * src/plugins/WebPage.py: Convert to new gconf usage. svn: r3274
2004-07-15 08:24:04 +05:30
if GrampsCfg.get_media_global():
ImageSelect.GlobalMediaProperties(self.db,photo,None,
self,self.topWindow)
2002-10-20 19:55:16 +05:30