diff options
author | Marco Pesenti Gritti <marco@localhost.localdomain> | 2006-08-19 09:56:49 (GMT) |
---|---|---|
committer | Marco Pesenti Gritti <marco@localhost.localdomain> | 2006-08-19 09:56:49 (GMT) |
commit | 58ddb555c44a02f33ddf9ef549db0a2395c82da1 (patch) | |
tree | 2af84134fddd64c77a90b0e4f45aedf7636641da /sugar | |
parent | e5eef2e183fc13a0d7bbc1faff7c6c48c75671ae (diff) | |
parent | f8c4f0bd66309fda38047d47fb57c47409dcecf0 (diff) |
Merge demo4 branch
Diffstat (limited to 'sugar')
-rw-r--r-- | sugar/Makefile.am | 2 | ||||
-rw-r--r-- | sugar/canvas/DonutItem.py | 114 | ||||
-rw-r--r-- | sugar/canvas/IconItem.py | 52 | ||||
-rw-r--r-- | sugar/canvas/Makefile.am | 5 | ||||
-rw-r--r-- | sugar/canvas/__init__.py | 0 | ||||
-rw-r--r-- | sugar/util.py | 14 |
6 files changed, 186 insertions, 1 deletions
diff --git a/sugar/Makefile.am b/sugar/Makefile.am index 6fad678..d339fba 100644 --- a/sugar/Makefile.am +++ b/sugar/Makefile.am @@ -1,4 +1,4 @@ -SUBDIRS = activity chat p2p presence +SUBDIRS = activity canvas chat p2p presence sugardir = $(pythondir)/sugar sugar_PYTHON = \ diff --git a/sugar/canvas/DonutItem.py b/sugar/canvas/DonutItem.py new file mode 100644 index 0000000..a6b49ea --- /dev/null +++ b/sugar/canvas/DonutItem.py @@ -0,0 +1,114 @@ +import math + +import goocanvas + +from sugar.canvas.IconItem import IconItem + +class PieceIcon(IconItem): + def __init__(self, piece_item, icon_name, color, **kwargs): + IconItem.__init__(self, icon_name, color, 48, **kwargs) + self._piece_item = piece_item + + def construct(self): + angle_start = self._piece_item.get_angle_start() + angle_end = self._piece_item.get_angle_end() + radius = self.get_parent().get_radius() + inner_radius = self.get_parent().get_inner_radius() + + icon_radius = (radius + inner_radius) / 2 + icon_angle = (angle_start + angle_end) / 2 + x = icon_radius * math.cos(icon_angle) + y = - icon_radius * math.sin(icon_angle) + + icon_width = self.get_property('width') + icon_height = self.get_property('height') + self.set_property('x', x - icon_width / 2) + self.set_property('y', y - icon_height / 2) + +class PieceItem(goocanvas.Path): + def __init__(self, angle_start, angle_end, **kwargs): + goocanvas.Path.__init__(self, **kwargs) + self._angle_start = angle_start + self._angle_end = angle_end + + self.set_property('fill-color', '#e8e8e8') + self.set_property('stroke-color', '#d8d8d8') + self.set_property('line-width', 4) + + def get_icon(self): + return self._icon + + def set_icon(self, icon_name, color): + self._icon = PieceIcon(self, icon_name, color) + self.get_parent().add_child(self._icon) + self._icon.construct() + + def get_angle_start(self): + return self._angle_start + + def get_angle_end(self): + return self._angle_end + + def construct(self): + r = self.get_parent().get_radius() + + data = 'M0,0 ' + + dx = r * math.cos(self._angle_start) + dy = - r * math.sin(self._angle_start) + + data += 'l%f,%f ' % (dx, dy) + + dx = r * math.cos(self._angle_end) + dy = - r * math.sin(self._angle_end) + + data += 'A%f,%f 0 0,0 %f,%f ' % (r, r, dx, dy) + + data += 'z' + + self.set_property('data', data) + +class DonutItem(goocanvas.Group): + def __init__(self, radius, **kwargs): + goocanvas.Group.__init__(self, **kwargs) + self._radius = radius + self._angle_start = 0 + + bg = goocanvas.Ellipse(radius_x=radius, radius_y=radius, + fill_color='#c2c3c5', line_width=0) + self.add_child(bg) + + self._inner_radius = radius / 2 + fg = goocanvas.Ellipse(radius_x=self._inner_radius, + radius_y=self._inner_radius, + fill_color='#d8d8d8', line_width=0) + self.add_child(fg) + + def add_piece(self, perc, icon_name, color): + # FIXME can't override set_parent on the + # PieceItem and there is no signal. So we + # call a construct method on the childs for now. + + angle_end = self._angle_start + perc * 2 * math.pi / 100 + piece_item = PieceItem(self._angle_start, angle_end) + self._angle_start = angle_end + + self.add_child(piece_item, 1) + piece_item.construct() + piece_item.set_icon(icon_name, color) + + return piece_item + + def remove_piece(self, piece_item): + index = self.find_child(piece_item) + self.remove_child(index) + + icon = piece_item.get_icon() + index = self.find_child(icon) + self.remove_child(index) + + def get_radius(self): + return self._radius + + def get_inner_radius(self): + return self._inner_radius diff --git a/sugar/canvas/IconItem.py b/sugar/canvas/IconItem.py new file mode 100644 index 0000000..73098b7 --- /dev/null +++ b/sugar/canvas/IconItem.py @@ -0,0 +1,52 @@ +import re + +import gobject +import gtk +import goocanvas + +from sugar.util import GObjectSingletonMeta + +class IconCache(gobject.GObject): + __metaclass__ = GObjectSingletonMeta + + def __init__(self): + gobject.GObject.__init__(self) + self._icons = {} + + def _create_icon(self, name, color, size): + theme = gtk.icon_theme_get_default() + info = theme.lookup_icon(name, size, 0) + icon_file = open(info.get_filename(), 'r') + data = icon_file.read() + icon_file.close() + + if color != None: + style = '.fill-color {fill: %s;}' % (color) + data = re.sub('\.fill-color \{.*\}', style, data) + + style = '.fill-and-stroke-color {fill: %s; stroke: %s;}' % (color, color) + data = re.sub('\.fill-and-stroke-color \{.*\}', style, data) + + loader = gtk.gdk.pixbuf_loader_new_with_mime_type('image/svg-xml') + loader.set_size(size, size) + loader.write(data) + loader.close() + + return loader.get_pixbuf() + + def get_icon(self, name, color, size): + key = (name, color, size) + if self._icons.has_key(key): + return self._icons[key] + else: + icon = self._create_icon(name, color, size) + self._icons[key] = icon + return icon + +class IconItem(goocanvas.Image): + def __init__(self, icon_name, color, size, **kwargs): + goocanvas.Image.__init__(self, **kwargs) + + icon_cache = IconCache() + pixbuf = icon_cache.get_icon(icon_name, color, size) + self.set_property('pixbuf', pixbuf) diff --git a/sugar/canvas/Makefile.am b/sugar/canvas/Makefile.am new file mode 100644 index 0000000..3514669 --- /dev/null +++ b/sugar/canvas/Makefile.am @@ -0,0 +1,5 @@ +sugardir = $(pythondir)/sugar/canvas +sugar_PYTHON = \ + __init__.py \ + DonutItem.py \ + IconItem.py diff --git a/sugar/canvas/__init__.py b/sugar/canvas/__init__.py new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/sugar/canvas/__init__.py diff --git a/sugar/util.py b/sugar/util.py index bfddf32..9cb7d55 100644 --- a/sugar/util.py +++ b/sugar/util.py @@ -4,6 +4,20 @@ import random import binascii import string +import gobject + +class GObjectSingletonMeta(gobject.GObjectMeta): + """GObject Singleton Metaclass""" + + def __init__(klass, name, bases, dict): + gobject.GObjectMeta.__init__(klass, name, bases, dict) + klass.__instance = None + + def __call__(klass, *args, **kwargs): + if klass.__instance is None: + klass.__instance = gobject.GObjectMeta.__call__(klass, *args, **kwargs) + return klass.__instance + def _stringify_sha(sha_hash): """Convert binary sha1 hash data into printable characters.""" print_sha = "" |