Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/src/person.py
blob: 75115f5fbfa21096bfe8b9d76b6ff70aea49b61c (plain)
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
from guid import Guid
from partialdate import PartialDate
import pango
import const


# Constants
const._person_width = 120
const._person_height = 48
const._person_wmargin_ratio = 12
const._person_hmargin_ratio = 2
const._male_color = (0, 0, 255)
const._female_color = (255, 0, 0)
const._person_font = 'Arial'
const._person_fontsize = 8


		
# Person class
class Person:
	"Class to represent a person in the tree"
	
	def __init__(self, name, sex):
		"Constructor: build"
		self.id = Guid().newid()
		self.name = name
		self.birthdate = PartialDate()
		self.deathdate = PartialDate()
		if sex == 'M' or sex == 'm':
			self.sex = 'M'
		else:
			self.sex = 'F'
		self.parents = None
		self.unions = []
		(self.x0, self.y0, self.x1, self.y1, self.fontsize) = (-1, -1, -1, -1, const._person_fontsize)
		
		
	def append_union(self, union):
		"Add an union"
		self.unions.append(union)
	
	
	def tostring(self, level=1):
		"Translate to a formatted string, tree is horizontal"
		buf = 'P'+str(self.id)+'\n'
		if self.unions == []:
			buf += '\t'*level + self.name + " " + self.sex
		else:
			for u in self.unions:
				if self.sex == 'M':
					opposite = u.mum
				else:
					opposite = u.dad
				buf += '\t'*level + self.name + " " + self.sex + '\n+' + '\t'*level + opposite.name + " " + opposite.sex + '\n'
				buf += u.tostring(level)
				
		return buf
		
		
	def size_to_draw(self):
		"Compute size to draw the person an its subtree"
		if self.unions == []:
			return 1
		totlen = 0
		for u in self.unions:
			totlen = totlen + u.size_to_draw()
		return totlen

	
	def point_inside(self, x, y):
		"Test if point is inside the drawing"
		return x >= self.x0 and x <= self.x1 and y >= self.y0 and y <= self.y1

		
	def child_count(self):
		totchild = 0
		for u in self.unions:
			totchild = totchild + len(u.childs)
		return totchild
		
		
	def draw_node(self, gc, pc):
		"Draw a person"
		
		# draw border
		if self.sex == 'M':
			gc.set_source_rgb(*const._male_color)
		else:
			gc.set_source_rgb(*const._female_color)
		gc.rectangle(self.x0, self.y0, self.x1-self.x0, self.y1-self.y0)		
		gc.stroke()
		
		# draw text
		layout = pango.Layout(pc)
		layout.set_width(int(self.x1-self.x0)*pango.SCALE)
		layout.set_text(self.name)
		layout.set_alignment(pango.ALIGN_CENTER)
		layout.set_wrap(pango.WRAP_WORD)
		layout.set_font_description(pango.FontDescription(const._person_font + " " + str(self.fontsize)))
		gc.move_to(self.x0, self.y0)
		gc.show_layout(layout)		

		
	def compute_draw(self, x, y):
		"Compute person and its subtree draw starting at origin"
		
		# Set person position
		(self.x0, self.y0) = (x, y)
		(self.x1, self.y1) = (x+const._person_width, y+const._person_height)
		self.fontsize = const._person_fontsize
		if self.unions != []:
			# Set union position
			wmargin = const._person_width / const._person_wmargin_ratio
			hmargin = const._person_height / const._person_hmargin_ratio
			for i, u in enumerate(self.unions):
				# Next union
				if i != 0:
					size = u.size_to_draw()
					x = x + (size*const._person_width+size*wmargin)/2
				
				# Set conjoint position
				if self.sex == 'M':
					opposite = u.mum
				else:
					opposite = u.dad
				(opposite.x0, opposite.y0) = (x + const._person_width+wmargin, y)
				(opposite.x1, opposite.y1) = (opposite.x0 + const._person_width, opposite.y0 + const._person_height)
				
				# Set the union and childs of this union
				u.compute_draw(x+const._person_width+(wmargin/2), y+const._person_height+hmargin)
				
				# Shift right
				size = u.size_to_draw()
				x = x + (size*const._person_width+size*wmargin)/2
				
	
	def translate(self, dx, dy):
		"Translate coordinate"
		self.x0 = self.x0 + dx
		self.y0 = self.y0 + dy
		self.x1 = self.x1 + dx
		self.y1 = self.y1 + dy
		
	
	def scale(self, scalerate):
		"Scale coordinate and font"
		self.x0 = self.x0 + ((self.x0 * scalerate) / 100)
		self.y0 = self.y0 + ((self.y0 * scalerate) / 100)
		self.x1 = self.x1 + ((self.x1 * scalerate) / 100)
		self.y1 = self.y1 + ((self.y1 * scalerate) / 100)
		self.fontsize = self.fontsize + (scalerate / 10) 

		
	def draw(self, gc, pc):
		"Draw person and its subtree in the graphical context"
		
		# Draw person
		self.draw_node(gc, pc)
		if self.unions != []:
			# Draw union
			for i, u in enumerate(self.unions):
				# Draw conjoint
				if self.sex == 'M':
					opposite = u.mum
				else:
					opposite = u.dad				
				opposite.draw_node(gc, pc)
				
				# Draw the union and childs of this union
				u.draw(gc, pc, i)