From 96a9985099ab47b65743bfadbbb97c8c04af0528 Mon Sep 17 00:00:00 2001 From: EKNr1 Date: Tue, 28 Jan 2025 18:23:43 +0100 Subject: [PATCH] Implemented deletion of playlists. --- wobuzz/player/playlist.py | 23 ++++++++++++++++++--- wobuzz/ui/playlist_tabs/tab_context_menu.py | 13 +++++++++++- wobuzz/ui/playlist_tabs/tab_title.py | 7 ++----- 3 files changed, 34 insertions(+), 9 deletions(-) diff --git a/wobuzz/player/playlist.py b/wobuzz/player/playlist.py index 2265ebe..1160128 100644 --- a/wobuzz/player/playlist.py +++ b/wobuzz/player/playlist.py @@ -128,14 +128,31 @@ class Playlist: wbz.write(wbz_data) wbz.close() - def delete(self): - # remove from unique names so a new playlist can have the old name - self.app.utils.unique_names.remove(self.title) + def rename(self, title: str): + # remove from unique names so a new playlist can have the old name and delete old playlist. + path = f"{self.app.settings.library_path}/playlists/{self.title.replace(" ", "_")}.wbz.m3u" + path = os.path.expanduser(path) if os.path.exists(path): os.remove(os.path.expanduser(path)) + old_title = self.title + self.title = self.app.utils.unique_name(title, ignore=old_title) + + if not old_title == self.title: # remove only when the playlist actually has a different name + self.app.utils.unique_names.remove(old_title) + + def delete(self): + path = f"{self.app.settings.library_path}/playlists/{self.title.replace(" ", "_")}.wbz.m3u" + path = os.path.expanduser(path) + + if os.path.exists(path): + os.remove(os.path.expanduser(path)) + + self.app.utils.unique_names.remove(self.title) + self.app.library.playlists.remove(self) + def append_track(self, track): self.tracks.append(track) diff --git a/wobuzz/ui/playlist_tabs/tab_context_menu.py b/wobuzz/ui/playlist_tabs/tab_context_menu.py index 8ed9269..6e285a2 100644 --- a/wobuzz/ui/playlist_tabs/tab_context_menu.py +++ b/wobuzz/ui/playlist_tabs/tab_context_menu.py @@ -13,17 +13,28 @@ class PlaylistContextMenu(QMenu): self.playlist_title = None - self.rename_action = QAction("Rename") + self.title = self.addSection("Playlist Actions") + self.rename_action = QAction("Rename") self.addAction(self.rename_action) + self.delete_action = QAction("Delete") + self.addAction(self.delete_action) + self.rename_action.triggered.connect(self.rename) + self.delete_action.triggered.connect(self.delete) def exec(self, pos: QPoint, title): self.playlist_title = title + self.title.setText(title.text()) # set section title + super().exec(pos) def rename(self): self.playlist_title.setFocus() + def delete(self): + self.playlist_title.playlist_view.playlist.delete() + self.playlist_title.playlist_view.deleteLater() + diff --git a/wobuzz/ui/playlist_tabs/tab_title.py b/wobuzz/ui/playlist_tabs/tab_title.py index b471482..bcae3e5 100644 --- a/wobuzz/ui/playlist_tabs/tab_title.py +++ b/wobuzz/ui/playlist_tabs/tab_title.py @@ -35,10 +35,7 @@ class TabTitle(QLineEdit): def on_edit(self): self.clearFocus() - self.playlist_view.playlist.delete() + self.playlist_view.playlist.rename(self.text()) - name = self.app.utils.unique_name(self.text(), ignore=self.playlist_view.playlist.title) - self.setText(name) - - self.playlist_view.playlist.title = name + self.setText(self.playlist_view.playlist.title)