diff options
Diffstat (limited to 'view.py')
-rw-r--r-- | view.py | 84 |
1 files changed, 48 insertions, 36 deletions
@@ -31,8 +31,8 @@ class MindMapView(Canvas): def __init__(self, model=None): self._model = None + self._row_changed_sid = None self._row_deleted_sid = None - self._row_inserted_sid = None self._selected_thought = None Canvas.__init__(self) @@ -63,8 +63,8 @@ class MindMapView(Canvas): thought_rect = thought.get_rect() if (x >= 0 and x + thought_rect.width <= rect.width) and \ (y >= 0 and y + thought_rect.height <= rect.height): - thought.model.x = x - thought.model.y = y + row = self.model.find_by_id(thought.id) + self.model.set(row.iter, 2, x, 3, y) def __button_release_event_cb(self, widget, event): logging.debug('button_release_event_cb') @@ -86,43 +86,55 @@ class MindMapView(Canvas): return if self._model is not None: + self._model.disconnect(self._row_changed_sid) self._model.disconnect(self._row_deleted_sid) - self._model.disconnect(self._row_inserted_sid) self._model = new_model if self._model is not None: + self._row_changed_sid = \ + self._model.connect('row-changed', self.__row_changed_cb) self._row_deleted_sid = \ self._model.connect('row-deleted', self.__row_deleted_cb) - self._row_inserted_sid = \ - self._model.connect('row-inserted', self.__row_inserted_cb) self.remove_all_elements() - for thought_model in self._model.get_thoughts(): - though_view = ThoughtView(thought_model) - self.add_element(though_view) + self._populate_from_model(self._model) + + def _populate_from_model(self, rows): + for row in rows: + thought_view = ThoughtView(row[0], row[1], row[2], row[3], row[4]) + self._populate_from_model(row.iterchildren()) + self.add_element(thought_view) def get_model(self): return self._model model = property(get_model, set_model) - def __row_inserted_cb(self, model, path, iter): - logging.debug('__row_inserted_cb %r' % path) + def __row_changed_cb(self, model, path, iter): + logging.debug('__row_changed_cb %r' % path) - thought_model = model.get_thought(path[0]) - though_view = ThoughtView(thought_model) - self.add_element(though_view) + row = model[iter] + thought_view = self._get_thought_by_id(row[0]) + if thought_view is None: + thought_view = ThoughtView(row[0], row[1], row[2], row[3], row[4]) + self.add_element(thought_view) + else: + thought_view.name = row[1] + thought_view.x = row[2] + thought_view.y = row[3] + thought_view.color = row[4] + thought_view.invalidate() def __row_deleted_cb(self, model, path): logging.debug('__row_deleted_cb %r' % path) - though_view = self._get_though_by_id(path[0]) - self.remove_element(though_view) + thought_view = self._get_thought_by_id(path[0]) + self.remove_element(thought_view) - def _get_though_by_id(self, thought_id): + def _get_thought_by_id(self, thought_id): for thought_view in self.get_elements(): - if thought_view.model.id == path[0]: + if thought_view.id == thought_id: return thought_view return None @@ -131,17 +143,22 @@ class ThoughtView(CanvasElement): _PADDING = style.zoom(15) _LINE_WIDTH = style.zoom(2) - def __init__(self, model): + def __init__(self, thought_id, name, x, y, color): CanvasElement.__init__(self) - self.model = model + logging.debug('ThoughtView %r %r' % (thought_id, name)) + + self.id = thought_id + self.name = name + self.x = x + self.y = y + self.color = color + self._last_width = -1 self._last_height = -1 self._dragging = False self._selected = False - self.model.changed.connect(self.__model_changed_cb) - def set_dragging(self, dragging): if self._dragging != dragging: self._dragging = dragging @@ -165,10 +182,10 @@ class ThoughtView(CanvasElement): pixmap = gtk.gdk.Pixmap(None, 1, 1, visual.depth) context = pixmap.cairo_create() - if self.model.name is None or not self.model.name: + if self.name is None or not self.name: name = _('Unnamed') else: - name = self.model.name + name = self.name layout = context.create_layout() layout.set_text(name) @@ -186,10 +203,10 @@ class ThoughtView(CanvasElement): CanvasElement.draw(self, context) def _draw_background(self, context): - if self.model.color is None or not self.model.color: + if self.color is None or not self.color: color = style.Color('#FFFFFF') else: - color = style.Color(self.model.color) + color = style.Color(self.color) r, g, b, a = color.get_rgba() context.save() @@ -208,8 +225,8 @@ class ThoughtView(CanvasElement): width += self._PADDING * 2 height += self._PADDING * 2 - x = self.model.x + self._PADDING - y = self.model.y + self._PADDING + x = self.x + self._PADDING + y = self.y + self._PADDING context.translate(x, y) context.show_layout(layout) context.restore() @@ -226,8 +243,8 @@ class ThoughtView(CanvasElement): context.set_source_rgb(0, 0, 0) context.set_line_width(self._LINE_WIDTH) - x = self.model.x + self._LINE_WIDTH / 2 - y = self.model.y + self._LINE_WIDTH / 2 + x = self.x + self._LINE_WIDTH / 2 + y = self.y + self._LINE_WIDTH / 2 rect_width = width - self._LINE_WIDTH rect_height = height - self._LINE_WIDTH @@ -235,11 +252,6 @@ class ThoughtView(CanvasElement): context.stroke() context.restore() - def __model_changed_cb(self, **kwargs): - self.x = self.model.x - self.y = self.model.y - self.invalidate() - def get_rect(self): if -1 in (self._last_width, self._last_height): layout = self._get_name_layout() @@ -247,6 +259,6 @@ class ThoughtView(CanvasElement): self._last_width = width + self._PADDING * 2 self._last_height = height + self._PADDING * 2 - return gtk.gdk.Rectangle(self.model.x, self.model.y, + return gtk.gdk.Rectangle(self.x, self.y, self._last_width, self._last_height) |