slightly improved cover loading (#39)

This commit is contained in:
Martin Wagner 2021-07-25 18:22:37 +02:00
parent f0789d7170
commit 968158dede

View File

@ -505,7 +505,7 @@ class ClientHelper():
"date": "", "date": "",
"genre": "" "genre": ""
} }
if "range" in song: # translate .cue 'range' to 'duration' if needed if "range" in song: # translate .cue 'range' to 'duration' if needed (unneeded since MPD 0.22.4)
start, end=song["range"].split("-") start, end=song["range"].split("-")
if start != "" and end != "": if start != "" and end != "":
base_song["duration"]=str((float(end)-float(start))) base_song["duration"]=str((float(end)-float(start)))
@ -787,6 +787,7 @@ class Client(MPDClient):
return None return None
def get_albums(self, artist, genre): def get_albums(self, artist, genre):
self.tagtypes("clear")
albums=[] albums=[]
artist_type=self._settings.get_artist_type() artist_type=self._settings.get_artist_type()
if genre is None: if genre is None:
@ -797,16 +798,21 @@ class Client(MPDClient):
for album in album_candidates: for album in album_candidates:
years=self.comp_list("date", "album", album, artist_type, artist, *genre_filter) years=self.comp_list("date", "album", album, artist_type, artist, *genre_filter)
for year in years: for year in years:
songs=self.find("album", album, "date", year, artist_type, artist, *genre_filter) count=self.count(artist_type, artist, "album", album, "date", year, *genre_filter)
cover_path=self.get_cover_path(songs[0]) song=self.find("album", album, "date", year, artist_type, artist, *genre_filter, "window", "0:1")[0]
cover_path=self.get_cover_path(song)
if cover_path is None: if cover_path is None:
cover_binary=self.get_cover_binary(songs[0].get("file")) cover_binary=self.get_cover_binary(song.get("file"))
if cover_binary is None: if cover_binary is None:
albums.append({"artist": artist, "album": album, "year": year, "songs": songs}) albums.append({"artist": artist,"album": album,"year": year,
"length": count["songs"],"duration": count["playtime"]})
else: else:
albums.append({"artist":artist,"album":album,"year":year,"songs":songs,"cover_binary":cover_binary}) albums.append({"artist": artist,"album": album,"year": year,
"length": count["songs"],"duration": count["playtime"],"cover_binary": cover_binary})
else: else:
albums.append({"artist": artist, "album": album, "year": year, "songs": songs, "cover_path": cover_path}) albums.append({"artist": artist,"album": album,"year": year,
"length": count["songs"],"duration": count["playtime"], "cover_path": cover_path})
self.tagtypes("all")
return albums return albums
def toggle_play(self): def toggle_play(self):
@ -2248,8 +2254,8 @@ class AlbumWindow(FocusFrame):
self._done=True self._done=True
self._pending=[] self._pending=[]
# cover, display_label, display_label_artist, tooltip(titles), album, year, artist # cover, display_label, display_label_artist, tooltip(titles), album, year, artist, index
self._store=Gtk.ListStore(GdkPixbuf.Pixbuf, str, str, str, str, str, str) self._store=Gtk.ListStore(GdkPixbuf.Pixbuf, str, str, str, str, str, str, int)
self._sort_settings() self._sort_settings()
# iconview # iconview
@ -2368,58 +2374,50 @@ class AlbumWindow(FocusFrame):
except MPDBase.ConnectionError: except MPDBase.ConnectionError:
self._done_callback() self._done_callback()
return return
# temporarily display all albums with fallback cover
def display_albums(): size=self._settings.get_int("album-cover")
for i, album in enumerate(albums): fallback_cover=GdkPixbuf.Pixbuf.new_from_file_at_size(FALLBACK_COVER, size, size)
# tooltip for i, album in enumerate(albums):
duration=ClientHelper.calc_display_duration(album["songs"]) # tooltip
length=len(album["songs"]) duration=ClientHelper.seconds_to_display_time(int(album["duration"]))
discs=album["songs"][-1].get("disc", 1) length=int(album["length"])
if isinstance(discs, list): tooltip=ngettext("{number} song ({duration})", "{number} songs ({duration})", length).format(
discs=int(discs[0]) number=length, duration=duration)
else: # album label
discs=int(discs) if album["year"] == "":
if discs > 1: display_label=f"<b>{album['album']}</b>"
tooltip=_("{number} songs on {discs} discs ({duration})").format( else:
number=length, discs=discs, duration=duration) display_label=f"<b>{album['album']}</b> ({album['year']})"
else: display_label_artist=f"{display_label}\n{album['artist']}"
tooltip=ngettext("{number} song ({duration})", "{number} songs ({duration})", length).format( display_label=display_label.replace("&", "&amp;")
number=length, duration=duration) display_label_artist=display_label_artist.replace("&", "&amp;")
# album label # add album
if album["year"] == "": self._store.append(
display_label=f"<b>{album['album']}</b>" [fallback_cover, display_label, display_label_artist,
else: tooltip, album["album"], album["year"], album["artist"], i]
display_label=f"<b>{album['album']}</b> ({album['year']})" )
display_label_artist=f"{display_label}\n{album['artist']}" self._iconview.set_model(self._store)
display_label=display_label.replace("&", "&amp;")
display_label_artist=display_label_artist.replace("&", "&amp;")
# add album
self._store.append(
[album["cover"], display_label, display_label_artist,
tooltip, album["album"], album["year"], album["artist"]]
)
self._iconview.set_model(self._store)
self._done_callback()
return False
def render_covers(): def render_covers():
def set_cover(row, cover):
row[0]=cover
size=self._settings.get_int("album-cover") size=self._settings.get_int("album-cover")
fallback_cover=GdkPixbuf.Pixbuf.new_from_file_at_size(FALLBACK_COVER, size, size)
total_albums=len(albums) total_albums=len(albums)
for i, album in enumerate(albums): for i, row in enumerate(self._store):
album=albums[row[7]]
if self._stop_flag: if self._stop_flag:
break break
if "cover_path" in album: if "cover_path" in album:
album["cover"]=ClientHelper.file_to_pixbuf(album["cover_path"], size) cover=ClientHelper.file_to_pixbuf(album["cover_path"], size)
else: else:
if "cover_binary" in album: if "cover_binary" in album:
album["cover"]=ClientHelper.binary_to_pixbuf(album["cover_binary"], size) cover=ClientHelper.binary_to_pixbuf(album["cover_binary"], size)
else: else:
album["cover"]=GdkPixbuf.Pixbuf.new_from_file_at_size(FALLBACK_COVER, size, size) cover=fallback_cover
GLib.idle_add(set_cover, row, cover)
GLib.idle_add(self._progress_bar.set_fraction, (i+1)/total_albums) GLib.idle_add(self._progress_bar.set_fraction, (i+1)/total_albums)
if self._stop_flag: GLib.idle_add(self._done_callback)
GLib.idle_add(self._done_callback)
else:
GLib.idle_add(display_albums)
cover_thread=threading.Thread(target=render_covers, daemon=True) cover_thread=threading.Thread(target=render_covers, daemon=True)
cover_thread.start() cover_thread.start()