2002-10-20 19:55:16 +05:30
|
|
|
#
|
|
|
|
# Gramps - a GTK+/GNOME based genealogy program
|
|
|
|
#
|
2006-03-19 09:20:47 +05:30
|
|
|
# Copyright (C) 2000-2006 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
|
|
|
|
#
|
|
|
|
|
2003-12-02 09:57:23 +05:30
|
|
|
# $Id$
|
|
|
|
|
2002-10-20 19:55:16 +05:30
|
|
|
"""
|
|
|
|
Provides the interface to allow a person to add a media object to the database.
|
|
|
|
"""
|
|
|
|
|
|
|
|
#-------------------------------------------------------------------------
|
|
|
|
#
|
|
|
|
# Standard python modules
|
|
|
|
#
|
|
|
|
#-------------------------------------------------------------------------
|
|
|
|
import os
|
2007-01-22 09:26:23 +05:30
|
|
|
import sys
|
2002-10-20 19:55:16 +05:30
|
|
|
|
|
|
|
#-------------------------------------------------------------------------
|
|
|
|
#
|
|
|
|
# internationalization
|
|
|
|
#
|
|
|
|
#-------------------------------------------------------------------------
|
2006-04-07 03:32:46 +05:30
|
|
|
from gettext import gettext as _
|
2002-10-20 19:55:16 +05:30
|
|
|
|
|
|
|
#-------------------------------------------------------------------------
|
|
|
|
#
|
|
|
|
# GTK/Gnome modules
|
|
|
|
#
|
|
|
|
#-------------------------------------------------------------------------
|
2006-03-19 09:20:47 +05:30
|
|
|
from QuestionDialog import ErrorDialog, WarningDialog
|
2002-10-20 19:55:16 +05:30
|
|
|
import gtk.glade
|
|
|
|
|
|
|
|
#-------------------------------------------------------------------------
|
|
|
|
#
|
|
|
|
# gramps modules
|
|
|
|
#
|
|
|
|
#-------------------------------------------------------------------------
|
|
|
|
import const
|
|
|
|
import Utils
|
2007-10-08 22:11:39 +05:30
|
|
|
import gen.lib
|
2006-03-03 05:53:04 +05:30
|
|
|
import Mime
|
2005-12-06 12:08:09 +05:30
|
|
|
import GrampsDisplay
|
2006-04-28 06:03:44 +05:30
|
|
|
import ManagedWindow
|
2002-10-20 19:55:16 +05:30
|
|
|
|
|
|
|
#-------------------------------------------------------------------------
|
|
|
|
#
|
|
|
|
# AddMediaObject
|
|
|
|
#
|
|
|
|
#-------------------------------------------------------------------------
|
2006-04-28 06:03:44 +05:30
|
|
|
class AddMediaObject(ManagedWindow.ManagedWindow):
|
2002-10-20 19:55:16 +05:30
|
|
|
"""
|
|
|
|
Displays the Add Media Dialog window, allowing the user to select
|
|
|
|
a media object from the file system, while providing a description.
|
|
|
|
"""
|
2007-05-15 09:47:12 +05:30
|
|
|
|
|
|
|
last_directory = None
|
2002-10-20 19:55:16 +05:30
|
|
|
|
2007-05-15 09:47:12 +05:30
|
|
|
def __init__(self, dbstate, uistate, track):
|
2002-10-20 19:55:16 +05:30
|
|
|
"""
|
|
|
|
Creates and displays the dialog box
|
|
|
|
|
|
|
|
db - the database in which the new object is to be stored
|
|
|
|
"""
|
2006-04-28 06:03:44 +05:30
|
|
|
|
|
|
|
ManagedWindow.ManagedWindow.__init__(self, uistate, track, self)
|
2006-05-10 07:20:45 +05:30
|
|
|
|
2007-05-15 09:47:12 +05:30
|
|
|
self.dbase = dbstate.db
|
2007-09-08 11:24:02 +05:30
|
|
|
self.glade = gtk.glade.XML(const.GLADE_FILE, "imageSelect", "gramps")
|
2006-04-28 06:03:44 +05:30
|
|
|
|
|
|
|
self.set_window(
|
|
|
|
self.glade.get_widget("imageSelect"),
|
|
|
|
self.glade.get_widget('title'),
|
|
|
|
_('Select a media object'))
|
|
|
|
|
2002-10-20 19:55:16 +05:30
|
|
|
self.description = self.glade.get_widget("photoDescription")
|
|
|
|
self.image = self.glade.get_widget("image")
|
|
|
|
self.file_text = self.glade.get_widget("fname")
|
2007-05-15 09:47:12 +05:30
|
|
|
if self.last_directory and os.path.isdir(self.last_directory):
|
|
|
|
self.file_text.set_current_folder(self.last_directory)
|
2006-05-10 07:20:45 +05:30
|
|
|
|
2005-02-16 11:11:33 +05:30
|
|
|
self.internal = self.glade.get_widget('internal')
|
2007-05-15 09:47:12 +05:30
|
|
|
self.internal.connect('toggled', self.internal_toggled)
|
2006-02-05 04:59:44 +05:30
|
|
|
self.relpath = self.glade.get_widget('relpath')
|
2002-10-20 19:55:16 +05:30
|
|
|
self.temp_name = ""
|
2003-09-10 19:08:02 +05:30
|
|
|
self.object = None
|
2003-08-13 09:58:07 +05:30
|
|
|
|
2006-03-08 10:52:58 +05:30
|
|
|
self.glade.get_widget('fname').connect('update_preview',
|
|
|
|
self.on_name_changed)
|
2006-04-28 06:03:44 +05:30
|
|
|
self.show()
|
|
|
|
|
|
|
|
def build_menu_names(self, obj):
|
2007-05-15 09:47:12 +05:30
|
|
|
"""
|
|
|
|
Build the menu name for the window manager
|
|
|
|
"""
|
|
|
|
return(_('Select media object'), None)
|
2002-10-20 19:55:16 +05:30
|
|
|
|
2005-02-16 11:11:33 +05:30
|
|
|
def internal_toggled(self, obj):
|
2007-05-15 09:47:12 +05:30
|
|
|
"""
|
|
|
|
Toggles the file_text icon.
|
|
|
|
"""
|
2005-02-16 11:11:33 +05:30
|
|
|
self.file_text.set_sensitive(not obj.get_active())
|
|
|
|
|
2007-05-15 09:47:12 +05:30
|
|
|
def on_help_imagesel_clicked(self, obj):
|
2003-12-02 09:57:23 +05:30
|
|
|
"""Display the relevant portion of GRAMPS manual"""
|
2005-12-06 12:08:09 +05:30
|
|
|
GrampsDisplay.help('gramps-edit-quick')
|
2007-05-15 09:47:12 +05:30
|
|
|
self.window.run()
|
2003-12-02 09:57:23 +05:30
|
|
|
|
2006-04-29 07:43:47 +05:30
|
|
|
def save(self):
|
2002-10-20 19:55:16 +05:30
|
|
|
"""
|
|
|
|
Callback function called with the save button is pressed.
|
|
|
|
A new media object is created, and added to the database.
|
|
|
|
"""
|
2003-12-17 10:53:16 +05:30
|
|
|
description = unicode(self.description.get_text())
|
2002-10-20 19:55:16 +05:30
|
|
|
|
2005-02-16 11:11:33 +05:30
|
|
|
if self.internal.get_active():
|
2007-10-08 22:11:39 +05:30
|
|
|
mobj = gen.lib.MediaObject()
|
2005-02-16 11:11:33 +05:30
|
|
|
mobj.set_description(description)
|
2006-04-29 07:43:47 +05:30
|
|
|
mobj.set_handle(Utils.create_id())
|
2005-02-16 11:11:33 +05:30
|
|
|
mobj.set_mime_type(None)
|
|
|
|
else:
|
2007-01-22 09:26:23 +05:30
|
|
|
filename = unicode(self.file_text.get_filename(),
|
|
|
|
sys.getfilesystemencoding())
|
2006-07-01 10:25:36 +05:30
|
|
|
full_file = filename
|
|
|
|
|
2006-02-05 04:59:44 +05:30
|
|
|
if self.relpath.get_active():
|
2007-05-15 09:47:12 +05:30
|
|
|
pname = self.dbase.get_save_path()
|
|
|
|
if not os.path.isdir(pname):
|
|
|
|
pname = os.path.dirname(pname)
|
|
|
|
filename = Utils.relative_path(filename, pname)
|
2006-07-01 10:25:36 +05:30
|
|
|
|
2005-02-16 11:11:33 +05:30
|
|
|
if os.path.exists(filename) == 0:
|
|
|
|
msgstr = _("Cannot import %s")
|
|
|
|
msgstr2 = _("The filename supplied could not be found.")
|
|
|
|
ErrorDialog(msgstr % filename, msgstr2)
|
|
|
|
return
|
|
|
|
|
2006-07-01 10:25:36 +05:30
|
|
|
mtype = Mime.get_type(full_file)
|
2005-02-16 11:11:33 +05:30
|
|
|
if description == "":
|
|
|
|
description = os.path.basename(filename)
|
|
|
|
|
2007-10-08 22:11:39 +05:30
|
|
|
mobj = gen.lib.MediaObject()
|
2005-02-16 11:11:33 +05:30
|
|
|
mobj.set_description(description)
|
|
|
|
mobj.set_mime_type(mtype)
|
|
|
|
name = filename
|
|
|
|
mobj.set_path(name)
|
2007-05-15 09:47:12 +05:30
|
|
|
self.last_directory = os.path.dirname(filename)
|
2004-07-10 10:19:53 +05:30
|
|
|
|
2006-04-29 07:43:47 +05:30
|
|
|
mobj.set_handle(Utils.create_id())
|
|
|
|
if not mobj.get_gramps_id():
|
2007-05-15 09:47:12 +05:30
|
|
|
mobj.set_gramps_id(self.dbase.find_next_object_gramps_id())
|
|
|
|
trans = self.dbase.transaction_begin()
|
2003-09-10 19:08:02 +05:30
|
|
|
self.object = mobj
|
2007-05-15 09:47:12 +05:30
|
|
|
self.dbase.commit_media_object(mobj, trans)
|
|
|
|
self.dbase.transaction_commit(trans, _("Add Media Object"))
|
2006-05-10 07:20:45 +05:30
|
|
|
|
2007-05-15 09:47:12 +05:30
|
|
|
def on_name_changed(self, *obj):
|
2002-10-20 19:55:16 +05:30
|
|
|
"""
|
|
|
|
Called anytime the filename text window changes. Checks to
|
|
|
|
see if the file exists. If it does, the imgae is loaded into
|
|
|
|
the preview window.
|
|
|
|
"""
|
2007-05-15 09:47:12 +05:30
|
|
|
fname = self.file_text.get_filename()
|
|
|
|
if not fname:
|
2007-01-22 10:29:23 +05:30
|
|
|
return
|
2007-05-15 09:47:12 +05:30
|
|
|
filename = unicode(fname, sys.getfilesystemencoding())
|
2002-10-20 19:55:16 +05:30
|
|
|
basename = os.path.basename(filename)
|
2007-05-15 09:47:12 +05:30
|
|
|
(root, ext) = os.path.splitext(basename)
|
2003-12-17 10:53:16 +05:30
|
|
|
old_title = unicode(self.description.get_text())
|
2002-10-20 19:55:16 +05:30
|
|
|
|
|
|
|
if old_title == '' or old_title == self.temp_name:
|
|
|
|
self.description.set_text(root)
|
|
|
|
self.temp_name = root
|
2005-12-06 12:08:09 +05:30
|
|
|
|
|
|
|
filename = Utils.find_file( filename)
|
|
|
|
if filename:
|
2006-03-03 05:53:04 +05:30
|
|
|
mtype = Mime.get_type(filename)
|
2005-05-24 18:38:06 +05:30
|
|
|
if mtype and mtype.startswith("image"):
|
2007-09-08 11:24:02 +05:30
|
|
|
image = scale_image(filename, const.THUMBSCALE)
|
2002-10-20 19:55:16 +05:30
|
|
|
else:
|
2006-03-03 05:53:04 +05:30
|
|
|
image = Mime.find_mime_type_pixbuf(mtype)
|
2003-02-08 22:58:41 +05:30
|
|
|
self.image.set_from_pixbuf(image)
|
2003-09-10 19:08:02 +05:30
|
|
|
|
|
|
|
def run(self):
|
2007-05-15 09:47:12 +05:30
|
|
|
"""
|
|
|
|
Run the dialog, returning the selected object.
|
|
|
|
"""
|
2005-04-09 08:41:03 +05:30
|
|
|
while True:
|
2003-12-02 09:57:23 +05:30
|
|
|
val = self.window.run()
|
|
|
|
|
|
|
|
if val == gtk.RESPONSE_OK:
|
2006-04-29 07:43:47 +05:30
|
|
|
self.save()
|
2006-04-28 06:03:44 +05:30
|
|
|
self.close()
|
2003-12-02 09:57:23 +05:30
|
|
|
return self.object
|
|
|
|
elif val == gtk.RESPONSE_HELP:
|
2004-10-08 09:29:55 +05:30
|
|
|
self.on_help_imagesel_clicked(None)
|
2003-12-02 09:57:23 +05:30
|
|
|
else:
|
2006-04-28 06:03:44 +05:30
|
|
|
self.close()
|
2003-12-02 09:57:23 +05:30
|
|
|
return None
|
2004-05-14 04:15:51 +05:30
|
|
|
return None
|
2006-03-19 09:20:47 +05:30
|
|
|
|
|
|
|
#-------------------------------------------------------------------------
|
|
|
|
#
|
|
|
|
# scale_image
|
|
|
|
#
|
|
|
|
#-------------------------------------------------------------------------
|
2007-05-15 09:47:12 +05:30
|
|
|
def scale_image(path, size):
|
|
|
|
"""
|
|
|
|
Scales the image to the specified size
|
|
|
|
"""
|
2006-03-19 09:20:47 +05:30
|
|
|
|
2006-03-30 04:21:27 +05:30
|
|
|
title_msg = _("Cannot display %s") % path
|
|
|
|
detail_msg = _('GRAMPS is not able to display the image file. '
|
|
|
|
'This may be caused by a corrupt file.')
|
|
|
|
|
2006-03-19 09:20:47 +05:30
|
|
|
try:
|
2006-03-30 04:21:27 +05:30
|
|
|
image1 = gtk.gdk.pixbuf_new_from_file(path)
|
|
|
|
width = image1.get_width()
|
|
|
|
height = image1.get_height()
|
|
|
|
|
2007-05-15 09:47:12 +05:30
|
|
|
scale = size / float(max(width, height))
|
2006-03-19 09:20:47 +05:30
|
|
|
return image1.scale_simple(int(scale*width), int(scale*height),
|
|
|
|
gtk.gdk.INTERP_BILINEAR)
|
|
|
|
except:
|
2006-03-30 04:21:27 +05:30
|
|
|
WarningDialog(title_msg, detail_msg)
|
2007-09-08 11:24:02 +05:30
|
|
|
return gtk.gdk.pixbuf_new_from_file(const.ICON)
|
2006-03-30 04:21:27 +05:30
|
|
|
|