From 683e76f9a03f12a17d14a625ab69a14724753e3d Mon Sep 17 00:00:00 2001 From: Keith Edmunds Date: Sat, 24 Dec 2022 20:24:27 +0000 Subject: [PATCH] Update database correctly when tabs are closed --- app/models.py | 11 ++++++++++- app/musicmuster.py | 20 +++++++++++++++++--- app/playlists.py | 10 ---------- 3 files changed, 27 insertions(+), 14 deletions(-) diff --git a/app/models.py b/app/models.py index 9d40edf..e8ddbd5 100644 --- a/app/models.py +++ b/app/models.py @@ -2,7 +2,7 @@ import os.path import re -import stackprinter +import stackprinter # type: ignore from dbconfig import Session @@ -263,8 +263,17 @@ class Playlists(Base): def close(self, session: Session) -> None: """Mark playlist as unloaded""" + # Closing this tab will mean all higher-number tabs have moved + # down by one + closed_idx = self.tab self.tab = None + session.execute( + update(Playlists) + .where(Playlists.tab > closed_idx) + .values(tab=Playlists.tab - 1) + ) + @classmethod def create_playlist_from_template(cls, session: Session, diff --git a/app/musicmuster.py b/app/musicmuster.py index 249fab8..d2f9c2a 100755 --- a/app/musicmuster.py +++ b/app/musicmuster.py @@ -358,7 +358,7 @@ class Window(QMainWindow, Ui_MainWindow): def close_tab(self, tab_index: int) -> None: """ - Close active playlist tab unless it holds the curren or next track. + Close playlist tab unless it holds the current or next track. Called from close_playlist_tab() or by clicking close button on tab. """ @@ -375,6 +375,12 @@ class Window(QMainWindow, Ui_MainWindow): "Can't close next track playlist", 5000) return + # Record playlist as closed and update remaining playlist tabs + with Session() as session: + playlist_id = self.tabPlaylist.widget(tab_index).playlist_id + playlist = session.get(Playlists, playlist_id) + playlist.close(session) + # Close playlist and remove tab self.tabPlaylist.widget(tab_index).close() self.tabPlaylist.removeTab(tab_index) @@ -420,8 +426,7 @@ class Window(QMainWindow, Ui_MainWindow): self.btnStop.clicked.connect(self.stop) self.hdrCurrentTrack.clicked.connect(self.show_current) self.hdrNextTrack.clicked.connect(self.show_next) - self.tabPlaylist.currentChanged.connect( - lambda: self.tabPlaylist.currentWidget().tab_visible()) + self.tabPlaylist.currentChanged.connect(self.tab_change) self.tabPlaylist.tabCloseRequested.connect(self.close_tab) self.tabBar = self.tabPlaylist.tabBar() self.tabBar.tabMoved.connect(self.move_tab) @@ -1207,6 +1212,15 @@ class Window(QMainWindow, Ui_MainWindow): # Run end-of-track actions self.end_of_track_actions() + def tab_change(self): + """Called when active tab changed""" + + try: + self.tabPlaylist.currentWidget().tab_visible() + except AttributeError: + # May also be called when last tab is closed + pass + def this_is_the_next_track(self, session: Session, playlist_tab: PlaylistTab, track: Tracks) -> None: diff --git a/app/playlists.py b/app/playlists.py index 1525207..391feee 100644 --- a/app/playlists.py +++ b/app/playlists.py @@ -203,16 +203,6 @@ class PlaylistTab(QTableWidget): # ########## Events other than cell editing ########## - def closeEvent(self, event) -> None: - """Handle closing playist tab""" - - with Session() as session: - # Record playlist as closed - playlist = session.get(Playlists, self.playlist_id) - playlist.close(session) - - event.accept() - def dropEvent(self, event: QDropEvent) -> None: """ Handle drag/drop of rows