diff --git a/components/PlaylistsPane.py b/components/PlaylistsPane.py index 885c74d..23c7753 100644 --- a/components/PlaylistsPane.py +++ b/components/PlaylistsPane.py @@ -1,8 +1,9 @@ -from PyQt5.QtWidgets import QAction, QListWidget, QMenu, QTreeWidget, QTreeWidgetItem +from PyQt5.QtWidgets import QAction, QListWidget, QMenu, QMessageBox, QTreeWidget, QTreeWidgetItem from PyQt5.QtCore import pyqtSignal, Qt, QPoint import DBA from logging import debug +from components import CreatePlaylistWindow class PlaylistWidgetItem(QTreeWidgetItem): def __init__(self, parent, id, name): @@ -16,51 +17,78 @@ class PlaylistWidgetItem(QTreeWidgetItem): class PlaylistsPane(QTreeWidget): playlistChoiceSignal = pyqtSignal(int) + playlistCreatedSignal = pyqtSignal() allSongsSignal = pyqtSignal() def __init__(self: QTreeWidget, parent=None): super().__init__(parent) self._library_root = QTreeWidgetItem(["Library"]) + self._playlists_root: QTreeWidgetItem = QTreeWidgetItem(["Playlists"]) self.addTopLevelItem(self._library_root) - # all_songs_branch = QTreeWidgetItem(["All Songs"]) - # library_root.addChild(all_songs_branch) - - self._playlists_root = QTreeWidgetItem(["Playlists"]) self.addTopLevelItem(self._playlists_root) - with DBA.DBAccess() as db: - playlists = db.query("SELECT id, name FROM playlist;", ()) - for playlist in playlists: - branch = PlaylistWidgetItem(self, playlist[0], playlist[1]) - self._playlists_root.addChild(branch) - - # library_root.setExpanded(True) - self._playlists_root.setExpanded(True) + self.reload_playlists() self.setContextMenuPolicy(Qt.ContextMenuPolicy.CustomContextMenu) self.customContextMenuRequested.connect(self.showContextMenu) self.currentItemChanged.connect(self.playlist_clicked) self.playlist_db_id_choice: int | None = None + def reload_playlists(self): + """ + Clears and reinitializes the playlists tree + each playlist is a branch/child of root node `Playlists` + """ + # take all children away + self._playlists_root.takeChildren() + # NOTE: implement user sorting by adding a column to playlist db table for 'rank' or something + with DBA.DBAccess() as db: + playlists = db.query("SELECT id, name FROM playlist ORDER BY date_created DESC LIMIT 1;", ()) + for playlist in playlists: + branch = PlaylistWidgetItem(self, playlist[0], playlist[1]) + self._playlists_root.addChild(branch) + + self._playlists_root.setExpanded(True) + def showContextMenu(self, position: QPoint): """Right-click context menu""" - if self.playlist_db_id_choice is None: - return - # dont delete or rename the root nodes menu = QMenu(self) - rename_action = QAction("Rename", self) - delete_action = QAction("Delete", self) - rename_action.triggered.connect(self.rename_playlist) - delete_action.triggered.connect(self.delete_playlist) - menu.addAction(rename_action) - menu.addAction(delete_action) + if self.playlist_db_id_choice is not None: + # only allow delete/rename non-root nodes + rename_action = QAction("Rename", self) + delete_action = QAction("Delete", self) + rename_action.triggered.connect(self.rename_playlist) + delete_action.triggered.connect(self.delete_playlist) + menu.addAction(rename_action) + menu.addAction(delete_action) + create_action = QAction("New playlist", self) + create_action.triggered.connect(self.create_playlist) + menu.addAction(create_action) menu.exec_(self.mapToGlobal(position)) # Show the menu + def create_playlist(self): + """Creates a database record for a playlist, given a name""" + window = CreatePlaylistWindow(self.playlistCreatedSignal) + window.playlistCreatedSignal.connect(self.reload_playlists) # type: ignore + window.exec_() + def rename_playlist(self, *args): # TODO: implement this pass def delete_playlist(self, *args): - # TODO: implement this - pass + """Deletes a playlist""" + reply = QMessageBox.question( + self, + "Confirmation", + "Really delete this playlist??", + QMessageBox.Yes | QMessageBox.No, + QMessageBox.Yes, + ) + if reply == QMessageBox.Yes: + with DBA.DBAccess() as db: + db.execute("DELETE FROM playlist WHERE id = ?;", (self.playlist_db_id_choice,)) + # reload + self.reload_playlists() + def playlist_clicked(self, item): """Specific playlist index was clicked""" @@ -77,12 +105,3 @@ class PlaylistsPane(QTreeWidget): # I have no idea why this has to be in its own function, but it does # or else it doesn't work self.allSongsSignal.emit() - - def add_latest_playlist_to_tree(self): - """Adds the most recently created playlist to the pane""" - with DBA.DBAccess() as db: - playlist = db.query( - "SELECT id, name FROM playlist ORDER BY date_created DESC LIMIT 1;", () - )[0] - branch = PlaylistWidgetItem(self, playlist[0], playlist[1]) - self.playlists_root.addChild(branch) diff --git a/main.py b/main.py index 0b925a9..1c23844 100644 --- a/main.py +++ b/main.py @@ -133,7 +133,6 @@ class Worker(QRunnable): class ApplicationWindow(QMainWindow, Ui_MainWindow): - playlistCreatedSignal = pyqtSignal() reloadConfigSignal = pyqtSignal() reloadDatabaseSignal = pyqtSignal() @@ -244,7 +243,7 @@ class ApplicationWindow(QMainWindow, Ui_MainWindow): # FILE MENU self.actionOpenFiles.triggered.connect(self.open_files) # Open files window - self.actionNewPlaylist.triggered.connect(self.create_playlist) + self.actionNewPlaylist.triggered.connect(self.playlistTreeView.create_playlist) self.actionExportPlaylist.triggered.connect(self.export_playlist) # EDIT MENU