forked from gramps-project/addons-source
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDataEntryGramplet.py
More file actions
1071 lines (1037 loc) · 54.4 KB
/
Copy pathDataEntryGramplet.py
File metadata and controls
1071 lines (1037 loc) · 54.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# Gramps - a GTK+/GNOME based genealogy program
#
# Copyright (C) 2007-2009 Douglas S. Blank <doug.blank@gmail.com>
# Copyright (C) 2011 Gary Burton
#
# 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.
# $Id$
#------------------------------------------------------------------------
#
# GRAMPS modules
#
#------------------------------------------------------------------------
from gramps.gen.plug import Gramplet
from gramps.gen.display.name import displayer as name_displayer
from gramps.gen.display.place import displayer as place_displayer
from gramps.gen.utils.db import get_birth_or_fallback, get_death_or_fallback
from gramps.gen.datehandler import get_date, parser
from gramps.gen.errors import WindowActiveError
from gramps.gen.lib import (Person, FamilyRelType, Family, ChildRef, Place,
Event, EventType, EventRef, Source, Citation,
Name, NameType, Surname, PlaceName)
from gramps.gen.const import GRAMPS_LOCALE as glocale
from gramps.gen.db import DbTxn
import gramps.gen.lib
try:
_trans = glocale.get_addon_translator(__file__)
except ValueError:
_trans = glocale.translation
_ = _trans.gettext
#------------------------------------------------------------------------
#
# Gramplet class
#
#------------------------------------------------------------------------
class DataEntryGramplet(Gramplet):
NO_REL = 0
AS_PARENT = 1
AS_MOTHER = 2
AS_FATHER = 3
AS_SPOUSE = 4
AS_WIFE = 5
AS_HUSBAND = 6
AS_SIBLING = 7
AS_CHILD = 8
def init(self):
self.de_column_width = 20
self.de_source_width = 10
self.show_source = True
from gi.repository import Gtk
rows = Gtk.VBox()
self._dirty = False
self._dirty_person = None
self._dirty_family = None
self.de_widgets = {}
for items in [("Active person", _("Active person"), None, True,
[("Edit person", "", "button", self.edit_person),
("Edit family", _("Family:"), "button", self.edit_family),
("Show sources", _("Sources?"), "checkbox", self.toggle_sources)],
False, 0, None),
("APName", _("Surname, Given"), None, False, [], True, 0, None),
("APGender", _("Gender"), [_("female"), _("male"), _("unknown"), _("other")],
False, [], True, 2, (_("Source"),
"APSource")),
("APBirth", _("Birth"), None, False, [], True, 0, (_("Source"), "APBirthSource")),
("APDeath", _("Death"), None, False, [], True, 0, (_("Source"), "APDeathSource"))
]:
pos, text, choices, readonly, callback, dirty, default, source = items
row = self.make_row(pos, text, choices, readonly, callback, dirty, default, source)
rows.pack_start(row, False, False, 0)
# Save and Abandon
row = Gtk.HBox()
button = Gtk.Button(_("Save"))
button.connect("clicked", self.save_data_edit)
row.pack_start(button, True, True, 0)
button = Gtk.Button(_("Abandon"))
button.connect("clicked", self.abandon_data_edit)
row.pack_start(button, True, True, 0)
rows.pack_start(row, False, False, 0)
for items in [("New person", _("New person"), None, True, 0, None),
("NPRelation", _("Add relation"),
[_("No relation to active person"),
_("Add as a Parent"),
_("Add as a Mother"),
_("Add as a Father"),
_("Add as a Spouse"),
_("Add as a Wife"),
_("Add as a Husband"),
_("Add as a Sibling"),
_("Add as a Child")],
False, 0, None),
("NPName", _("Surname, Given"), None, False, 0, None),
("NPGender", _("Gender"),
[_("female"), _("male"), _("unknown"), _("other")], False, 2, (_("Source"), "NPSource")),
("NPBirth", _("Birth"), None, False, 0, (_("Source"), "NPBirthSource")),
("NPDeath", _("Death"), None, False, 0, (_("Source"), "NPDeathSource"))
]:
pos, text, choices, readonly, default, source = items
row = self.make_row(pos, text, choices, readonly, default=default, source=source)
rows.pack_start(row, False, False, 0)
# Save, Abandon, Clear
row = Gtk.HBox()
button = Gtk.Button(_("Add"))
button.connect("clicked", self.add_data_entry)
row.pack_start(button, True, True, 0)
button = Gtk.Button(_("Copy Active Data"))
button.connect("clicked", self.copy_data_entry)
row.pack_start(button, True, True, 0)
button = Gtk.Button(_("Clear"))
button.connect("clicked", self.clear_data_entry)
row.pack_start(button, True, True, 0)
rows.pack_start(row, False, False, 0)
self.gui.get_container_widget().remove(self.gui.textview)
self.gui.get_container_widget().add_with_viewport(rows)
rows.show_all()
self.clear_data_entry(None)
def main(self): # return false finishes
if self._dirty:
return
self.de_widgets["Active person:Edit family"].hide()
self.de_widgets["Active person:Edit family:Label"].hide()
active_person = self.get_active_object("Person")
self._dirty_person = active_person
self._dirty_family = None
if active_person:
self.de_widgets["Active person:Edit person"].show()
self.de_widgets["Active person:Edit family"].hide()
self.de_widgets["Active person:Edit family:Label"].hide()
# Fill in current person edits:
name = name_displayer.display(active_person)
self.de_widgets["Active person"].set_text("<i>%s</i> " % name)
self.de_widgets["Active person"].set_use_markup(True)
# Name:
name_obj = active_person.get_primary_name()
if name_obj:
self.de_widgets["APName"].set_text("%s, %s" %
(name_obj.get_surname(), name_obj.get_first_name()))
self.de_widgets["APGender"].set_active(active_person.get_gender()) # gender
# Birth:
birth = get_birth_or_fallback(self.dbstate.db, active_person)
birth_text = ""
if birth:
sdate = get_date(birth)
birth_text += sdate + " "
place_text = place_displayer.display_event(self.dbstate.db, birth)
if place_text:
birth_text += _("in") + " " + place_text
self.de_widgets["APBirth"].set_text(birth_text)
# Death:
death = get_death_or_fallback(self.dbstate.db, active_person)
death_text = ""
if death:
sdate = get_date(death)
death_text += sdate + " "
place_text = place_displayer.display_event(self.dbstate.db, death)
if place_text:
death_text += _("in") + " " + place_text
self.de_widgets["APDeath"].set_text(death_text)
family_list = active_person.get_family_handle_list()
if len(family_list) > 0:
self._dirty_family = self.dbstate.db.get_family_from_handle(family_list[0])
self.de_widgets["Active person:Edit family"].show()
self.de_widgets["Active person:Edit family:Label"].show()
else:
family_list = active_person.get_parent_family_handle_list()
if len(family_list) > 0:
self._dirty_family = self.dbstate.db.get_family_from_handle(family_list[0])
self.de_widgets["Active person:Edit family"].show()
self.de_widgets["Active person:Edit family:Label"].show()
# lookup sources:
self.de_widgets["APSource"].set_text(self.get_last_source_title(active_person))
self.de_widgets["APBirthSource"].set_text(self.get_last_source_title(birth))
self.de_widgets["APDeathSource"].set_text(self.get_last_source_title(death))
else:
self.clear_data_edit(None)
self.de_widgets["Active person:Edit person"].hide()
self.de_widgets["Active person:Edit family"].hide()
self.de_widgets["Active person:Edit family:Label"].hide()
self._dirty = False
def make_row(self, pos, text, choices=None, readonly=False, callback_list=[],
mark_dirty=False, default=0, source=None):
from gi.repository import Gtk
# Data Entry: Active Person
row = Gtk.HBox()
label = Gtk.Label()
if readonly:
label.set_text("<b>%s</b>" % text)
label.set_width_chars(self.de_column_width)
label.set_use_markup(True)
self.de_widgets[pos] = Gtk.Label()
self.de_widgets[pos].set_alignment(0.0, 0.5)
self.de_widgets[pos].set_use_markup(True)
label.set_alignment(0.0, 0.5)
row.pack_start(label, False, False, 0)
row.pack_start(self.de_widgets[pos], False, False, 0)
else:
label.set_text("%s: " % text)
label.set_width_chars(self.de_column_width)
label.set_alignment(1.0, 0.5)
if choices == None:
self.de_widgets[pos] = Gtk.Entry()
if mark_dirty:
self.de_widgets[pos].connect("changed", self.mark_dirty)
row.pack_start(label, False, False, 0)
row.pack_start(self.de_widgets[pos], True, True, 0)
else:
eventBox = Gtk.EventBox()
self.de_widgets[pos] = Gtk.ComboBoxText()
eventBox.add(self.de_widgets[pos])
for add_type in choices:
self.de_widgets[pos].append_text(add_type)
self.de_widgets[pos].set_active(default)
if mark_dirty:
self.de_widgets[pos].connect("changed", self.mark_dirty)
row.pack_start(label, False, False, 0)
row.pack_start(eventBox, True, True, 0)
if source:
label = Gtk.Label()
label.set_text("%s: " % source[0])
label.set_width_chars(self.de_source_width)
label.set_alignment(1.0, 0.5)
self.de_widgets[source[1] + ":Label"] = label
self.de_widgets[source[1]] = Gtk.Entry()
if mark_dirty:
self.de_widgets[source[1]].connect("changed", self.mark_dirty)
row.pack_start(label, False, False, 0)
row.pack_start(self.de_widgets[source[1]], True, True, 0)
if not self.show_source:
self.de_widgets[source[1]].hide()
for name, text, cbtype, callback in callback_list:
if cbtype == "button":
label = Gtk.Label()
label.set_text(text)
self.de_widgets[pos + ":" + name + ":Label"] = label
row.pack_start(label, False, False, 0)
icon = Gtk.STOCK_EDIT
size = Gtk.IconSize.MENU
button = Gtk.Button()
image = Gtk.Image()
image.set_from_stock(icon, size)
button.add(image)
button.set_relief(Gtk.ReliefStyle.NONE)
button.connect("clicked", callback)
self.de_widgets[pos + ":" + name] = button
row.pack_start(button, False, False, 0)
elif cbtype == "checkbox":
button = Gtk.CheckButton(text)
button.set_active(True)
button.connect("clicked", callback)
self.de_widgets[pos + ":" + name] = button
row.pack_start(button, False, False, 0)
row.show_all()
return row
def mark_dirty(self, obj):
self._dirty = True
def abandon_data_edit(self, obj):
self._dirty = False
self.update()
def edit_callback(self, person):
self._dirty = False
self.update()
def edit_person(self, obj):
if self._dirty_person:
from gramps.gui.editors import EditPerson
try:
EditPerson(self.gui.dbstate,
self.gui.uistate, [],
self._dirty_person,
callback=self.edit_callback)
except WindowActiveError:
pass
else:
return
def edit_family(self, obj):
if self._dirty_family:
from gramps.gui.editors import EditFamily
try:
EditFamily(self.gui.dbstate,
self.gui.uistate, [],
self._dirty_family)
except WindowActiveError:
pass
else:
return
def process_dateplace(self, text):
if text == "": return None, None
prep_in = _("in") # word or phrase that separates date from place
text = text.strip()
if (" %s " % prep_in) in text:
date, place = text.split((" %s " % prep_in), 1)
elif text.startswith("%s " % prep_in):
date, place = text.split(("%s " % prep_in), 1)
else:
date, place = text, ""
date = date.strip()
place = place.strip()
if date != "":
date = parser.parse(date)
else:
date = None
if place != "":
newq, place = self.get_or_create_place(place)
else:
place = None
return date, place
def get_or_create_place(self, place_name):
if place_name == "": return (-1, None)
place_list = self.dbstate.db.get_place_handles()
for place_handle in place_list:
place = self.dbstate.db.get_place_from_handle(place_handle)
place_title = place_displayer.display(self.dbstate.db, place)
if place_title.strip() == place_name:
return (0, place) # (old, object)
place = Place()
place.set_title(place_name)
place.set_name(PlaceName(value=place_name))
self.dbstate.db.add_place(place,self.trans)
return (1, place) # (new, object)
def get_or_create_event(self, object, type, date, place):
""" Add or find a type event on object """
if date == place == None: return (-1, None)
# first, see if it exists
ref_list = object.get_event_ref_list()
# look for a match, and possible correction
for ref in ref_list:
event = self.dbstate.db.get_event_from_handle(ref.ref)
if event is not None:
if int(event.get_type()) == type:
# Match! Let's update
if date:
event.set_date_object(date)
if place:
event.set_place_handle(place.get_handle())
self.dbstate.db.commit_event(event, self.trans)
return (0, event)
# else create it:
event = Event()
if type:
event.set_type(EventType(type))
if date:
event.set_date_object(date)
if place:
event.set_place_handle(place.get_handle())
self.dbstate.db.add_event(event, self.trans)
return (1, event)
def get_or_create_source(self, source_text):
source_list = self.dbstate.db.get_source_handles()
for source_handle in source_list:
source = self.dbstate.db.get_source_from_handle(source_handle)
if source.get_title() == source_text:
return (0, source)
source = Source()
source.set_title(source_text)
self.dbstate.db.add_source(source, self.trans)
return (1, source)
def get_last_source_title(self, obj):
if obj:
citation_list = obj.get_citation_list()
if len(citation_list) > 0:
citation_handle = citation_list[-1]
citation = self.dbstate.db.get_citation_from_handle(citation_handle)
if citation:
source = self.dbstate.db.get_source_from_handle(citation.source_handle)
if source:
return source.get_title()
return ""
def make_event(self, type, date, place):
if date == place == None: return None
event = Event()
event.set_type(EventType(type))
if date:
event.set_date_object(date)
if place:
event.set_place_handle(place.get_handle())
self.dbstate.db.add_event(event, self.trans)
return event
def make_person(self, firstname, surname, gender):
person = Person()
name = Name()
name.set_type(NameType(NameType.BIRTH))
name.set_first_name(firstname)
surname_obj = Surname()
surname_obj.set_surname(surname)
name.set_surname_list([surname_obj])
name.set_primary_surname(0)
person.set_primary_name(name)
person.set_gender(gender)
return person
def save_data_edit(self, obj):
if self._dirty:
if not self.dbstate.is_open():
from gramps.gui.dialog import ErrorDialog
ErrorDialog(_("No Family Tree is open."),
_("Please open a Family Tree to edit data."))
return
# Save the edits ----------------------------------
person = self._dirty_person
# First, get the data:
gender = self.de_widgets["APGender"].get_active()
if "," in self.de_widgets["APName"].get_text():
surname, firstname = self.de_widgets["APName"].get_text().split(",", 1)
else:
surname, firstname = self.de_widgets["APName"].get_text(), ""
surname = surname.strip()
firstname = firstname.strip()
if person:
name = person.get_primary_name()
else:
return
# Now, edit it:
with DbTxn(_("Gramplet Data Edit: %s") % name_displayer.display(person), self.dbstate.db) as self.trans:
surname_obj = name.get_primary_surname()
surname_obj.set_surname(surname)
name.set_first_name(firstname)
person.set_gender(gender)
birthdate, birthplace = self.process_dateplace(self.de_widgets["APBirth"].get_text().strip())
new, birthevent = self.get_or_create_event(person, EventType.BIRTH, birthdate, birthplace)
# reference it, if need be:
birthref = person.get_birth_ref()
if birthevent:
if birthref is None:
# need new
birthref = EventRef()
birthref.set_reference_handle(birthevent.get_handle())
person.set_birth_ref(birthref)
# Only add if there is an event:
source_text = self.de_widgets["APBirthSource"].get_text().strip()
if source_text and self.de_widgets["Active person:Show sources"].get_active():
new, source = self.get_or_create_source(source_text)
self.add_source(birthevent, source)
self.dbstate.db.commit_event(birthevent, self.trans)
deathdate, deathplace = self.process_dateplace(self.de_widgets["APDeath"].get_text().strip())
new, deathevent = self.get_or_create_event(person, EventType.DEATH, deathdate, deathplace)
# reference it, if need be:
deathref = person.get_death_ref()
if deathevent:
if deathref is None:
# need new
deathref = EventRef()
deathref.set_reference_handle(deathevent.get_handle())
person.set_death_ref(deathref)
# Only add if there is an event:
source_text = self.de_widgets["APDeathSource"].get_text().strip()
if source_text and self.de_widgets["Active person:Show sources"].get_active():
new, source = self.get_or_create_source(source_text)
self.add_source(deathevent, source)
self.dbstate.db.commit_event(deathevent, self.trans)
source_text = self.de_widgets["APSource"].get_text().strip()
if source_text and self.de_widgets["Active person:Show sources"].get_active():
new, source = self.get_or_create_source(source_text)
self.add_source(person, source)
self.dbstate.db.commit_person(person,self.trans)
self._dirty = False
self.update()
def add_source(self, obj, source):
found = 0
for handle in obj.get_citation_list():
citation = self.dbstate.db.get_citation_from_handle(handle)
if citation.get_reference_handle() == source.get_handle():
found = 1
break
if not found:
citation = Citation()
citation.set_reference_handle(source.get_handle())
self.dbstate.db.add_citation(citation, self.trans)
obj.add_citation(citation.get_handle())
def add_data_entry(self, obj):
from gramps.gui.dialog import ErrorDialog
if not self.dbstate.is_open():
ErrorDialog(_("No Family Tree is open."),
_("Please open a Family Tree before adding a person."))
return
# First, get the data:
if "," in self.de_widgets["NPName"].get_text():
surname, firstname = self.de_widgets["NPName"].get_text().split(",", 1)
else:
surname, firstname = self.de_widgets["NPName"].get_text(), ""
surname = surname.strip()
firstname = firstname.strip()
gender = self.de_widgets["NPGender"].get_active()
if self._dirty:
current_person = self._dirty_person
else:
current_person = self.get_active_object("Person")
# Pre-check to make sure everything is ok: -------------------------------------------
if surname == "" and firstname == "":
ErrorDialog(_("Please provide a name."), _("Can't add new person."))
return
if self.de_widgets["NPRelation"].get_active() == self.NO_REL:
# "No relation to active person"
pass
elif self.de_widgets["NPRelation"].get_active() == self.AS_PARENT:
# "Add as a Parent"
if current_person == None:
ErrorDialog(_("Please set an active person."), _("Can't add new person as a parent."))
return
elif gender == Person.UNKNOWN: # unknown
ErrorDialog(_("Please set the new person's gender."), _("Can't add new person as a parent."))
return
elif self.de_widgets["NPRelation"].get_active() == self.AS_MOTHER:
# "Add as a Mother"
if current_person == None:
ErrorDialog(_("Please set an active person."), _("Can't add new person as a mother."))
return
elif self.de_widgets["NPRelation"].get_active() == self.AS_FATHER:
# "Add as a Father"
if current_person == None:
ErrorDialog(_("Please set an active person."), _("Can't add new person as a father."))
return
elif self.de_widgets["NPRelation"].get_active() == self.AS_SPOUSE:
# "Add as a Spouse"
if current_person == None:
ErrorDialog(_("Please set an active person."), _("Can't add new person as a spouse."))
return
elif (gender == Person.UNKNOWN and
current_person.get_gender() == Person.UNKNOWN): # both genders unknown
ErrorDialog(_("Please set the new person's gender."), _("Can't add new person as a spouse."))
return
elif self.de_widgets["NPRelation"].get_active() == self.AS_WIFE:
# "Add as a Wife"
if current_person == None:
ErrorDialog(_("Please set an active person."), _("Can't add new person as a wife."))
return
elif self.de_widgets["NPRelation"].get_active() == self.AS_HUSBAND:
# "Add as a Husband"
if current_person == None:
ErrorDialog(_("Please set an active person."), _("Can't add new person as a husband."))
return
elif self.de_widgets["NPRelation"].get_active() == self.AS_SIBLING:
# "Add as a Sibling"
if current_person == None:
ErrorDialog(_("Please set an active person."), _("Can't add new person as a sibling."))
return
elif self.de_widgets["NPRelation"].get_active() == self.AS_CHILD:
# "Add as a Child"
if current_person == None:
ErrorDialog(_("Please set an active person."), _("Can't add new person as a child."))
return
# Start the transaction: ------------------------------------------------------------
person = self.make_person(firstname, surname, gender)
with DbTxn(_("Gramplet Data Edit: %s") % name_displayer.display(person), self.dbstate.db) as self.trans:
# New person --------------------------------------------------
# Add birth
new_birth_date, new_birth_place = self.process_dateplace(self.de_widgets["NPBirth"].get_text().strip())
birth_event = self.make_event(EventType.BIRTH, new_birth_date, new_birth_place)
if birth_event:
# Only add if there is an event:
source_text = self.de_widgets["NPBirthSource"].get_text().strip()
if source_text and self.de_widgets["Active person:Show sources"].get_active():
new, source = self.get_or_create_source(source_text)
self.add_source(birth_event, source)
self.dbstate.db.commit_event(birth_event, self.trans)
# Add death
new_death_date, new_death_place = self.process_dateplace(self.de_widgets["NPDeath"].get_text())
death_event = self.make_event(EventType.DEATH, new_death_date, new_death_place)
if death_event:
# Only add if there is an event:
source_text = self.de_widgets["NPDeathSource"].get_text().strip()
if source_text and self.de_widgets["Active person:Show sources"].get_active():
new, source = self.get_or_create_source(source_text)
self.add_source(death_event, source)
self.dbstate.db.commit_event(death_event, self.trans)
# Now, create the person and events:
# New birth for person:
if birth_event:
birth_ref = EventRef()
birth_ref.set_reference_handle(birth_event.get_handle())
person.set_birth_ref(birth_ref)
# New death for person:
if death_event:
death_ref = EventRef()
death_ref.set_reference_handle(death_event.get_handle())
person.set_death_ref(death_ref)
# Need to add here to get a handle:
self.dbstate.db.add_person(person, self.trans)
# All error checking done; just add relation:
if self.de_widgets["NPRelation"].get_active() == self.NO_REL:
# "No relation to active person"
current_person = None
elif self.de_widgets["NPRelation"].get_active() == self.AS_PARENT:
# "Add as a Parent"
# Go through current_person parent families
added = False
for family_handle in current_person.get_parent_family_handle_list():
family = self.dbstate.db.get_family_from_handle(family_handle)
if family:
# find one that person would fit as a parent
fam_husband_handle = family.get_father_handle()
fam_wife_handle = family.get_mother_handle()
# can we add person as wife?
if fam_wife_handle == None and person.get_gender() == Person.FEMALE:
# add the person
family.set_mother_handle(person.get_handle())
family.set_relationship(FamilyRelType.MARRIED)
person.add_family_handle(family.get_handle())
added = True
break
elif fam_husband_handle == None and person.get_gender() == Person.MALE:
# add the person
family.set_father_handle(person.get_handle())
family.set_relationship(FamilyRelType.MARRIED)
person.add_family_handle(family.get_handle())
added = True
break
if added:
self.dbstate.db.commit_family(family, self.trans)
else:
family = Family()
self.dbstate.db.add_family(family, self.trans)
if person.get_gender() == Person.MALE:
family.set_father_handle(person.get_handle())
elif person.get_gender() == Person.FEMALE:
family.set_mother_handle(person.get_handle())
family.set_relationship(FamilyRelType.MARRIED)
# add curent_person as child
childref = ChildRef()
childref.set_reference_handle(current_person.get_handle())
family.add_child_ref( childref)
current_person.add_parent_family_handle(family.get_handle())
# finalize
person.add_family_handle(family.get_handle())
self.dbstate.db.commit_family(family, self.trans)
elif self.de_widgets["NPRelation"].get_active() == self.AS_MOTHER:
# "Add as a Mother"
# Go through current_person parent families
added = False
for family_handle in current_person.get_parent_family_handle_list():
family = self.dbstate.db.get_family_from_handle(family_handle)
if family:
fam_wife_handle = family.get_mother_handle()
# can we add person as mother?
if fam_wife_handle == None:
# add the person
family.set_mother_handle(person.get_handle())
family.set_relationship(FamilyRelType.MARRIED)
person.add_family_handle(family.get_handle())
added = True
break
if added:
self.dbstate.db.commit_family(family, self.trans)
else:
family = Family()
self.dbstate.db.add_family(family, self.trans)
family.set_mother_handle(person.get_handle())
family.set_relationship(FamilyRelType.MARRIED)
# add curent_person as child
childref = ChildRef()
childref.set_reference_handle(current_person.get_handle())
family.add_child_ref( childref)
current_person.add_parent_family_handle(family.get_handle())
# finalize
person.add_family_handle(family.get_handle())
self.dbstate.db.commit_family(family, self.trans)
elif self.de_widgets["NPRelation"].get_active() == self.AS_FATHER:
# "Add as a Father"
# Go through current_person parent families
added = False
for family_handle in current_person.get_parent_family_handle_list():
family = self.dbstate.db.get_family_from_handle(family_handle)
if family:
fam_husband_handle = family.get_father_handle()
# can we add person as father?
if fam_husband_handle == None:
# add the person
family.set_father_handle(person.get_handle())
family.set_relationship(FamilyRelType.MARRIED)
person.add_family_handle(family.get_handle())
added = True
break
if added:
self.dbstate.db.commit_family(family, self.trans)
else:
family = Family()
self.dbstate.db.add_family(family, self.trans)
family.set_father_handle(person.get_handle())
family.set_relationship(FamilyRelType.MARRIED)
# add curent_person as child
childref = ChildRef()
childref.set_reference_handle(current_person.get_handle())
family.add_child_ref( childref)
current_person.add_parent_family_handle(family.get_handle())
# finalize
person.add_family_handle(family.get_handle())
self.dbstate.db.commit_family(family, self.trans)
elif self.de_widgets["NPRelation"].get_active() == self.AS_SPOUSE:
# "Add as a Spouse"
added = False
family = None
for family_handle in current_person.get_family_handle_list():
family = self.dbstate.db.get_family_from_handle(family_handle)
if family:
fam_husband_handle = family.get_father_handle()
fam_wife_handle = family.get_mother_handle()
if current_person.get_handle() == fam_husband_handle:
# can we add person as wife?
if fam_wife_handle == None:
if person.get_gender() == Person.FEMALE:
# add the person
family.set_mother_handle(person.get_handle())
family.set_relationship(FamilyRelType.MARRIED)
person.add_family_handle(family.get_handle())
added = True
break
elif person.get_gender() == Person.UNKNOWN:
family.set_mother_handle(person.get_handle())
family.set_relationship(FamilyRelType.MARRIED)
person.set_gender(Person.FEMALE)
self.de_widgets["NPGender"].set_active(Person.FEMALE)
person.add_family_handle(family.get_handle())
added = True
break
elif current_person.get_handle() == fam_wife_handle:
# can we add person as husband?
if fam_husband_handle == None:
if person.get_gender() == Person.MALE:
# add the person
family.set_father_handle(person.get_handle())
family.set_relationship(FamilyRelType.MARRIED)
person.add_family_handle(family.get_handle())
added = True
break
elif person.get_gender() == Person.UNKNOWN:
family.set_father_handle(person.get_handle())
family.set_relationship(FamilyRelType.MARRIED)
person.add_family_handle(family.get_handle())
person.set_gender(Person.MALE)
self.de_widgets["NPGender"].set_active(Person.MALE)
added = True
break
if added:
self.dbstate.db.commit_family(family, self.trans)
else:
if person.get_gender() == Person.UNKNOWN:
if current_person is None:
ErrorDialog(_("Please set Active person."),
_("Can't add new person as a spouse to no one."))
return
elif current_person.get_gender() == Person.UNKNOWN:
ErrorDialog(_("Please set gender on Active or new person."),
_("Can't add new person as a spouse."))
return
elif current_person.get_gender() == Person.MALE:
family = Family()
self.dbstate.db.add_family(family, self.trans)
family.set_father_handle(current_person.get_handle())
family.set_mother_handle(person.get_handle())
family.set_relationship(FamilyRelType.MARRIED)
person.set_gender(Person.FEMALE)
self.de_widgets["NPGender"].set_active(Person.FEMALE)
person.add_family_handle(family.get_handle())
current_person.add_family_handle(family.get_handle())
self.dbstate.db.commit_family(family, self.trans)
elif current_person.get_gender() == Person.FEMALE:
family = Family()
self.dbstate.db.add_family(family, self.trans)
family.set_father_handle(person.get_handle())
family.set_mother_handle(current_person.get_handle())
family.set_relationship(FamilyRelType.MARRIED)
person.set_gender(Person.MALE)
self.de_widgets["NPGender"].set_active(Person.MALE)
person.add_family_handle(family.get_handle())
current_person.add_family_handle(family.get_handle())
self.dbstate.db.commit_family(family, self.trans)
elif person.get_gender() == Person.MALE:
if current_person.get_gender() == Person.UNKNOWN:
family = Family()
self.dbstate.db.add_family(family, self.trans)
family.set_father_handle(person.get_handle())
family.set_mother_handle(current_person.get_handle())
family.set_relationship(FamilyRelType.MARRIED)
current_person.set_gender(Person.FEMALE)
person.add_family_handle(family.get_handle())
current_person.add_family_handle(family.get_handle())
self.dbstate.db.commit_family(family, self.trans)
elif current_person.get_gender() == Person.MALE:
ErrorDialog(_("Same genders on Active and new person."),
_("Can't add new person as a spouse."))
return
elif current_person.get_gender() == Person.FEMALE:
family = Family()
self.dbstate.db.add_family(family, self.trans)
family.set_father_handle(person.get_handle())
family.set_mother_handle(current_person.get_handle())
family.set_relationship(FamilyRelType.MARRIED)
person.add_family_handle(family.get_handle())
current_person.add_family_handle(family.get_handle())
self.dbstate.db.commit_family(family, self.trans)
elif person.get_gender() == Person.FEMALE:
if current_person.get_gender() == Person.UNKNOWN:
family = Family()
self.dbstate.db.add_family(family, self.trans)
family.set_father_handle(current_person.get_handle())
family.set_mother_handle(person.get_handle())
family.set_relationship(FamilyRelType.MARRIED)
current_person.set_gender(gramps.gen.lib.Person.MALE)
person.add_family_handle(family.get_handle())
current_person.add_family_handle(family.get_handle())
self.dbstate.db.commit_family(family, self.trans)
elif current_person.get_gender() == Person.MALE:
family = Family()
self.dbstate.db.add_family(family, self.trans)
family.set_father_handle(current_person.get_handle())
family.set_mother_handle(person.get_handle())
family.set_relationship(gramps.gen.lib.FamilyRelType.MARRIED)
person.add_family_handle(family.get_handle())
current_person.add_family_handle(family.get_handle())
self.dbstate.db.commit_family(family, self.trans)
elif current_person.get_gender() == Person.FEMALE:
ErrorDialog(_("Same genders on Active and new person."),
_("Can't add new person as a spouse."))
return
elif self.de_widgets["NPRelation"].get_active() == self.AS_WIFE:
# "Add as a Wife"
added = False
family = None
for family_handle in current_person.get_family_handle_list():
family = self.dbstate.db.get_family_from_handle(family_handle)
if family:
fam_wife_handle = family.get_mother_handle()
# can we add person as wife?
if fam_wife_handle == None:
# add the person
family.set_mother_handle(person.get_handle())
family.set_relationship(gramps.gen.lib.FamilyRelType.MARRIED)
person.add_family_handle(family.get_handle())
added = True
break
if added:
self.dbstate.db.commit_family(family, self.trans)
else:
family = Family()
self.dbstate.db.add_family(family, self.trans)
family.set_mother_handle(person.get_handle())
family.set_father_handle(current_person.get_handle())
family.set_relationship(gramps.gen.lib.FamilyRelType.MARRIED)
person.add_family_handle(family.get_handle())
current_person.add_family_handle(family.get_handle())
self.dbstate.db.commit_family(family, self.trans)
elif self.de_widgets["NPRelation"].get_active() == self.AS_HUSBAND:
# "Add as a Husband"
added = False
family = None
for family_handle in current_person.get_family_handle_list():
family = self.dbstate.db.get_family_from_handle(family_handle)
if family:
fam_husband_handle = family.get_father_handle()
# can we add person as husband?
if fam_husband_handle == None:
# add the person
family.set_father_handle(person.get_handle())
family.set_relationship(gramps.gen.lib.FamilyRelType.MARRIED)
person.add_family_handle(family.get_handle())
added = True
break
if added:
self.dbstate.db.commit_family(family, self.trans)
else:
family = Family()
self.dbstate.db.add_family(family, self.trans)
family.set_father_handle(person.get_handle())
family.set_mother_handle(current_person.get_handle())
family.set_relationship(gramps.gen.lib.FamilyRelType.MARRIED)
person.add_family_handle(family.get_handle())
current_person.add_family_handle(family.get_handle())
self.dbstate.db.commit_family(family, self.trans)
elif self.de_widgets["NPRelation"].get_active() == self.AS_SIBLING and current_person is not None:
# "Add as a Sibling"
added = False
for family_handle in current_person.get_parent_family_handle_list():
family = self.dbstate.db.get_family_from_handle(family_handle)
if family:
childref = ChildRef()
childref.set_reference_handle(person.get_handle())
family.add_child_ref( childref)
person.add_parent_family_handle(family.get_handle())
added = True
break
if added:
self.dbstate.db.commit_family(family, self.trans)
else:
family = Family()
self.dbstate.db.add_family(family, self.trans)
childref = ChildRef()
childref.set_reference_handle(person.get_handle())
family.add_child_ref( childref)
childref = ChildRef()
childref.set_reference_handle(current_person.get_handle())
family.add_child_ref( childref)
person.add_parent_family_handle(family.get_handle())
current_person.add_parent_family_handle(family.get_handle())
self.dbstate.db.commit_family(family, self.trans)
elif self.de_widgets["NPRelation"].get_active() == self.AS_CHILD and current_person is not None:
# "Add as a Child"
added = False
family = None
for family_handle in current_person.get_family_handle_list():
family = self.dbstate.db.get_family_from_handle(family_handle)
if family:
childref = ChildRef()
childref.set_reference_handle(person.get_handle())
family.add_child_ref( childref)
person.add_parent_family_handle(family.get_handle())
added = True
break
if added:
self.dbstate.db.commit_family(family, self.trans)
else:
if current_person.get_gender() == Person.UNKNOWN:
ErrorDialog(_("Please set gender on Active person."),
_("Can't add new person as a child."))
return
else:
family = Family()
self.dbstate.db.add_family(family, self.trans)
childref = ChildRef()
childref.set_reference_handle(person.get_handle())
family.add_child_ref( childref)
person.add_parent_family_handle(family.get_handle())
current_person.add_family_handle(family.get_handle())
if current_person.get_gender() == Person.FEMALE:
family.set_mother_handle(current_person.get_handle())
else:
family.set_father_handle(current_person.get_handle())
self.dbstate.db.commit_family(family, self.trans)
# Commit changes -------------------------------------------------
if current_person: # if related to current person
self.dbstate.db.commit_person(current_person, self.trans)
if person:
source_text = self.de_widgets["NPSource"].get_text().strip()
if source_text and self.de_widgets["Active person:Show sources"].get_active():
new, source = self.get_or_create_source(source_text)
self.add_source(person, source)
self.dbstate.db.commit_person(person, self.trans)
def copy_data_entry(self, obj):
self.de_widgets["NPName"].set_text(self.de_widgets["APName"].get_text())
self.de_widgets["NPBirth"].set_text(self.de_widgets["APBirth"].get_text())
self.de_widgets["NPDeath"].set_text(self.de_widgets["APDeath"].get_text())
self.de_widgets["NPGender"].set_active(self.de_widgets["APGender"].get_active())
self.de_widgets["NPSource"].set_text(self.de_widgets["APSource"].get_text())
self.de_widgets["NPBirthSource"].set_text(self.de_widgets["APBirthSource"].get_text())
self.de_widgets["NPDeathSource"].set_text(self.de_widgets["APDeathSource"].get_text())
self.de_widgets["NPSource"].set_text(self.de_widgets["APSource"].get_text())
self.de_widgets["NPBirthSource"].set_text(self.de_widgets["APBirthSource"].get_text())
self.de_widgets["NPDeathSource"].set_text(self.de_widgets["APDeathSource"].get_text())
# FIXME: put cursor in add surname
def clear_data_edit(self, obj):
self.de_widgets["Active person"].set_text("")
self.de_widgets["APName"].set_text("")
self.de_widgets["APBirth"].set_text("")
self.de_widgets["APDeath"].set_text("")
self.de_widgets["APGender"].set_active(Person.UNKNOWN)
self.de_widgets["APSource"].set_text("")
self.de_widgets["APBirthSource"].set_text("")
self.de_widgets["APDeathSource"].set_text("")
def clear_data_entry(self, obj):
self.de_widgets["NPName"].set_text("")
self.de_widgets["NPBirth"].set_text("")
self.de_widgets["NPDeath"].set_text("")
self.de_widgets["NPRelation"].set_active(self.NO_REL)
self.de_widgets["NPGender"].set_active(Person.UNKNOWN)