|
| 1 | +# |
| 2 | +# Gramps - a GTK+/GNOME based genealogy program |
| 3 | +# |
| 4 | +# Copyright (C) 2026 Javad Razavian <javadr@gmail.com> |
| 5 | +# |
| 6 | +# This program is free software; you can redistribute it and/or modify |
| 7 | +# it under the terms of the GNU General Public License as published by |
| 8 | +# the Free Software Foundation; either version 2 of the License, or |
| 9 | +# (at your option) any later version. |
| 10 | +# |
| 11 | +# This program is distributed in the hope that it will be useful, |
| 12 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | +# GNU General Public License for more details. |
| 15 | +# |
| 16 | +# You should have received a copy of the GNU General Public License |
| 17 | +# along with this program; if not, write to the Free Software |
| 18 | +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 19 | +# |
| 20 | + |
| 21 | +from gramps.gen.plug import Gramplet |
| 22 | +from gramps.gen.const import GRAMPS_LOCALE as glocale |
| 23 | +from gramps.gen.display.name import displayer as name_displayer |
| 24 | +from gramps.gen.lib.date import Today, Date, gregorian |
| 25 | +import gramps.gen.datehandler |
| 26 | +from gramps.gen.plug.menu import EnumeratedListOption |
| 27 | +try: |
| 28 | + _trans = glocale.get_addon_translator(__file__) |
| 29 | +except ValueError: |
| 30 | + _trans = glocale.translation |
| 31 | +_ = _trans.gettext |
| 32 | + |
| 33 | + |
| 34 | +class DateOfDeathGramplet(Gramplet): |
| 35 | + def init(self): |
| 36 | + self.set_text(_("No Family Tree loaded.")) |
| 37 | + self.sort_mode = 'proximity' |
| 38 | + |
| 39 | + def build_options(self): |
| 40 | + name_sort = _("Sort dates of death by") |
| 41 | + self.opt_sort = EnumeratedListOption(name_sort, self.sort_mode) |
| 42 | + self.opt_sort.add_item("proximity", _("Proximity to current date")) |
| 43 | + self.opt_sort.add_item("month_day", _("Month and day")) |
| 44 | + |
| 45 | + self.add_option(self.opt_sort) |
| 46 | + |
| 47 | + def save_options(self): |
| 48 | + self.sort_mode = self.opt_sort.get_value() |
| 49 | + |
| 50 | + def save_update_options(self, obj): |
| 51 | + self.save_options() |
| 52 | + self.gui.data = [self.sort_mode] |
| 53 | + self.update() |
| 54 | + |
| 55 | + def on_load(self): |
| 56 | + if len(self.gui.data) >= 1: |
| 57 | + self.sort_mode = self.gui.data[0] |
| 58 | + else: |
| 59 | + self.sort_mode = 'proximity' |
| 60 | + |
| 61 | + def db_changed(self): |
| 62 | + self.connect(self.dbstate.db, 'person-add', self.update) |
| 63 | + self.connect(self.dbstate.db, 'person-delete', self.update) |
| 64 | + self.connect(self.dbstate.db, 'person-update', self.update) |
| 65 | + |
| 66 | + def main(self): |
| 67 | + self.set_text(_("Processing...")) |
| 68 | + database = self.dbstate.db |
| 69 | + self.result = [] |
| 70 | + |
| 71 | + for person in database.iter_people(): |
| 72 | + death_ref = person.get_death_ref() |
| 73 | + if not death_ref: |
| 74 | + continue |
| 75 | + death_event = database.get_event_from_handle(death_ref.ref) |
| 76 | + date_of_death = death_event.get_date_object() |
| 77 | + if not date_of_death.is_regular(): |
| 78 | + continue |
| 79 | + |
| 80 | + self.__calculate(database, person) |
| 81 | + |
| 82 | + sort_by = self.opt_sort.get_value() |
| 83 | + if sort_by == "proximity": |
| 84 | + self.result.sort(key=lambda item: -item[0]) |
| 85 | + else: |
| 86 | + self.result.sort(key=lambda item: (item[1].get_month(), |
| 87 | + item[1].get_day())) |
| 88 | + self.clear_text() |
| 89 | + |
| 90 | + for diff_days, date, person, age in self.result: |
| 91 | + name = person.get_primary_name() |
| 92 | + displayer = gramps.gen.datehandler.displayer |
| 93 | + self.append_text("{}: ".format(displayer.display(date))) |
| 94 | + self.link(name_displayer.display_name(name), "Person", |
| 95 | + person.handle) |
| 96 | + if age: |
| 97 | + self.append_text(" ({})\n".format(age)) |
| 98 | + else: |
| 99 | + self.append_text("\n") |
| 100 | + self.append_text("", scroll_to="begin") |
| 101 | + |
| 102 | + def __calculate(self, database, person): |
| 103 | + today = Today() |
| 104 | + death_ref = person.get_death_ref() |
| 105 | + if not death_ref: |
| 106 | + return |
| 107 | + death_event = database.get_event_from_handle(death_ref.ref) |
| 108 | + date_of_death = death_event.get_date_object() |
| 109 | + if not date_of_death.is_regular(): |
| 110 | + return |
| 111 | + |
| 112 | + death_greg = gregorian(date_of_death) |
| 113 | + death_this_year = Date(today.get_year(), |
| 114 | + death_greg.get_month(), |
| 115 | + death_greg.get_day()) |
| 116 | + diff = today - death_this_year |
| 117 | + diff_days = diff[1] * 30 + diff[2] |
| 118 | + |
| 119 | + birth_ref = person.get_birth_ref() |
| 120 | + age = "" |
| 121 | + if birth_ref: |
| 122 | + birth = database.get_event_from_handle(birth_ref.ref) |
| 123 | + birth_date = birth.get_date_object() |
| 124 | + if birth_date.is_regular(): |
| 125 | + age = date_of_death - birth_date |
| 126 | + |
| 127 | + if diff_days <= 0: |
| 128 | + self.result.append((diff_days, date_of_death, person, age)) |
| 129 | + else: |
| 130 | + self.result.append((diff_days - 365, date_of_death, person, age)) |
0 commit comments