emmental/emmental/sidebar/decade.py

50 lines
1.8 KiB
Python

# Copyright 2022 (c) Anna Schumaker.
"""Displays our decade and year tree."""
from gi.repository import GObject
from .. import db
from . import row
from . import section
class DecadeRow(row.TreeRow):
"""A TreeRow for setting Decade & Year icons properly."""
def __init__(self, *args, **kwargs):
"""Initialize a GenreRow."""
super().__init__(*args, **kwargs)
self.child = row.Row()
def do_bind(self) -> None:
"""Bind the decade and year number to the row year property."""
super().do_bind()
if isinstance(self.item, db.decades.Decade):
self.bind_and_set_property("decade", "year")
else:
self.bind_and_set_property("year", "year")
class Section(section.Section):
"""A sidebar Section for the decade and year playlist tree."""
year_table = GObject.Property(type=db.years.Table)
def __init__(self, decade_table: db.decades.Table,
year_table: db.years.Table):
"""Initialize our decade & year section."""
super().__init__(decade_table, DecadeRow, title="Decades & Years",
subtitle="0 decades, 0 years",
icon_name="year-alt", year_table=year_table)
self.year_table.connect("items-changed", self.__update_subtitle)
def __update_subtitle(self, table: db.years.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_decades = len(self.table)
s_decades = "s" if n_decades != 1 else ""
n_years = len(self.year_table)
s_years = "s" if n_years != 1 else ""
return f"{n_decades} decade{s_decades}, {n_years} year{s_years}"