Merge demo4 branch
This commit is contained in:
+1
-1
@@ -1,4 +1,4 @@
|
||||
SUBDIRS = activity chat p2p presence
|
||||
SUBDIRS = activity canvas chat p2p presence
|
||||
|
||||
sugardir = $(pythondir)/sugar
|
||||
sugar_PYTHON = \
|
||||
|
||||
@@ -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
|
||||
@@ -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)
|
||||
@@ -0,0 +1,5 @@
|
||||
sugardir = $(pythondir)/sugar/canvas
|
||||
sugar_PYTHON = \
|
||||
__init__.py \
|
||||
DonutItem.py \
|
||||
IconItem.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 = ""
|
||||
|
||||
Reference in New Issue
Block a user