From 9ee11dda200e1ae317d7ec9b919692f41fd39c52 Mon Sep 17 00:00:00 2001 From: Tomeu Vizoso Date: Sat, 24 Jan 2009 15:16:00 +0000 Subject: Move ThoughtView to its own module, it's going to grow soon --- diff --git a/thoughtview.py b/thoughtview.py new file mode 100644 index 0000000..5d02365 --- /dev/null +++ b/thoughtview.py @@ -0,0 +1,151 @@ +# 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 + +# We'd rather not depend on sugar in this class +from sugar.graphics import style + +from canvas import CanvasElement + +class ThoughtView(CanvasElement): + + _PADDING = style.zoom(15) + _LINE_WIDTH = style.zoom(2) + + def __init__(self, thought_id, name, x, y, color): + CanvasElement.__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 + + 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) + + def set_selected(self, selected): + if self._selected != selected: + self._selected = selected + self.invalidate() + + selected = property(None, set_selected) + + 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() + + if self.name is None or not self.name: + name = _('Unnamed') + else: + name = self.name + + layout = context.create_layout() + layout.set_text(name) + + return layout + + def draw(self, context): + self._draw_background(context) + width, height = self._draw_text(context) + self._draw_bounding_box(context, width, height) + + 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) + + r, g, b, a = color.get_rgba() + context.save() + context.set_source_rgb(r, g, b) + context.paint() + context.restore() + + def _draw_text(self, context): + context.save() + + context.set_source_rgb(0.0, 0.0, 0.0) + + layout = self._get_name_layout(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) + + 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 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) + diff --git a/view.py b/view.py index 3708b6a..a47d10b 100644 --- a/view.py +++ b/view.py @@ -23,7 +23,8 @@ import gtk # We'd rather not depend on sugar in this class from sugar.graphics import style -from canvas import Canvas, CanvasElement +from canvas import Canvas +from thoughtview import ThoughtView class MindMapView(Canvas): @@ -139,127 +140,3 @@ class MindMapView(Canvas): return thought_view return None -class ThoughtView(CanvasElement): - - _PADDING = style.zoom(15) - _LINE_WIDTH = style.zoom(2) - - def __init__(self, thought_id, name, x, y, color): - CanvasElement.__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 - - 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) - - def set_selected(self, selected): - if self._selected != selected: - self._selected = selected - self.invalidate() - - selected = property(None, set_selected) - - 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() - - if self.name is None or not self.name: - name = _('Unnamed') - else: - name = self.name - - layout = context.create_layout() - layout.set_text(name) - - return layout - - def draw(self, context): - self._draw_background(context) - width, height = self._draw_text(context) - self._draw_bounding_box(context, width, height) - - 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) - - r, g, b, a = color.get_rgba() - context.save() - context.set_source_rgb(r, g, b) - context.paint() - context.restore() - - def _draw_text(self, context): - context.save() - - context.set_source_rgb(0.0, 0.0, 0.0) - - layout = self._get_name_layout(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) - - 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 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) - -- cgit v0.9.1