delete playlist implemented, create playlist functionality moved inside playlistpane class
This commit is contained in:
parent
8684db497f
commit
b533b39f0c
@ -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
|
from PyQt5.QtCore import pyqtSignal, Qt, QPoint
|
||||||
import DBA
|
import DBA
|
||||||
from logging import debug
|
from logging import debug
|
||||||
|
|
||||||
|
from components import CreatePlaylistWindow
|
||||||
|
|
||||||
class PlaylistWidgetItem(QTreeWidgetItem):
|
class PlaylistWidgetItem(QTreeWidgetItem):
|
||||||
def __init__(self, parent, id, name):
|
def __init__(self, parent, id, name):
|
||||||
@ -16,51 +17,78 @@ class PlaylistWidgetItem(QTreeWidgetItem):
|
|||||||
|
|
||||||
class PlaylistsPane(QTreeWidget):
|
class PlaylistsPane(QTreeWidget):
|
||||||
playlistChoiceSignal = pyqtSignal(int)
|
playlistChoiceSignal = pyqtSignal(int)
|
||||||
|
playlistCreatedSignal = pyqtSignal()
|
||||||
allSongsSignal = pyqtSignal()
|
allSongsSignal = pyqtSignal()
|
||||||
|
|
||||||
def __init__(self: QTreeWidget, parent=None):
|
def __init__(self: QTreeWidget, parent=None):
|
||||||
super().__init__(parent)
|
super().__init__(parent)
|
||||||
self._library_root = QTreeWidgetItem(["Library"])
|
self._library_root = QTreeWidgetItem(["Library"])
|
||||||
|
self._playlists_root: QTreeWidgetItem = QTreeWidgetItem(["Playlists"])
|
||||||
self.addTopLevelItem(self._library_root)
|
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)
|
self.addTopLevelItem(self._playlists_root)
|
||||||
with DBA.DBAccess() as db:
|
self.reload_playlists()
|
||||||
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.setContextMenuPolicy(Qt.ContextMenuPolicy.CustomContextMenu)
|
self.setContextMenuPolicy(Qt.ContextMenuPolicy.CustomContextMenu)
|
||||||
self.customContextMenuRequested.connect(self.showContextMenu)
|
self.customContextMenuRequested.connect(self.showContextMenu)
|
||||||
self.currentItemChanged.connect(self.playlist_clicked)
|
self.currentItemChanged.connect(self.playlist_clicked)
|
||||||
self.playlist_db_id_choice: int | None = None
|
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):
|
def showContextMenu(self, position: QPoint):
|
||||||
"""Right-click context menu"""
|
"""Right-click context menu"""
|
||||||
if self.playlist_db_id_choice is None:
|
|
||||||
return
|
|
||||||
# dont delete or rename the root nodes
|
|
||||||
menu = QMenu(self)
|
menu = QMenu(self)
|
||||||
rename_action = QAction("Rename", self)
|
if self.playlist_db_id_choice is not None:
|
||||||
delete_action = QAction("Delete", self)
|
# only allow delete/rename non-root nodes
|
||||||
rename_action.triggered.connect(self.rename_playlist)
|
rename_action = QAction("Rename", self)
|
||||||
delete_action.triggered.connect(self.delete_playlist)
|
delete_action = QAction("Delete", self)
|
||||||
menu.addAction(rename_action)
|
rename_action.triggered.connect(self.rename_playlist)
|
||||||
menu.addAction(delete_action)
|
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
|
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):
|
def rename_playlist(self, *args):
|
||||||
# TODO: implement this
|
# TODO: implement this
|
||||||
pass
|
pass
|
||||||
|
|
||||||
def delete_playlist(self, *args):
|
def delete_playlist(self, *args):
|
||||||
# TODO: implement this
|
"""Deletes a playlist"""
|
||||||
pass
|
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):
|
def playlist_clicked(self, item):
|
||||||
"""Specific playlist index was clicked"""
|
"""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
|
# I have no idea why this has to be in its own function, but it does
|
||||||
# or else it doesn't work
|
# or else it doesn't work
|
||||||
self.allSongsSignal.emit()
|
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)
|
|
||||||
|
|||||||
3
main.py
3
main.py
@ -133,7 +133,6 @@ class Worker(QRunnable):
|
|||||||
|
|
||||||
|
|
||||||
class ApplicationWindow(QMainWindow, Ui_MainWindow):
|
class ApplicationWindow(QMainWindow, Ui_MainWindow):
|
||||||
playlistCreatedSignal = pyqtSignal()
|
|
||||||
reloadConfigSignal = pyqtSignal()
|
reloadConfigSignal = pyqtSignal()
|
||||||
reloadDatabaseSignal = pyqtSignal()
|
reloadDatabaseSignal = pyqtSignal()
|
||||||
|
|
||||||
@ -244,7 +243,7 @@ class ApplicationWindow(QMainWindow, Ui_MainWindow):
|
|||||||
|
|
||||||
# FILE MENU
|
# FILE MENU
|
||||||
self.actionOpenFiles.triggered.connect(self.open_files) # Open files window
|
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)
|
self.actionExportPlaylist.triggered.connect(self.export_playlist)
|
||||||
|
|
||||||
# EDIT MENU
|
# EDIT MENU
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user