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
|
# Base import
import gtk
import logging
from gettext import gettext as _
# Local import
from src.person import Person
from src.union import Union
from src.tree import Tree
from src.tree import sample_family1, sample_family2
import src.const as const
# Sugar import
try:
from sugar.activity import activity
from sugar.graphics.toolbutton import ToolButton
from gtk import Toolbar
const.inSugar = True
except ImportError:
from sugardummy import *
const.inSugar = False
class RootsActivity(activity.Activity):
def __init__(self, handle):
"Set up the activity."
# Sugar init
activity.Activity.__init__(self, handle)
# Create toolbox
toolbox = activity.ActivityToolbox(self)
toolbar = Toolbar()
tool = ToolButton('zoom-out')
tool.set_tooltip(_('Zoom out'))
tool.set_accelerator(_('<ctrl>minus'))
tool.connect('clicked', self.zoom_out)
toolbar.insert(tool, -1)
tool = ToolButton('zoom-in')
tool.set_tooltip(_('Zoom in'))
tool.set_accelerator(_('<ctrl>equal'))
tool.connect('clicked', self.zoom_in)
toolbar.insert(tool, -1)
toolbox.add_toolbar(_('View'), toolbar)
self.set_toolbox(toolbox)
toolbox.show()
# Create drawing area
self.zoomlevel = 0
self.area = gtk.DrawingArea()
self.area.set_size_request(750, 600)
self.area.set_events(gtk.gdk.BUTTON_PRESS_MASK|gtk.gdk.BUTTON_RELEASE_MASK|gtk.gdk.BUTTON_MOTION_MASK|gtk.gdk.POINTER_MOTION_MASK)
self.area.connect("expose_event", self.area_expose_cb)
self.area.connect("button_press_event", self.press_button)
self.area.connect("button_release_event", self.release_button)
self.area.connect("motion_notify_event", self.mouse_move)
self.moving = False
# Create detail view
self.fixed = gtk.VBox()
self.fixed.set_size_request(200, 300)
self.fixed.pack_start(gtk.Label("Name:"), False, False, 0)
self.detail_name = gtk.TextView()
self.detail_name.set_cursor_visible(True)
self.detail_name.get_buffer().connect("changed", self.detail_changed)
self.fixed.pack_start(self.detail_name, False, False, 0)
self.detail_chkmale = gtk.RadioButton(None, "Male")
self.detail_chkmale.connect("toggled", self.sexradio_checked, 'M')
self.fixed.pack_start(self.detail_chkmale, False, False, 0)
self.detail_chkfemale = gtk.RadioButton(self.detail_chkmale, "Female")
self.detail_chkfemale.connect("toggled", self.sexradio_checked, 'F')
self.fixed.pack_start(self.detail_chkfemale, False, False, 0)
self.detail_btnaddunion = gtk.Button("Add union")
self.detail_btnaddunion.connect("clicked", self.addunion_clicked)
self.detail_btnaddchild = gtk.Button("Add child")
self.detail_btnaddchild.connect("clicked", self.addchild_clicked)
self.detail_btndelete = gtk.Button("Delete")
self.detail_btndelete.connect("clicked", self.delete_clicked)
self.fixed.pack_start(self.detail_btnaddunion, False, False, 0)
self.fixed.pack_start(self.detail_btnaddchild, False, False, 0)
self.fixed.pack_start(self.detail_btndelete, False, False, 0)
self.box = gtk.HBox(False)
self.box.pack_start(self.fixed, True, True, 0)
self.box.pack_start(self.area, True, True, 0)
self.set_canvas(self.box)
# Create sample data
(self.initx, self.inity) = (500, 50)
self.tree = sample_family2()
self.tree.root.compute_draw(self.initx, self.inity)
self.show_detail(None)
# Show all
self.show_all()
self.area.window.set_cursor(gtk.gdk.Cursor(gtk.gdk.ARROW))
def set_center(self, person):
"Set the center of the draw on a person"
rect = self.area.allocation
dx = (rect.width/2) - self.tree.root.x0 - (person.x0 - self.tree.root.x0) - (person.x1 - person.x0)/2
dy = (rect.height/2) - self.tree.root.y0 - (person.y0 - self.tree.root.y0) - (person.y1 - person.y0)/2
self.translate(dx, dy)
def show_detail(self, person):
"Show detail information for a person"
# Change selection
self.selected = person
# No selection
if person == None:
self.detail_name.set_sensitive(False)
self.detail_name.get_buffer().set_text("Click one a node to select it")
self.detail_chkmale.set_active(True)
self.detail_chkmale.set_sensitive(False)
self.detail_chkfemale.set_sensitive(False)
self.detail_btnaddunion.set_sensitive(False)
self.detail_btnaddchild.set_sensitive(False)
self.detail_btndelete.set_sensitive(False)
return
# A node is selected
self.detail_name.set_sensitive(True)
self.detail_name.grab_focus()
self.detail_name.get_buffer().set_text(person.name)
if person.sex == 'M':
self.detail_chkmale.set_active(True)
else:
self.detail_chkfemale.set_active(True)
# Compute button status
checkable = (len(person.unions) == 0)
self.detail_chkmale.set_sensitive(checkable)
self.detail_chkfemale.set_sensitive(checkable)
self.detail_btnaddchild.set_sensitive(len(person.unions) > 0)
unionscount = len(person.unions)
childcount = person.child_count()
isdescendant = self.tree.is_descendant(person)
if person == self.tree.root:
deletable = False
elif unionscount == 0:
deletable = True
elif unionscount > 1:
deletable = False
elif unionscount == 1 and isdescendant:
deletable = False
elif childcount > 0:
deletable = False
else:
deletable = True
self.detail_btndelete.set_sensitive(deletable)
self.detail_btnaddunion.set_sensitive(isdescendant)
def area_expose_cb(self, area, event):
"Draw tree event"
# Create context then draw tree inside
gc = self.area.window.cairo_create()
pc = self.create_pango_context()
self.tree.root.draw(gc, pc)
gc.stroke()
# Draw cross in middle to debug position
rect = self.area.allocation
gc.move_to((rect.width/2)-10, (rect.height/2))
gc.line_to((rect.width/2)+10, (rect.height/2))
gc.move_to((rect.width/2), (rect.height/2)-10)
gc.line_to((rect.width/2), (rect.height/2)+10)
gc.set_source_rgb(255, 255, 255)
gc.stroke()
def press_button(self, widget, event):
"Mouse button clicked, detail a person or start the moving mode"
# Look if click in a person
for p in self.tree.persons:
# Found one, show detail
if p.point_inside(event.x, event.y):
self.set_center(p)
self.show_detail(p)
self.moving = False
return
# Not found, pass in moving mode
self.show_detail(None)
self.movingStart = (event.x, event.y)
self.moving = True
self.area.window.set_cursor(gtk.gdk.Cursor(gtk.gdk.FLEUR))
def translate(self, dx, dy):
# Translate all the tree from deltax, deltay
(self.initx, self.inity) = (self.initx+dx, self.inity+dy)
for p in self.tree.persons:
p.translate(dx, dy)
self.redraw()
def mouse_move(self, widget, event):
"Mouse move event, in moving mode translate draw if in moving mode else change cursor on person"
# In moving mode ?
if self.moving:
# Compute translation
self.translate(event.x-self.movingStart[0], event.y-self.movingStart[1])
self.movingStart = (event.x, event.y)
else:
# Look if a person is under the cursor
for p in self.tree.persons:
# Found one, change cursor
if p.point_inside(event.x, event.y):
self.area.window.set_cursor(gtk.gdk.Cursor(gtk.gdk.HAND1))
return
# Not found, set standard cursor
self.area.window.set_cursor(gtk.gdk.Cursor(gtk.gdk.ARROW))
def release_button(self, widget, event):
"Mouse button released, stop moving mode"
self.area.window.set_cursor(gtk.gdk.Cursor(gtk.gdk.ARROW))
self.moving = False
def zoom_in(self, event):
"ToolBar button zoom in clicked"
self.zoomlevel = self.zoomlevel + 1
for p in self.tree.persons:
p.scale(20)
self.redraw()
def zoom_out(self, event):
"ToolBar button zoom out clicked"
if self.zoomlevel == -3:
return
self.zoomlevel = self.zoomlevel - 1
for p in self.tree.persons:
p.scale(-20)
self.redraw()
def detail_changed(self, event):
"Textfield for the detail has changed, change the matching person name"
if self.selected == None:
self.redraw()
return
buffer = self.detail_name.get_buffer()
self.selected.name = buffer.get_text(buffer.get_start_iter(), buffer.get_end_iter())
self.redraw()
def sexradio_checked(self, widget, data):
"Radio button for sex checked, change sex for the selected person"
if self.selected is not None:
self.selected.sex = data
self.redraw()
def addchild_clicked(self, event):
"Add child clicked"
# TODO: Child add to the first union
newchild = self.tree.Person("", "M")
self.selected.unions[0].append_child(newchild)
self.tree.root.compute_draw(self.tree.root.x0, self.tree.root.y0)
self.show_detail(newchild)
def addunion_clicked(self, event):
"Add union clicked"
if self.selected.sex == "M":
male = self.selected
female = newunion = self.tree.Person("", "F")
else:
male = newunion = self.tree.Person("", "M")
female = self.selected
self.tree.Union(male, female)
self.tree.root.compute_draw(self.tree.root.x0, self.tree.root.y0)
self.show_detail(newunion)
def delete_clicked(self, event):
"Delete button clicked"
# Remove from union
for u in self.selected.unions:
self.tree.unions.remove(u)
if u.dad == self.selected:
u.mum.unions.remove(u)
else:
u.dad.unions.remove(u)
# Remove from parent child
if self.selected.parents is not None:
self.selected.parents.childs.remove(self.selected)
# Remove from tree
self.tree.persons.remove(self.selected)
# Recompute and redraw
self.tree.root.compute_draw(self.tree.root.x0, self.tree.root.y0)
self.show_detail(None)
def redraw(self):
"Redraw area"
self.area.queue_draw_area(0, 0, self.area.allocation.width, self.area.allocation.height)
# Dummy call to allow running on Windows
if not const.inSugar:
RootsActivity(0)
gtk.main()
|