emmental/emmental/tracklist/sorter.py

56 lines
1.9 KiB
Python

# Copyright 2022 (c) Anna Schumaker.
"""A Sorter designed for sorting Playlists."""
from gi.repository import GObject
from gi.repository import Gio
SORT_FIELDS = {"Album": ["album"], "Artist": ["artist"],
"Album Artist": ["albumartist"],
"Filepath": ["filepath"], "Length": ["length"],
"Last Played Date": ["lastplayed"],
"Last Started Date": ["laststarted"],
"Play Count": ["playcount"], "Release Date": ["release"],
"Title": ["title"], "Track Number": ["mediumno", "number"]}
class SortField(GObject.GObject):
"""Used to represent a single field in the sort order."""
name = GObject.Property(type=str)
model = GObject.Property(type=Gio.ListModel)
enabled = GObject.Property(type=bool, default=False)
reversed = GObject.Property(type=bool, default=False)
def __init__(self, model: Gio.ListStore, name: str):
"""Initialize a Sort Field."""
if name not in SORT_FIELDS:
raise KeyError
super().__init__(model=model, name=name)
def __str__(self) -> str:
"""Convert this Field into a string."""
columns = SORT_FIELDS[self.name]
if self.reversed:
columns = [f"{col} DESC" for col in columns]
return ", ".join(columns)
def disable(self) -> bool:
"""Disable this Sort Field."""
return self.model.disable(self)
def enable(self) -> bool:
"""Enable this Sort Field."""
return self.model.enable(self)
def move_down(self) -> bool:
"""Move this Sort Field down in the list."""
return self.model.move_down(self)
def move_up(self) -> bool:
"""Move this Sort Field up in the list."""
return self.model.move_up(self)
def reverse(self) -> None:
"""Reverse the direction of this Sort Field."""
self.model.reverse(self)