Implement a canvas element that can draw svg icons
with different colors.
This commit is contained in:
parent
fa90ec41aa
commit
115eefb4c2
@ -1,6 +1,8 @@
|
||||
import gtk
|
||||
import goocanvas
|
||||
|
||||
from sugar.canvas.IconItem import IconItem
|
||||
|
||||
class Model(goocanvas.CanvasModelSimple):
|
||||
def __init__(self):
|
||||
goocanvas.CanvasModelSimple.__init__(self)
|
||||
@ -9,7 +11,10 @@ class Model(goocanvas.CanvasModelSimple):
|
||||
|
||||
item = goocanvas.Rect(x=0, y=0, width=693, height=520,
|
||||
fill_color="red")
|
||||
root.add_child(item)
|
||||
|
||||
item = IconItem('buddy')
|
||||
#item.set_color('blue')
|
||||
root.add_child(item)
|
||||
|
||||
class HomeWindow(gtk.Window):
|
||||
|
40
sugar/canvas/IconItem.py
Normal file
40
sugar/canvas/IconItem.py
Normal file
@ -0,0 +1,40 @@
|
||||
import re
|
||||
|
||||
import gtk
|
||||
import goocanvas
|
||||
|
||||
class IconItem(goocanvas.Image):
|
||||
def __init__(self, icon_name):
|
||||
goocanvas.Image.__init__(self)
|
||||
|
||||
self._icon_name = icon_name
|
||||
self._color = None
|
||||
|
||||
# FIXME changing the icon color will cause
|
||||
# the svg to be read in memory and rendered
|
||||
# two times.
|
||||
self._update_pixbuf()
|
||||
|
||||
def set_parent(self, parent):
|
||||
goocanvas.Image.set_parent(self, parent)
|
||||
|
||||
def set_color(self, color):
|
||||
self._color = color
|
||||
self._update_pixbuf()
|
||||
|
||||
def _update_pixbuf(self):
|
||||
theme = gtk.icon_theme_get_default()
|
||||
info = theme.lookup_icon(self._icon_name, 48, 0)
|
||||
icon_file = open(info.get_filename(), 'r')
|
||||
data = icon_file.read()
|
||||
icon_file.close()
|
||||
|
||||
if self._color != None:
|
||||
style = '.icon-color {fill: %s;}' % self._color
|
||||
data = re.sub('\.icon-color \{.*\}', style, data)
|
||||
|
||||
loader = gtk.gdk.pixbuf_loader_new_with_mime_type('image/svg+xml')
|
||||
loader.write(data)
|
||||
loader.close()
|
||||
|
||||
self.set_property('pixbuf', loader.get_pixbuf())
|
4
sugar/canvas/Makefile.am
Normal file
4
sugar/canvas/Makefile.am
Normal file
@ -0,0 +1,4 @@
|
||||
sugardir = $(pythondir)/sugar/canvas
|
||||
sugar_PYTHON = \
|
||||
__init__.py \
|
||||
IconItem.py
|
0
sugar/canvas/__init__.py
Normal file
0
sugar/canvas/__init__.py
Normal file
Loading…
Reference in New Issue
Block a user