def do_draw(self, cairo_context): pixbuf = self._get_pixbuf() if not pixbuf: return alloc = self.get_allocation() width, height = alloc.width, alloc.height scale_factor = self.get_scale_factor() width *= scale_factor height *= scale_factor if self._path: if width < (2 * scale_factor) or height < (2 * scale_factor): return pixbuf = scale( pixbuf, (width - 2 * scale_factor, height - 2 * scale_factor)) pixbuf = add_border_widget(pixbuf, self) else: pixbuf = scale(pixbuf, (width, height)) style_context = self.get_style_context() surface = get_surface_for_pixbuf(self, pixbuf) Gtk.render_icon_surface(style_context, cairo_context, surface, 0, 0)
def do_draw(self, cairo_context): pixbuf = self._get_pixbuf() if not pixbuf: return alloc = self.get_allocation() width, height = alloc.width, alloc.height scale_factor = get_scale_factor(self) width *= scale_factor height *= scale_factor if self._path: if width < (2 * scale_factor) or height < (2 * scale_factor): return round_thumbs = config.getboolean("albumart", "round") pixbuf = scale( pixbuf, (width - 2 * scale_factor, height - 2 * scale_factor)) pixbuf = add_border_widget(pixbuf, self, round_thumbs) else: pixbuf = scale(pixbuf, (width, height)) style_context = self.get_style_context() pbosf = get_pbosf_for_pixbuf(self, pixbuf) pbosf_render(style_context, cairo_context, pbosf, 0, 0)
def do_draw(self, cairo_context): pixbuf = self._get_pixbuf() if not pixbuf: return alloc = self.get_allocation() width, height = alloc.width, alloc.height scale_factor = get_scale_factor(self) width *= scale_factor height *= scale_factor if self._path: if width < 2 or height < 2: return round_thumbs = config.getboolean("albumart", "round") pixbuf = scale( pixbuf, (width - 2 * scale_factor, height - 2 * scale_factor)) pixbuf = add_border_widget(pixbuf, self, None, round_thumbs) else: pixbuf = scale(pixbuf, (width, height)) style_context = self.get_style_context() pbosf = get_pbosf_for_pixbuf(self, pixbuf) pbosf_render(style_context, cairo_context, pbosf, 0, 0)
def test_scale(s): nw = scale(s.wide, (50, 30)) s.failUnlessEqual((nw.get_width(), nw.get_height()), (50, 3)) nh = scale(s.high, (100, 20)) s.failUnlessEqual((nh.get_width(), nh.get_height()), (2, 20)) ns = scale(s.small, (500, 300)) s.failUnlessEqual((ns.get_width(), ns.get_height()), (150, 300)) ns = scale(s.small, (500, 300), scale_up=False) s.failUnlessEqual((ns.get_width(), ns.get_height()), (10, 20))
def get_scaled_cover(album): # XXX: Cache this somewhere else cover = None if not hasattr(album, "_scaled_cover"): scale_factor = self.get_scale_factor() album.scan_cover(scale_factor=scale_factor) if album.cover: s = 25 * scale_factor cover = scale(album.cover, (s, s)) album._scaled_cover = cover else: cover = album._scaled_cover return cover
def __scale_pixbuf(self, *data): if not self.current_pixbuf: return pixbuf = self.current_pixbuf if self.window_fit.get_active(): alloc = self.scrolled.get_allocation() width = alloc.width height = alloc.height scale_factor = self.get_scale_factor() boundary = (width * scale_factor, height * scale_factor) pixbuf = scale(pixbuf, boundary, scale_up=False) surface = get_surface_for_pixbuf(self, pixbuf) self.image.set_from_surface(surface)
def __scale_pixbuf(self, *data): if not self.current_pixbuf: return pixbuf = self.current_pixbuf if not self.window_fit.get_active(): pbosf = pixbuf else: alloc = self.scrolled.get_allocation() width = alloc.width height = alloc.height scale_factor = get_scale_factor(self) boundary = (width * scale_factor, height * scale_factor) pixbuf = scale(pixbuf, boundary, scale_up=False) pbosf = get_pbosf_for_pixbuf(self, pixbuf) set_image_from_pbosf(self.image, pbosf)
def get_thumbnail(path, boundary): """Get a thumbnail pixbuf of an image at `path`. Will create/use a thumbnail in the user's thumbnail directory if possible. Follows the Free Desktop specification: http://specifications.freedesktop.org/thumbnail-spec/ Can raise GLib.GError. Thread-safe. """ width, height = boundary new_from_file_at_size = GdkPixbuf.Pixbuf.new_from_file_at_size # larger than thumbnails, load directly if width > ThumbSize.LARGEST or height > ThumbSize.LARGEST: return new_from_file_at_size(path, width, height) path_mtime = mtime(path) if path_mtime == 0: return new_from_file_at_size(path, width, height) # embedded thumbnails come from /tmp/ # FIXME: move this to another layer if path.startswith(tempfile.gettempdir()): return new_from_file_at_size(path, width, height) thumb_path, thumb_size = get_cache_info(path, boundary) cache_dir = os.path.dirname(thumb_path) try: mkdir(cache_dir, 0o700) except OSError: return new_from_file_at_size(path, width, height) try: pb = new_from_file_at_size(thumb_path, width, height) except GLib.GError: # in case it fails to load, we recreate it pass else: meta_mtime = pb.get_option("tEXt::Thumb::MTime") if meta_mtime is not None: try: meta_mtime = int(meta_mtime) except ValueError: pass else: if meta_mtime == int(path_mtime): return pb info, pw, ph = GdkPixbuf.Pixbuf.get_file_info(path) # Too small picture, no thumbnail needed if pw < thumb_size and ph < thumb_size: return new_from_file_at_size(path, width, height) thumb_pb = new_from_file_at_size(path, thumb_size, thumb_size) uri = "file://" + pathname2url(path) mime = info.get_mime_types()[0] options = { "tEXt::Thumb::Image::Width": str(pw), "tEXt::Thumb::Image::Height": str(ph), "tEXt::Thumb::URI": uri, "tEXt::Thumb::MTime": str(int(path_mtime)), "tEXt::Thumb::Size": str(os.path.getsize(path)), "tEXt::Thumb::Mimetype": mime, "tEXt::Software": "QuodLibet" } thumb_pb.savev(thumb_path, "png", options.keys(), options.values()) try: os.chmod(thumb_path, 0o600) except OSError: pass return scale(thumb_pb, boundary)