Update database correctly when tabs are closed

This commit is contained in:
Keith Edmunds 2022-12-24 20:24:27 +00:00
parent abd6ad0a64
commit 683e76f9a0
3 changed files with 27 additions and 14 deletions

View File

@ -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,

View File

@ -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:

View File

@ -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