emmental/emmental/sidebar/artist.py

57 lines
2.3 KiB
Python

# Copyright 2022 (c) Anna Schumaker.
"""Displays our artist and album tree."""
from gi.repository import GObject
from ..buttons import ImageToggle
from .. import db
from . import row
from . import section
class ArtistRow(row.TreeRow):
"""A factory for setting Album covers properly."""
def __init__(self, *args, **kwargs):
"""Initialize an ArtistRow."""
super().__init__(*args, **kwargs)
self.child = row.Row()
def do_bind(self) -> None:
"""Bind the album cover property."""
super().do_bind()
if isinstance(self.item, db.albums.Album):
self.bind_and_set_property("cover", "image")
class Section(section.Section):
"""A sidebar Section for the artist and album playlist tree."""
album_table = GObject.Property(type=db.albums.Table)
show_all = GObject.Property(type=bool, default=False)
def __init__(self, artist_table: db.artists.Table,
album_table: db.albums.Table):
"""Initialize our artist & album section."""
super().__init__(artist_table, ArtistRow, title="Artists & Albums",
subtitle="0 artists, 0 albums",
icon_name="library-artists", album_table=album_table)
self.extra_widget = ImageToggle("music-artist", "music-artist2",
"show album artists",
"show all artists",
large_icon=False, has_frame=False)
self.album_table.connect("items-changed", self.__update_subtitle)
self.bind_property("show-all", self.extra_widget, "active",
GObject.BindingFlags.BIDIRECTIONAL)
self.bind_property("show-all", artist_table, "show-all")
def __update_subtitle(self, table: db.albums.Table, position: int,
removed: int, added: int) -> None:
self.subtitle = self.do_get_subtitle(0)
def do_get_subtitle(self, n_items: int) -> str:
"""Return a subtitle for this section."""
n_artists = len(self.table)
s_artists = "s" if n_artists != 1 else ""
n_albums = len(self.album_table)
s_albums = "s" if n_albums != 1 else ""
return f"{n_artists} artist{s_artists}, {n_albums} album{s_albums}"