Wobuzz/wobuzz/library/library.py

68 lines
2.1 KiB
Python

#!/usr/bin/python3
from PyQt6.QtCore import Qt
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]
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.itemDoubleClicked.connect(self.on_track_doubleclick)
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(1, track.tags.title)
track_item.setText(2, track.tags.artist)
track_item.setText(3, track.tags.album)
track_item.setText(4, str(i + 1))
i += 1
return view
def on_track_doubleclick(self, item, column):
index = item.treeWidget().indexOfTopLevelItem(item)
self.app.player.play_track_in_playlist(index)