The sidebar gets 8px of bare window on its left, and the Library button spans exactly the playlist list below it, scrollbar included. The Library button, search box, playlist name, times and playhead all stand on one floor 5px above the lists (text by its baseline). The top row floats: equal space before, between and after the controls, with the now-playing panel a fixed share of the width (700px at 1920). The header row grows if the app font makes the Library button taller than the strip.
157 lines
6.2 KiB
Python
157 lines
6.2 KiB
Python
from PyQt6.QtWidgets import QWidget, QVBoxLayout, QApplication
|
|
from PyQt6.QtCore import pyqtSignal
|
|
|
|
from lintunes.gui.fit_label import FitLabel
|
|
from lintunes.gui.table_settings import TableSettingsMixin
|
|
from lintunes.gui.track_table import TrackTableView, parse_tracks_mime
|
|
from lintunes.gui.playlist_ops import add_tracks_with_dup_check
|
|
|
|
|
|
class PlaylistView(TableSettingsMixin, QWidget):
|
|
play_requested = pyqtSignal(list, int)
|
|
info_requested = pyqtSignal(list)
|
|
files_dropped = pyqtSignal(list, str, object) # paths, pid, row or None
|
|
show_in_playlist_requested = pyqtSignal(int, str) # track_id, playlist pid
|
|
|
|
def __init__(self, manager, parent=None):
|
|
super().__init__(parent)
|
|
self._manager = manager
|
|
self._playlist = None
|
|
|
|
layout = QVBoxLayout(self)
|
|
layout.setContentsMargins(0, 0, 0, 0)
|
|
layout.setSpacing(0)
|
|
|
|
# The playlist's name. Not in this view's layout: it is this view's
|
|
# ``header``, shown in the transport bar's header row (see
|
|
# TransportBar.set_view_header), shrinking a little when it's long.
|
|
self._name_label = FitLabel(parent=self)
|
|
font = self._name_label.font()
|
|
font.setBold(True)
|
|
self._name_label.setFont(font)
|
|
self._name_label.setContentsMargins(4, 0, 0, 0)
|
|
self._name_label.hide()
|
|
self.header = self._name_label
|
|
self._name_label.header_owner = self
|
|
|
|
self.table = TrackTableView(playlist_mode=True)
|
|
layout.addWidget(self.table)
|
|
|
|
self.table.playlists_for_track = manager.playlists_containing
|
|
self.table.play_requested.connect(self.play_requested)
|
|
self.table.info_requested.connect(self.info_requested)
|
|
self.table.show_in_playlist_requested.connect(
|
|
self.show_in_playlist_requested)
|
|
self._wire_table_settings()
|
|
self.table.reorder_requested.connect(self._on_reorder)
|
|
self.table.tracks_dropped.connect(self._on_tracks_dropped)
|
|
self.table.files_dropped.connect(self._on_files_dropped)
|
|
self.table.remove_requested.connect(self._on_remove)
|
|
self.table.cut_requested.connect(self._on_cut)
|
|
self.table.paste_requested.connect(self._on_paste)
|
|
# Undoable; rating isn't a file tag, so this only touches the library.
|
|
self.table.rating_edited.connect(
|
|
lambda tid, rating: manager.edit_track_fields(
|
|
tid, {"rating": rating}))
|
|
|
|
manager.playlist_content_changed.connect(self._on_content_changed)
|
|
manager.track_updated.connect(self.table.model_.refresh_track)
|
|
manager.playlists_changed.connect(self._on_playlists_changed)
|
|
|
|
@property
|
|
def playlist_id(self) -> str:
|
|
return self._playlist.persistent_id if self._playlist else ""
|
|
|
|
def reveal_track(self, track_id: int):
|
|
self.table.reveal_track(track_id)
|
|
|
|
def show_playlist(self, playlist):
|
|
self._playlist = playlist
|
|
self.table.set_source_playlist(playlist.persistent_id)
|
|
# Smart playlists are auto-populated: no manual add/remove/reorder.
|
|
self.table.set_content_editable(not playlist.is_smart)
|
|
self.table.apply_settings(playlist.settings)
|
|
self._reload_tracks()
|
|
|
|
def _reload_tracks(self):
|
|
if self._playlist is None:
|
|
return
|
|
tracks = [self._manager.library.tracks[tid]
|
|
for tid in self._playlist.track_ids
|
|
if tid in self._manager.library.tracks]
|
|
self.table.set_tracks(tracks)
|
|
# Count/time now live in the bottom status bar, not next to the name.
|
|
prefix = "❧ " if self._playlist.is_smart else ""
|
|
self._name_label.setText(prefix + self._playlist.name)
|
|
|
|
def _on_content_changed(self, pid):
|
|
if self._playlist is not None and pid == self._playlist.persistent_id:
|
|
self._reload_tracks()
|
|
|
|
def _on_playlists_changed(self):
|
|
# Playlist may have been renamed or deleted
|
|
if self._playlist is None:
|
|
return
|
|
if self._playlist.persistent_id not in self._manager.library.playlists:
|
|
self._playlist = None
|
|
self.table.set_tracks([])
|
|
self._name_label.setText("")
|
|
else:
|
|
self._reload_tracks()
|
|
|
|
# ---- settings persistence (TableSettingsMixin hooks) ----
|
|
|
|
def _current_settings(self):
|
|
return self._playlist.settings if self._playlist else None
|
|
|
|
def _mark_settings_dirty(self):
|
|
self._manager.mark_playlist_settings_dirty(self._playlist.persistent_id)
|
|
|
|
# ---- content edits ----
|
|
|
|
def _editable(self) -> bool:
|
|
"""Manual content edits are blocked for smart playlists (membership is
|
|
derived from criteria)."""
|
|
return self._playlist is not None and not self._playlist.is_smart
|
|
|
|
def _on_reorder(self, rows, dest):
|
|
if self._editable():
|
|
self._manager.move_tracks_in_playlist(
|
|
self._playlist.persistent_id, rows, dest)
|
|
|
|
def _on_tracks_dropped(self, track_ids, drop_row):
|
|
if self._editable():
|
|
add_tracks_with_dup_check(
|
|
self._manager, self, self._playlist.persistent_id,
|
|
track_ids, drop_row)
|
|
|
|
def _on_files_dropped(self, paths, drop_row):
|
|
if self._editable():
|
|
self.files_dropped.emit(
|
|
paths, self._playlist.persistent_id, drop_row)
|
|
|
|
def _on_remove(self, rows):
|
|
if self._editable():
|
|
self._manager.remove_tracks_from_playlist(
|
|
self._playlist.persistent_id, rows)
|
|
|
|
def _on_cut(self, rows):
|
|
# Cut removes immediately (so the track visibly leaves); the ids are
|
|
# already on the clipboard for a later paste to re-insert.
|
|
if self._editable():
|
|
self._manager.remove_tracks_from_playlist(
|
|
self._playlist.persistent_id, rows)
|
|
|
|
def _on_paste(self):
|
|
if not self._editable():
|
|
return
|
|
payload = parse_tracks_mime(QApplication.clipboard().mimeData())
|
|
if not payload:
|
|
return
|
|
track_ids = payload.get("track_ids", [])
|
|
if track_ids:
|
|
# Paste above the selected track (None -> append).
|
|
add_tracks_with_dup_check(
|
|
self._manager, self, self._playlist.persistent_id,
|
|
track_ids, self.table.paste_anchor_row())
|