# 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 gaphas.examples import Box, Text from gaphas.connector import Handle from gaphas.geometry import Rectangle class ThoughtView(Box): _PADDING = style.zoom(15) _LINE_WIDTH = style.zoom(2) def __init__(self, thought_id, name, x, y, color): Box.__init__(self) self.new_thought_handle = Handle() self._handles.append(self.new_thought_handle) self.id = thought_id self.line_to_parent = None self._name = name self._color = color self.set_position(x, y) def set_name(self, name): if self._name != name: self._name = name self.request_update(update=True, matrix=True) def get_name(self): return self._name name = property(get_name, set_name) def set_color(self, color): if self._color != color: self._color = color self.request_update(update=True, matrix=True) def get_color(self): return self._color color = property(get_color, set_color) def set_position(self, x, y): if (x, y) != self.get_position(): self.matrix = (1.0, 0.0, 0.0, 1, x, y) def get_position(self): return self.matrix[4], self.matrix[5] position = property(get_position, set_position) def draw(self, context): super(ThoughtView, self).draw(context) self._draw_label(context) def _draw_label(self, context): context.cairo.save() layout = context.cairo.create_layout() if self._name is not None and self._name: layout.set_text(self._name) else: layout.set_text(_('Untitled')) width, height = layout.get_pixel_size() #logging.debug('_draw_label %r %r %r' % (layout.get_text(), width, height)) self.min_width = width + self._PADDING * 2 self.min_height = height + self._PADDING * 2 self.width = max(self.width, self.min_width) self.height = max(self.height, self.min_height) x = self._PADDING y = self._PADDING context.cairo.translate(x, y) context.cairo.show_layout(layout) context.cairo.restore() def normalize(self): updated = super(ThoughtView, self).normalize() self.new_thought_handle.x = 20 self.new_thought_handle.y = 20 return updated def get_rectangle(self): x, y = self.get_position() return Rectangle(x, y, self.width, self.height)