emmental/emmental/tracklist/row.py

69 lines
2.3 KiB
Python

# Copyright 2023 (c) Anna Schumaker.
"""Widgets for displaying Track information in the TrackView."""
from gi.repository import GObject
from gi.repository import Gtk
from .. import factory
class TrackRow(factory.ListRow):
"""Base class for Track Row widgets."""
property = GObject.Property(type=str)
def __init__(self, listitem: Gtk.ListItem, property: str):
"""Initialize a TrackRow."""
super().__init__(listitem, property=property)
def do_bind(self) -> None:
"""Bind a Track to this Row."""
super().do_bind()
library = self.item.get_library()
self.bind_and_set(library, "online", self, "online")
self.bind_active("active")
@GObject.Property(type=bool, default=False)
def active(self) -> bool:
"""Get the active state of this Row."""
if parent := self.listitem.get_child().get_parent():
if parent := parent.get_parent():
return parent.get_state_flags() & Gtk.StateFlags.CHECKED
return False
@active.setter
def active(self, newval: bool) -> None:
if parent := self.listitem.get_child().get_parent():
if parent := parent.get_parent():
if newval:
parent.set_state_flags(Gtk.StateFlags.CHECKED, False)
else:
parent.unset_state_flags(Gtk.StateFlags.CHECKED)
@GObject.Property(type=bool, default=True)
def online(self) -> bool:
"""Get the online state of this Row."""
return self.listitem.get_activatable()
@online.setter
def online(self, newval: bool) -> None:
self.listitem.set_activatable(newval)
self.child.set_sensitive(newval)
class InscriptionRow(TrackRow):
"""Base class for Track Rows displaying a Gtk.Inscription."""
def __init__(self, listitem: Gtk.ListItem, property: str):
"""Initialize a LabelRow."""
super().__init__(listitem, property)
self.child = Gtk.Inscription(xalign=0.0)
self.child.bind_property("text", self.child, "tooltip-text")
class TrackString(InscriptionRow):
"""An InscriptionRow displaying a string Track property."""
def do_bind(self) -> None:
"""Bind a track string to the Track Inscription."""
super().do_bind()
self.bind_and_set_property(self.property, "text")