[gnome-music/wip/mschraal/coverart: 3/5] albumartcache: Add ArtSize enum



commit 22122fa4130573a453e558f6bad3233d3b688823
Author: Marinus Schraal <mschraal src gnome org>
Date:   Sun Sep 25 20:52:19 2016 +0200

    albumartcache: Add ArtSize enum

 gnomemusic/albumartcache.py |   50 ++++++++++++++++++++++++++++--------------
 gnomemusic/player.py        |   11 +++------
 gnomemusic/view.py          |   41 +++++++++++++++-------------------
 gnomemusic/widgets.py       |   15 +++++++------
 4 files changed, 63 insertions(+), 54 deletions(-)
---
diff --git a/gnomemusic/albumartcache.py b/gnomemusic/albumartcache.py
index b93f885..d562557 100644
--- a/gnomemusic/albumartcache.py
+++ b/gnomemusic/albumartcache.py
@@ -86,6 +86,20 @@ def _make_icon_frame(pixbuf):
     return border_pixbuf
 
 
+class ArtSize(Enum):
+    """Enum for icon sizes"""
+    xsmall = (34, 34)
+    small = (48, 48)
+    medium = (128, 128)
+    large = (256, 256)
+    xlarge = (512, 512)
+
+    def __init__(self, width, height):
+        """Intialize width and height"""
+        self.width = width
+        self.height = height
+
+
 class DefaultIcon(GObject.GObject):
     """Provides the symbolic fallback and loading icons."""
 
@@ -99,7 +113,10 @@ class DefaultIcon(GObject.GObject):
         return '<DefaultIcon>'
 
     @log
-    def _make_default_icon(self, width, height, icon_type):
+    def _make_default_icon(self, icon_type, art_size):
+        width = art_size.width
+        height = art_size.height
+
         icon = Gtk.IconTheme.get_default().load_icon(icon_type.value,
                                                      max(width, height) / 4,
                                                      0)
@@ -126,24 +143,23 @@ class DefaultIcon(GObject.GObject):
         return final_icon
 
     @log
-    def get(self, width, height, icon_type):
+    def get(self, icon_type, art_size):
         """Returns the requested symbolic icon
 
         Returns a GdkPixbuf of the requested symbolic icon
         in the given size.
 
-        :param int width: The width of the icon
-        :param int height: The height of the icon
         :param enum icon_type: The DefaultIcon.Type of the icon
+        :param enum art_size: The ArtSize requested
 
         :return: The symbolic icon
         :rtype: GdkPixbuf
         """
-        if (width, height, icon_type) not in self._cache.keys():
-            new_icon = self._make_default_icon(width, height, icon_type)
-            self._cache[(width, height, icon_type)] = new_icon
+        if (icon_type, art_size) not in self._cache.keys():
+            new_icon = self._make_default_icon(icon_type, art_size)
+            self._cache[(icon_type, art_size)] = new_icon
 
-        return self._cache[(width, height, icon_type)]
+        return self._cache[(icon_type, art_size)]
 
 
 class AlbumArtCache(GObject.GObject):
@@ -171,7 +187,7 @@ class AlbumArtCache(GObject.GObject):
                 return
 
     @log
-    def lookup(self, item, width, height, callback, itr):
+    def lookup(self, item, art_size, callback, itr):
         """Find art for the given item
 
         :param item: Grilo media item
@@ -180,10 +196,10 @@ class AlbumArtCache(GObject.GObject):
         :param callback: Callback function when retrieved
         :param itr: Iter to return with callback
         """
-        self._lookup_local(item, callback, itr, width, height)
+        self._lookup_local(item, callback, itr, art_size)
 
     @log
-    def _lookup_local(self, item, callback, itr, width, height):
+    def _lookup_local(self, item, callback, itr, art_size):
         """Checks if there is already a local art file, if not calls
         the remote lookup function"""
         album = utils.get_media_title(item)
@@ -215,10 +231,10 @@ class AlbumArtCache(GObject.GObject):
 
         def do_callback(pixbuf):
             if not pixbuf:
-                pixbuf = DefaultIcon().get(width, height,
-                                           DefaultIcon.Type.music)
+                pixbuf = DefaultIcon().get(DefaultIcon.Type.music, art_size)
             else:
