gramps/src/Editors/AddMedia.py

247 lines
8.5 KiB
Python
Raw Normal View History

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
#
# $Id$
2002-10-20 19:55:16 +05:30
"""
Provide the interface to allow a person to add a media object to the database.
2002-10-20 19:55:16 +05:30
"""
#-------------------------------------------------------------------------
#
# Standard python modules
#
#-------------------------------------------------------------------------
import os
#-------------------------------------------------------------------------
#
# internationalization
#
#-------------------------------------------------------------------------
from gettext import gettext as _
2002-10-20 19:55:16 +05:30
#-------------------------------------------------------------------------
#
# GTK/Gnome modules
#
#-------------------------------------------------------------------------
import gtk
2002-10-20 19:55:16 +05:30
#-------------------------------------------------------------------------
#
# gramps modules
#
#-------------------------------------------------------------------------
import const
import Config
2002-10-20 19:55:16 +05:30
import Utils
2006-03-03 05:53:04 +05:30
import Mime
2005-12-06 12:08:09 +05:30
import GrampsDisplay
import ManagedWindow
from QuestionDialog import ErrorDialog, WarningDialog
2002-10-20 19:55:16 +05:30
#-------------------------------------------------------------------------
#
# global variables
#
#-------------------------------------------------------------------------
_GLADE_FILE = 'addmedia.glade'
2002-10-20 19:55:16 +05:30
#-------------------------------------------------------------------------
#
# AddMediaObject
#
#-------------------------------------------------------------------------
class AddMediaObject(ManagedWindow.ManagedWindow):
2002-10-20 19:55:16 +05:30
"""
Displays the Add Media Dialog window, allowing the user to select
a file from the file system, while providing a description.
2002-10-20 19:55:16 +05:30
"""
def __init__(self, dbstate, uistate, track, mediaobj, callback=None):
2002-10-20 19:55:16 +05:30
"""
Create and displays the dialog box
2002-10-20 19:55:16 +05:30
db - the database in which the new object is to be stored
The mediaobject is updated with the information, and on save, the
callback function is called
2002-10-20 19:55:16 +05:30
"""
ManagedWindow.ManagedWindow.__init__(self, uistate, track, self)
self.dbase = dbstate.db
self.obj = mediaobj
self.callback = callback
self.last_directory = Config.get(Config.ADDMEDIA_IMGDIR)
self.relative_path = Config.get(Config.ADDMEDIA_RELPATH)
glade_file = os.path.join(const.GLADE_DIR, _GLADE_FILE)
self.glade = gtk.Builder()
self.glade.add_from_file(glade_file)
self.set_window(
self.glade.get_object("imageSelect"),
self.glade.get_object('title'),
_('Select a media object'))
self.description = self.glade.get_object("photoDescription")
self.image = self.glade.get_object("image")
self.file_text = self.glade.get_object("fname")
if not(self.last_directory and os.path.isdir(self.last_directory)):
self.last_directory = const.USER_HOME
#if existing path, use dir of path
if not self.obj.get_path() == "":
fullname = Utils.media_path_full(self.dbase, self.obj.get_path())
dir = os.path.dirname(fullname)
if os.path.isdir(dir):
self.last_directory = dir
self.file_text.select_filename(fullname)
else:
self.file_text.set_current_folder(self.last_directory)
else:
self.file_text.set_current_folder(self.last_directory)
if not self.obj.get_description() == "":
self.description.set_text(self.obj.get_description())
self.relpath = self.glade.get_object('relpath')
self.relpath.set_active(self.relative_path)
2002-10-20 19:55:16 +05:30
self.temp_name = ""
self.object = None
self.glade.get_object('fname').connect('update_preview',
2006-03-08 10:52:58 +05:30
self.on_name_changed)
self.ok_button = self.glade.get_object('button79')
self.help_button = self.glade.get_object('button103')
self.cancel_button = self.glade.get_object('button81')
self.ok_button.connect('clicked',self.save)
self.ok_button.set_sensitive(not self.dbase.readonly)
self.help_button.connect('clicked', lambda x: GrampsDisplay.help())
self.cancel_button.connect('clicked', self.close)
self.show()
self.modal_call()
def build_menu_names(self, obj):
"""
Build the menu name for the window manager
"""
return(_('Select media object'), None)
def save(self, *obj):
2002-10-20 19:55:16 +05:30
"""
Callback function called with the save button is pressed.
The media object is updated, and callback called
2002-10-20 19:55:16 +05:30
"""
* src/PlaceView.py: Make sure to add new place after edit * src/AddMedia.py: unicode conversion from gtk.Entry * src/AddSpouse.py: unicode conversion from gtk.Entry * src/AddrEdit.py: unicode conversion from gtk.Entry * src/AttrEdit.py: unicode conversion from gtk.Entry * src/AutoComp.py: unicode conversion from gtk.Entry * src/ChooseParents.py: unicode conversion from gtk.Entry * src/DateEdit.py: unicode conversion from gtk.Entry * src/EditPerson.py: unicode conversion from gtk.Entry * src/EditPlace.py: unicode conversion from gtk.Entry * src/EditSource.py: unicode conversion from gtk.Entry * src/EventEdit.py: unicode conversion from gtk.Entry * src/Find.py: unicode conversion from gtk.Entry * src/GrampsCfg.py: unicode conversion from gtk.Entry * src/ImageSelect.py: unicode conversion from gtk.Entry * src/LocEdit.py: unicode conversion from gtk.Entry * src/Marriage.py: unicode conversion from gtk.Entry * src/MergeData.py: unicode conversion from gtk.Entry * src/NameEdit.py: unicode conversion from gtk.Entry * src/PeopleView.py: unicode conversion from gtk.Entry * src/Report.py: unicode conversion from gtk.Entry * src/SelectChild.py: unicode conversion from gtk.Entry * src/Sources.py: unicode conversion from gtk.Entry * src/StartupDialog.py: unicode conversion from gtk.Entry * src/StyleEditor.py: unicode conversion from gtk.Entry * src/UrlEdit.py: unicode conversion from gtk.Entry * src/Utils.py: unicode conversion from gtk.Entry * src/VersionControl.py: unicode conversion from gtk.Entry * src/Witness.py: unicode conversion from gtk.Entry svn: r2534
2003-12-17 10:53:16 +05:30
description = unicode(self.description.get_text())
2002-10-20 19:55:16 +05:30
if self.file_text.get_filename() is None:
msgstr = _("Import failed")
msgstr2 = _("The filename supplied could not be found.")
ErrorDialog(msgstr, msgstr2)
return
filename = Utils.get_unicode_path(self.file_text.get_filename())
full_file = filename
pname = unicode(Utils.media_path(self.dbase))
if self.relpath.get_active():
filename = Utils.relative_path(filename, pname)
if not os.path.exists(pname):
msgstr = _("Cannot import %s")
msgstr2 = _("The filename supplied could not be found.")
ErrorDialog(msgstr % filename, msgstr2)
return
mtype = Mime.get_type(full_file)
description = description or os.path.basename(filename)
self.obj.set_description(description)
self.obj.set_mime_type(mtype)
name = filename
self.obj.set_path(name)
self.last_directory = os.path.dirname(full_file)
self.relative_path = self.relpath.get_active()
self._cleanup_on_exit()
if self.callback:
self.callback(self.obj)
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.
"""
fname = self.file_text.get_filename()
if not fname:
2007-01-22 10:29:23 +05:30
return
filename = Utils.get_unicode_path(fname)
2002-10-20 19:55:16 +05:30
basename = os.path.basename(filename)
(root, ext) = os.path.splitext(basename)
* src/PlaceView.py: Make sure to add new place after edit * src/AddMedia.py: unicode conversion from gtk.Entry * src/AddSpouse.py: unicode conversion from gtk.Entry * src/AddrEdit.py: unicode conversion from gtk.Entry * src/AttrEdit.py: unicode conversion from gtk.Entry * src/AutoComp.py: unicode conversion from gtk.Entry * src/ChooseParents.py: unicode conversion from gtk.Entry * src/DateEdit.py: unicode conversion from gtk.Entry * src/EditPerson.py: unicode conversion from gtk.Entry * src/EditPlace.py: unicode conversion from gtk.Entry * src/EditSource.py: unicode conversion from gtk.Entry * src/EventEdit.py: unicode conversion from gtk.Entry * src/Find.py: unicode conversion from gtk.Entry * src/GrampsCfg.py: unicode conversion from gtk.Entry * src/ImageSelect.py: unicode conversion from gtk.Entry * src/LocEdit.py: unicode conversion from gtk.Entry * src/Marriage.py: unicode conversion from gtk.Entry * src/MergeData.py: unicode conversion from gtk.Entry * src/NameEdit.py: unicode conversion from gtk.Entry * src/PeopleView.py: unicode conversion from gtk.Entry * src/Report.py: unicode conversion from gtk.Entry * src/SelectChild.py: unicode conversion from gtk.Entry * src/Sources.py: unicode conversion from gtk.Entry * src/StartupDialog.py: unicode conversion from gtk.Entry * src/StyleEditor.py: unicode conversion from gtk.Entry * src/UrlEdit.py: unicode conversion from gtk.Entry * src/Utils.py: unicode conversion from gtk.Entry * src/VersionControl.py: unicode conversion from gtk.Entry * src/Witness.py: unicode conversion from gtk.Entry svn: r2534
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"):
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)
def _cleanup_on_exit(self):
Config.set(Config.ADDMEDIA_IMGDIR, self.last_directory)
Config.set(Config.ADDMEDIA_RELPATH, self.relative_path)
Config.sync()
2006-03-19 09:20:47 +05:30
#-------------------------------------------------------------------------
#
# scale_image
#
#-------------------------------------------------------------------------
def scale_image(path, size):
"""
Scales the image to the specified size
"""
2006-03-19 09:20:47 +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:
image1 = gtk.gdk.pixbuf_new_from_file(path)
width = image1.get_width()
height = image1.get_height()
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:
WarningDialog(title_msg, detail_msg)
return gtk.gdk.pixbuf_new_from_file(const.ICON)