gramps/src/AddMedia.py

211 lines
7.2 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
"""
Provides the interface to allow a person to add a media object to the database.
"""
#-------------------------------------------------------------------------
#
# Standard python modules
#
#-------------------------------------------------------------------------
import os
#-------------------------------------------------------------------------
#
# internationalization
#
#-------------------------------------------------------------------------
from TransUtils import sgettext 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
import RelLib
2006-03-03 05:53:04 +05:30
import Mime
2005-12-06 12:08:09 +05:30
import GrampsDisplay
2002-10-20 19:55:16 +05:30
#-------------------------------------------------------------------------
#
# AddMediaObject
#
#-------------------------------------------------------------------------
class AddMediaObject:
"""
Displays the Add Media Dialog window, allowing the user to select
a media object from the file system, while providing a description.
"""
def __init__(self,db):
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
"""
self.db = db
self.glade = gtk.glade.XML(const.gladeFile,"imageSelect","gramps")
2002-10-20 19:55:16 +05:30
self.window = self.glade.get_widget("imageSelect")
self.description = self.glade.get_widget("photoDescription")
self.image = self.glade.get_widget("image")
self.file_text = self.glade.get_widget("fname")
self.internal = self.glade.get_widget('internal')
self.internal.connect('toggled',self.internal_toggled)
self.relpath = self.glade.get_widget('relpath')
2002-10-20 19:55:16 +05:30
self.temp_name = ""
self.object = None
Utils.set_titles(self.window,self.glade.get_widget('title'),
_('Select a media object'))
2005-12-06 12:08:09 +05:30
2006-03-08 10:52:58 +05:30
self.glade.get_widget('fname').connect('update_preview',
self.on_name_changed)
2002-10-20 19:55:16 +05:30
self.window.show()
def internal_toggled(self, obj):
self.file_text.set_sensitive(not obj.get_active())
def on_help_imagesel_clicked(self,obj):
"""Display the relevant portion of GRAMPS manual"""
2005-12-06 12:08:09 +05:30
GrampsDisplay.help('gramps-edit-quick')
self.val = self.window.run()
def on_savephoto_clicked(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.
"""
* 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.internal.get_active():
mobj = RelLib.MediaObject()
mobj.set_description(description)
mobj.set_mime_type(None)
else:
filename = self.file_text.get_filename()
if self.relpath.get_active():
p = self.db.get_save_path()
if not os.path.isdir(p):
p = os.path.dirname(p)
filename = Utils.relative_path(filename,p)
if os.path.exists(filename) == 0:
msgstr = _("Cannot import %s")
msgstr2 = _("The filename supplied could not be found.")
ErrorDialog(msgstr % filename, msgstr2)
return
2006-03-03 05:53:04 +05:30
mtype = Mime.get_type(filename)
if description == "":
description = os.path.basename(filename)
mobj = RelLib.MediaObject()
mobj.set_handle(Utils.create_id())
mobj.set_description(description)
mobj.set_mime_type(mtype)
name = filename
mobj.set_path(name)
trans = self.db.transaction_begin()
self.object = mobj
self.db.commit_media_object(mobj,trans)
self.db.transaction_commit(trans,_("Add Media Object"))
2002-10-20 19:55:16 +05:30
2004-05-14 04:15:51 +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.
"""
filename = unicode(self.file_text.get_filename())
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"):
2006-03-19 09:20:47 +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)
def run(self):
while True:
val = self.window.run()
if val == gtk.RESPONSE_OK:
self.on_savephoto_clicked()
self.window.destroy()
return self.object
elif val == gtk.RESPONSE_HELP:
self.on_help_imagesel_clicked(None)
else:
self.window.destroy()
return None
2004-05-14 04:15:51 +05:30
return None
2006-03-19 09:20:47 +05:30
#-------------------------------------------------------------------------
#
# scale_image
#
#-------------------------------------------------------------------------
def scale_image(path,size):
try:
image1 = gtk.gdk.pixbuf_new_from_file(path)
except:
WarningDialog(_("Cannot display %s") % path,
_('GRAMPS is not able to display the image file. '
'This may be caused by a corrupt file.'))
return gtk.gdk.pixbuf_new_from_file(const.icon)
width = image1.get_width()
height = image1.get_height()
scale = size / float(max(width,height))
try:
return image1.scale_simple(int(scale*width), int(scale*height),
gtk.gdk.INTERP_BILINEAR)
except:
WarningDialog(_("Cannot display %s") % path,
_('GRAMPS is not able to display the image file. '
'This may be caused by a corrupt file.'))
return gtk.gdk.pixbuf_new_from_file(const.icon)