From 2302c3d2cf1688a5d8b44c4f927f96a0e4d58c04 Mon Sep 17 00:00:00 2001 From: Aleksey Lim Date: Tue, 26 May 2009 11:16:01 +0000 Subject: Update sugar-port --- (limited to 'port/temposlider.py') diff --git a/port/temposlider.py b/port/temposlider.py index 9d14be8..6950453 100644 --- a/port/temposlider.py +++ b/port/temposlider.py @@ -12,8 +12,9 @@ # along with this program; if not, write to the Free Software # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA -import os import gtk +import rsvg +import cairo from sugar.graphics import style @@ -26,7 +27,7 @@ class TempoSlider(gtk.HBox): self._image.show() # used to store tempo updates while the slider is active - self._delayed = 0 + self._delayed = 0 self._active = False self.adjustment = gtk.Adjustment(min_value, min_value, max_value, @@ -68,10 +69,10 @@ class TempoSlider(gtk.HBox): self.adjustment.upper, 0, 7) if not self._pixbuf[img]: - self._pixbuf[img] = gtk.gdk.pixbuf_new_from_file_at_size( - os.path.join(os.path.dirname(__file__), 'images', - 'tempo' + str(img+1) + '.svg'), - style.STANDARD_ICON_SIZE, style.STANDARD_ICON_SIZE) + svg = rsvg.Handle(data=IMAGE[img]) + self._pixbuf[img] = _from_svg_at_size(handle=svg, + width=style.STANDARD_ICON_SIZE, + height=style.STANDARD_ICON_SIZE) self._image.set_from_pixbuf(self._pixbuf[img]) @@ -83,3 +84,132 @@ class TempoSlider(gtk.HBox): if self._delayed != 0: self.set_value(self._delayed, True) self._delayed = 0 + +def _from_svg_at_size(filename=None, width=None, height=None, handle=None, + keep_ratio=True): + """ import from pixbuf.py """ + + if not handle: + handle = rsvg.Handle(filename) + + dimensions = handle.get_dimension_data() + icon_width = dimensions[0] + icon_height = dimensions[1] + + if icon_width != width or icon_height != height: + ratio_width = float(width) / icon_width + ratio_height = float(height) / icon_height + + if keep_ratio: + ratio = min(ratio_width, ratio_height) + if ratio_width != ratio: + ratio_width = ratio + width = int(icon_width * ratio) + elif ratio_height != ratio: + ratio_height = ratio + height = int(icon_height * ratio) + else: + ratio_width = 1 + ratio_height = 1 + + surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, width, height) + context = cairo.Context(surface) + context.scale(ratio_width, ratio_height) + handle.render_cairo(context) + + loader = gtk.gdk.pixbuf_loader_new_with_mime_type('image/png') + surface.write_to_png(loader) + loader.close() + + return loader.get_pixbuf() + +IMAGE = [None] * 8 + +IMAGE[0] = """ + + + + + +""" + +IMAGE[1] = """ + + + + + +""" + +IMAGE[2] = """ + + + + + +""" + +IMAGE[3] = """ + + + + + +""" + +IMAGE[4] = """ + + + + + +""" + +IMAGE[5] = """ + + + + + + +""" + +IMAGE[6] = """ + + + + + +""" + +IMAGE[7] = """ + + + + + +""" -- cgit v0.9.1