From 507dfecc095d8bfb6b832698b2a66c205562cd7f Mon Sep 17 00:00:00 2001 From: Gonzalo Odiard Date: Wed, 23 Mar 2011 23:37:03 +0000 Subject: [PATCH] Add alpha and scale functionality to the icon, can be used in the launcher to do the animation - v2 Signed-off-by: Gonzalo Odiard Acked-By: Simon Schampijer --- src/sugar/graphics/icon.py | 50 +++++++++++++++++++++++++++++++++++--- 1 file changed, 46 insertions(+), 4 deletions(-) diff --git a/src/sugar/graphics/icon.py b/src/sugar/graphics/icon.py index 83a80d91..62a79f60 100644 --- a/src/sugar/graphics/icon.py +++ b/src/sugar/graphics/icon.py @@ -328,6 +328,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) @@ -416,8 +418,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): """ @@ -520,6 +536,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): @@ -530,6 +562,7 @@ class CanvasIcon(hippo.CanvasBox, hippo.CanvasItem): self._buffer = _IconBuffer() self._palette_invoker = CanvasInvoker() + self._alpha = 1.0 hippo.CanvasBox.__init__(self, **kwargs) @@ -771,8 +804,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() @@ -793,6 +824,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 @@ -874,7 +913,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): """