2006-08-16 19:55:13 +02:00
|
|
|
import re
|
|
|
|
|
2006-08-16 23:20:22 +02:00
|
|
|
import gobject
|
2006-08-16 19:55:13 +02:00
|
|
|
import gtk
|
|
|
|
import goocanvas
|
|
|
|
|
2006-08-17 10:32:59 +02:00
|
|
|
from sugar.util import GObjectSingletonMeta
|
2006-08-23 21:03:17 +02:00
|
|
|
from sugar.canvas.IconColor import IconColor
|
2006-08-19 14:27:56 +02:00
|
|
|
|
2006-08-17 10:32:59 +02:00
|
|
|
class IconCache(gobject.GObject):
|
|
|
|
__metaclass__ = GObjectSingletonMeta
|
2006-08-16 19:55:13 +02:00
|
|
|
|
2006-08-17 10:32:59 +02:00
|
|
|
def __init__(self):
|
|
|
|
gobject.GObject.__init__(self)
|
|
|
|
self._icons = {}
|
2006-08-16 23:20:22 +02:00
|
|
|
|
2006-08-17 10:32:59 +02:00
|
|
|
def _create_icon(self, name, color, size):
|
2006-08-16 19:55:13 +02:00
|
|
|
theme = gtk.icon_theme_get_default()
|
2006-08-17 10:32:59 +02:00
|
|
|
info = theme.lookup_icon(name, size, 0)
|
2006-08-16 19:55:13 +02:00
|
|
|
icon_file = open(info.get_filename(), 'r')
|
|
|
|
data = icon_file.read()
|
|
|
|
icon_file.close()
|
|
|
|
|
2006-08-17 10:32:59 +02:00
|
|
|
if color != None:
|
2006-08-19 14:27:56 +02:00
|
|
|
fill = color.get_fill_color()
|
|
|
|
stroke = color.get_stroke_color()
|
|
|
|
|
|
|
|
style = '.fill {fill:%s;stroke:%s;}' % (fill, fill)
|
2006-08-19 13:39:13 +02:00
|
|
|
data = re.sub('\.fill \{.*\}', style, data)
|
2006-08-16 19:55:13 +02:00
|
|
|
|
2006-08-19 14:27:56 +02:00
|
|
|
style = '.shape {stroke:%s;fill:%s;}' % (stroke, stroke)
|
2006-08-19 13:39:13 +02:00
|
|
|
data = re.sub('\.shape \{.*\}', style, data)
|
|
|
|
|
2006-08-19 14:27:56 +02:00
|
|
|
style = '.shape-and-fill {fill:%s; stroke:%s;}' % (fill, stroke)
|
2006-08-19 13:39:13 +02:00
|
|
|
data = re.sub('\.shape-and-fill \{.*\}', style, data)
|
2006-08-19 11:54:41 +02:00
|
|
|
|
2006-08-17 10:32:59 +02:00
|
|
|
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):
|
2006-08-28 12:44:46 +02:00
|
|
|
__gproperties__ = {
|
|
|
|
'icon-name': (str, None, None, None,
|
|
|
|
gobject.PARAM_CONSTRUCT_ONLY |
|
|
|
|
gobject.PARAM_READWRITE),
|
|
|
|
'color' : (object, None, None,
|
|
|
|
gobject.PARAM_CONSTRUCT_ONLY |
|
|
|
|
gobject.PARAM_READWRITE),
|
|
|
|
'size' : (int, None, None,
|
|
|
|
0, 1024, 24,
|
|
|
|
gobject.PARAM_CONSTRUCT_ONLY |
|
|
|
|
gobject.PARAM_READWRITE)
|
|
|
|
}
|
|
|
|
|
|
|
|
def do_set_property(self, pspec, value):
|
|
|
|
if pspec.name == 'icon-name':
|
|
|
|
self._icon_name = value
|
|
|
|
elif pspec.name == 'color':
|
|
|
|
self._color = value
|
|
|
|
elif pspec.name == 'size':
|
|
|
|
self._size = value
|
|
|
|
|
|
|
|
def __init__(self, **kwargs):
|
2006-08-17 10:32:59 +02:00
|
|
|
goocanvas.Image.__init__(self, **kwargs)
|
2006-08-16 19:55:13 +02:00
|
|
|
|
2006-08-28 12:44:46 +02:00
|
|
|
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
|