Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLionel LASKE <llaske@c2s.fr>2011-12-20 21:42:15 (GMT)
committer Lionel LASKE <llaske@c2s.fr>2011-12-20 21:42:15 (GMT)
commit0be8bfea3f05a081640772457af903a4beb597bc (patch)
tree951c16049748f1cc22b03651da7a3ba075eff114
parent9766a80536a351e45d67e0b3787bd7a6b6bcd569 (diff)
Add read/write to journal
-rw-r--r--activity.py41
-rw-r--r--src/guid.py9
-rw-r--r--src/person.py46
-rw-r--r--src/tree.py148
-rw-r--r--src/union.py11
-rw-r--r--sugardummy.py18
6 files changed, 195 insertions, 78 deletions
diff --git a/activity.py b/activity.py
index fec5426..d5c6750 100644
--- a/activity.py
+++ b/activity.py
@@ -56,11 +56,11 @@ class RootsActivity(activity.Activity):
tool.connect('clicked', self.emptytree)
toolbarsample.insert(tool, -1)
tool = ToolButton('sample1')
- tool.set_tooltip(_('Sample 1'))
+ tool.set_tooltip(_('Test'))
tool.connect('clicked', self.sample1)
toolbarsample.insert(tool, -1)
tool = ToolButton('sample2')
- tool.set_tooltip(_('Sample 2'))
+ tool.set_tooltip(_('Napoleon'))
tool.connect('clicked', self.sample2)
toolbarsample.insert(tool, -1)
toolbox.add_toolbar(_('Samples'), toolbarsample)
@@ -111,19 +111,17 @@ class RootsActivity(activity.Activity):
# Create empty tree
self.tree = None
- self.init_tree(empty_tree)
+ self.init_tree(empty_tree())
# Show all
self.show_all()
self.area.window.set_cursor(gtk.gdk.Cursor(gtk.gdk.ARROW))
- def init_tree(self, treecallback):
+ def init_tree(self, tree):
"Create and init a tree"
- if self.tree is not None:
- self.tree.persons = []
- self.tree.unions = []
- self.tree = treecallback()
+ self.zoomlevel = 0
+ self.tree = tree
(self.initx, self.inity) = (const.tree_initx, const.tree_inity)
self.tree.set_position(self.initx, self.inity)
self.show_detail(None)
@@ -363,17 +361,38 @@ class RootsActivity(activity.Activity):
def emptytree(self, event):
"Init with an empty tree"
- self.init_tree(empty_tree)
+ self.init_tree(empty_tree())
def sample1(self, event):
"Init with a sample tree"
- self.init_tree(sample_family1)
+ self.init_tree(sample_family1())
def sample2(self, event):
"Init with a sample tree"
- self.init_tree(sample_family2)
+ self.init_tree(sample_family2())
+
+
+ def write_file(self, file_path):
+ "Called when activity is saved, save the tree in the file"
+ #self.metadata['current_page'] = '3'
+ file = open(file_path, 'wb')
+ try:
+ self.tree.write_to(file)
+ finally:
+ file.close()
+
+
+ def read_file(self, file_path):
+ "Called when activity is loaded, load the tree from the file"
+ file = open(file_path, 'rb')
+ try:
+ tree = Tree().read_from(file)
+ finally:
+ file.close()
+ self.init_tree(tree)
+
# Dummy call to allow running on Windows
diff --git a/src/guid.py b/src/guid.py
index d163137..30905f9 100644
--- a/src/guid.py
+++ b/src/guid.py
@@ -8,9 +8,14 @@ class Guid:
"Constructor, init reference count"
self.count=0
- def newid(self):
+ def newid(self, start=None):
"Generate a new id"
- self.count = self.count+1
+ if start is None:
+ self.count = self.count+1
+ else:
+ if self.count < start:
+ self.count = start
+ return start
return self.count
__instance=None
diff --git a/src/person.py b/src/person.py
index c960fa3..c0f89bf 100644
--- a/src/person.py
+++ b/src/person.py
@@ -20,9 +20,9 @@ const._person_fontsize = 8
class Person:
"Class to represent a person in the tree"
- def __init__(self, name, sex):
+ def __init__(self, name, sex, id=None):
"Constructor: build"
- self.id = Guid().newid()
+ self.id = Guid().newid(id)
self.name = name
self.birthdate = PartialDate()
self.deathdate = PartialDate()
@@ -51,7 +51,7 @@ class Person:
def tostring(self, level=1):
"Translate to a formatted string, tree is horizontal"
- buf = 'P'+str(self.id)+'\n'
+ buf = "P"+str(self.id)+'\n'
if self.unions == []:
buf += '\t'*level + self.name + " " + self.sex
else:
@@ -71,8 +71,10 @@ class Person:
if self.unions == []:
return 1
totlen = 0
- for u in self.unions:
+ for i, u in enumerate(self.unions):
totlen = totlen + u.size_desc()
+ if i != 0:
+ totlen = totlen+self.unions[i-1].child_count()
return totlen
@@ -85,7 +87,7 @@ class Person:
"Compute number of childs"
totchild = 0
for u in self.unions:
- totchild = totchild + len(u.childs)
+ totchild = totchild + u.child_count()
return totchild
@@ -120,7 +122,7 @@ class Person:
for i, u in enumerate(self.unions):
# Next union
if i != 0:
- size = u.size_desc()+self.unions[i-1].size_desc()
+ size = u.size_desc()+self.unions[i-1].child_count()
x = x + (size*const._person_width+size*self.width_margin())/2
# Set conjoint position
@@ -136,38 +138,6 @@ class Person:
# Shift right
size = u.size_desc()
x = x + (size*const._person_width+size*self.width_margin())/2
-
-
- def compute_asc_pos(self):
- "Compute ascendant and its subtree draw starting at origin"
- if self.parents is None:
- return
-
- # Set parent position
- #size = self.parents.size_desc()
- dad = self.parents.dad
- mum = self.parents.mum
- if len(self.unions) == 0:
- x = self.x0 - (const._person_width+self.width_margin())/2
- else:
- conjoint = self.unions[0].conjoint(self)
- if conjoint.parents is None:
- x = self.x0 - (const._person_width+self.width_margin())/2
- elif self.x0 < conjoint.x0:
- size = 1
- x = self.x0 - (size*3*const._person_width+size*self.width_margin())/2
- else:
- size = 1
- x = self.x0 + (size*const._person_width+size*self.width_margin())/2
- y = self.y0 - const._person_height - self.height_margin()
- dad.set_pos(x, y)
- mum.set_pos(x + const._person_width+self.width_margin(), y)
-
- # Set parent parent position and their child
- dad.compute_asc_pos()
- mum.compute_asc_pos()
- dad.compute_desc_pos(dad.x0, dad.y0)
- mum.compute_desc_pos(mum.x0, mum.y0)
def translate(self, dx, dy):
diff --git a/src/tree.py b/src/tree.py
index a22b3e8..f7bd330 100644
--- a/src/tree.py
+++ b/src/tree.py
@@ -2,6 +2,7 @@
from person import Person
from union import Union
import const
+import pickle
# Tree class
@@ -15,19 +16,104 @@ class Tree:
self.unions = []
- def Person(self, name, sex):
+ def read_from(self, file):
+ "Read tree from the file"
+
+ # Load persons
+ count = pickle.load(file)
+ for i in range(count):
+ id = pickle.load(file)
+ name = pickle.load(file)
+ sex = pickle.load(file)
+ self.Person(name, sex, id)
+
+ # Load unions
+ count = pickle.load(file)
+ for i in range(count):
+ # Create union
+ id = pickle.load(file)
+ dadid = pickle.load(file)
+ mumid = pickle.load(file)
+ dad = self.get_person(dadid)
+ if dad is None:
+ print "ERROR on "+str(dadid)
+ mum = self.get_person(mumid)
+ if mum is None:
+ print "ERROR on "+str(mumid)
+ u = self.Union(dad, mum, id)
+
+ # Create childs
+ childcount = pickle.load(file)
+ for j in range(childcount):
+ childid = pickle.load(file)
+ child = self.get_person(childid)
+ if child is None:
+ print "ERROR on "+str(childid)
+ u.append_child(child)
+
+ # Get root
+ rootid = pickle.load(file)
+ root = self.get_person(rootid)
+ if root is None:
+ print "ERROR on "+str(rootid)
+ self.root = root
+
+ return self
+
+
+ def write_to(self, file):
+ "Write tree to file"
+
+ # Save persons
+ pickle.dump(len(self.persons), file)
+ for p in self.persons:
+ pickle.dump(p.id, file)
+ pickle.dump(p.name, file)
+ pickle.dump(p.sex, file)
+
+ # Save unions
+ pickle.dump(len(self.unions), file)
+ for u in self.unions:
+ pickle.dump(u.id, file)
+ pickle.dump(u.dad.id, file)
+ pickle.dump(u.mum.id, file)
+ pickle.dump(len(u.childs), file)
+ for c in u.childs:
+ pickle.dump(c.id, file)
+
+ # Save root
+ pickle.dump(self.root.id, file)
+
+
+ def Person(self, name, sex, id=None):
"Add a new person"
- p = Person(name, sex)
+ p = Person(name, sex, id)
self.persons.append(p)
return p
- def Union(self, dad, mum):
+ def Union(self, dad, mum, id=None):
"Add a new union"
- u = Union(dad, mum)
+ u = Union(dad, mum, id)
self.unions.append(u)
return u
+
+ def get_person(self, id):
+ "Look for a person with an id"
+ for p in self.persons:
+ if p.id == id:
+ return p
+ return None
+
+
+ def get_union(self, id):
+ "Look for an union with an id"
+ for u in self.unions:
+ if u.id == id:
+ return u
+ return None
+
def is_descendant(self, person, root=None):
"Look if person is a descendant of the tree"
@@ -105,7 +191,7 @@ class Tree:
root.compute_desc_pos(x, y)
size = root.size_desc()
if i > 0:
- size = size + roots[i].size_desc()
+ size = size + roots[i].child_count()
x = x + (size*const._person_width+size*root.width_margin())/2
x = initx
y = y + const._person_height+root.height_margin()
@@ -183,23 +269,9 @@ def empty_tree():
return tree
-
-def sample_family1():
- # Empty family
- tree = Tree()
- tree.root = tree.Person("C", "M")
-
- a = tree.Person("A", "M")
- b = tree.Person("B", "F")
- tree.Union(a, b).append_child(tree.root)
- bb = tree.Person("BB", "F")
- tree.Union(a, bb)
- return tree
-
-
-def sample_family2():
- # Large family
+def sample_family1():
+ # Large family sample
tree = Tree()
l = tree.Person("Lucien", "M")
@@ -247,6 +319,38 @@ def sample_family2():
uc2.append_child(pi)
uc2.append_child(tree.Person("Camille", "F"))
- tree.root = d #vm
+ tree.root = d
return tree
+
+
+def sample_family2():
+ # Napoleon family
+ tree = Tree()
+ tree.root = p1 = tree.Person("Chales-Marie Bonaparte", "M")
+ p2 = tree.Person("Letizia Ramolino", "F")
+ u1 = tree.Union(p2, p1)
+ p4 = tree.Person("Napoleon", "M")
+ p6 = tree.Person("Louis Bonaparte", "M")
+ u1.append_child(p4)
+ u1.append_child(p6)
+ p3 = tree.Person("Josephine", "F")
+ u2 = tree.Union(p4, p3)
+ p5 = tree.Person("Marie-Louise d'Autriche", "F")
+ u3 = tree.Union(p4, p5)
+
+ p10 = tree.Person("Napoleon II", "M")
+ u3.append_child(p10)
+
+ p7 = tree.Person("Hortense de Beauharnais", "F")
+ u4 = tree.Union(p6, p7)
+ p8 = tree.Person("Napoleon III", "M")
+ u4.append_child(p8)
+
+ p9 = tree.Person("Eugenie de Palafox-Guzman", "F")
+ u5 = tree.Union(p8, p9)
+ p11 = tree.Person("Louis Napoleon", "M")
+ u5.append_child(p11)
+
+ return tree
+
diff --git a/src/union.py b/src/union.py
index fdfff51..689b59f 100644
--- a/src/union.py
+++ b/src/union.py
@@ -11,9 +11,9 @@ const._union_color = (0, 0, 0)
class Union:
"Class to process union between two persons"
- def __init__(self, dad, mum):
+ def __init__(self, dad, mum, id=None):
"Constructor, init dad, mum and childs"
- self.id = Guid().newid()
+ self.id = Guid().newid(id)
self.dad = dad
dad.append_union(self)
self.mum = mum
@@ -30,7 +30,7 @@ class Union:
def tostring(self, level=1):
"Translate to a formatted string, tree is horizontal"
- str = 'U{0}\n'.format(self.id)
+ str = "U"+str(self.id)+"\n"
for c in self.childs:
str += c.tostring(level+1) + '\n'
@@ -81,6 +81,11 @@ class Union:
return self.dad
+ def child_count(self):
+ "Compute number of childs"
+ return len(self.childs)
+
+
def draw(self, gc, pc):
"Draw person and its subtree in the graphical context"
diff --git a/sugardummy.py b/sugardummy.py
index 6a2f796..5ebea5e 100644
--- a/sugardummy.py
+++ b/sugardummy.py
@@ -1,5 +1,6 @@
import gtk
+import os.path
# Dummy class for Sugar
class activity:
@@ -12,7 +13,8 @@ class activity:
def __init__(self, handle):
self.window = gtk.Window()
self.window.set_title("Roots")
- self.window.connect("destroy", gtk.main_quit)
+ self.window.connect("destroy", self.on_close)
+ self.metadata = {}
def set_canvas(self, canvas):
self._canvas = canvas
@@ -22,7 +24,9 @@ class activity:
vbox.pack_start(self._toolbox, True, True, 0)
vbox.pack_start(self._canvas, True, True, 0)
self.window.add(vbox)
- self.window.show_all()
+ self.window.show_all()
+ if os.path.isfile("../tmp/sugar_dummy_journal.txt"):
+ self.read_file("../tmp/sugar_dummy_journal.txt")
def create_pango_context(self):
return gtk.Widget.create_pango_context(self.window)
@@ -30,6 +34,16 @@ class activity:
def set_toolbox(self, toolbox):
self._toolbox = toolbox
+ def read_file(self, file_path):
+ pass
+
+ def write_file(self, file_path):
+ pass
+
+ def on_close(self, event):
+ gtk.main_quit()
+ self.write_file("../tmp/sugar_dummy_journal.txt")
+
# Dummy class for Toolbox
class ActivityToolbox(gtk.Toolbar):