Reimplement the icon item to render svg directly.

Some regressions sorry, need also to fix pygoocanvas...
This commit is contained in:
Marco Pesenti Gritti 2006-08-31 22:29:31 +02:00
parent 110d1bf8ce
commit 0ae39c93e7
4 changed files with 132 additions and 32 deletions

View File

@ -20,8 +20,8 @@ class PieceIcon(IconItem):
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')
icon_width = self.get_property('size')
icon_height = self.get_property('size')
self.set_property('x', x - icon_width / 2)
self.set_property('y', y - icon_height / 2)

View File

@ -68,8 +68,8 @@ class HomeGroup(goocanvas.Group):
profile = sugar.conf.get_profile()
me = IconItem(icon_name = 'stock-buddy',
color = profile.get_color(), size = 150)
me.translate(600 - (me.get_property('width') / 2),
450 - (me.get_property('height') / 2))
me.translate(600 - (me.get_property('size') / 2),
450 - (me.get_property('size') / 2))
self.add_child(me)
def __theme_changed_cb(self, theme):

View File

@ -20,8 +20,8 @@ class IconLayout:
self._icons.remove(icon)
def _is_valid_position(self, icon, x, y):
h_border = icon.props.width + 4
v_border = icon.props.height + 4
h_border = icon.props.size + 4
v_border = icon.props.size + 4
if x < self._x1 - h_border or x > self._x2 + h_border:
return True
if y < self._y1 - v_border or y > self._y2 + v_border:

View File

@ -3,15 +3,18 @@ import re
import gobject
import gtk
import goocanvas
import rsvg
from sugar.util import GObjectSingletonMeta
from sugar.canvas.IconColor import IconColor
class IconCache(gobject.GObject):
__metaclass__ = GObjectSingletonMeta
class _Icon:
def __init__(self, data, size):
self.data = data
self.size = size
class _IconCache:
def __init__(self):
gobject.GObject.__init__(self)
self._icons = {}
def _create_icon(self, name, color, size):
@ -34,29 +37,114 @@ class IconCache(gobject.GObject):
style = '.shape-and-fill {fill:%s; stroke:%s;}' % (fill, stroke)
data = re.sub('\.shape-and-fill \{.*\}', 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()
return _Icon(data, info.get_base_size())
def get_icon(self, name, color, size):
key = (name, color, size)
key = (name, color.get_fill_color(), size)
if self._icons.has_key(key):
return self._icons[key]
icon = self._icons[key]
else:
icon = self._create_icon(name, color, size)
self._icons[key] = icon
return icon
return icon
class IconItem(goocanvas.Image, goocanvas.Item):
class IconView(goocanvas.ItemViewSimple, goocanvas.ItemView):
__gtype_name__ = 'IconView'
_icon_cache = _IconCache()
def __init__(self, canvas_view, parent_view, item):
goocanvas.ItemViewSimple.__init__(self)
self.parent_view = parent_view
self.canvas_view = canvas_view
self.item = item
def do_get_item_view_at(self, x, y, cr, is_pointer_event, parent_is_visible):
result = self
cr.save()
#if self.item.transform != None:
# cr.transform(self.item.transform)
#if self.transform != None:
# cr.transform(self.transform)
[user_x, user_y] = cr.device_to_user(x, y)
if user_x < self.item.x or \
user_x > self.item.x + self.item.size or \
user_y < self.item.y or \
user_y > self.item.y + self.item.size:
result = None
cr.restore()
return result
def do_update(self, entire_tree, cr):
if entire_tree or self.flags & goocanvas.ITEM_VIEW_NEED_UPDATE:
self.flags &= ~goocanvas.ITEM_VIEW_NEED_UPDATE
cr.save()
#if self.item.transform != None:
# cr.transform(self.item.transform)
#if self.transform != None:
# cr.transform(self.transform)
bounds = goocanvas.Bounds()
bounds.x1 = self.item.x
bounds.y1 = self.item.y
bounds.x2 = self.item.x + self.item.size
bounds.y2 = self.item.y + self.item.size
self.item.user_bounds_to_device(cr, bounds)
self.bounds = bounds
cr.restore()
return self.bounds
def do_paint(self, cr, bounds, scale):
if self.item.color == None:
theme = gtk.icon_theme_get_default()
info = theme.lookup_icon(self.item.icon_name, self.item.size, 0)
handle = rsvg.Handle(file=info.get_filename())
icon_size = info.get_base_size()
else:
icon = IconView._icon_cache.get_icon(self.item.icon_name,
self.item.color,
self.item.size)
handle = rsvg.Handle(data=icon.data)
icon_size = icon.size
cr.save()
#if self.item.transform != None:
# cr.transform(self.item.transform)
#if self.transform != None:
# cr.transform(self.transform)
cr.translate(self.item.x, self.item.y)
scale_factor = float(self.item.size) / float(icon_size)
cr.scale(scale_factor, scale_factor)
handle.render_cairo(cr)
cr.restore()
return self.bounds
class IconItem(goocanvas.ItemSimple, goocanvas.Item):
__gsignals__ = {
'clicked': (gobject.SIGNAL_RUN_FIRST,
gobject.TYPE_NONE, ([])),
}
__gproperties__ = {
'x' : (float, None, None, -10e6, 10e6, 0,
gobject.PARAM_READWRITE),
'y' : (float, None, None, -10e6, 10e6, 0,
gobject.PARAM_READWRITE),
'icon-name': (str, None, None, None,
gobject.PARAM_CONSTRUCT_ONLY |
gobject.PARAM_READWRITE),
@ -70,27 +158,39 @@ class IconItem(goocanvas.Image, goocanvas.Item):
}
def __init__(self, **kwargs):
goocanvas.Image.__init__(self, **kwargs)
goocanvas.ItemSimple.__init__(self, **kwargs)
if self._color:
cache = IconCache()
pixbuf = cache.get_icon(self._icon_name, self._color, self._size)
self.props.pixbuf = pixbuf
else:
theme = gtk.icon_theme_get_default()
pixbuf = theme.load_icon(self._icon_name, self._size, 0)
self.props.pixbuf = pixbuf
self.x = 0.0
self.y = 0.0
self.width = 0.0
self.height = 0.0
def do_set_property(self, pspec, value):
if pspec.name == 'icon-name':
self._icon_name = value
self.icon_name = value
elif pspec.name == 'color':
self._color = value
self.color = value
elif pspec.name == 'size':
self._size = value
self.size = value
elif pspec.name == 'x':
self.x = value
elif pspec.name == 'y':
self.y = value
def do_get_property(self, pspec):
if pspec.name == 'x':
return self.x
elif pspec.name == 'y':
return self.y
elif pspec.name == 'size':
return self.size
elif pspec.name == 'icon-name':
return self.icon_name
elif pspec.name == 'color':
return self.color
def do_create_view(self, canvas, parent_view):
view = goocanvas.Image.do_create_view(self, canvas, parent_view)
view = IconView(canvas, parent_view, self)
view.connect('button-press-event', self.__button_press_cb)
return view