Implement active_tab and active_model

This commit is contained in:
Keith Edmunds 2023-10-30 16:39:02 +00:00
parent 3832d9300c
commit e4b986fd2e

View File

@ -364,9 +364,8 @@ class Window(QMainWindow, Ui_MainWindow):
self.widgetFadeVolume.setBackground(Config.FADE_CURVE_BACKGROUND) self.widgetFadeVolume.setBackground(Config.FADE_CURVE_BACKGROUND)
FadeCurve.GraphWidget = self.widgetFadeVolume FadeCurve.GraphWidget = self.widgetFadeVolume
self.visible_playlist_tab: Callable[ self.active_tab = lambda: self.tabPlaylist.currentWidget()
[], PlaylistTab self.active_model = lambda: self.tabPlaylist.currentWidget().model()
] = self.tabPlaylist.currentWidget
self.load_last_playlists() self.load_last_playlists()
if Config.CARTS_HIDE: if Config.CARTS_HIDE:
@ -544,8 +543,8 @@ class Window(QMainWindow, Ui_MainWindow):
"""Clear selected row""" """Clear selected row"""
# Unselect any selected rows # Unselect any selected rows
if self.visible_playlist_tab(): if self.active_tab():
self.visible_playlist_tab().clear_selection() self.active_tab().clear_selection()
# Clear the search bar # Clear the search bar
self.search_playlist_clear() self.search_playlist_clear()
@ -746,14 +745,14 @@ class Window(QMainWindow, Ui_MainWindow):
with Session() as session: with Session() as session:
# Save the selected PlaylistRows items ready for a later # Save the selected PlaylistRows items ready for a later
# paste # paste
self.selected_plrs = self.visible_playlist_tab().get_selected_playlistrows( self.selected_plrs = self.active_tab().get_selected_playlistrows(
session session
) )
def debug(self): def debug(self):
"""Invoke debugger""" """Invoke debugger"""
visible_playlist_id = self.visible_playlist_tab().playlist_id visible_playlist_id = self.active_tab().playlist_id
print(f"Active playlist id={visible_playlist_id}") print(f"Active playlist id={visible_playlist_id}")
import ipdb # type: ignore import ipdb # type: ignore
@ -765,7 +764,7 @@ class Window(QMainWindow, Ui_MainWindow):
""" """
with Session() as session: with Session() as session:
playlist_id = self.visible_playlist_tab().playlist_id playlist_id = self.active_tab().playlist_id
playlist = session.get(Playlists, playlist_id) playlist = session.get(Playlists, playlist_id)
if playlist: if playlist:
if helpers.ask_yes_no( if helpers.ask_yes_no(
@ -880,10 +879,10 @@ class Window(QMainWindow, Ui_MainWindow):
def export_playlist_tab(self) -> None: def export_playlist_tab(self) -> None:
"""Export the current playlist to an m3u file""" """Export the current playlist to an m3u file"""
if not self.visible_playlist_tab(): if not self.active_tab():
return return
playlist_id = self.visible_playlist_tab().playlist_id playlist_id = self.active_tab().playlist_id
with Session() as session: with Session() as session:
# Get output filename # Get output filename
@ -961,7 +960,7 @@ class Window(QMainWindow, Ui_MainWindow):
self.btnHidePlayed.setText("Show played") self.btnHidePlayed.setText("Show played")
# Update displayed playlist # Update displayed playlist
self.visible_playlist_tab().hide_or_show_played_tracks() self.active_tab().hide_or_show_played_tracks()
def import_track(self) -> None: def import_track(self) -> None:
"""Import track file""" """Import track file"""
@ -1011,9 +1010,9 @@ class Window(QMainWindow, Ui_MainWindow):
# Import in separate thread # Import in separate thread
self.import_thread = QThread() self.import_thread = QThread()
self.worker = ImportTrack( self.worker = ImportTrack(
self.visible_playlist_tab(), self.active_tab(),
new_tracks, new_tracks,
self.visible_playlist_tab().get_new_row_number(), self.active_tab().get_new_row_number(),
) )
self.worker.moveToThread(self.import_thread) self.worker.moveToThread(self.import_thread)
self.import_thread.started.connect(self.worker.run) self.import_thread.started.connect(self.worker.run)
@ -1040,7 +1039,7 @@ class Window(QMainWindow, Ui_MainWindow):
"""Show dialog box to enter header text and add to playlist""" """Show dialog box to enter header text and add to playlist"""
try: try:
model = cast(PlaylistModel, self.visible_playlist_tab().model()) model = cast(PlaylistModel, self.active_tab().model())
if model is None: if model is None:
return return
except AttributeError: except AttributeError:
@ -1055,7 +1054,7 @@ class Window(QMainWindow, Ui_MainWindow):
ok = dlg.exec() ok = dlg.exec()
if ok: if ok:
model.insert_header_row( model.insert_header_row(
self.visible_playlist_tab().get_selected_row_number(), self.active_tab().get_selected_row_number(),
dlg.textValue() dlg.textValue()
) )
@ -1107,7 +1106,7 @@ class Window(QMainWindow, Ui_MainWindow):
# Identify destination playlist # Identify destination playlist
playlists = [] playlists = []
visible_tab = self.visible_playlist_tab() visible_tab = self.active_tab()
source_playlist_id = visible_tab.playlist_id source_playlist_id = visible_tab.playlist_id
for playlist in Playlists.get_all(session): for playlist in Playlists.get_all(session):
@ -1160,7 +1159,7 @@ class Window(QMainWindow, Ui_MainWindow):
""" """
with Session() as session: with Session() as session:
selected_plrs = self.visible_playlist_tab().get_selected_playlistrows( selected_plrs = self.active_tab().get_selected_playlistrows(
session session
) )
if not selected_plrs: if not selected_plrs:
@ -1179,7 +1178,7 @@ class Window(QMainWindow, Ui_MainWindow):
Move unplayed rows to another playlist Move unplayed rows to another playlist
""" """
playlist_id = self.visible_playlist_tab().playlist_id playlist_id = self.active_tab().playlist_id
with Session() as session: with Session() as session:
unplayed_plrs = PlaylistRows.get_unplayed_rows(session, playlist_id) unplayed_plrs = PlaylistRows.get_unplayed_rows(session, playlist_id)
if helpers.ask_yes_no( if helpers.ask_yes_no(
@ -1234,9 +1233,9 @@ class Window(QMainWindow, Ui_MainWindow):
if not self.selected_plrs: if not self.selected_plrs:
return return
playlist_tab = self.visible_playlist_tab() playlist_tab = self.active_tab()
dst_playlist_id = playlist_tab.playlist_id dst_playlist_id = playlist_tab.playlist_id
dst_row = self.visible_playlist_tab().get_new_row_number() dst_row = self.active_tab().get_new_row_number()
with Session() as session: with Session() as session:
# Create space in destination playlist # Create space in destination playlist
@ -1263,7 +1262,7 @@ class Window(QMainWindow, Ui_MainWindow):
session.flush() session.flush()
# Update display # Update display
self.visible_playlist_tab().populate_display( self.active_tab().populate_display(
session, dst_playlist_id, scroll_to_top=False session, dst_playlist_id, scroll_to_top=False
) )
@ -1380,7 +1379,7 @@ class Window(QMainWindow, Ui_MainWindow):
if self.btnPreview.isChecked(): if self.btnPreview.isChecked():
# Get track path for first selected track if there is one # Get track path for first selected track if there is one
track_path = self.visible_playlist_tab().get_selected_row_track_path() track_path = self.active_tab().get_selected_row_track_path()
if not track_path: if not track_path:
# Otherwise get path to next track to play # Otherwise get path to next track to play
track_path = self.next_track.path track_path = self.next_track.path
@ -1399,7 +1398,7 @@ class Window(QMainWindow, Ui_MainWindow):
""" """
with Session() as session: with Session() as session:
playlist_id = self.visible_playlist_tab().playlist_id playlist_id = self.active_tab().playlist_id
playlist = session.get(Playlists, playlist_id) playlist = session.get(Playlists, playlist_id)
if playlist: if playlist:
new_name = self.solicit_playlist_name(playlist.name) new_name = self.solicit_playlist_name(playlist.name)
@ -1478,7 +1477,7 @@ class Window(QMainWindow, Ui_MainWindow):
self, "Duplicate template", "Template name already in use" self, "Duplicate template", "Template name already in use"
) )
Playlists.save_as_template( Playlists.save_as_template(
session, self.visible_playlist_tab().playlist_id, template_name session, self.active_tab().playlist_id, template_name
) )
helpers.show_OK(self, "Template", "Template saved") helpers.show_OK(self, "Template", "Template saved")
@ -1497,7 +1496,7 @@ class Window(QMainWindow, Ui_MainWindow):
"""Tidy up and reset search bar""" """Tidy up and reset search bar"""
# Clear the search text # Clear the search text
self.visible_playlist_tab().set_search("") self.active_tab().set_search("")
# Clean up search bar # Clean up search bar
self.txtSearch.setText("") self.txtSearch.setText("")
self.txtSearch.setHidden(True) self.txtSearch.setHidden(True)
@ -1505,7 +1504,7 @@ class Window(QMainWindow, Ui_MainWindow):
def search_playlist_return(self) -> None: def search_playlist_return(self) -> None:
"""Initiate search when return pressed""" """Initiate search when return pressed"""
self.visible_playlist_tab().set_search(self.txtSearch.text()) self.active_tab().set_search(self.txtSearch.text())
self.enable_play_next_controls() self.enable_play_next_controls()
def select_duplicate_rows(self) -> None: def select_duplicate_rows(self) -> None:
@ -1516,7 +1515,7 @@ class Window(QMainWindow, Ui_MainWindow):
If there a track is selected on three or more rows, only the last one is selected. If there a track is selected on three or more rows, only the last one is selected.
""" """
visible_playlist_id = self.visible_playlist_tab().playlist_id visible_playlist_id = self.active_tab().playlist_id
# Get row number of duplicate rows # Get row number of duplicate rows
sql = text( sql = text(
f""" f"""
@ -1532,7 +1531,7 @@ class Window(QMainWindow, Ui_MainWindow):
with Session() as session: with Session() as session:
row_numbers = [int(a) for a in session.execute(sql).scalars().all()] row_numbers = [int(a) for a in session.execute(sql).scalars().all()]
if row_numbers: if row_numbers:
self.visible_playlist_tab().select_rows(row_numbers) self.active_tab().select_rows(row_numbers)
self.statusbar.showMessage( self.statusbar.showMessage(
f"{len(row_numbers)} duplicate rows selected", 10000 f"{len(row_numbers)} duplicate rows selected", 10000
) )
@ -1540,12 +1539,12 @@ class Window(QMainWindow, Ui_MainWindow):
def select_next_row(self) -> None: def select_next_row(self) -> None:
"""Select next or first row in playlist""" """Select next or first row in playlist"""
self.visible_playlist_tab().select_next_row() self.active_tab().select_next_row()
def select_previous_row(self) -> None: def select_previous_row(self) -> None:
"""Select previous or first row in playlist""" """Select previous or first row in playlist"""
self.visible_playlist_tab().select_previous_row() self.active_tab().select_previous_row()
def set_main_window_size(self) -> None: def set_main_window_size(self) -> None:
"""Set size of window from database""" """Set size of window from database"""
@ -1579,14 +1578,14 @@ class Window(QMainWindow, Ui_MainWindow):
def show_current(self) -> None: def show_current(self) -> None:
"""Scroll to show current track""" """Scroll to show current track"""
if self.current_track.playlist_tab != self.visible_playlist_tab(): if self.current_track.playlist_tab != self.active_tab():
self.tabPlaylist.setCurrentWidget(self.current_track.playlist_tab) self.tabPlaylist.setCurrentWidget(self.current_track.playlist_tab)
self.tabPlaylist.currentWidget().scroll_current_to_top() self.tabPlaylist.currentWidget().scroll_current_to_top()
def show_next(self) -> None: def show_next(self) -> None:
"""Scroll to show next track""" """Scroll to show next track"""
if self.next_track.playlist_tab != self.visible_playlist_tab(): if self.next_track.playlist_tab != self.active_tab():
self.tabPlaylist.setCurrentWidget(self.next_track.playlist_tab) self.tabPlaylist.setCurrentWidget(self.next_track.playlist_tab)
self.tabPlaylist.currentWidget().scroll_next_to_top() self.tabPlaylist.currentWidget().scroll_next_to_top()
@ -1660,7 +1659,7 @@ class Window(QMainWindow, Ui_MainWindow):
Set currently-selected row on visible playlist tab as next track Set currently-selected row on visible playlist tab as next track
""" """
playlist_tab = self.visible_playlist_tab() playlist_tab = self.active_tab()
selected_plr_ids = playlist_tab.get_selected_playlistrow_ids() selected_plr_ids = playlist_tab.get_selected_playlistrow_ids()
if len(selected_plr_ids) != 1: if len(selected_plr_ids) != 1:
log.error(f"set_next_track:_from_mm {selected_plr_ids=}") log.error(f"set_next_track:_from_mm {selected_plr_ids=}")
@ -2007,11 +2006,11 @@ class DbDialog(QDialog):
return return
if track: if track:
self.musicmuster.visible_playlist_tab().insert_track( self.musicmuster.active_tab().insert_track(
self.session, track, note self.session, track, note
) )
else: else:
self.musicmuster.visible_playlist_tab().insert_header(self.session, note) self.musicmuster.active_tab().insert_header(self.session, note)
self.ui.txtNote.clear() self.ui.txtNote.clear()
self.select_searchtext() self.select_searchtext()