From ef1942b57e7117d7118d2fde4c519627e5378249 Mon Sep 17 00:00:00 2001 From: Tomeu Vizoso Date: Sun, 25 Jan 2009 18:24:47 +0000 Subject: Move to gaphas --- diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..a477b3b --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "gaphas"] + path = gaphas + url = gitorious@git.sugarlabs.org:gaphas/mainline.git diff --git a/gaphas b/gaphas new file mode 160000 +Subproject c7a3c5e791d3a6382ed46ffee53695502f608a9 diff --git a/mindmap.py b/mindmap.py new file mode 100644 index 0000000..3740d50 --- /dev/null +++ b/mindmap.py @@ -0,0 +1,62 @@ +# Copyright (C) 2009, Tomeu Vizoso +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + +import logging +from gettext import gettext as _ + +import gobject +import gtk + +from model import MindMapModel +from view import MindMapView +from treeview import TreeView + +class MindMap(gtk.Window): + + __gtype_name__ = 'MindMapWindow' + + def __init__(self): + gtk.Window.__init__(self) + + self._model = MindMapModel() + + pane = gtk.HPaned() + self.add(pane) + pane.show() + + self._tree_view = TreeView(self._model) + pane.add1(self._tree_view) + self._tree_view.show() + + self._view = MindMapView(model=self._model) + pane.pack2(self._view) + self._view.show() + + gobject.idle_add(self.__add_first_thought_cb) + + def __add_first_thought_cb(self): + x, y, width, height = self._view.get_allocation() + #TODO: place better the first thought + thought_id = self._model.create_new_thought(name=_('Initial thought'), + color='#8888FF', + x=width / 2, + y=height / 2) + return False + +window = MindMap() +window.show() + +gtk.main() diff --git a/mindmapactivity.py b/mindmapactivity.py index ef20fad..a3c6744 100755 --- a/mindmapactivity.py +++ b/mindmapactivity.py @@ -17,6 +17,11 @@ import logging from gettext import gettext as _ +# HACK: What else can we do? +import sys +import os +sys.path.insert(0, os.path.join(os.environ['SUGAR_BUNDLE_PATH'], 'gaphas')) + import gobject import gtk diff --git a/thoughtview.py b/thoughtview.py index cd79682..1651e09 100644 --- a/thoughtview.py +++ b/thoughtview.py @@ -23,153 +23,69 @@ import gtk # We'd rather not depend on sugar in this class from sugar.graphics import style -from canvas import CanvasElement +from gaphas.examples import Box, Text -class ThoughtView(CanvasElement): +class ThoughtView(Box): _PADDING = style.zoom(15) _LINE_WIDTH = style.zoom(2) def __init__(self, thought_id, name, x, y, color): - CanvasElement.__init__(self) + Box.__init__(self) 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._add_control_rect = gtk.gdk.Rectangle(0, 0, 0, 0) - - def set_dragging(self, dragging): - if self._dragging != dragging: - self._dragging = dragging - self.invalidate() - - def get_dragging(self): - return self._dragging - - dragging = property(get_dragging, set_dragging) + self._name = name + self._color = color - def set_selected(self, selected): - if self._selected != selected: - self._selected = selected - self.invalidate() + self.set_position(x, y) - selected = property(None, set_selected) + def set_name(self, name): + if self._name != name: + self._name = name + self.request_update(update=True, matrix=True) - def _get_name_layout(self, context=None): - if context is None: - visual = gtk.gdk.screen_get_default().get_system_visual() - pixmap = gtk.gdk.Pixmap(None, 1, 1, visual.depth) - context = pixmap.cairo_create() + def get_name(self): + return self._name - if self.name is None or not self.name: - name = _('Unnamed') - else: - name = self.name + name = property(get_name, set_name) - layout = context.create_layout() - layout.set_text(name) + def set_color(self, color): + if self._color != color: + self._color = color + self.request_update(update=True, matrix=True) - return layout + def get_color(self): + return self._color - def draw(self, context): - self._draw_background(context) - width, height = self._draw_text(context) - self._draw_bounding_box(context, width, height) + color = property(get_color, set_color) - if self.hovering: - self._draw_controls(context, width, height) + def set_position(self, x, y): + if (x, y) != self.get_position(): + self.matrix = (1.0, 0.0, 0.0, 1, x, y) - self._last_width = width - self._last_height = height - - CanvasElement.draw(self, context) - - def _draw_background(self, context): - if self.color is None or not self.color: - color = style.Color('#FFFFFF') - else: - color = style.Color(self.color) + def get_position(self): + return self.matrix[4], self.matrix[5] - r, g, b, a = color.get_rgba() - context.save() - context.set_source_rgb(r, g, b) - context.paint() - context.restore() + position = property(get_position, set_position) - def _draw_text(self, context): - context.save() - - context.set_source_rgb(0.0, 0.0, 0.0) - - layout = self._get_name_layout(context) + def draw(self, context): + super(ThoughtView, self).draw(context) + self._draw_label(context) - width, height = layout.get_pixel_size() - width += self._PADDING * 2 - height += self._PADDING * 2 - - x = self.x + self._PADDING - y = self.y + self._PADDING - context.translate(x, y) - context.show_layout(layout) - context.restore() - - return width, height - - def _draw_bounding_box(self, context, width, height): - context.save() - - if self._selected or self._dragging: - context.set_source_rgb(0.6, 0.6, 0.6) - context.set_line_width(self._LINE_WIDTH * 2) - else: - context.set_source_rgb(0, 0, 0) - context.set_line_width(self._LINE_WIDTH) + def _draw_label(self, context): + context.cairo.save() + layout = context.cairo.create_layout() + layout.set_text(self._name) - 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 - - context.rectangle(x, y, rect_width, rect_height) - context.stroke() - context.restore() - - def _draw_controls(self, context, width, height): - context.save() - - context.set_source_rgb(0.0, 0.0, 0.0) - - layout = context.create_layout() - layout.set_text('+') - - label_width, label_height = layout.get_pixel_size() - - x = self.x + width - label_width - y = self.y - context.translate(x, y) - context.show_layout(layout) - - context.restore() - - self._add_control_rect = gtk.gdk.Rectangle(x, y, label_width, - label_height) - - def get_rect(self): - if -1 in (self._last_width, self._last_height): - layout = self._get_name_layout() - width, height = layout.get_pixel_size() - self._last_width = width + self._PADDING * 2 - self._last_height = height + self._PADDING * 2 - - return gtk.gdk.Rectangle(self.x, self.y, - self._last_width, self._last_height) + width, height = layout.get_pixel_size() + self.width = width + self._PADDING * 2 + self.height = height + self._PADDING * 2 + + x = self._PADDING + y = self._PADDING + context.cairo.translate(x, y) + context.cairo.show_layout(layout) + context.cairo.restore() diff --git a/view.py b/view.py index a47d10b..8a4d492 100644 --- a/view.py +++ b/view.py @@ -19,14 +19,14 @@ from gettext import gettext as _ import gobject import gtk +from gaphas import GtkView, Canvas, state # We'd rather not depend on sugar in this class from sugar.graphics import style -from canvas import Canvas from thoughtview import ThoughtView -class MindMapView(Canvas): +class MindMapView(GtkView): __gtype_name__ = 'MindMapView' @@ -36,16 +36,29 @@ class MindMapView(Canvas): self._row_deleted_sid = None self._selected_thought = None - Canvas.__init__(self) + GtkView.__init__(self) - self.dragging_started.connect(self.__dragging_started_cb) - self.dragging_finished.connect(self.__dragging_finished_cb) + self.canvas = Canvas() - self.connect('button-release-event', self.__button_release_event_cb) + #self.dragging_started.connect(self.__dragging_started_cb) + #self.dragging_finished.connect(self.__dragging_finished_cb) + + #self.connect('button-release-event', self.__button_release_event_cb) if model is not None: self.model = model + state.observers.add(self.__state_changed_cb) + + def __state_changed_cb(self, event): + func, args, dict_ = event + if len(args) > 1 and isinstance(args[1], ThoughtView): + thought_view = args[1] + x, y = thought_view.get_position() + row = self.model.find_by_id(thought_view.id) + if row[2] != x or row[3] != y: + self.model.set(row.iter, 2, x, 3, y) + def __dragging_started_cb(self, **kwargs): x, y = kwargs['position'] logging.debug('__dragging_started_cb %r %r' % (x, y)) @@ -98,7 +111,8 @@ class MindMapView(Canvas): self._row_deleted_sid = \ self._model.connect('row-deleted', self.__row_deleted_cb) - self.remove_all_elements() + for item in self.canvas.get_all_items(): + self.canvas.remove(item) self._populate_from_model(self._model) def _populate_from_model(self, rows): @@ -113,19 +127,21 @@ class MindMapView(Canvas): model = property(get_model, set_model) def __row_changed_cb(self, model, path, iter): - logging.debug('__row_changed_cb %r' % path) + #logging.debug('__row_changed_cb %r' % path) 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) + self.canvas.add(thought_view) else: thought_view.name = row[1] + thought_view.set_position(row[2], row[3]) thought_view.x = row[2] thought_view.y = row[3] thought_view.color = row[4] - thought_view.invalidate() + + self.canvas.request_update(thought_view) def __row_deleted_cb(self, model, path): logging.debug('__row_deleted_cb %r' % path) @@ -135,8 +151,8 @@ class MindMapView(Canvas): self.remove_element(thought_view) def _get_thought_by_id(self, thought_id): - for thought_view in self.get_elements(): - if thought_view.id == thought_id: - return thought_view + for item in self.canvas.get_all_items(): + if isinstance(item, ThoughtView) and item.id == thought_id: + return item return None -- cgit v0.9.1