-                pixbuf = pixbuf.scale_simple(width, height,
+                pixbuf = pixbuf.scale_simple(art_size.width,
+                                             art_size.height,
                                              GdkPixbuf.InterpType.HYPER)
                 pixbuf = _make_icon_frame(pixbuf)
 
@@ -245,10 +261,10 @@ class AlbumArtCache(GObject.GObject):
             do_callback(None)
             return
 
-        self._lookup_remote(item, callback, itr, width, height)
+        self._lookup_remote(item, callback, itr, art_size)
 
     @log
-    def _lookup_remote(self, item, callback, itr, width, height):
+    def _lookup_remote(self, item, callback, itr, art_size):
         """Lookup remote art
 
         Lookup remote art through Grilo and if found copy locally. Call
@@ -346,6 +362,6 @@ class AlbumArtCache(GObject.GObject):
                 album_stripped = MediaArt.strip_invalid_entities(album)
                 self.blacklist[artist].append(album_stripped)
 
-            self.lookup(item, width, height, callback, itr)
+            self.lookup(item, art_size, callback, itr)
 
         grilo.get_album_art_for_item(item, album_art_for_item_cb)
diff --git a/gnomemusic/player.py b/gnomemusic/player.py
index d260a86..66719d1 100644
--- a/gnomemusic/player.py
+++ b/gnomemusic/player.py
@@ -42,7 +42,7 @@ from gi.repository import Gtk, Gdk, GLib, Gio, GObject, Gst, GstAudio, GstPbutil
 from gettext import gettext as _, ngettext
 from random import randint
 from collections import deque
-from gnomemusic.albumartcache import AlbumArtCache, DefaultIcon
+from gnomemusic.albumartcache import AlbumArtCache, DefaultIcon, ArtSize
 from gnomemusic.playlists import Playlists
 import gnomemusic.utils as utils
 playlists = Playlists.get_default()
@@ -56,8 +56,6 @@ from gnomemusic import log
 import logging
 logger = logging.getLogger(__name__)
 
-ART_SIZE = 34
-
 
 class RepeatType:
     NONE = 0
@@ -111,9 +109,8 @@ class Player(GObject.GObject):
         self.currentTrackUri = None
         self._lastState = Gst.State.PAUSED
         self.cache = AlbumArtCache()
-        self._no_artwork_icon = DefaultIcon().get(ART_SIZE,
-                                                  ART_SIZE,
-                                                  DefaultIcon.Type.music)
+        self._no_artwork_icon = DefaultIcon().get(DefaultIcon.Type.music,
+                                                  ArtSize.xsmall)
         self._missingPluginMessages = []
 
         Gst.init(None)
@@ -610,7 +607,7 @@ class Player(GObject.GObject):
 
         self.coverImg.set_from_pixbuf(self._no_artwork_icon)
         self.cache.lookup(
-            media, ART_SIZE, ART_SIZE, self._on_cache_lookup, None)
+            media, ArtSize.xsmall, self._on_cache_lookup, None)
 
         self._currentTitle = utils.get_media_title(media)
         self.titleLabel.set_label(self._currentTitle)
diff --git a/gnomemusic/view.py b/gnomemusic/view.py
index 25e1c07..742d51d 100644
--- a/gnomemusic/view.py
+++ b/gnomemusic/view.py
@@ -48,7 +48,7 @@ import gnomemusic.widgets as Widgets
 from gnomemusic.player import DiscoveryStatus
 from gnomemusic.playlists import Playlists, StaticPlaylists
 import gnomemusic.utils as utils
-from gnomemusic.albumartcache import AlbumArtCache, DefaultIcon
+from gnomemusic.albumartcache import AlbumArtCache, DefaultIcon, ArtSize
 from gnomemusic import log
 import logging
 logger = logging.getLogger(__name__)
@@ -70,8 +70,6 @@ class ViewContainer(Gtk.Stack):
         Gtk.Stack.__init__(self,
                            transition_type=Gtk.StackTransitionType.CROSSFADE)
         self._grid = Gtk.Grid(orientation=Gtk.Orientation.HORIZONTAL)
-        self._iconWidth = 128
-        self._iconHeight = 128
         self._offset = 0
         self._adjustmentValueId = 0
         self._adjustmentChangedId = 0
@@ -130,9 +128,8 @@ class ViewContainer(Gtk.Stack):
         self.view.hide()
         self._items = []
         self.cache = AlbumArtCache()
-        self._loading_icon = DefaultIcon().get(self._iconWidth,
-                                               self._iconHeight,
-                                               DefaultIcon.Type.loading)
+        self._loading_icon = DefaultIcon().get(DefaultIcon.Type.loading,
+                                               ArtSize.medium)
 
         self._init = False
         grilo.connect('ready', self._on_grilo_ready)
@@ -348,7 +345,8 @@ class InitialState(Empty):
         icon.set_margin_bottom(32)
         icon.set_opacity(1)
         icon.set_from_resource('/org/gnome/Music/initial-state.png')
-        icon.set_size_request(256, 256)
+        icon.set_size_request(ArtSize.large.width,
+                              ArtSize.large.height)
 
         # Update label
         label = self.builder.get_object('label')
@@ -500,8 +498,8 @@ class Albums(ViewContainer):
         # In the case of off-sized icons (eg. provided in the soundfile)
         # keep the size request equal to all other icons to get proper
         # alignment with GtkFlowBox.
-        child.image.set_property("width-request", self._iconWidth)
-        child.image.set_property("height-request", self._iconHeight)
+        child.image.set_property("width-request", ArtSize.medium.width)
+        child.image.set_property("height-request", ArtSize.medium.height)
 
         child.events.connect('button-release-event',
                              self._on_album_event_triggered,
@@ -517,8 +515,10 @@ class Albums(ViewContainer):
         child.add(builder.get_object('main_box'))
         child.show()
 
-        self.cache.lookup(item, self._iconWidth, self._iconHeight,
-                          self._on_lookup_ready, child)
+        self.cache.lookup(item,
+                          ArtSize.medium,
+                          self._on_lookup_ready,
+                          child)
 
         return child
 
@@ -1527,14 +1527,10 @@ class Search(ViewContainer):
         self._items = {}
         self.isStarred = None
         self.iter_to_clean = None
-        self._iconHeight = 48
-        self._iconWidth = 48
-        self._loading_icon = DefaultIcon().get(self._iconWidth,
-                                               self._iconHeight,
-                                               DefaultIcon.Type.loading)
-        self._no_albumart_icon = DefaultIcon().get(self._iconWidth,
-                                                   self._iconHeight,
-                                                   DefaultIcon.Type.music)
+        self._loading_icon = DefaultIcon().get(DefaultIcon.Type.loading,
+                                               ArtSize.small)
+        self._no_albumart_icon = DefaultIcon().get(DefaultIcon.Type.music,
+                                                   ArtSize.small)
         self._add_list_renderers()
         self.player = player
         self.head_iters = [None, None, None, None]
@@ -1702,8 +1698,7 @@ class Search(ViewContainer):
                 [0, 2, 3, 4, 5, 9, 11],
                 [str(item.get_id()), title, artist,
                  self._loading_icon, item, 2, category])
-            self.cache.lookup(item, self._iconWidth, self._iconHeight,
-                              self._on_lookup_ready, _iter)
+            self.cache.lookup(item, ArtSize.small, self._on_lookup_ready, _iter)
         elif category == 'song':
             _iter = self.model.insert_with_values(
                 self.head_iters[group], -1,
@@ -1717,8 +1712,8 @@ class Search(ViewContainer):
                     [0, 2, 4, 5, 9, 11],
                     [str(item.get_id()), artist,
                      self._loading_icon, item, 2, category])
-                self.cache.lookup(item, self._iconWidth, self._iconHeight,
-                                  self._on_lookup_ready, _iter)
+                self.cache.lookup(item, ArtSize.small, self._on_lookup_ready,
+                                  _iter)
                 self._artists[artist.casefold()] = {'iter': _iter, 'albums': []}
 
             self._artists[artist.casefold()]['albums'].append(item)
diff --git a/gnomemusic/widgets.py b/gnomemusic/widgets.py
index cd9d4a4..bd1bf6a 100644
--- a/gnomemusic/widgets.py
+++ b/gnomemusic/widgets.py
@@ -35,7 +35,7 @@ import logging
 from gi.repository import Gtk, Gdk, Gd, GLib, GObject, Pango, Gio, GdkPixbuf
 from gettext import gettext as _, ngettext
 
-from gnomemusic.albumartcache import AlbumArtCache, DefaultIcon
+from gnomemusic.albumartcache import AlbumArtCache, DefaultIcon, ArtSize
 from gnomemusic.grilo import grilo
 from gnomemusic import log
 from gnomemusic.player import DiscoveryStatus
@@ -123,8 +123,8 @@ class AlbumWidget(Gtk.EventBox):
     """
 
     _duration = 0
-    _loading_icon = DefaultIcon().get(256, 256, DefaultIcon.Type.loading)
-    _no_artwork_icon = DefaultIcon().get(256, 256, DefaultIcon.Type.music)
+    _loading_icon = DefaultIcon().get(DefaultIcon.Type.loading, ArtSize.small)
+    _no_artwork_icon = DefaultIcon().get(DefaultIcon.Type.music, ArtSize.small)
 
     def __repr__(self):
         return '<AlbumWidget>'
@@ -291,7 +291,7 @@ class AlbumWidget(Gtk.EventBox):
         self._header_bar = header_bar
         self._album = album
         self._ui.get_object('cover').set_from_pixbuf(self._loading_icon)
-        self._cache.lookup(item, 256, 256, self._on_look_up, None)
+        self._cache.lookup(item, ArtSize.large, self._on_look_up, None)
         self._duration = 0
         self._create_model()
         GLib.idle_add(grilo.populate_album_songs, item, self.add_item)
@@ -626,8 +626,8 @@ class ArtistAlbumWidget(Gtk.Box):
         'tracks-loaded': (GObject.SignalFlags.RUN_FIRST, None, ()),
     }
 
-    _loading_icon = DefaultIcon().get(256, 256, DefaultIcon.Type.loading)
-    _no_artwork_icon = DefaultIcon().get(256, 256, DefaultIcon.Type.music)
+    _loading_icon = DefaultIcon().get(DefaultIcon.Type.loading, ArtSize.large)
+    _no_artwork_icon = DefaultIcon().get(DefaultIcon.Type.music, ArtSize.large)
 
     def __repr__(self):
         return '<ArtistAlbumWidget>'
@@ -715,7 +715,8 @@ class ArtistAlbumWidget(Gtk.Box):
 
     @log
     def _update_album_art(self):
-        self._cache.lookup(self.album, 128, 128, self._get_album_cover, None)
+        self._cache.lookup(self.album, ArtSize.medium, self._get_album_cover,
+                           None)
 
     @log
     def _get_album_cover(self, pixbuf, path, data=None):


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]