-
Notifications
You must be signed in to change notification settings - Fork 112
/
Copy pathFamilyTree.py
1216 lines (1023 loc) · 52.8 KB
/
FamilyTree.py
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 - Family Tree plugin
#
# Copyright (C) 2008,2009,2010,2014 Reinhard Mueller
# Copyright (C) 2010 lcc <lcc.mailaddress@gmail.com>
# Copyright (C) 2014 Gerald Kunzmann
#
# 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$
"""Reports/Graphical Reports/Family Tree"""
import colorsys
#------------------------------------------------------------------------
#
# GRAMPS modules
#
#------------------------------------------------------------------------
import gramps.gen.display.name
from gramps.gen.display.place import displayer as place_displayer
from gramps.gen.lib import Date, Event, EventType, FamilyRelType, Name
from gramps.gen.lib import StyledText, StyledTextTag, StyledTextTagType
import gramps.gen.plug.docgen
import gramps.gen.plug.menu
import gramps.gen.plug.report
from gramps.gen.plug.report.utils import pt2cm
import gramps.gui.plug.report
import gramps.gen.datehandler
from gramps.gen.const import GRAMPS_LOCALE as glocale
try:
_trans = glocale.get_addon_translator(__file__)
except ValueError:
_trans = glocale.translation
_ = _trans.gettext
#------------------------------------------------------------------------
#
# Constants
#
#------------------------------------------------------------------------
empty_birth = Event()
empty_birth.set_type(EventType.BIRTH)
empty_marriage = Event()
empty_marriage.set_type(EventType.MARRIAGE)
#------------------------------------------------------------------------
#
# FamilyTree report
#
#------------------------------------------------------------------------
class FamilyTree(gramps.gen.plug.report.Report):
def __init__(self, database, options, user):
gramps.gen.plug.report.Report.__init__(self, database, options, user)
menu = options.menu
family_id = menu.get_option_by_name('family_id').get_value()
self.center_family = database.get_family_from_gramps_id(family_id)
self.max_ancestor_generations = menu.get_option_by_name('max_ancestor_generations').get_value()
self.max_descendant_generations = menu.get_option_by_name('max_descendant_generations').get_value()
self.fit_on_page = menu.get_option_by_name('fit_on_page').get_value()
self.color = menu.get_option_by_name('color').get_value()
self.shuffle_colors = menu.get_option_by_name('shuffle_colors').get_value()
self.kekule_start_number = menu.get_option_by_name('kekule_start_number').get_value()
try:
self.callname = menu.get_option_by_name('callname').get_value()
except:
self.callname = FamilyTreeOptions.CALLNAME_DONTUSE
self.include_occupation = menu.get_option_by_name('include_occupation').get_value()
self.include_notes = menu.get_option_by_name('include_notes').get_value()
self.include_residence = menu.get_option_by_name('include_residence').get_value()
self.eventstyle_dead = menu.get_option_by_name('eventstyle_dead').get_value()
self.eventstyle_living = menu.get_option_by_name('eventstyle_living').get_value()
self.fallback_birth = menu.get_option_by_name('fallback_birth').get_value()
self.fallback_death = menu.get_option_by_name('fallback_death').get_value()
self.protect_private = menu.get_option_by_name('protect_private').get_value()
self.missinginfo = menu.get_option_by_name('missinginfo').get_value()
self.include_event_description = menu.get_option_by_name('include_event_description').get_value()
self.title = menu.get_option_by_name('title').get_value()
self.footer = menu.get_option_by_name('footer').get_value()
if not self.title:
name = self.__family_get_display_name(self.center_family)
self.title = StyledText(_("Family Tree for %s") % name)
style_sheet = self.doc.get_style_sheet()
self.line_width = pt2cm(style_sheet.get_draw_style("FTR-box").get_line_width())
# Size constants, all in unscaled cm:
# Size of shadow around boxes
self.shadow = style_sheet.get_draw_style("FTR-box").get_shadow_space()
# Offset from left
self.xoffset = self.line_width / 2
# Offset from top
tfont = style_sheet.get_paragraph_style("FTR-Title").get_font()
tfont_height = pt2cm(tfont.get_size()) * 1.2
self.yoffset = tfont_height * 2
# Space for footer
ffont = style_sheet.get_paragraph_style("FTR-Footer").get_font()
ffont_height = pt2cm(ffont.get_size()) * 1.2
self.ybottom = ffont_height
# Padding inside box == half size of shadow
self.box_pad = self.shadow / 2
# Gap between boxes == 2 times size of shadow
self.box_gap = 2 * self.shadow
# Width of a box (calculated in __build_*_tree)
self.box_width = 0
# Number of generations used (calculated in __build_*_tree)
self.ancestor_generations = 0
self.descendant_generations = 0
# Number of colors used so far
self.ancestor_max_color = 0
self.descendant_max_color = 0
self.descendants_tree = None
self.ancestors_tree = self.__build_ancestors_tree(self.center_family.get_handle(), 0, 0, 0, 0, self.kekule_start_number)
if self.ancestors_tree is None:
return
(self.descendants_tree, descendants_space) = self.__build_descendants_tree(self.center_family.get_child_ref_list(), 0, 0, 0)
needed_width = self.xoffset + (self.ancestor_generations + self.descendant_generations) * (self.box_width + 2 * self.box_gap) - 2 * self.box_gap + self.shadow
needed_height = self.yoffset + max(self.ancestors_tree['space'], descendants_space) + self.shadow + self.ybottom * 2
usable_width = self.doc.get_usable_width()
usable_height = self.doc.get_usable_height()
if self.fit_on_page:
self.scale = min(
usable_height / needed_height,
usable_width / needed_width)
if self.scale < 0.4:
user.warn(_('Paper too small'),
_('Some elements may not appear or be badly '
'rendered.'))
self.__scale_styles()
# Convert usable size into unscaled cm
usable_width = usable_width / self.scale
usable_height = usable_height / self.scale
else:
self.scale = 1
# Center the whole tree on the usable page area
self.xoffset += (usable_width - needed_width) / 2
self.yoffset += (usable_height - needed_height) / 2
# Since center person has an x of 0, add space needed by ancestors
self.xoffset += (self.ancestor_generations - 1) * (self.box_width + 2 * self.box_gap)
# Align ancestors part and descendants part vertically
root_a = self.ancestors_tree['top'] + self.ancestors_tree['height'] / 2
root_d = descendants_space / 2
if root_a > root_d:
self.yoffset_a = self.yoffset
self.yoffset_d = self.yoffset + root_a - root_d
else:
self.yoffset_a = self.yoffset + root_d - root_a
self.yoffset_d = self.yoffset
def write_report(self):
self.doc.start_page()
# Workaround for center_text not accepting StyledText
if isinstance(self.title, StyledText):
if not self.title.get_tags():
self.title = str(self.title)
self.doc.center_text('FTR-title',
self.title,
self.doc.get_usable_width() / 2,
0)
self.__print_ancestors_tree(self.ancestors_tree, 0)
if self.ancestors_tree:
anchor = self.yoffset_a + self.ancestors_tree['top'] + self.ancestors_tree['height'] / 2
if self.descendants_tree:
self.__print_descendants_tree(self.descendants_tree, anchor, 1)
self.doc.center_text('FTR-footer',
self.footer,
self.doc.get_usable_width() / 2,
self.doc.get_usable_height() - self.ybottom * self.scale)
self.doc.end_page()
def __build_ancestors_tree(self, family_handle, generation, color, top, center, kekule):
"""Build an in-memory data structure containing all ancestors"""
self.ancestor_generations = max(self.ancestor_generations, generation + 1)
# This is a dictionary containing all interesting data for a box that
# will be printed later:
# text: text to be printed in the box, as a list of (style, text) tuples
# top: top edge of the box in unscaled cm
# height: height of the box in unscaled cm
# space: total height that this box and all its ancestor boxes (left to
# it) need, in unscaled cm
# anchor: y position to where the line right of this box should end
# mother_node: dictionary representing the box with the mother's
# ancestors
# father_node: dictionary representing the box with the father's
# ancestors
family_node = {}
family = self.database.get_family_from_handle(family_handle)
if family.private and self.protect_private:
return None
father_handle = family.get_father_handle()
if father_handle:
father = self.database.get_person_from_handle(father_handle)
if father.private and self.protect_private:
father = None
else:
father = None
if father:
if kekule:
father_text = [('FTR-name', StyledText(str(kekule) + " ") + self.__person_get_display_name(father))] + [('FTR-data', p) for p in self.__person_get_display_data(father)]
else:
father_text = [('FTR-name', self.__person_get_display_name(father))] + [('FTR-data', p) for p in self.__person_get_display_data(father)]
father_height = self.__make_space(father_text)
father_family = father.get_main_parents_family_handle()
else:
father_text = []
father_height = 0
father_family = None
mother_handle = family.get_mother_handle()
if mother_handle:
mother = self.database.get_person_from_handle(mother_handle)
if mother.private and self.protect_private:
mother = None
else:
mother = None
if mother:
if kekule > 1:
mother_text = [('FTR-name', StyledText(str(kekule+1) + " ") + self.__person_get_display_name(mother))] + [('FTR-data', p) for p in self.__person_get_display_data(mother)]
else:
mother_text = [('FTR-name', self.__person_get_display_name(mother))] + [('FTR-data', p) for p in self.__person_get_display_data(mother)]
mother_height = self.__make_space(mother_text)
mother_family = mother.get_main_parents_family_handle()
else:
mother_text = []
mother_height = 0
mother_family = None
family_node['text'] = father_text + [('FTR-data', p) for p in self.__family_get_display_data(family)] + mother_text
family_node['color'] = color
family_node['height'] = self.__make_space(family_node['text'])
# If this box is small, align it centered, if it is too big for that,
# align it to the top.
family_node['top'] = max(top, center - family_node['height'] / 2)
father_node = None
if father_family and generation < self.max_ancestor_generations:
if (self.color == FamilyTreeOptions.COLOR_FEMALE_LINE) or \
(self.color == FamilyTreeOptions.COLOR_FIRST_GEN and generation == 0) or \
(self.color == FamilyTreeOptions.COLOR_SECOND_GEN and generation == 1) or \
(self.color == FamilyTreeOptions.COLOR_THIRD_GEN and generation == 2):
self.ancestor_max_color += 1
father_color = self.ancestor_max_color
else:
father_color = color
# Where should the father's box be placed?
father_top = top
father_center = family_node['top'] + father_height / 2
# Create father's box.
if kekule:
father_node = self.__build_ancestors_tree(father_family, generation + 1, father_color, father_top, father_center, kekule * 2)
else:
father_node = self.__build_ancestors_tree(father_family, generation + 1, father_color, father_top, father_center, 0)
if father_node:
if mother_family:
if self.database.get_family_from_handle(mother_family).private and self.protect_private:
pass
else:
# This box has father and mother: move it down so its center is
# just at the end of the father's ancestors space.
family_node['top'] = max(family_node['top'], top + father_node['space'] + self.box_gap / 2 - family_node['height'] / 2)
else:
# This box has only father: move it down to the center of the
# father's parents.
family_node['top'] = max(family_node['top'], father_node['top'] + father_node['height'] / 2 - father_height / 2)
mother_node = None
if mother_family and generation < self.max_ancestor_generations:
if (self.color == FamilyTreeOptions.COLOR_MALE_LINE) or \
(self.color == FamilyTreeOptions.COLOR_MALE_LINE_WEAK and family.get_relationship() != FamilyRelType.UNMARRIED) or \
(self.color == FamilyTreeOptions.COLOR_FIRST_GEN and generation == 0) or \
(self.color == FamilyTreeOptions.COLOR_SECOND_GEN and generation == 1) or \
(self.color == FamilyTreeOptions.COLOR_THIRD_GEN and generation == 2):
self.ancestor_max_color += 1
mother_color = self.ancestor_max_color
else:
mother_color = color
# Where should the mother's box be placed?
if father_handle:
# There is also a father: mother's box must be below the center
# of this box.
mother_top = family_node['top'] + family_node['height'] / 2 + self.box_gap / 2
else:
# There is no father: mother's box can use all the vertical
# space of this box.
mother_top = top
mother_center = family_node['top'] + family_node['height'] - mother_height / 2
# Create mother's box.
if kekule > 1:
mother_node = self.__build_ancestors_tree(mother_family, generation + 1, mother_color, mother_top, mother_center, (kekule+1)*2 )
else:
mother_node = self.__build_ancestors_tree(mother_family, generation + 1, mother_color, mother_top, mother_center, 0 )
if mother_node:
# If this family is only a mother, move her down to the center of
# her parents box.
if not father_node:
family_node['top'] = max(family_node['top'], mother_node['top'] + mother_node['height'] / 2 - (family_node['height'] - mother_height / 2))
bottom = family_node['top'] + family_node['height']
if father_node:
bottom = max(bottom, father_top + father_node['space'])
if mother_node:
bottom = max(bottom, mother_top + mother_node['space'])
family_node['space'] = bottom - top
family_node['father_node'] = father_node
family_node['mother_node'] = mother_node
if father_node:
father_node['anchor'] = family_node['top'] + father_height / 2
if mother_node:
mother_node['anchor'] = family_node['top'] + family_node['height'] - mother_height / 2
return family_node
def __build_descendants_tree(self, person_ref_list, generation, color, top):
"""Build an in-memory data structure containing all descendants"""
if generation >= self.max_descendant_generations:
return ([], 0)
self.descendant_generations = max(self.descendant_generations, generation + 1)
node_list = []
space = 0
for person_ref in person_ref_list:
if person_ref.private and self.protect_private:
continue
# This is a dictionary containing all interesting data for a box
# that contains a single person.
# text: text to be printed in the box, as a list of (style, text)
# tuples
# color: background color to be used for this box
# top: top edge of the box in unscaled cm
# height: height of the box in unscaled cm
# space: total height that this box, all the family boxes of this
# person and all its descendant boxes (right to it) need, in
# unscaled cm
# family_list: list of family_node style dictionaries containing
# families in which this person is a parent.
# If the person has at least one family in which it is parent, this
# box will actually not be printed, but all the boxes in the
# family_list.
person_node = {}
person = self.database.get_person_from_handle(person_ref.ref)
if person.private and self.protect_private:
continue
person_node['text'] = [('FTR-name', self.__person_get_display_name(person))] + [('FTR-data', p) for p in self.__person_get_display_data(person)]
if (self.color == FamilyTreeOptions.COLOR_FIRST_GEN and generation == 0) or \
(self.color == FamilyTreeOptions.COLOR_SECOND_GEN and generation == 1) or \
(self.color == FamilyTreeOptions.COLOR_THIRD_GEN and generation == 2):
self.descendant_max_color += 1
person_node['color'] = self.descendant_max_color
else:
person_node['color'] = color
person_node['top'] = top + space
person_node['height'] = self.__make_space(person_node['text'])
person_node['family_list'] = []
person_node['space'] = 0
family_top = person_node['top']
family_handles = person.get_family_handle_list()
for family_handle in family_handles:
family = self.database.get_family_from_handle(family_handle)
if family.private and self.protect_private:
continue
# This is a dictionary containing all interesting data for a
# box that contains the parents of a family.
# text: text to be printed in the box, as a list of (style,
# text) tuples
# color: background color for this box
# top: top edge of the box in unscaled cm
# height: height of the box in unscaled cm
# space: total height that this box and all the descendant
# boxes of this family (right to it) need, in unscaled cm
# child_list: list of person_node style dictionaries containing
# the children of this family.
family_node = {}
family_node['text'] = [('FTR-data', p) for p in self.__family_get_display_data(family)]
father_handle = family.get_father_handle()
mother_handle = family.get_mother_handle()
if person_ref.ref == father_handle:
spouse_handle = mother_handle
else:
spouse_handle = father_handle
if len(family_handles) > 1:
spouse_number = StyledText(chr(0x2160 + len(person_node['family_list'])) + ". ")
else:
spouse_number = StyledText("")
if spouse_handle is not None:
spouse = self.database.get_person_from_handle(spouse_handle)
family_node['text'] += [('FTR-name', spouse_number + self.__person_get_display_name(spouse))] + [('FTR-data', p) for p in self.__person_get_display_data(spouse)]
elif spouse_number:
family_node['text'] += [('FTR-name', spouse_number)]
# Include data of the actual person in the first family box.
if not person_node['family_list']:
family_node['text'] = person_node['text'] + family_node['text']
# Decide if a new color is needed.
if (self.color == FamilyTreeOptions.COLOR_MALE_LINE and person_ref.ref == mother_handle) or \
(self.color == FamilyTreeOptions.COLOR_MALE_LINE_WEAK and person_ref.ref == mother_handle and family.get_relationship() != FamilyRelType.UNMARRIED) or \
(self.color == FamilyTreeOptions.COLOR_FEMALE_LINE and person_ref.ref == father_handle):
self.descendant_max_color += 1
family_node['color'] = self.descendant_max_color
else:
family_node['color'] = person_node['color']
family_node['top'] = family_top
family_node['height'] = self.__make_space(family_node['text'])
(family_node['child_list'], children_space) = self.__build_descendants_tree(family.get_child_ref_list(), generation + 1, family_node['color'], family_top)
family_node['space'] = max(family_node['height'], children_space)
# Vertically center parents within the space their descendants
# use.
family_node['top'] += (family_node['space'] - family_node['height']) / 2
# This is where the next family will start
family_top += family_node['space'] + self.box_gap
person_node['family_list'].append(family_node)
if person_node['space'] > 0:
person_node['space'] += self.box_gap
person_node['space'] += family_node['space']
if person_node['space'] == 0:
person_node['space'] = person_node['height']
if person_node['family_list']:
person_node['top'] = person_node['family_list'][0]['top']
node_list.append(person_node)
space += person_node['space'] + self.box_gap
return (node_list, space - self.box_gap)
def __print_ancestors_tree(self, family_node, generation):
if family_node is None:
return
self.__draw_box(family_node['text'], family_node['color'], self.ancestor_max_color + 1, generation, self.yoffset_a + family_node['top'], family_node['height'])
for parent_node in [family_node['father_node'], family_node['mother_node']]:
if not parent_node:
continue
self.__print_ancestors_tree(parent_node, generation - 1)
y1 = self.yoffset_a + parent_node['anchor']
y2 = self.yoffset_a + parent_node['top'] + parent_node['height'] / 2
x1 = self.xoffset + generation * (self.box_width + 2 * self.box_gap)
x2 = x1 - self.box_gap
x3 = x2 - self.box_gap
self.doc.draw_line("FTR-line",
self.scale * x1,
self.scale * y1,
self.scale * x2,
self.scale * y1)
self.doc.draw_line("FTR-line",
self.scale * x2,
self.scale * y1,
self.scale * x2,
self.scale * y2)
self.doc.draw_line("FTR-line",
self.scale * x2,
self.scale * y2,
self.scale * x3,
self.scale * y2)
def __print_descendants_tree(self, person_node_list, anchor, generation):
if not person_node_list:
return
x3 = self.xoffset + (generation) * (self.box_width + 2 * self.box_gap)
x2 = x3 - self.box_gap
x1 = x2 - self.box_gap
self.doc.draw_line("FTR-line",
self.scale * x1,
self.scale * anchor,
self.scale * x2,
self.scale * anchor)
self.doc.draw_line("FTR-line",
self.scale * x2,
self.scale * min(self.yoffset_d + person_node_list[0]['top'] + person_node_list[0]['height'] / 2, anchor),
self.scale * x2,
self.scale * max(self.yoffset_d + person_node_list[-1]['top'] + person_node_list[-1]['height'] / 2, anchor))
for person_node in person_node_list:
self.doc.draw_line("FTR-line",
self.scale * x2,
self.scale * (self.yoffset_d + person_node['top'] + person_node['height'] / 2),
self.scale * x3,
self.scale * (self.yoffset_d + person_node['top'] + person_node['height'] / 2))
if person_node['family_list']:
last_bottom = 0
for family_node in person_node['family_list']:
if last_bottom > 0:
x = self.xoffset + generation * (self.box_width + 2 * self.box_gap) + self.box_width / 2
self.doc.draw_line("FTR-line",
self.scale * x,
self.scale * last_bottom,
self.scale * x,
self.scale * (self.yoffset_d + family_node['top']))
last_bottom = self.yoffset_d + family_node['top'] + family_node['height']
self.__draw_box(family_node['text'], family_node['color'], self.descendant_max_color + 1, generation, self.yoffset_d + family_node['top'], family_node['height'])
if family_node['child_list']:
self.__print_descendants_tree(
family_node['child_list'],
self.yoffset_d + family_node['top'] + family_node['height'] / 2,
generation + 1)
else:
self.__draw_box(person_node['text'], person_node['color'], self.descendant_max_color + 1, generation, self.yoffset_d + person_node['top'], person_node['height'])
# -------------------------------------------------------------------
# Scaling methods
# -------------------------------------------------------------------
def __scale_styles(self):
"""
Scale the styles for this report.
"""
style_sheet = self.doc.get_style_sheet()
self.__scale_font(style_sheet, "FTR-Title")
self.__scale_font(style_sheet, "FTR-Name")
self.__scale_font(style_sheet, "FTR-Data")
self.__scale_font(style_sheet, "FTR-Footer")
self.__scale_line_width(style_sheet, "FTR-box")
self.__scale_line_width(style_sheet, "FTR-line")
self.doc.set_style_sheet(style_sheet)
def __scale_font(self, style_sheet, style_name):
p = style_sheet.get_paragraph_style(style_name)
font = p.get_font()
font.set_size(font.get_size() * self.scale)
p.set_font(font)
style_sheet.add_paragraph_style(style_name, p)
def __scale_line_width(self, style_sheet, style_name):
g = style_sheet.get_draw_style(style_name)
g.set_shadow(g.get_shadow(), g.get_shadow_space() * self.scale)
g.set_line_width(g.get_line_width() * self.scale)
style_sheet.add_draw_style(style_name, g)
# -------------------------------------------------------------------
# Drawing methods
# -------------------------------------------------------------------
def __make_space(self, text):
h = 0
for (style_name, line) in text:
w = pt2cm(self.doc.string_width(self.__get_font(style_name), str(line)))
self.box_width = max(self.box_width, w)
h += self.__get_font_height(style_name) * 1.2
return h + 2 * self.box_pad
def __draw_box(self, text, color, color_count, generation, top, height):
if self.color == FamilyTreeOptions.COLOR_GENERATION:
col = self.descendant_generations - generation
col_count = self.ancestor_generations + self.descendant_generations
else:
col = color
col_count = color_count
if self.color != FamilyTreeOptions.COLOR_NONE:
self.__set_fill_color("FTR-box", col, col_count)
box_x = self.xoffset + generation * (self.box_width + 2 * self.box_gap)
box_y = top
self.doc.draw_box("FTR-box",
"",
self.scale * box_x,
self.scale * box_y,
self.scale * self.box_width,
self.scale * height)
x = self.scale * (box_x + self.box_pad)
y = self.scale * (box_y + self.box_pad)
for (style_name, line) in text:
# Workaround for draw_text not accepting StyledText
if isinstance(line, StyledText):
if not line.get_tags():
line = str(line)
self.doc.draw_text(style_name, line, x, y)
y += self.__get_font_height(style_name) * 1.2
def __get_font_height(self, style_name):
return pt2cm(self.__get_font(style_name).get_size())
def __get_font(self, style_name):
style_sheet = self.doc.get_style_sheet()
draw_style = style_sheet.get_draw_style(style_name)
paragraph_style_name = draw_style.get_paragraph_style()
paragraph_style = style_sheet.get_paragraph_style(paragraph_style_name)
return paragraph_style.get_font()
# -------------------------------------------------------------------
# Person name and data formatting methods
# -------------------------------------------------------------------
def __family_get_display_name(self, family):
father_name = _("Unknown")
father_handle = family.get_father_handle()
if father_handle:
father = self.database.get_person_from_handle(father_handle)
if father:
father_name = self.__person_get_display_name(father)
mother_name = _("Unknown")
mother_handle = family.get_mother_handle()
if mother_handle:
mother = self.database.get_person_from_handle(mother_handle)
if mother:
mother_name = self.__person_get_display_name(mother)
return StyledText(_("%(father)s and %(mother)s") % {
'father': father_name,
'mother': mother_name})
def __person_get_display_name(self, person):
if person.get_primary_name().private and self.protect_private:
return _("Anonymous")
# Make a copy of the name object so we don't mess around with the real
# data.
n = Name(source=person.get_primary_name())
# Insert placeholders.
if self.missinginfo:
if not n.first_name:
n.first_name = "____________"
if not n.get_surname():
n.get_primary_surname().set_surname("____________")
if n.call:
if self.callname == FamilyTreeOptions.CALLNAME_REPLACE:
# Replace first name with call name.
n.first_name = n.call
elif self.callname == FamilyTreeOptions.CALLNAME_UNDERLINE_ADD:
if n.call not in n.first_name:
# Add call name to first name.
n.first_name = "\"%(call)s\" (%(first)s)" % {
'call': n.call,
'first': n.first_name}
text = gramps.gen.display.name.displayer.display_name(n)
tags = []
if n.call:
if self.callname == FamilyTreeOptions.CALLNAME_UNDERLINE_ADD:
if n.call in person.get_primary_name().first_name:
# Underline call name
callpos = text.find(n.call)
tags = [StyledTextTag(StyledTextTagType.UNDERLINE, True,
[(callpos, callpos + len(n.call))])]
return StyledText(text, tags)
def __person_get_display_data(self, person):
result = []
occupations = []
notes = []
baptism = None
residences = []
burial = None
cremation = None
for event_ref in person.get_event_ref_list():
if event_ref.private and self.protect_private:
continue
event = self.database.get_event_from_handle(event_ref.ref)
if event.private and self.protect_private:
continue
if event.get_type() == EventType.OCCUPATION:
occupations.append(event.description)
elif event.get_type() == EventType.BAPTISM:
baptism = event
elif event.get_type() == EventType.RESIDENCE:
residences.append(event)
elif event.get_type() == EventType.BURIAL:
burial = event
elif event.get_type() == EventType.CREMATION:
cremation = event
if self.include_occupation and occupations:
result.append(', '.join(occupations))
birth_ref = person.get_birth_ref()
death_ref = person.get_death_ref()
if birth_ref:
if birth_ref.private and self.protect_private:
birth = None
else:
birth = self.database.get_event_from_handle(birth_ref.ref)
elif not self.fallback_birth or baptism is None:
birth = empty_birth
else:
birth = None
if birth and birth.private and self.protect_private:
birth = None
if death_ref and not (death_ref.private and self.protect_private):
death = self.database.get_event_from_handle(death_ref.ref)
else:
death = None
if death and death.private and self.protect_private:
death = None
if death:
eventstyle = self.eventstyle_dead
else:
eventstyle = self.eventstyle_living
if eventstyle == FamilyTreeOptions.EVENTSTYLE_DATEPLACE:
if birth is not None:
result.extend(self.__event_get_display_data(birth))
elif self.fallback_birth and baptism is not None:
result.extend(self.__event_get_display_data(baptism))
if self.include_residence:
for residence in residences:
result.extend(self.__event_get_display_data(residence))
if death:
result.extend(self.__event_get_display_data(death))
elif self.fallback_death and burial is not None:
result.extend(self.__event_get_display_data(burial))
elif self.fallback_death and cremation is not None:
result.extend(self.__event_get_display_data(cremation))
elif eventstyle != FamilyTreeOptions.EVENTSTYLE_NONE:
if birth is None and self.fallback_birth:
birth = baptism
if death is None and self.fallback_death:
death = burial
if death is None and self.fallback_death:
death = cremation
if birth:
birth_text = self.__date_get_display_text(birth.get_date_object(), eventstyle)
else:
birth_text = None
if death:
death_text = self.__date_get_display_text(death.get_date_object(), eventstyle)
else:
death_text = None
if birth_text:
if death_text:
result.append("%s - %s" % (birth_text, death_text))
else:
result.append("* %s" % birth_text)
else:
if death_text:
result.append("\u271D %s" % death_text)
notelist = person.get_note_list()
note = ""
for notehandle in notelist:
noteobj = self.database.get_note_from_handle(notehandle)
note += noteobj.get()
note += ", "
# replace all new lines and carriage returns with spaces to prevent notes
# being written beyond the bottom edge of the drawn box or overwriting other text
# if they contain multiple lines
note = note.replace('\n', ' ').replace('\r', ' ')
# cut "," from end of the string and limit length of note to 50 characters
note_len = len(note)
if note_len > 50:
note = note[:48]
note += "..."
else:
note_len -= 2
note = note[:note_len]
if self.include_notes and note and note != "":
result.append(note)
return result
def __family_get_display_data(self, family):
marriage = None
divorce = None
residences = []
for event_ref in family.get_event_ref_list():
if event_ref.private and self.protect_private:
continue
event = self.database.get_event_from_handle(event_ref.ref)
if event.private and self.protect_private:
continue
if event.get_type() == EventType.MARRIAGE:
marriage = event
elif event.get_type() == EventType.RESIDENCE:
residences.append(event)
elif event.get_type() == EventType.DIVORCE:
divorce = event
if family.get_relationship() == FamilyRelType.MARRIED and not marriage:
marriage = empty_marriage
eventstyle = self.eventstyle_dead
father_handle = family.get_father_handle()
if father_handle:
father = self.database.get_person_from_handle(father_handle)
if not father.get_death_ref():
eventstyle = self.eventstyle_living
mother_handle = family.get_mother_handle()
if mother_handle:
mother = self.database.get_person_from_handle(mother_handle)
if not mother.get_death_ref():
eventstyle = self.eventstyle_living
if eventstyle == FamilyTreeOptions.EVENTSTYLE_NONE:
return []
elif eventstyle == FamilyTreeOptions.EVENTSTYLE_DATEPLACE:
result = []
if marriage:
result.extend(self.__event_get_display_data(marriage))
if self.include_residence:
for residence in residences:
result.extend(self.__event_get_display_data(residence))
if divorce:
result.extend(self.__event_get_display_data(divorce))
return result
else:
if marriage:
marriage_text = self.__date_get_display_text(marriage.get_date_object(), eventstyle)
else:
marriage_text = None
if divorce:
divorce_text = self.__date_get_display_text(divorce.get_date_object(), eventstyle)
else:
divorce_text = None
if marriage_text:
if divorce_text:
return ["\u26AD %s - %s" % (marriage_text, divorce_text)]
else:
return ["\u26AD %s" % marriage_text]
else:
if divorce_text:
return ["\u26AE %s" % divorce_text]
else:
return []
def __event_get_display_data(self, event):
if event.get_type() == EventType.BIRTH:
event_text = _("born")
elif event.get_type() == EventType.BAPTISM:
event_text = _("baptised")
elif event.get_type() == EventType.DEATH:
event_text = _("died")
elif event.get_type() == EventType.BURIAL:
event_text = _("buried")
elif event.get_type() == EventType.CREMATION:
event_text = _("cremated")
elif event.get_type() == EventType.MARRIAGE:
event_text = _("married")
elif event.get_type() == EventType.DIVORCE:
event_text = _("divorced")
elif event.get_type() == EventType.RESIDENCE:
event_text = _("resident")
date = event.get_date_object()
date_text = gramps.gen.datehandler.displayer.display(date)
if date.get_modifier() == Date.MOD_NONE and date.get_quality() == Date.QUAL_NONE:
if date.get_day_valid():
date_text = _("on %(ymd_date)s") % {'ymd_date': date_text}
elif date.get_month_valid():
date_text = _("in %(ym_date)s") % {'ym_date': date_text}
elif date.get_year_valid():
date_text = _("in %(y_date)s") % {'y_date': date_text}
if self.missinginfo:
if date.is_empty():
date_text = _("on %(placeholder)s") % {
'placeholder': "__________"}
elif not date.is_regular():
date_text = _("on %(placeholder)s (%(partial)s)") % {
'placeholder': "__________",
'partial': date_text}
place_handle = event.get_place_handle()
if place_handle:
place = self.database.get_place_from_handle(place_handle)
if place.private and self.protect_private:
place_text = ""
else:
place_text = place_displayer.display_event(self.database, event)
elif self.missinginfo:
place_text = "____________"
else:
place_text = ""
if place_text:
place_text = _("in %(place)s") % {'place': place_text}
if not date_text and not place_text:
return []
result = event_text
if date_text:
result += " " + date_text
if place_text:
result += " " + place_text
if self.include_event_description and event.description:
result += " " + _("(%(description)s)") % {
'description': event.description}
return [result]
def __date_get_display_text(self, date, eventstyle):
if not date:
return None
elif eventstyle == FamilyTreeOptions.EVENTSTYLE_YEARONLY:
year = date.get_year()
if year:
return str(year)
else:
return None
else:
return gramps.gen.datehandler.displayer.display(date)
# -------------------------------------------------------------------
# Person name and data formatting methods
# -------------------------------------------------------------------
def __set_fill_color(self, style_name, number, count):
if self.shuffle_colors:
number = int(number * (count + 1) / int(pow(count, 0.5))) % count
(r, g, b) = colorsys.hsv_to_rgb((number + 1) / count, .20, 1.0)
(r, g, b) = int(255 * r), int(255 * g), int(255 * b)
style_sheet = self.doc.get_style_sheet()