From a4848bf6ea047b90cbd16712b2b3cb6c9747365c Mon Sep 17 00:00:00 2001 From: Tomeu Vizoso Date: Tue, 20 Jan 2009 18:09:38 +0000 Subject: Added background colors to the model and display them --- diff --git a/model.py b/model.py index 75ad6a8..2084773 100644 --- a/model.py +++ b/model.py @@ -24,7 +24,7 @@ from sugar import dispatch class MindMapModel(gtk.GenericTreeModel): - _COLUMN_TYPES = (str, str, long, long) + _COLUMN_TYPES = (str, str, long, long, str) def __init__(self): gobject.GObject.__init__(self) @@ -65,6 +65,7 @@ class MindMapModel(gtk.GenericTreeModel): thought_dict['name'] = thought.name thought_dict['x'] = thought.x thought_dict['y'] = thought.y + thought_dict['color'] = thought.color thoughts.append(thought_dict) return cjson.encode({'thoughts': thoughts}) @@ -73,9 +74,10 @@ class MindMapModel(gtk.GenericTreeModel): thoughts = cjson.decode(data)['thoughts'] for thought_dict in thoughts: thought = Thought(thought_dict['id']) - thought.name = thought_dict['name'] - thought.x = thought_dict['x'] - thought.y = thought_dict['y'] + 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 @@ -151,18 +153,19 @@ class MindMapModel(gtk.GenericTreeModel): class Thought(object): - def __init__(self, thought_id, name=None, x=0, y=0): + 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) + return (self.id, self.name, self.x, self.y, self.color) def set_id(self, new_id): if self._id == new_id: @@ -208,3 +211,14 @@ class Thought(object): 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) + diff --git a/treeview.py b/treeview.py index 42fdbf4..cec8562 100644 --- a/treeview.py +++ b/treeview.py @@ -58,6 +58,13 @@ class TreeView(gtk.ScrolledWindow): column = gtk.TreeViewColumn(_('y'), cell, text=3) self._tree_view.append_column(column) + cell = gtk.CellRendererText() + cell.connect('edited', self.__color_edited_cb) + cell.props.editable = True + + column = gtk.TreeViewColumn(_('Color'), cell, text=4) + self._tree_view.append_column(column) + # TODO: should be calculated based on the text size self.set_size_request(200, -1) @@ -73,3 +80,7 @@ class TreeView(gtk.ScrolledWindow): thought = self._tree_view.props.model.get_thought(path[0]) thought.y = int(new_text) + def __color_edited_cb(self, cell_renderer, path, new_text): + thought = self._tree_view.props.model.get_thought(path[0]) + thought.color = new_text + diff --git a/view.py b/view.py index 8ba64ea..c4a1a3f 100644 --- a/view.py +++ b/view.py @@ -148,11 +148,25 @@ class ThoughtView(CanvasElement): return layout def draw(self, context): + + # draw background + if self.model.color is None or not self.model.color: + color = style.Color('#FFFFFF') + else: + color = style.Color(self.model.color) + + r, g, b, a = color.get_rgba() + context.save() + context.set_source_rgb(r, g, b) + context.paint() + context.restore() + if self._dragging: context.set_source_rgb(0.8, 0.8, 0.8) else: context.set_source_rgb(0, 0, 0) + # draw text context.save() layout = self._get_name_layout(context) @@ -166,6 +180,7 @@ class ThoughtView(CanvasElement): context.show_layout(layout) context.restore() + # draw bounding box context.save() context.set_line_width(4) -- cgit v0.9.1