diff options
Diffstat (limited to 'model.py')
-rw-r--r-- | model.py | 204 |
1 files changed, 25 insertions, 179 deletions
@@ -22,50 +22,29 @@ import cjson from sugar import dispatch -class MindMapModel(gtk.GenericTreeModel): - - _COLUMN_TYPES = (str, str, long, long, str) +class MindMapModel(gtk.TreeStore): def __init__(self): - gobject.GObject.__init__(self) + gtk.TreeStore.__init__(self, int, str, long, long, str) self._next_thought_id = 0 self._thoughts_by_id = {} self._thoughts = [] def create_new_thought(self): - thought = Thought(self._next_thought_id) - self._add_thought(thought) - - def _add_thought(self, thought): - thought.changed.connect(self.__thought_changed_cb) - self._thoughts.append(thought) - self._thoughts_by_id[thought.id] = thought + self.append(None, (self._next_thought_id, '', 0, 0, '')) self._next_thought_id += 1 - path = (thought.id,) - self.row_inserted(path, self.get_iter(path)) - - def get_thought(self, thought_id): - return self._thoughts_by_id[int(thought_id)] - - def __thought_changed_cb(self, **kwargs): - thought = kwargs['sender'] - path = (thought.id,) - self.row_changed(path, self.get_iter(path)) - - def get_thoughts(self): - return self._thoughts - def serialize(self): thoughts = [] - for thought in self._thoughts: + for row in self: + logging.debug('serialize %r' % row[0]) thought_dict = {} - thought_dict['id'] = thought.id - thought_dict['name'] = thought.name - thought_dict['x'] = thought.x - thought_dict['y'] = thought.y - thought_dict['color'] = thought.color + 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) return cjson.encode({'thoughts': thoughts}) @@ -73,152 +52,19 @@ class MindMapModel(gtk.GenericTreeModel): def unserialize(self, data): thoughts = cjson.decode(data)['thoughts'] for thought_dict in thoughts: - thought = Thought(thought_dict['id']) - thought.name = thought_dict.get('name', None) - thought.x = thought_dict.get('x', None) - thought.y = thought_dict.get('y', None) - thought.color = thought_dict.get('color', None) - self._add_thought(thought) - - # gtk.GenericTreeModel methods - # paths and rowrefs are the thought ids - def on_get_flags(self): - return gtk.TREE_MODEL_ITERS_PERSIST - - def on_get_n_columns(self): - return len(self._COLUMN_TYPES) - - def on_get_column_type(self, n): - return self._COLUMN_TYPES[n] - - def on_get_iter(self, path): - logging.debug('on_get_iter %r %r' % (type(path), path)) - if path[0] in self._thoughts_by_id: - return path[0] - else: - return None - - def on_get_path(self, rowref): - logging.debug('on_get_path %r' % rowref) - return (rowref,) - - def on_get_value(self, rowref, column): - #logging.debug('on_get_value %r %r' % (rowref, column)) - if rowref >= len(self._thoughts): - return None - thought = self._thoughts[rowref] - value_tuple = thought.get_tuple() - return value_tuple[column] - - def on_iter_next(self, rowref): - #logging.debug('on_iter_next %r' % rowref) - if rowref not in self._thoughts_by_id: - return None - thought = self._thoughts_by_id[rowref] - - index = self._thoughts.index(thought) - if index + 1 >= len(self._thoughts): - return None - next_thought = self._thoughts[index + 1] - #logging.debug('on_iter_next returning %r' % next_thought.id) - return next_thought.id - - def on_iter_children(self, rowref): - logging.debug('on_iter_children %r' % rowref) - if rowref: - return None - if self._thoughts: - return self._thoughts[0].id - else: - return None - - def on_iter_has_child(self, rowref): - return False - - def on_iter_n_children(self, rowref): - logging.debug('on_iter_n_children %r' % rowref) - if rowref: - return 0 - return len(self._thoughts) - - def on_iter_nth_child(self, rowref, n): - logging.debug('on_iter_nth_child %r %r' % (rowref, n)) - if rowref is not None: - return None - if n < len(self._thoughts): - return self._thoughts[n].id - - def on_iter_parent(child): - return None - -class Thought(object): - - def __init__(self, thought_id, name=None, x=0, y=0, color=None): - if thought_id is None: - raise ValueError('thought_id cannot be None') - self._id = thought_id - self._name = name - self._x = x - self._y = y - self._color = color - - self.changed = dispatch.Signal() - - def get_tuple(self): - return (self.id, self.name, self.x, self.y, self.color) - - def set_id(self, new_id): - if self._id == new_id: - return - self._id = new_id - self.changed.send(self) - - def get_id(self): - return self._id - - id = property(get_id, set_id) - - def set_name(self, new_name): - if self._name == new_name: - return - self._name = new_name - self.changed.send(self) - - def get_name(self): - return self._name - - name = property(get_name, set_name) - - def set_x(self, new_x): - if self._x == new_x: - return - self._x = new_x - self.changed.send(self) - - def get_x(self): - return self._x - - x = property(get_x, set_x) - - def set_y(self, new_y): - if self._y == new_y: - return - self._y = new_y - self.changed.send(self) - - def get_y(self): - return self._y - - y = property(get_y, set_y) - - def set_color(self, new_color): - if self._color == new_color: - return - self._color = new_color - self.changed.send(self) - - def get_color(self): - return self._color - - color = property(get_color, set_color) + 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))) + + def find_by_id(self, thought_id, rows=None): + if rows is None: + rows = self + for row in rows: + if row[0] == thought_id: + return row + self.find_by_id(thought_id, row.iterchildren()) |