* src/DataViews/MediaView.py:

* src/DataViews/RepositoryView.py:
	* src/DataViews/SourceView.py:
	* src/DataViews/EventView.py:
	* src/DataViews/PlaceView.py:
	* src/PageView.py:
	Remove unused double click procedure
	* src/plugins/SimpleBookTitle.py: remove use of AddMedia
	* src/Editors/AddMedia.py: added
	* src/AddMedia.py: removed
	* src/Editors/Makefile.am:
	* src/Makefile.am:
	* po/POTFILES.in:
	Move AddMedia to the editors directory
	* src/Editors/_EditMedia.py:
	Edit Media now works as other editors, spawning AddMedia when 
	select is clicked.
	* src/ManagedWindow.py: typo in comment
	* src/Config/_GrampsConfigKeys.py: Two new keys, rerun ./autogen.sh
	Still todo: make relative path work

2008-01-06  Benny Malengier <benny.malengier@gramps-project.org>


svn: r9722
This commit is contained in:
Benny Malengier
2008-01-06 10:51:20 +00:00
parent aabef20651
commit 9664842702
15 changed files with 138 additions and 137 deletions

240
src/Editors/AddMedia.py Normal file
View File

@@ -0,0 +1,240 @@
#
# Gramps - a GTK+/GNOME based genealogy program
#
# Copyright (C) 2000-2006 Donald N. Allingham
#
# 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$
"""
Provides the interface to allow a person to add a media object to the database.
"""
#-------------------------------------------------------------------------
#
# Standard python modules
#
#-------------------------------------------------------------------------
import os
import sys
#-------------------------------------------------------------------------
#
# internationalization
#
#-------------------------------------------------------------------------
from gettext import gettext as _
#-------------------------------------------------------------------------
#
# GTK/Gnome modules
#
#-------------------------------------------------------------------------
from QuestionDialog import ErrorDialog, WarningDialog
import gtk.glade
#-------------------------------------------------------------------------
#
# gramps modules
#
#-------------------------------------------------------------------------
import const
import Config
import Utils
import gen.lib
import Mime
import GrampsDisplay
import ManagedWindow
#-------------------------------------------------------------------------
#
# global variables
#
#-------------------------------------------------------------------------
#-------------------------------------------------------------------------
#
# AddMediaObject
#
#-------------------------------------------------------------------------
class AddMediaObject(ManagedWindow.ManagedWindow):
"""
Displays the Add Media Dialog window, allowing the user to select
a file from the file system, while providing a description.
"""
def __init__(self, dbstate, uistate, track, mediaobj, callback=None):
"""
Creates and displays the dialog box
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
"""
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)
self.glade = gtk.glade.XML(const.GLADE_FILE, "imageSelect", "gramps")
self.set_window(
self.glade.get_widget("imageSelect"),
self.glade.get_widget('title'),
_('Select a media object'))
self.description = self.glade.get_widget("photoDescription")
self.image = self.glade.get_widget("image")
self.file_text = self.glade.get_widget("fname")
if not(self.last_directory and os.path.isdir(self.last_directory)):
self.last_directory = const.HOME_DIR
print 'test', self.last_directory
self.file_text.set_current_folder(self.last_directory)
self.relpath = self.glade.get_widget('relpath')
self.relpath.set_active(self.relative_path)
self.temp_name = ""
self.object = None
self.glade.get_widget('fname').connect('update_preview',
self.on_name_changed)
self.ok_button = self.glade.get_widget('button79')
self.help_button = self.glade.get_widget('button103')
self.cancel_button = self.glade.get_widget('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(
'gramps-edit-quick'))
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):
"""
Callback function called with the save button is pressed.
The media object is updated, and callback called
"""
description = unicode(self.description.get_text())
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 = self.dbase.get_save_path()
if not os.path.isdir(pname):
pname = os.path.dirname(pname)
if self.relpath.get_active():
filename = Utils.relative_path(filename, pname)
if os.path.exists(pname) == 0:
msgstr = _("Cannot import %s")
msgstr2 = _("The filename supplied could not be found.")
ErrorDialog(msgstr % filename, msgstr2)
return
mtype = Mime.get_type(full_file)
if description == "":
description = 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):
"""
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:
return
filename = Utils.get_unicode_path(fname)
basename = os.path.basename(filename)
(root, ext) = os.path.splitext(basename)
old_title = unicode(self.description.get_text())
if old_title == '' or old_title == self.temp_name:
self.description.set_text(root)
self.temp_name = root
filename = Utils.find_file( filename)
if filename:
mtype = Mime.get_type(filename)
if mtype and mtype.startswith("image"):
image = scale_image(filename, const.THUMBSCALE)
else:
image = Mime.find_mime_type_pixbuf(mtype)
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()
#-------------------------------------------------------------------------
#
# scale_image
#
#-------------------------------------------------------------------------
def scale_image(path, size):
"""
Scales the image to the specified size
"""
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.')
try:
image1 = gtk.gdk.pixbuf_new_from_file(path)
width = image1.get_width()
height = image1.get_height()
scale = size / float(max(width, height))
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)

View File

@@ -6,6 +6,7 @@
pkgdatadir = $(datadir)/@PACKAGE@/Editors
pkgdata_PYTHON = \
AddMedia.py\
__init__.py\
_EditAddress.py \
_EditAttribute.py \

View File

@@ -48,6 +48,7 @@ import Mime
import ThumbNails
import Utils
from _EditPrimary import EditPrimary
from AddMedia import AddMediaObject
from GrampsWidgets import *
from DisplayTabs import SourceEmbedList,AttrEmbedList,NoteTab,MediaBackRefList
@@ -59,10 +60,14 @@ from DisplayTabs import SourceEmbedList,AttrEmbedList,NoteTab,MediaBackRefList
#-------------------------------------------------------------------------
class EditMedia(EditPrimary):
def __init__(self,state,uistate,track,obj):
def __init__(self, state, uistate, track, obj, callback=None):
EditPrimary.__init__(self, state, uistate, track, obj,
state.db.get_object_from_handle)
state.db.get_object_from_handle, callback)
if not self.obj.get_handle():
#show the addmedia dialog immediately, with track of parent.
AddMediaObject(state, self.uistate, self.track, self.obj,
self._update_addmedia)
def empty_object(self):
return gen.lib.MediaObject()
@@ -94,8 +99,9 @@ class EditMedia(EditPrimary):
def _connect_signals(self):
self.define_cancel_button(self.glade.get_widget('button91'))
self.define_ok_button(self.glade.get_widget('ok'),self.save)
self.define_help_button(self.glade.get_widget('button102'),'adv-media')
self.define_ok_button(self.glade.get_widget('ok'), self.save)
self.define_help_button(self.glade.get_widget('button102'),
'adv-media')
def _setup_fields(self):
self.date_field = MonitoredDate(
@@ -122,25 +128,36 @@ class EditMedia(EditPrimary):
self.glade.get_widget("private"),
self.obj, self.db.readonly)
pixmap = self.glade.get_widget("pixmap")
self.pixmap = self.glade.get_widget("pixmap")
ebox = self.glade.get_widget('eventbox')
ebox.connect('button-press-event', self.button_press_event)
self.mimetext = self.glade.get_widget("type")
self.draw_preview()
self.setup_filepath()
def draw_preview(self):
mtype = self.obj.get_mime_type()
if mtype:
pb = ThumbNails.get_thumbnail_image(
Utils.find_file(self.obj.get_path()),mtype)
pixmap.set_from_pixbuf(pb)
Utils.find_file(self.obj.get_path()), mtype)
self.pixmap.set_from_pixbuf(pb)
descr = Mime.get_description(mtype)
if descr:
self.glade.get_widget("type").set_text(descr)
self.mimetext.set_text(descr)
else:
pb = Mime.find_mime_type_pixbuf('text/plain')
pixmap.set_from_pixbuf(pb)
self.glade.get_widget("type").set_text(_('Note'))
self.pixmap.set_from_pixbuf(pb)
self.mimetext.set_text(_('Note'))
def setup_filepath(self):
self.select = self.glade.get_widget('file_select')
self.file_path = self.glade.get_widget("path")
fname = self.obj.get_path()
self.file_path.set_text(fname)
self.select.connect('clicked', self.select_file)
self.setup_filepath()
def _create_tabbed_pages(self):
notebook = gtk.Notebook()
@@ -176,7 +193,6 @@ class EditMedia(EditPrimary):
self.view_media(obj)
def view_media(self, obj):
ref_obj = self.dbstate.db.get_object_from_handle(self.obj.handle)
mime_type = ref_obj.get_mime_type()
app = Mime.get_application(mime_type)
@@ -184,34 +200,24 @@ class EditMedia(EditPrimary):
import Utils
Utils.launch(app[0],ref_obj.get_path())
def select_file(self,obj):
f = gtk.FileChooserDialog(
_('Select Media Object'),
action=gtk.FILE_CHOOSER_ACTION_OPEN,
buttons=(gtk.STOCK_CANCEL,
gtk.RESPONSE_CANCEL,
gtk.STOCK_OPEN,
gtk.RESPONSE_OK))
def select_file(self, val):
AddMediaObject(self.dbstate, self.uistate, self.track, self.obj,
self._update_addmedia)
text = self.file_path.get_text()
path = os.path.dirname(text)
f.set_filename(path)
status = f.run()
if status == gtk.RESPONSE_OK:
self.file_path.set_text(Utils.get_unicode_path(f.get_filename()))
f.destroy()
def setup_filepath(self):
self.select = self.glade.get_widget('file_select')
self.file_path = self.glade.get_widget("path")
def _update_addmedia(self, obj):
"""
Called when the add media dialog has been called.
This allows us to update the main form in response to
any changes: Redraw relevant fields: description, mimetype and path
"""
for obj in (self.descr_window, ):
obj.update()
fname = self.obj.get_path()
self.file_path.set_text(fname)
self.select.connect('clicked', self.select_file)
self.draw_preview()
def save(self, *obj):
self.ok_button.set_sensitive(False)
path = self.glade.get_widget('path').get_text()
if path != self.obj.get_path():
@@ -221,8 +227,15 @@ class EditMedia(EditPrimary):
self.obj.set_path(Utils.get_unicode_path(path))
trans = self.db.transaction_begin()
self.db.commit_media_object(self.obj,trans)
self.db.transaction_commit(trans,_("Edit Media Object"))
if self.obj.get_handle():
self.db.commit_media_object(self.obj, trans)
else:
self.db.add_object(self.obj, trans)
self.db.transaction_commit(trans, _("Edit Media Object (%s)"
) % self.obj.get_description())
if self.callback:
self.callback(self.obj)
self.close()
def _cleanup_on_exit(self):