Wobuzz/wobuzz/library/library.py

88 lines
2.8 KiB
Python
Raw Permalink Normal View History

#!/usr/bin/python3
from PyQt6.QtCore import Qt
from PyQt6.QtGui import QIcon
from PyQt6.QtWidgets import QTabWidget, QTreeWidgetItem, QAbstractItemView
from ..player.playlist import Playlist
from ..ui.library_dock import LibraryDock
from ..ui.playlist import PlaylistView
class Library:
"""
Class that manages and syncs the library and library gui dock widgets.
"""
def __init__(self, app):
self.app = app
self.main_library_dock = LibraryDock()
self.library_docks = [self.main_library_dock]
self.temporary_playlist = Playlist(self.app, "Temporary Playlist")
self.playlists = [self.temporary_playlist]
self.play_icon = QIcon.fromTheme(QIcon.ThemeIcon.MediaPlaybackStart)
def create_playlist_views(self):
for library_dock in self.library_docks:
playlist_tabs: QTabWidget = library_dock.library.playlist_tabs
playlist_tabs.playlists = {}
for playlist in self.playlists:
playlist_view = self.create_playlist_view(playlist)
playlist_tabs.playlists[playlist.title] = playlist_view
playlist_tabs.addTab(playlist_view, playlist.title)
playlist_view.itemActivated.connect(self.on_track_activation)
def create_playlist_view(self, playlist: Playlist):
view = PlaylistView()
view.setDragDropMode(QAbstractItemView.DragDropMode.InternalMove)
num_tracks = len(playlist.tracks)
i = 0
while i < num_tracks:
track = playlist.tracks[i]
track_item = QTreeWidgetItem(view)
track_item.setFlags(
Qt.ItemFlag.ItemIsEnabled |
Qt.ItemFlag.ItemIsSelectable # |
# Qt.ItemFlag.ItemIsDragEnabled
)
track_item.track = track
track_item.setText(2, track.tags.title)
track_item.setText(3, track.tags.artist)
track_item.setText(4, track.tags.album)
track_item.setText(5, str(i + 1))
i += 1
return view
def on_track_activation(self, item, column):
index = item.treeWidget().indexOfTopLevelItem(item)
self.app.player.play_track_in_playlist(index)
def get_track_tree_item(self, track):
index = self.app.player.current_playlist.tracks.index(track)
playlist_view = self.main_library_dock.library.playlist_tabs.playlists[self.app.player.current_playlist.title]
item = playlist_view.topLevelItem(index)
return item
def on_track_change(self, previous_track, track):
if previous_track:
item = self.get_track_tree_item(previous_track)
item.setIcon(0, QIcon(None))
if track:
item = self.get_track_tree_item(track)
item.setIcon(0, self.play_icon)