From 115eefb4c2bb8154f6834caa62be2bcba80f70dc Mon Sep 17 00:00:00 2001 From: Marco Pesenti Gritti Date: Wed, 16 Aug 2006 19:55:13 +0200 Subject: [PATCH] Implement a canvas element that can draw svg icons with different colors. --- shell/HomeWindow.py | 5 +++++ sugar/canvas/IconItem.py | 40 ++++++++++++++++++++++++++++++++++++++++ sugar/canvas/Makefile.am | 4 ++++ sugar/canvas/__init__.py | 0 4 files changed, 49 insertions(+) create mode 100644 sugar/canvas/IconItem.py create mode 100644 sugar/canvas/Makefile.am create mode 100644 sugar/canvas/__init__.py diff --git a/shell/HomeWindow.py b/shell/HomeWindow.py index ee567818..1609fe78 100644 --- a/shell/HomeWindow.py +++ b/shell/HomeWindow.py @@ -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): diff --git a/sugar/canvas/IconItem.py b/sugar/canvas/IconItem.py new file mode 100644 index 00000000..54697b67 --- /dev/null +++ b/sugar/canvas/IconItem.py @@ -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()) diff --git a/sugar/canvas/Makefile.am b/sugar/canvas/Makefile.am new file mode 100644 index 00000000..9120ba25 --- /dev/null +++ b/sugar/canvas/Makefile.am @@ -0,0 +1,4 @@ +sugardir = $(pythondir)/sugar/canvas +sugar_PYTHON = \ + __init__.py \ + IconItem.py diff --git a/sugar/canvas/__init__.py b/sugar/canvas/__init__.py new file mode 100644 index 00000000..e69de29b