From 6ff9ed00f0aea6341ab210e84da16befaf93a666 Mon Sep 17 00:00:00 2001 From: Nick Hall Date: Tue, 21 Oct 2014 19:24:35 +0100 Subject: [PATCH] Add new place locations gramplet --- gramps/plugins/gramplet/gramplet.gpr.py | 14 ++ gramps/plugins/gramplet/locations.py | 168 ++++++++++++++++++++++++ gramps/plugins/lib/libplaceview.py | 1 + 3 files changed, 183 insertions(+) create mode 100644 gramps/plugins/gramplet/locations.py diff --git a/gramps/plugins/gramplet/gramplet.gpr.py b/gramps/plugins/gramplet/gramplet.gpr.py index 034c88f90..46f6b54e5 100644 --- a/gramps/plugins/gramplet/gramplet.gpr.py +++ b/gramps/plugins/gramplet/gramplet.gpr.py @@ -1232,3 +1232,17 @@ register(GRAMPLET, gramplet = 'SoundGen', gramplet_title=_("SoundEx"), ) + +register(GRAMPLET, + id="Place Locations", + name=_("Place Locations"), + description = _("Gramplet showing the locations of a place over time"), + version="1.0.0", + gramps_target_version=MODULE_VERSION, + status = STABLE, + fname="locations.py", + height=200, + gramplet = 'Locations', + gramplet_title=_("Locations"), + navtypes=["Place"], + ) diff --git a/gramps/plugins/gramplet/locations.py b/gramps/plugins/gramplet/locations.py new file mode 100644 index 000000000..4672f7fd7 --- /dev/null +++ b/gramps/plugins/gramplet/locations.py @@ -0,0 +1,168 @@ +# Gramps - a GTK+/GNOME based genealogy program +# +# Copyright (C) 2014 Nick Hall +# +# 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +# + +#------------------------------------------------------------------------- +# +# Python modules +# +#------------------------------------------------------------------------- +from gi.repository import Gtk + +#------------------------------------------------------------------------- +# +# Gramps modules +# +#------------------------------------------------------------------------- +from gramps.gen.plug import Gramplet +from gramps.gui.dbguielement import DbGUIElement +from gramps.gui.editors import EditPlace +from gramps.gen.errors import WindowActiveError +from gramps.gui.listmodel import ListModel, NOSORT +from gramps.gen.datehandler import get_date +from gramps.gen.const import GRAMPS_LOCALE as glocale +_ = glocale.translation.gettext + +#------------------------------------------------------------------------- +# +# Locations gramplet +# +#------------------------------------------------------------------------- +class Locations(Gramplet, DbGUIElement): + + def __init__(self, gui, nav_group=0): + Gramplet.__init__(self, gui, nav_group) + DbGUIElement.__init__(self, self.dbstate.db) + + """ + Gramplet showing the locations of a place over time. + """ + def init(self): + self.gui.WIDGET = self.build_gui() + self.gui.get_container_widget().remove(self.gui.textview) + self.gui.get_container_widget().add(self.gui.WIDGET) + self.gui.WIDGET.show() + + def _connect_db_signals(self): + """ + called on init of DbGUIElement, connect to db as required. + """ + self.callman.register_callbacks({'place-update': self.changed}) + self.callman.connect_all(keys=['place']) + + def db_changed(self): + self.connect_signal('Place', self.update) + + def changed(self, handle): + """ + Called when a registered place is updated. + """ + self.update() + + def build_gui(self): + """ + Build the GUI interface. + """ + tip = _('Double-click on a row to edit the selected place.') + self.set_tooltip(tip) + top = Gtk.TreeView() + titles = [('', 0, 50), + (_('Name'), 1, 300), + (_('Type'), 2, 150), + (_('Date'), 4, 150), + ('', NOSORT, 50)] + self.model = ListModel(top, titles, list_mode="tree", + event_func=self.edit_place) + return top + + def active_changed(self, handle): + self.update() + + def update_has_data(self): + active_handle = self.get_active('Place') + if active_handle: + active = self.dbstate.db.get_place_from_handle(active_handle) + self.set_has_data(self.get_has_data(active)) + else: + self.set_has_data(False) + + def main(self): + self.model.clear() + self.callman.unregister_all() + active_handle = self.get_active('Place') + if active_handle: + active = self.dbstate.db.get_place_from_handle(active_handle) + if active: + self.display_place(active, None, [active_handle]) + else: + self.set_has_data(False) + else: + self.set_has_data(False) + + def display_place(self, place, node, visited): + """ + Display the location hierarchy for the active place. + """ + self.callman.register_obj(place) + for placeref in place.get_placeref_list(): + if placeref.ref in visited: + continue + + place_date = get_date(placeref) + place_sort = '%012d' % placeref.get_date_object().get_sort_value() + parent_place = self.dbstate.db.get_place_from_handle(placeref.ref) + parent_name = parent_place.get_name() + parent_type = str(parent_place.get_type()) + + parent_node = self.model.add([placeref.ref, + parent_name, + parent_type, + place_date, + place_sort], + node=node) + + self.display_place(parent_place, + parent_node, + visited + [placeref.ref]) + + self.set_has_data(self.model.count > 0) + self.model.tree.expand_all() + + def get_has_data(self, place): + """ + Return True if the gramplet has data, else return False. + """ + if place is None: + return False + if len(place.get_placeref_list()) > 0: + return True + else: + return False + + def edit_place(self, treeview): + """ + Edit the selected place. + """ + model, iter_ = treeview.get_selection().get_selected() + if iter_: + handle = model.get_value(iter_, 0) + place = self.dbstate.db.get_place_from_handle(handle) + try: + EditPlace(self.dbstate, self.uistate, [], place) + except WindowActiveError: + pass diff --git a/gramps/plugins/lib/libplaceview.py b/gramps/plugins/lib/libplaceview.py index caba2f782..2f4b6f02b 100644 --- a/gramps/plugins/lib/libplaceview.py +++ b/gramps/plugins/lib/libplaceview.py @@ -464,6 +464,7 @@ class PlaceBaseView(ListView): """ return (("Place Filter",), ("Place Details", + "Place Locations", "Place Gallery", "Place Citations", "Place Notes",