gramps/src/EditPlace.py

684 lines
21 KiB
Python
Raw Normal View History

#! /usr/bin/python -O
#
# Gramps - a GTK+/GNOME based genealogy program
#
# Copyright (C) 2000 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
#
#-------------------------------------------------------------------------
#
# Standard python modules
#
#-------------------------------------------------------------------------
import os
import string
#-------------------------------------------------------------------------
#
# GTK/Gnome modules
#
#-------------------------------------------------------------------------
from gtk import *
from gnome.ui import *
import gnome.mime
import libglade
#-------------------------------------------------------------------------
#
# gramps modules
#
#-------------------------------------------------------------------------
import intl
import const
import utils
from RelLib import *
import RelImage
_ = intl.gettext
#-------------------------------------------------------------------------
#
# Constants
#
#-------------------------------------------------------------------------
INDEX = "i"
PLACE = "p"
class EditPlace:
def __init__(self,place,db,func):
self.place = place
self.db = db
self.callback = func
self.path = db.getSavePath()
self.not_loaded = 1
self.selectedIcon = -1
self.currentImages = []
self.top_window = libglade.GladeXML(const.placesFile,"placeEditor")
self.title = self.top_window.get_widget("place_title")
self.city = self.top_window.get_widget("city")
self.county = self.top_window.get_widget("county")
self.state = self.top_window.get_widget("state")
self.country = self.top_window.get_widget("country")
self.longitude = self.top_window.get_widget("longitude")
self.latitude = self.top_window.get_widget("latitude")
self.note = self.top_window.get_widget("place_note")
2001-08-19 23:01:52 +05:30
self.web_list = self.top_window.get_widget("web_list")
self.web_url = self.top_window.get_widget("url_addr")
self.web_description = self.top_window.get_widget("url_des")
self.ulist = place.getUrlList()[:]
self.urls_changed = 0
self.title.set_text(place.get_title())
mloc = place.get_main_location()
self.city.set_text(mloc.get_city())
self.county.set_text(mloc.get_county())
self.state.set_text(mloc.get_state())
self.country.set_text(mloc.get_country())
self.longitude.set_text(place.get_longitude())
self.latitude.set_text(place.get_latitude())
self.note.set_point(0)
self.note.insert_defaults(place.getNote())
self.note.set_word_wrap(1)
self.photo_list = self.top_window.get_widget("photolist")
self.top_window.signal_autoconnect({
"destroy_passed_object" : utils.destroy_passed_object,
"on_photolist_select_icon" : on_photo_select_icon,
"on_photolist_button_press_event" : on_photolist_button_press_event,
"on_switch_page" : on_switch_page,
"on_addphoto_clicked" : on_add_photo_clicked,
2001-08-19 23:01:52 +05:30
"on_browse_clicked": on_browse_clicked,
"on_deletephoto_clicked" : on_delete_photo_clicked,
2001-08-19 23:01:52 +05:30
"on_add_url_clicked" : on_add_url_clicked,
"on_delete_url_clicked" : on_delete_url_clicked,
"on_update_url_clicked" : on_update_url_clicked,
"on_web_list_select_row" : on_web_list_select_row,
"on_apply_clicked" : on_place_apply_clicked
})
self.top = self.top_window.get_widget("placeEditor")
self.top.set_data(PLACE,self)
if self.place.getId() == "":
self.top_window.get_widget("add_photo").set_sensitive(0)
self.top_window.get_widget("delete_photo").set_sensitive(0)
2001-08-19 23:01:52 +05:30
self.web_list.set_data(PLACE,self)
self.web_list.set_data(INDEX,-1)
self.redraw_url_list()
#-------------------------------------------------------------------------
#
#
#
#-------------------------------------------------------------------------
def update_urls(self):
self.place.setUrlList(self.ulist)
#---------------------------------------------------------------------
#
# redraw_url_list - redraws the altername name list for the person
#
#---------------------------------------------------------------------
def redraw_url_list(self):
self.web_list.freeze()
self.web_list.clear()
self.web_index = 0
for url in self.ulist:
self.web_list.append([url.get_path(),url.get_description()])
self.web_list.set_row_data(self.web_index,url)
self.web_index = self.web_index + 1
current_row = self.web_list.get_data(INDEX)
if self.web_index > 0:
if current_row <= 0:
current_row = 0
elif self.web_index <= current_row:
current_row = current_row - 1
self.web_list.select_row(current_row,0)
self.web_list.moveto(current_row,0)
self.web_list.set_data(INDEX,current_row)
self.web_list.thaw()
#-------------------------------------------------------------------------
#
# add_thumbnail - Scale the image and add it to the IconList.
#
#-------------------------------------------------------------------------
def add_thumbnail(self,photo):
src = photo.getPath()
thumb = self.db.getSavePath() + os.sep + ".thumb" + os.sep + \
os.path.basename(src)
RelImage.check_thumb(src,thumb,const.thumbScale)
self.photo_list.append(thumb,photo.getDescription())
#-------------------------------------------------------------------------
#
# load_images - clears the currentImages list to free up any cached
# Imlibs. Then add each photo in the place's list of photos to the
# photolist window.
#
#-------------------------------------------------------------------------
def load_images(self):
if len(self.place.getPhotoList()) == 0:
return
self.photo_list.freeze()
self.photo_list.clear()
for photo in self.place.getPhotoList():
self.add_thumbnail(photo)
self.photo_list.thaw()
#-----------------------------------------------------------------------------
#
#
#
#-----------------------------------------------------------------------------
def on_place_apply_clicked(obj):
edit = obj.get_data(PLACE)
title = edit.title.get_text()
city = edit.city.get_text()
county = edit.county.get_text()
state = edit.state.get_text()
country = edit.country.get_text()
longitude = edit.longitude.get_text()
latitude = edit.latitude.get_text()
note = edit.note.get_chars(0,-1)
mloc = edit.place.get_main_location()
if city != mloc.get_city():
mloc.set_city(city)
utils.modified()
if state != mloc.get_state():
mloc.set_state(state)
utils.modified()
if county != mloc.get_county():
mloc.set_county(county)
utils.modified()
if country != mloc.get_country():
mloc.set_country(country)
utils.modified()
if title != edit.place.get_title():
edit.place.set_title(title)
utils.modified()
if longitude != edit.place.get_longitude():
edit.place.set_longitude(longitude)
utils.modified()
if latitude != edit.place.get_latitude():
edit.place.set_latitude(latitude)
utils.modified()
if note != edit.place.getNote():
edit.place.setNote(note)
utils.modified()
2001-08-19 23:01:52 +05:30
edit.update_urls()
if edit.urls_changed:
utils.modified()
utils.destroy_passed_object(edit.top)
edit.callback(edit.place)
#-------------------------------------------------------------------------
#
#
#
#-------------------------------------------------------------------------
def on_switch_page(obj,a,page):
src = obj.get_data(PLACE)
if page == 2 and src.not_loaded:
src.not_loaded = 0
src.load_images()
#-------------------------------------------------------------------------
#
#
#
#-------------------------------------------------------------------------
def on_photo_select_icon(obj,iconNumber,event):
obj.get_data(PLACE).selectedIcon = iconNumber
#-------------------------------------------------------------------------
#
#
#
#-------------------------------------------------------------------------
def on_delete_photo_clicked(obj):
edit_place_obj = obj.get_data(PLACE)
icon = edit_place_obj.selectedIcon
if icon == -1:
return
photolist = edit_place_obj.place.getPhotoList()
edit_place_obj.photo_list.remove(icon)
del photolist[edit_place_obj.selectedIcon]
#-------------------------------------------------------------------------
#
#
#
#-------------------------------------------------------------------------
def on_add_photo_clicked(obj):
edit_place = obj.get_data(PLACE)
image_select = libglade.GladeXML(const.imageselFile,"imageSelect")
edit_place.isel = image_select
image_select.signal_autoconnect({
"on_savephoto_clicked" : on_savephoto_clicked,
"on_name_changed" : on_name_changed,
"destroy_passed_object" : utils.destroy_passed_object
})
edit_place.fname = image_select.get_widget("fname")
edit_place.add_image = image_select.get_widget("image")
edit_place.external = image_select.get_widget("private")
image_select.get_widget("imageSelect").set_data(PLACE,edit_place)
image_select.get_widget("imageSelect").show()
#-------------------------------------------------------------------------
#
#
#
#-------------------------------------------------------------------------
def on_savephoto_clicked(obj):
edit_place_obj = obj.get_data(PLACE)
image_select = edit_place_obj.isel
filename = image_select.get_widget("photosel").get_full_path(0)
description = image_select.get_widget("photoDescription").get_text()
if os.path.exists(filename) == 0:
return
prefix = "s%s" % edit_place_obj.place.getId()
if edit_place_obj.external.get_active() == 1:
if os.path.isfile(filename):
name = filename
else:
return
else:
name = RelImage.import_photo(filename,edit_place_obj.path,prefix)
if name == None:
return
photo = Photo()
photo.setPath(name)
photo.setDescription(description)
edit_place_obj.place.addPhoto(photo)
edit_place_obj.add_thumbnail(photo)
utils.modified()
utils.destroy_passed_object(obj)
#-------------------------------------------------------------------------
#
#
#
#-------------------------------------------------------------------------
def on_photolist_button_press_event(obj,event):
myobj = obj.get_data(PLACE)
icon = myobj.selectedIcon
if icon == -1:
return
if event.button == 3:
photo = myobj.place.getPhotoList()[icon]
menu = GtkMenu()
item = GtkTearoffMenuItem()
item.show()
view = GtkMenuItem(_("View Image"))
view.set_data("m",myobj)
view.connect("activate",on_view_photo)
view.show()
edit = GtkMenuItem(_("Edit Image"))
edit.set_data("m",myobj)
edit.connect("activate",on_edit_photo)
edit.show()
change = GtkMenuItem(_("Edit Description"))
change.set_data("m",myobj)
change.connect("activate",on_change_description)
change.show()
menu.append(item)
menu.append(view)
menu.append(edit)
menu.append(change)
if photo.getPrivate() == 0:
private = GtkMenuItem(_("Convert to private copy"))
private.set_data("m",myobj)
private.connect("activate",on_convert_to_private)
private.show()
menu.append(private)
menu.popup(None,None,None,0,0)
#-------------------------------------------------------------------------
#
#
#
#-------------------------------------------------------------------------
def on_convert_to_private(obj):
edit_place_obj = obj.get_data("m")
photo = edit_place_obj.place.getPhotoList()[edit_place_obj.selectedIcon]
prefix = "i%s" % edit_place_obj.place.getId()
name = RelImage.import_photo(photo.getPath(),edit_place_obj.path,prefix)
photo.setPath(name)
photo.setPrivate(1)
#-------------------------------------------------------------------------
#
#
#
#-------------------------------------------------------------------------
def on_view_photo(obj):
myobj = obj.get_data("m")
photo = myobj.place.getPhotoList()[myobj.selectedIcon]
type = gnome.mime.type(photo.getPath())
prog = string.split(gnome.mime.get_value(type,'view'))
args = []
for val in prog:
if val == "%f":
args.append(photo.getPath())
else:
args.append(val)
if os.fork() == 0:
os.execvp(args[0],args)
#-------------------------------------------------------------------------
#
#
#
#-------------------------------------------------------------------------
def on_edit_photo(obj):
myobj = obj.get_data("m")
photo = myobj.place.getPhotoList()[myobj.selectedIcon]
if os.fork() == 0:
os.execvp(const.editor,[const.editor, photo.getPath()])
#-------------------------------------------------------------------------
#
#
#
#-------------------------------------------------------------------------
def on_change_description(obj):
myobj = obj.get_data("m")
photo = myobj.place.getPhotoList()[myobj.selectedIcon]
window = libglade.GladeXML(const.imageselFile,"dialog1")
text = window.get_widget("text")
text.set_text(photo.getDescription())
image2 = RelImage.scale_image(photo.getPath(),200.0)
window.get_widget("photo").load_imlib(image2)
window.get_widget("dialog1").set_data("p",photo)
window.get_widget("dialog1").set_data("t",text)
window.get_widget("dialog1").set_data("m",obj.get_data("m"))
window.signal_autoconnect({
"on_cancel_clicked" : utils.destroy_passed_object,
"on_ok_clicked" : on_ok_clicked,
"on_apply_clicked" : on_apply_clicked
})
#-------------------------------------------------------------------------
#
#
#
#-------------------------------------------------------------------------
def on_ok_clicked(obj):
on_apply_clicked(obj)
utils.destroy_passed_object(obj)
#-------------------------------------------------------------------------
#
#
#
#-------------------------------------------------------------------------
def on_apply_clicked(obj):
photo = obj.get_data("p")
text = obj.get_data("t").get_text()
if text != photo.getDescription():
photo.setDescription(text)
edit_window = obj.get_data("m")
edit_window.load_images()
utils.modified()
#-------------------------------------------------------------------------
#
#
#
#-------------------------------------------------------------------------
def on_name_changed(obj):
edit_person = obj.get_data(PLACE)
file = edit_person.fname.get_text()
if os.path.isfile(file):
image = RelImage.scale_image(file,const.thumbScale)
edit_person.add_image.load_imlib(image)
2001-08-19 23:01:52 +05:30
#-------------------------------------------------------------------------
#
#
#
#-------------------------------------------------------------------------
def on_update_url_clicked(obj):
row = obj.get_data(INDEX)
if row < 0:
return
UrlEditor(obj.get_data(PLACE),obj.get_row_data(row))
#-------------------------------------------------------------------------
#
#
#
#-------------------------------------------------------------------------
def on_delete_url_clicked(obj):
row = obj.get_data(INDEX)
if row < 0:
return
epo = obj.get_data(PLACE)
del epo.ulist[row]
if row > len(epo.ulist)-1:
obj.set_data(INDEX,row-1)
epo.redraw_url_list()
utils.modified()
#-------------------------------------------------------------------------
#
#
#
#-------------------------------------------------------------------------
def on_add_url_clicked(obj):
epo = obj.get_data(PLACE)
UrlEditor(obj.get_data(PLACE),None)
#-------------------------------------------------------------------------
#
# UrlEditor class
#
#-------------------------------------------------------------------------
class UrlEditor:
def __init__(self,parent,url):
self.parent = parent
self.url = url
self.top = libglade.GladeXML(const.editPersonFile, "url_edit")
self.window = self.top.get_widget("url_edit")
self.des = self.top.get_widget("url_des")
self.addr = self.top.get_widget("url_addr")
self.priv = self.top.get_widget("priv")
if parent.place:
name = _("Internet Address Editor for %s") % parent.place.get_title()
else:
name = _("Internet Address Editor")
self.top.get_widget("urlTitle").set_text(name)
if url != None:
self.des.set_text(url.get_description())
self.addr.set_text(url.get_path())
self.priv.set_active(url.getPrivacy())
self.window.set_data("o",self)
self.top.signal_autoconnect({
"destroy_passed_object" : utils.destroy_passed_object,
"on_url_edit_ok_clicked" : on_url_edit_ok_clicked
})
#-------------------------------------------------------------------------
#
#
#
#-------------------------------------------------------------------------
def on_url_edit_ok_clicked(obj):
ee = obj.get_data("o")
url = ee.url
des = ee.des.get_text()
addr = ee.addr.get_text()
priv = ee.priv.get_active()
if url == None:
url = Url()
ee.parent.ulist.append(url)
if update_url(url,des,addr,priv):
ee.parent.urls_changed = 1
ee.parent.redraw_url_list()
utils.destroy_passed_object(obj)
#-------------------------------------------------------------------------
#
#
#
#-------------------------------------------------------------------------
def get_detail_flags(obj):
detail = ""
if Config.show_detail:
if obj.getNote() != "":
detail = "N"
if obj.getSourceRef().getBase():
detail = detail + "S"
if obj.getPrivacy():
detail = detail + "P"
return detail
#-------------------------------------------------------------------------
#
#
#
#-------------------------------------------------------------------------
def get_detail_text(obj):
if obj.getNote() != "":
details = "%s" % _("Note")
else:
details = ""
if obj.getSourceRef().getBase() != None:
if details == "":
details = _("Source")
else:
details = "%s, %s" % (details,_("Source"))
if obj.getPrivacy() == 1:
if details == "":
details = _("Private")
else:
details = "%s, %s" % (details,_("Private"))
return details
#-------------------------------------------------------------------------
#
# on_name_list_select_row - sets the row object attached to the passed
# object, and then updates the display with the data corresponding to
# the row.
#
#-------------------------------------------------------------------------
def on_web_list_select_row(obj,row,b,c):
obj.set_data(INDEX,row)
epo = obj.get_data(PLACE)
url = obj.get_row_data(row)
epo.web_url.set_text(": %s " % url.get_path())
epo.web_description.set_text(": %s" % url.get_description())
#-------------------------------------------------------------------------
#
# update_attrib
#
# Updates the specified event with the specified date. Compares against
# the previous value, so the that modified flag is not set if nothing has
# actually changed.
#
#-------------------------------------------------------------------------
def update_url(url,des,addr,priv):
changed = 0
if url.get_path() != addr:
url.set_path(addr)
changed = 1
if url.get_description() != des:
url.set_description(des)
changed = 1
if url.getPrivacy() != priv:
url.setPrivacy(priv)
changed = 1
return changed
#-------------------------------------------------------------------------
#
#
#
#-------------------------------------------------------------------------
def on_browse_clicked(obj):
import gnome.url
path = obj.get()[2:]
if path != "":
gnome.url.show(path)