diff options
author | Tomeu Vizoso <tomeu@sugarlabs.org> | 2009-01-29 12:59:12 (GMT) |
---|---|---|
committer | Tomeu Vizoso <tomeu@sugarlabs.org> | 2009-01-29 12:59:12 (GMT) |
commit | fbda5e94e81247be0dabe5f5a0dac44376427b4e (patch) | |
tree | 91f793400b3b300335ea4103e7b1b17d56b57f07 | |
parent | 05a816a38fe398e9b80712764bd4946df23eb4d0 (diff) |
Serialize descendants
-rw-r--r-- | model.py | 31 |
1 files changed, 21 insertions, 10 deletions
@@ -42,30 +42,41 @@ class MindMapModel(gtk.TreeStore): self._next_thought_id += 1 return thought_id - def serialize(self): + def _serialize_thoughts(self, rows): thoughts = [] - for row in self: - logging.debug('serialize %r' % row[0]) + for row in rows: thought_dict = {} + thoughts.append(thought_dict) + thought_dict['id'] = row[0] thought_dict['name'] = row[1] thought_dict['x'] = row[2] thought_dict['y'] = row[3] thought_dict['color'] = row[4] - thoughts.append(thought_dict) + + children = self._serialize_thoughts(row.iterchildren()) + thought_dict['children'] = children + + return thoughts - return cjson.encode({'thoughts': thoughts}) + def serialize(self): + return cjson.encode({'thoughts': self._serialize_thoughts(self)}) def unserialize(self, data): thoughts = cjson.decode(data)['thoughts'] + self._unserialize_thoughts(parent=None, thoughts=thoughts) + + def _unserialize_thoughts(self, parent, thoughts): for thought_dict in thoughts: self._next_thought_id = max(self._next_thought_id + 1, thought_dict['id'] + 1) - self.append(None, (thought_dict['id'], - thought_dict.get('name', None), - thought_dict.get('x', None), - thought_dict.get('y', None), - thought_dict.get('color', None))) + new_row = self.append(parent, (thought_dict['id'], + thought_dict.get('name', None), + thought_dict.get('x', None), + thought_dict.get('y', None), + thought_dict.get('color', None))) + children = thought_dict.get('children', []) + self._unserialize_thoughts(parent=new_row, thoughts=children) def find_by_id(self, thought_id, rows=None): if rows is None: |