Code Search for Developers
 
 
  

AddMedia.py from gramps at Krugle


Show AddMedia.py syntax highlighted

#
# 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: AddMedia.py 8473 2007-05-15 04:17:12Z dallingham $

"""
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 Utils
import RelLib
import Mime
import GrampsDisplay
import ManagedWindow

#-------------------------------------------------------------------------
#
# AddMediaObject
#
#-------------------------------------------------------------------------
class AddMediaObject(ManagedWindow.ManagedWindow):
    """
    Displays the Add Media Dialog window, allowing the user to select
    a media object from the file system, while providing a description.
    """

    last_directory = None
    
    def __init__(self, dbstate, uistate, track):
        """
        Creates and displays the dialog box

        db - the database in which the new object is to be stored
        """

        ManagedWindow.ManagedWindow.__init__(self, uistate, track, self)

        self.dbase = dbstate.db
        self.glade = gtk.glade.XML(const.gladeFile, "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 self.last_directory and os.path.isdir(self.last_directory):
            self.file_text.set_current_folder(self.last_directory)
            
        self.internal = self.glade.get_widget('internal')
        self.internal.connect('toggled', self.internal_toggled)
        self.relpath = self.glade.get_widget('relpath')
        self.temp_name = ""
        self.object = None

        self.glade.get_widget('fname').connect('update_preview',
                                               self.on_name_changed)
        self.show()

    def build_menu_names(self, obj):
        """
        Build the menu name for the window manager
        """
        return(_('Select media object'), None)

    def internal_toggled(self, obj):
        """
        Toggles the file_text icon.
        """
        self.file_text.set_sensitive(not obj.get_active())
        
    def on_help_imagesel_clicked(self, obj):
        """Display the relevant portion of GRAMPS manual"""
        GrampsDisplay.help('gramps-edit-quick')
        self.window.run()

    def save(self):
        """
        Callback function called with the save button is pressed.
        A new media object is created, and added to the database.
        """
        description = unicode(self.description.get_text())

        if self.internal.get_active():
            mobj = RelLib.MediaObject()
            mobj.set_description(description)
            mobj.set_handle(Utils.create_id())
            mobj.set_mime_type(None)
        else:
            filename = unicode(self.file_text.get_filename(), 
                               sys.getfilesystemencoding())
            full_file = filename

            if self.relpath.get_active():
                pname = self.dbase.get_save_path()
                if not os.path.isdir(pname):
                    pname = os.path.dirname(pname)
                filename = Utils.relative_path(filename, pname)

            if os.path.exists(filename) == 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)

            mobj = RelLib.MediaObject()
            mobj.set_description(description)
            mobj.set_mime_type(mtype)
            name = filename
            mobj.set_path(name)
            self.last_directory = os.path.dirname(filename)

        mobj.set_handle(Utils.create_id())
        if not mobj.get_gramps_id():
            mobj.set_gramps_id(self.dbase.find_next_object_gramps_id())
        trans = self.dbase.transaction_begin()
        self.object = mobj
        self.dbase.commit_media_object(mobj, trans)
        self.dbase.transaction_commit(trans, _("Add Media Object"))

    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 = unicode(fname, sys.getfilesystemencoding())
        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 run(self):
        """
        Run the dialog, returning the selected object.
        """
        while True:
            val = self.window.run()

            if val == gtk.RESPONSE_OK:
                self.save()
                self.close()
                return self.object
            elif val == gtk.RESPONSE_HELP: 
                self.on_help_imagesel_clicked(None)
            else:
                self.close()
                return None
        return None

#-------------------------------------------------------------------------
#
# 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)
    




See more files for this project here

gramps

GRAMPS is a GNOME genealogy program for Linux and FreeBSD that allows you to easily build\r\nand keep track of your family tree.

Project homepage: http://sourceforge.net/projects/gramps
Programming language(s): Python
License: other

  BasicUtils/
    Makefile.am
    _NameDisplay.py
    _UpdateCallback.py
    __init__.py
  Config/
    Makefile.am
    _GrampsConfigKeys.py
    _GrampsGconfKeys.py
    _GrampsIniKeys.py
    __init__.py
    gen_schema_keys.py
  DataViews/
    Makefile.am
    _EventView.py
    _FamilyList.py
    _MapView.py
    _MediaView.py
    _NoteView.py
    _PedigreeView.py
    _PersonView.py
    _PlaceView.py
    _RelationView.py
    _RepositoryView.py
    _SourceView.py
    __init__.py
  DateHandler/
    Makefile.am
    _DateDisplay.py
    _DateHandler.py
  DisplayModels/
  DisplayTabs/
  Editors/
  FilterEditor/
  Filters/
  GrampsDb/
  GrampsDbUtils/
  GrampsLocale/
  GrampsLogger/
  Merge/
  Mime/
  Models/
  ObjectSelector/
  PluginUtils/
  RelLib/
  ReportBase/
  Selectors/
  Simple/
  TreeViews/
  data/
  docgen/
  glade/
  images/
  plugins/
  AddMedia.py
  ArgHandler.py
  Assistant.py
  AutoComp.py
  BaseDoc.py
  Bookmarks.py
  ColumnOrder.py
  Date.py
  DateEdit.py
  DbLoader.py
  DbManager.py
  DbState.py
  DdTargets.py
  DisplayState.py
  Errors.py
  ExportAssistant.py
  ExportOptions.py
  FontScale.py
  GrampsCfg.py
  GrampsDisplay.py
  GrampsWidgets.py
  ImgManip.py
  LdsUtils.py
  ListModel.py
  Lru.py
  Makefile.am
  ManagedWindow.py
  MarkupText.py
  Navigation.py
  PageView.py
  PlaceUtils.py
  ProgressDialog.py
  QuestionDialog.py
  QuickReports.py
  RecentFiles.py
  Relationship.py
  Reorder.py
  ScratchPad.py
  Sort.py
  Spell.py
  SubstKeywords.py
  TipOfDay.py
  ToolTips.py
  TransUtils.py
  TreeTips.py
  UndoHistory.py
  Utils.py
  ViewManager.py
  accent.py
  ansel_utf8.py
  build_cmdplug
  const.py.in
  date_test.py
  gramps.py
  gramps_main.py
  soundex.py