[Sugar-devel] [PATCH sugar-toolkit] Add alpha and scale functionality to the icon, can be used in the launcher to do the animation - v2

godiard at sugarlabs.org godiard at sugarlabs.org
Fri Aug 12 14:39:15 EDT 2011


From: Gonzalo Odiard <godiard at sugarlabs.org>

From: Gonzalo Odiard <godiard at gmail.com>

	Signed-off-by: Gonzalo Odiard <gonzalo at laptop.org>
        Acked-By: Simon Schampijer <simon at laptop.org>

---
src/sugar/graphics/icon.py |   50 ++++++++++++++++++++++++++++++++++++++++---
 1 files changed, 46 insertions(+), 4 deletions(-)

diff --git a/src/sugar/graphics/icon.py b/src/sugar/graphics/icon.py
index 870c49e..7d84f2b 100644
--- a/src/sugar/graphics/icon.py
+++ b/src/sugar/graphics/icon.py
@@ -332,6 +332,8 @@ class Icon(gtk.Image):
         # collected while it's still used if it's a sugar.util.TempFilePath.
         # See #1175
         self._file = None
+        self._alpha = 1.0
+        self._scale = 1.0
 
         gobject.GObject.__init__(self, **kwargs)
 
@@ -420,8 +422,22 @@ class Icon(gtk.Image):
             (allocation.height - requisition[1]) * yalign)
 
         cr = self.window.cairo_create()
+
+        if self._scale != 1.0:
+            cr.scale(self._scale, self._scale)
+
+            margin = self._buffer.width * (1 - self._scale) / 2
+            x, y = x + margin, y + margin
+
+            x = x / self._scale
+            y = y / self._scale
+
         cr.set_source_surface(surface, x, y)
-        cr.paint()
+
+        if self._alpha == 1.0:
+            cr.paint()
+        else:
+            cr.paint_with_alpha(self._alpha)
 
     def set_xo_color(self, value):
         """
@@ -524,6 +540,22 @@ class Icon(gtk.Image):
     badge_name = gobject.property(
         type=str, getter=get_badge_name, setter=set_badge_name)
 
+    def set_alpha(self, value):
+        if self._alpha != value:
+            self._alpha = value
+            self.queue_draw()
+
+    alpha = gobject.property(
+        type=float, setter=set_alpha)
+
+    def set_scale(self, value):
+        if self._scale != value:
+            self._scale = value
+            self.queue_draw()
+
+    scale = gobject.property(
+        type=float, setter=set_scale)
+
 
 class CanvasIcon(hippo.CanvasBox, hippo.CanvasItem):
 
@@ -534,6 +566,7 @@ class CanvasIcon(hippo.CanvasBox, hippo.CanvasItem):
 
         self._buffer = _IconBuffer()
         self._palette_invoker = CanvasInvoker()
+        self._alpha = 1.0
 
         hippo.CanvasBox.__init__(self, **kwargs)
 
@@ -775,8 +808,6 @@ class CanvasIcon(hippo.CanvasBox, hippo.CanvasItem):
         None
 
         """
-        logging.warning(
-            'CanvasIcon: the scale parameter is currently unsupported')
         if self._buffer.scale != value:
             self._buffer.scale = value
             self.emit_request_changed()
@@ -797,6 +828,14 @@ class CanvasIcon(hippo.CanvasBox, hippo.CanvasItem):
     scale = gobject.property(
         type=float, getter=get_scale, setter=set_scale)
 
+    def set_alpha(self, alpha):
+        if self._alpha != alpha:
+            self._alpha = alpha
+            self.emit_paint_needed(0, 0, -1, -1)
+
+    alpha = gobject.property(
+        type=float, setter=set_alpha)
+
     def set_cache(self, value):
         """
         Parameters
@@ -878,7 +917,10 @@ class CanvasIcon(hippo.CanvasBox, hippo.CanvasItem):
             y = (height - surface.get_height()) / 2
 
             cr.set_source_surface(surface, x, y)
-            cr.paint()
+            if self._alpha == 1.0:
+                cr.paint()
+            else:
+                cr.paint_with_alpha(self._alpha)
 
     def do_get_content_width_request(self):
         """


More information about the Sugar-devel mailing list