Created
July 23, 2016 18:46
-
-
Save lazka/87b59993b3cf01ed420d78dda03e1eac to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
diff --git a/quodlibet/quodlibet/qltk/x.py b/quodlibet/quodlibet/qltk/x.py | |
index df4bdb8..0a84b3a 100644 | |
--- a/quodlibet/quodlibet/qltk/x.py | |
+++ b/quodlibet/quodlibet/qltk/x.py | |
@@ -10,9 +10,11 @@ Things that are more or less direct wrappers around GTK widgets to | |
ease constructors. | |
""" | |
-from gi.repository import Gtk, GObject, GLib, Gio | |
+from gi.repository import Gtk, GObject, GLib, Gio, GdkPixbuf | |
from quodlibet import util | |
+from quodlibet.compat import urlopen | |
+from quodlibet.util.thread import call_async, Cancellable | |
from quodlibet.qltk import add_css, is_accel, gtk_version | |
from .paned import Paned, RPaned, RHPaned, RVPaned, ConfigRPaned, \ | |
@@ -400,3 +402,49 @@ class ToggleAction(Gtk.ToggleAction): | |
class RadioAction(Gtk.RadioAction): | |
def __init__(self, *args, **kargs): | |
GObject.Object.__init__(self, *args, **kargs) | |
+ | |
+ | |
+class WebImage(Gtk.Image): | |
+ """A Gtk.Image which loads the image over http in the background | |
+ and displays it when available. | |
+ """ | |
+ | |
+ def __init__(self, url, width=-1, height=-1): | |
+ """ | |
+ Args: | |
+ url (str): a HTTP url | |
+ width (int): a width to reserve for the image or -1 | |
+ height (int): a height to reserve for the image or -1 | |
+ """ | |
+ | |
+ super(WebImage, self).__init__() | |
+ | |
+ self._cancel = Cancellable() | |
+ call_async(self._fetch_image, self._cancel, self._finished, (url,)) | |
+ self.connect("destroy", self._on_destroy) | |
+ self.set_size_request(width, height) | |
+ self.set_from_icon_name("image-loading", Gtk.IconSize.BUTTON) | |
+ | |
+ def _on_destroy(self, *args): | |
+ self._cancel.cancel() | |
+ | |
+ def _fetch_image(self, url): | |
+ try: | |
+ r = urlopen(url) | |
+ data = r.read() | |
+ except Exception: | |
+ return | |
+ | |
+ try: | |
+ loader = GdkPixbuf.PixbufLoader() | |
+ loader.write(data) | |
+ loader.close() | |
+ return loader.get_pixbuf() | |
+ except GLib.GError: | |
+ return | |
+ | |
+ def _finished(self, pixbuf): | |
+ if pixbuf is None: | |
+ self.set_from_icon_name("image-missing", Gtk.IconSize.BUTTON) | |
+ else: | |
+ self.set_from_pixbuf(pixbuf) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment