2024-12-28 20:41:18 +01:00
|
|
|
#!/usr/bin/python3
|
|
|
|
|
2025-02-23 16:38:56 +01:00
|
|
|
from PyQt6.QtCore import Qt, pyqtSignal
|
|
|
|
from PyQt6.QtGui import QDropEvent, QIcon
|
2025-02-22 18:25:17 +01:00
|
|
|
from PyQt6.QtWidgets import QTreeWidget, QAbstractItemView
|
2025-01-25 22:41:29 +01:00
|
|
|
|
2025-01-25 17:21:43 +01:00
|
|
|
from .track import TrackItem
|
2024-12-28 20:41:18 +01:00
|
|
|
|
|
|
|
|
|
|
|
class PlaylistView(QTreeWidget):
|
2025-01-25 11:58:34 +01:00
|
|
|
itemDropped = pyqtSignal(QTreeWidget, list)
|
|
|
|
|
2025-02-22 18:25:17 +01:00
|
|
|
playing_mark = QIcon.fromTheme(QIcon.ThemeIcon.MediaPlaybackStart)
|
2025-02-02 16:08:25 +01:00
|
|
|
|
2025-02-04 14:43:08 +01:00
|
|
|
def __init__(self, playlist, dock, parent=None):
|
2025-01-25 11:58:34 +01:00
|
|
|
super().__init__(parent)
|
|
|
|
|
|
|
|
self.playlist = playlist
|
2025-02-04 14:43:08 +01:00
|
|
|
self.library_dock = dock
|
|
|
|
|
2025-01-25 17:21:43 +01:00
|
|
|
self.app = playlist.app
|
|
|
|
|
2025-02-23 16:38:56 +01:00
|
|
|
self.header = self.header()
|
|
|
|
self.header.setSectionsClickable(True)
|
|
|
|
|
2025-02-04 14:43:08 +01:00
|
|
|
playlist.views[id(dock)] = self
|
2025-01-25 11:58:34 +01:00
|
|
|
|
|
|
|
self.setSelectionMode(QAbstractItemView.SelectionMode.ExtendedSelection)
|
2024-12-28 20:41:18 +01:00
|
|
|
|
2024-12-29 18:55:55 +01:00
|
|
|
self.setColumnCount(4)
|
2024-12-28 20:41:18 +01:00
|
|
|
|
|
|
|
headers = [
|
|
|
|
"#",
|
|
|
|
"Title",
|
|
|
|
"Artist",
|
2024-12-29 15:10:51 +01:00
|
|
|
"Album",
|
|
|
|
"# Custom Sorting"
|
2024-12-28 20:41:18 +01:00
|
|
|
]
|
|
|
|
|
2025-01-25 11:58:34 +01:00
|
|
|
self.setHeaderLabels(headers)
|
|
|
|
|
2025-01-25 17:21:43 +01:00
|
|
|
self.itemActivated.connect(self.on_track_activation)
|
2025-02-23 16:38:56 +01:00
|
|
|
self.header.sectionClicked.connect(self.on_header_click)
|
2025-01-25 17:21:43 +01:00
|
|
|
|
2025-02-23 16:38:56 +01:00
|
|
|
def on_header_click(self, section_index: int):
|
|
|
|
if section_index == 0: # this would just invert the current sorting
|
|
|
|
return
|
2025-01-25 18:04:46 +01:00
|
|
|
|
2025-02-23 16:38:56 +01:00
|
|
|
sorting = self.playlist.sorting
|
|
|
|
last_sort_section_index, order = sorting[4]
|
2025-01-25 18:04:46 +01:00
|
|
|
|
2025-02-23 16:38:56 +01:00
|
|
|
if last_sort_section_index == section_index:
|
|
|
|
order = not order # invert order
|
|
|
|
|
|
|
|
self.playlist.sorting[4] = (section_index, order) # set sorting
|
|
|
|
|
|
|
|
# convert True/False to Qt.SortOrder.AscendingOrder/Qt.SortOrder.DescendingOrder
|
|
|
|
qorder = Qt.SortOrder.AscendingOrder if order else Qt.SortOrder.DescendingOrder
|
|
|
|
|
|
|
|
self.header.setSortIndicator(section_index, qorder)
|
|
|
|
|
|
|
|
else:
|
|
|
|
del sorting[0] # remove first sort
|
|
|
|
sorting.append((section_index, True)) # last sort is this section index, ascending
|
2025-01-25 18:04:46 +01:00
|
|
|
|
2025-02-23 16:38:56 +01:00
|
|
|
self.header.setSortIndicator(section_index, Qt.SortOrder.AscendingOrder)
|
2025-01-26 16:49:09 +01:00
|
|
|
|
2025-02-23 16:38:56 +01:00
|
|
|
self.sort()
|
2025-01-25 18:04:46 +01:00
|
|
|
|
2025-02-23 16:38:56 +01:00
|
|
|
def sort(self):
|
|
|
|
for index, order in self.playlist.sorting:
|
|
|
|
# convert True/False to Qt.SortOrder.AscendingOrder/Qt.SortOrder.DescendingOrder
|
|
|
|
qorder = Qt.SortOrder.AscendingOrder if order else Qt.SortOrder.DescendingOrder
|
2025-01-25 18:04:46 +01:00
|
|
|
|
2025-02-23 16:38:56 +01:00
|
|
|
self.sortItems(index, qorder)
|
|
|
|
|
|
|
|
self.on_sort()
|
|
|
|
|
|
|
|
def on_sort(self, user_sort: bool=False):
|
|
|
|
num_tracks = self.topLevelItemCount()
|
|
|
|
|
|
|
|
i = 0
|
|
|
|
|
|
|
|
while i < num_tracks:
|
|
|
|
track = self.topLevelItem(i)
|
2025-01-26 16:49:09 +01:00
|
|
|
|
2025-01-25 18:04:46 +01:00
|
|
|
i += 1
|
|
|
|
|
2025-02-23 16:38:56 +01:00
|
|
|
track.setText(0, str(i)) # 0 = index
|
|
|
|
|
|
|
|
if user_sort:
|
|
|
|
track.setText(4, str(i)) # 4 = user sort index
|
|
|
|
|
|
|
|
self.playlist.sync(self, user_sort) # sync playlist to this view
|
2025-01-25 11:58:34 +01:00
|
|
|
|
|
|
|
def dropEvent(self, event: QDropEvent):
|
2025-01-25 22:41:29 +01:00
|
|
|
# receive items that were dropped and create new items from its tracks (new items bc. widgets can only have
|
|
|
|
# one parent)
|
|
|
|
if event.source() == self:
|
|
|
|
items = self.selectedItems() # dragged items are always selected items
|
|
|
|
|
|
|
|
self.itemDropped.emit(self, items)
|
|
|
|
|
|
|
|
else:
|
|
|
|
items = self.app.gui.dropped
|
|
|
|
|
|
|
|
i = 0
|
|
|
|
|
|
|
|
for item in items:
|
|
|
|
track = item.track
|
2025-01-25 11:58:34 +01:00
|
|
|
|
2025-01-25 22:59:19 +01:00
|
|
|
self.playlist.tracks.append(track)
|
|
|
|
|
2025-01-25 22:41:29 +01:00
|
|
|
track_item = TrackItem(track, i, self)
|
|
|
|
|
|
|
|
i += 1
|
2025-01-25 11:58:34 +01:00
|
|
|
|
|
|
|
super().dropEvent(event)
|
|
|
|
|
2025-01-25 22:41:29 +01:00
|
|
|
event.accept()
|
|
|
|
|
2025-02-23 16:38:56 +01:00
|
|
|
self.on_sort(True)
|
2025-01-25 22:59:19 +01:00
|
|
|
|
2025-01-25 22:41:29 +01:00
|
|
|
def dragEnterEvent(self, event):
|
|
|
|
# store dragged items in gui.dropped, so the other playlist can receive it
|
|
|
|
if event.source() == self:
|
|
|
|
items = self.selectedItems()
|
|
|
|
|
|
|
|
self.app.gui.dropped = items
|
|
|
|
|
|
|
|
super().dragEnterEvent(event)
|
2025-01-25 18:04:46 +01:00
|
|
|
|
2025-01-25 11:58:34 +01:00
|
|
|
event.accept()
|
|
|
|
|
2025-01-25 17:21:43 +01:00
|
|
|
def load_tracks(self):
|
2025-01-25 18:04:46 +01:00
|
|
|
i = 0
|
|
|
|
|
2025-01-25 17:21:43 +01:00
|
|
|
for track in self.playlist.tracks:
|
2025-01-25 18:04:46 +01:00
|
|
|
track_item = TrackItem(track, i, self)
|
|
|
|
|
|
|
|
i += 1
|
2025-01-25 17:21:43 +01:00
|
|
|
|
|
|
|
def on_track_activation(self, item, column):
|
2025-01-25 22:59:19 +01:00
|
|
|
if not self.app.player.current_playlist == self.playlist:
|
|
|
|
self.app.player.current_playlist = self.playlist
|
|
|
|
|
2025-01-25 17:21:43 +01:00
|
|
|
index = self.indexOfTopLevelItem(item)
|
|
|
|
self.app.player.play_track_in_playlist(index)
|
|
|
|
|
|
|
|
def on_track_change(self, previous_track, track):
|
2025-01-27 14:30:45 +01:00
|
|
|
# unmark the previous track and playlist and mark the current track and playlist as playing
|
|
|
|
|
|
|
|
playlist_tabs = self.parent().parent()
|
|
|
|
index = playlist_tabs.indexOf(self) # tab index of this playlist
|
|
|
|
|
2025-01-25 17:21:43 +01:00
|
|
|
if previous_track:
|
2025-01-27 14:30:45 +01:00
|
|
|
# unmark all playlists by looping through the tabs
|
|
|
|
for i in range(playlist_tabs.count()):
|
|
|
|
playlist_tabs.setTabIcon(i, QIcon(None))
|
|
|
|
|
|
|
|
# unmark the previous track in all playlists
|
2025-01-26 16:49:09 +01:00
|
|
|
for item in previous_track.items:
|
2025-02-22 18:25:17 +01:00
|
|
|
item.unmark()
|
2025-01-25 17:21:43 +01:00
|
|
|
|
|
|
|
if track:
|
2025-01-27 14:30:45 +01:00
|
|
|
playlist_tabs.setTabIcon(index, self.playing_mark) # mark this playlist
|
|
|
|
|
|
|
|
# mark the current track in this playlist
|
2025-01-26 16:49:09 +01:00
|
|
|
item = self.topLevelItem(self.app.player.current_playlist.current_track_index)
|
2025-02-22 18:25:17 +01:00
|
|
|
item.mark()
|
2025-01-26 13:51:31 +01:00
|
|
|
|
|
|
|
def append_track(self, track):
|
|
|
|
TrackItem(track, self.topLevelItemCount() - 1, self)
|
2025-01-25 17:21:43 +01:00
|
|
|
|