diff --git a/bak.main2 b/bak.main2 index 23a67e4..1cfdc23 100644 --- a/bak.main2 +++ b/bak.main2 @@ -585,7 +585,7 @@ class MainWindow(QMainWindow): """Scans for new files in the configured library folder Refreshes the datagridview""" scan_for_music() - self.tableView.fetch_library() + self.tableView.load_music_table() def clear_database(self) -> None: """Clears all songs from the database""" @@ -598,7 +598,7 @@ class MainWindow(QMainWindow): ) if reply: delete_and_create_library_database() - self.tableView.fetch_library() + self.tableView.load_music_table() def delete_database(self) -> None: """Deletes the entire database""" @@ -611,7 +611,7 @@ class MainWindow(QMainWindow): ) if reply: initialize_db() - self.tableView.fetch_library() + self.tableView.load_music_table() def reinitialize_database(self) -> None: """Clears all tables in database and recreates""" @@ -624,7 +624,7 @@ class MainWindow(QMainWindow): ) if reply: initialize_db() - self.tableView.fetch_library() + self.tableView.load_music_table() def process_probe(self, buff) -> None: buff.startTime() diff --git a/components/MusicTable.py b/components/MusicTable.py index e99d5d2..c042228 100644 --- a/components/MusicTable.py +++ b/components/MusicTable.py @@ -81,7 +81,7 @@ class MusicTable(QTableView): self.doubleClicked.connect(self.set_current_song_filepath) self.enterKey.connect(self.set_current_song_filepath) self.deleteKey.connect(self.delete_songs) - self.fetch_library() + self.load_music_table() self.setup_keyboard_shortcuts() self.model.dataChanged.connect(self.on_cell_data_changed) # editing cells self.model.layoutChanged.connect(self.restore_scroll_position) @@ -137,7 +137,7 @@ class MusicTable(QTableView): model.removeRow(index) except Exception as e: logging.info(f"MusicTable.py delete_songs() failed | {e}") - self.fetch_library() + self.load_music_table() self.model.dataChanged.connect(self.on_cell_data_changed) def open_directory(self): @@ -316,7 +316,7 @@ class MusicTable(QTableView): print(f"Error moving file: {filepath} | {e}") # Draw the rest of the owl self.model.dataChanged.disconnect(self.on_cell_data_changed) - self.fetch_library() + self.load_music_table() self.model.dataChanged.connect(self.on_cell_data_changed) QMessageBox.information( self, "Reorganization complete", "Files successfully reorganized" @@ -328,7 +328,7 @@ class MusicTable(QTableView): self.set_current_song_filepath() self.playPauseSignal.emit() - def fetch_library(self): + def load_music_table(self, *playlist_id): """Initializes the tableview model""" self.vertical_scroll_position = ( self.verticalScrollBar().value() @@ -336,16 +336,37 @@ class MusicTable(QTableView): # temporarily disconnect the datachanged signal to avoid EVERY SONG getting triggered self.model.clear() self.model.setHorizontalHeaderLabels(self.table_headers) - # Fetch library data - try: - with DBA.DBAccess() as db: - data = db.query( - "SELECT id, title, artist, album, track_number, genre, codec, album_date, filepath FROM song;", - (), + if playlist_id: + playlist_id = playlist_id[0] + # Fetch playlist data + print( + f"MusicTable.py load_music_table() | fetching playlist data, playlist_id: {playlist_id}" + ) + try: + with DBA.DBAccess() as db: + data = db.query( + "SELECT s.id, s.title, s.artist, s.album, s.track_number, s.genre, s.codec, s.album_date, s.filepath FROM song s JOIN song_playlist sp ON s.id = sp.id WHERE sp.id = ?", + (playlist_id,), + ) + except Exception as e: + logging.warning( + f"MusicTable.py | load_music_table | Unhandled exception: {e}" ) - except Exception as e: - logging.warning(f"MusicTable.py | fetch_library | Unhandled exception: {e}") - return + return + else: + print("MusicTable.py load_music_table() | fetching library data") + # Fetch library data + try: + with DBA.DBAccess() as db: + data = db.query( + "SELECT id, title, artist, album, track_number, genre, codec, album_date, filepath FROM song;", + (), + ) + except Exception as e: + logging.warning( + f"MusicTable.py | load_music_table | Unhandled exception: {e}" + ) + return # Populate the model for row_data in data: id, *rest_of_data = row_data @@ -373,7 +394,7 @@ class MusicTable(QTableView): number_of_files_added = add_files_to_library(files) if number_of_files_added: self.model.dataChanged.disconnect(self.on_cell_data_changed) - self.fetch_library() + self.load_music_table() self.model.dataChanged.connect(self.on_cell_data_changed) def get_selected_rows(self) -> list[int]: diff --git a/components/LeftPane.py b/components/PlaylistsPane.py similarity index 82% rename from components/LeftPane.py rename to components/PlaylistsPane.py index 14d606f..d2ae7b9 100644 --- a/components/LeftPane.py +++ b/components/PlaylistsPane.py @@ -1,4 +1,5 @@ from PyQt5.QtWidgets import QListWidget, QTreeWidget, QTreeWidgetItem +from PyQt5.QtCore import pyqtSignal import DBA @@ -8,7 +9,9 @@ class PlaylistWidgetItem(QTreeWidgetItem): self.id = id -class LeftPane(QTreeWidget): +class PlaylistsPane(QTreeWidget): + playlistChoiceSignal = pyqtSignal(int) + def __init__(self: QTreeWidget, parent=None): super().__init__(parent) library_root = QTreeWidgetItem(["Library"]) @@ -25,10 +28,13 @@ class LeftPane(QTreeWidget): playlists_root.addChild(branch) self.currentItemChanged.connect(self.playlist_clicked) + self.playlist_db_id_choice: int | None = None def playlist_clicked(self, item): if isinstance(item, PlaylistWidgetItem): print(f"ID: {item.id}, name: {item.text(0)}") + self.playlist_db_id_choice = item.id + self.playlistChoiceSignal.emit(item.id) elif item.text(0).lower() == "all songs": self.all_songs_selected() diff --git a/components/__init__.py b/components/__init__.py index 55054ab..ed9e8d5 100644 --- a/components/__init__.py +++ b/components/__init__.py @@ -6,4 +6,4 @@ from .ErrorDialog import ErrorDialog from .LyricsWindow import LyricsWindow from .AddToPlaylistWindow import AddToPlaylistWindow from .CreatePlaylistWindow import CreatePlaylistWindow -from .LeftPane import LeftPane +from .PlaylistsPane import PlaylistsPane diff --git a/main.py b/main.py index c2961a3..1cbd76a 100644 --- a/main.py +++ b/main.py @@ -117,6 +117,11 @@ class ApplicationWindow(QMainWindow, Ui_MainWindow): self.on_play_clicked ) # Spacebar toggle play/pause signal + ## Playlist triggers + self.playlistTreeView.playlistChoiceSignal.connect( + self.tableView.load_music_table + ) + # albumGraphicsView self.albumGraphicsView.albumArtDropped.connect( self.set_album_art_for_selected_songs @@ -357,7 +362,7 @@ class ApplicationWindow(QMainWindow, Ui_MainWindow): """Scans for new files in the configured library folder Refreshes the datagridview""" scan_for_music() - self.tableView.fetch_library() + self.tableView.load_music_table() def clear_database(self) -> None: """Clears all songs from the database""" @@ -370,7 +375,7 @@ class ApplicationWindow(QMainWindow, Ui_MainWindow): ) if reply: delete_and_create_library_database() - self.tableView.fetch_library() + self.tableView.load_music_table() def delete_database(self) -> None: """Deletes the entire database""" @@ -383,7 +388,7 @@ class ApplicationWindow(QMainWindow, Ui_MainWindow): ) if reply: initialize_db() - self.tableView.fetch_library() + self.tableView.load_music_table() def reinitialize_database(self) -> None: """Clears all tables in database and recreates""" @@ -396,7 +401,7 @@ class ApplicationWindow(QMainWindow, Ui_MainWindow): ) if reply: initialize_db() - self.tableView.fetch_library() + self.tableView.load_music_table() def process_probe(self, buff) -> None: buff.startTime() diff --git a/ui.py b/ui.py index fa5b705..1e1c66f 100644 --- a/ui.py +++ b/ui.py @@ -2,7 +2,7 @@ # Form implementation generated from reading ui file 'ui.ui' # -# Created by: PyQt5 UI code generator 5.15.10 +# Created by: PyQt5 UI code generator 5.15.11 # # WARNING: Any manual changes made to this file will be lost when pyuic5 is # run again. Do not edit this file unless you know what you are doing. @@ -21,7 +21,8 @@ class Ui_MainWindow(object): self.verticalLayout_3 = QtWidgets.QVBoxLayout(self.centralwidget) self.verticalLayout_3.setObjectName("verticalLayout_3") self.verticalLayout = QtWidgets.QVBoxLayout() - self.verticalLayout.setContentsMargins(-1, -1, 10, -1) + self.verticalLayout.setContentsMargins(-1, -1, 0, -1) + self.verticalLayout.setSpacing(6) self.verticalLayout.setObjectName("verticalLayout") self.hLayoutHead = QtWidgets.QHBoxLayout() self.hLayoutHead.setObjectName("hLayoutHead") @@ -101,7 +102,7 @@ class Ui_MainWindow(object): self.hLayoutMusicTable = QtWidgets.QHBoxLayout() self.hLayoutMusicTable.setContentsMargins(0, -1, 0, -1) self.hLayoutMusicTable.setObjectName("hLayoutMusicTable") - self.playlistTreeView = LeftPane(self.centralwidget) + self.playlistTreeView = PlaylistsPane(self.centralwidget) self.playlistTreeView.setObjectName("playlistTreeView") self.hLayoutMusicTable.addWidget(self.playlistTreeView) self.tableView = MusicTable(self.centralwidget) @@ -129,6 +130,7 @@ class Ui_MainWindow(object): self.verticalLayout.addLayout(self.hLayoutMusicTable) self.verticalLayout_3.addLayout(self.verticalLayout) self.hLayoutControls = QtWidgets.QHBoxLayout() + self.hLayoutControls.setSpacing(6) self.hLayoutControls.setObjectName("hLayoutControls") self.previousButton = QtWidgets.QPushButton(self.centralwidget) font = QtGui.QFont() @@ -150,6 +152,7 @@ class Ui_MainWindow(object): self.hLayoutControls.addWidget(self.nextButton) self.verticalLayout_3.addLayout(self.hLayoutControls) self.hLayoutControls2 = QtWidgets.QHBoxLayout() + self.hLayoutControls2.setSpacing(6) self.hLayoutControls2.setObjectName("hLayoutControls2") self.volumeSlider = QtWidgets.QSlider(self.centralwidget) self.volumeSlider.setMaximum(100) @@ -226,5 +229,5 @@ class Ui_MainWindow(object): self.actionOpenFiles.setText(_translate("MainWindow", "Open file(s)")) self.actionDeleteDatabase.setText(_translate("MainWindow", "Delete Database")) self.actionNewPlaylist.setText(_translate("MainWindow", "New playlist")) -from components import AlbumArtGraphicsView, LeftPane, MusicTable +from components import AlbumArtGraphicsView, MusicTable, PlaylistsPane from pyqtgraph import PlotWidget diff --git a/ui.ui b/ui.ui index 0cb5076..3d3685d 100644 --- a/ui.ui +++ b/ui.ui @@ -173,7 +173,7 @@ 0 - + @@ -386,7 +386,7 @@
components
- LeftPane + PlaylistsPane QTreeView
components