From 7ed54f2babc954260f16cc3d8ef74eae9ce2605d Mon Sep 17 00:00:00 2001 From: Keith Edmunds Date: Fri, 8 Dec 2023 14:00:59 +0000 Subject: [PATCH] Fix issues saving/restoring active tab Fixes #212 --- app/models.py | 14 ++++++++++++++ app/musicmuster.py | 10 +++++++++- 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/app/models.py b/app/models.py index 6e1aa2b..7e51b39 100644 --- a/app/models.py +++ b/app/models.py @@ -239,6 +239,20 @@ class Playlists(Base): session.add(self) session.flush() + @staticmethod + def clear_tabs(session: scoped_session, playlist_ids: List[int]) -> None: + """ + Make all tab records NULL + """ + + session.execute( + update(Playlists) + .where( + (Playlists.id.in_(playlist_ids)) + ) + .values(tab=None) + ) + def close(self) -> None: """Mark playlist as unloaded""" diff --git a/app/musicmuster.py b/app/musicmuster.py index 7c99c32..1b11e02 100755 --- a/app/musicmuster.py +++ b/app/musicmuster.py @@ -875,15 +875,23 @@ class Window(QMainWindow, Ui_MainWindow): def load_last_playlists(self) -> None: """Load the playlists that were open when the last session closed""" + playlist_ids = [] with Session() as session: for playlist in Playlists.get_open(session): if playlist: _ = self.create_playlist_tab(playlist) + playlist_ids.append(playlist.id) # Set active tab record = Settings.get_int_settings(session, "active_tab") - if record.f_int and record.f_int >= 0: + if record.f_int is not None and record.f_int >= 0: self.tabPlaylist.setCurrentIndex(record.f_int) + # Tabs may move during use. Rather than track where tabs + # are, we record the tab index when we close the main + # window. To avoid possible duplicate tab entries, we null + # them all out now. + Playlists.clear_tabs(session, playlist_ids) + def lookup_row_in_songfacts(self) -> None: """ Display songfacts page for title in highlighted row