From 9eac5caf09bce3324707e596042ee20e117a4e11 Mon Sep 17 00:00:00 2001 From: Keith Edmunds Date: Fri, 4 Jun 2021 12:37:37 +0100 Subject: [PATCH] Separate out sessions. Starts addressing issue #10 We were creating a session in the "Add from database" dialog, and then creating another session (with the first still active) in _repaint(). Separated out sessions; also don't call _save_playlist from _repaint as it doesn't really have any connection. --- app/musicmuster.py | 60 +++++++++++++++++++++++--------------------- app/playlists.py | 62 ++++++++++++++++++++++++---------------------- 2 files changed, 64 insertions(+), 58 deletions(-) diff --git a/app/musicmuster.py b/app/musicmuster.py index 441245a..bc0d1bd 100755 --- a/app/musicmuster.py +++ b/app/musicmuster.py @@ -74,7 +74,7 @@ class Window(QMainWindow, Ui_MainWindow): with Session() as session: for fname in dlg.selectedFiles(): track = add_path_to_db(session, fname) - self.visible_playlist()._add_to_playlist(track) + self.visible_playlist().add_to_playlist(session, track) def set_main_window_size(self): @@ -623,7 +623,6 @@ class Window(QMainWindow, Ui_MainWindow): class DbDialog(QDialog): def __init__(self, parent=None): super().__init__(parent) - self.session = Session() self.ui = Ui_Dialog() self.ui.setupUi(self) self.ui.searchString.textEdited.connect(self.chars_typed) @@ -633,20 +632,22 @@ class DbDialog(QDialog): self.ui.btnClose.clicked.connect(self.close) self.ui.matchList.itemSelectionChanged.connect(self.selection_changed) - record = Settings.get_int(self.session, "dbdialog_width") - width = record.f_int or 800 - record = Settings.get_int(self.session, "dbdialog_height") - height = record.f_int or 600 - self.resize(width, height) + with Session() as session: + record = Settings.get_int(session, "dbdialog_width") + width = record.f_int or 800 + record = Settings.get_int(session, "dbdialog_height") + height = record.f_int or 600 + self.resize(width, height) def __del__(self): - record = Settings.get_int(self.session, "dbdialog_height") - if record.f_int != self.height(): - record.update(self.session, {'f_int': self.height()}) + with Session() as session: + record = Settings.get_int(session, "dbdialog_height") + if record.f_int != self.height(): + record.update(session, {'f_int': self.height()}) - record = Settings.get_int(self.session, "dbdialog_width") - if record.f_int != self.width(): - record.update(self.session, {'f_int': self.width()}) + record = Settings.get_int(session, "dbdialog_width") + if record.f_int != self.width(): + record.update(session, {'f_int': self.width()}) def add_selected(self): if not self.ui.matchList.selectedItems(): @@ -661,18 +662,19 @@ class DbDialog(QDialog): self.close() def chars_typed(self, s): - if len(s) >= 3: - matches = Tracks.search_titles(self.session, s) - self.ui.matchList.clear() - if matches: - for track in matches: - t = QListWidgetItem() - t.setText( - f"{track.title} - {track.artist} " - f"[{helpers.ms_to_mmss(track.duration)}]" - ) - t.setData(Qt.UserRole, track.id) - self.ui.matchList.addItem(t) + if len(s) > 0: + with Session() as session: + matches = Tracks.search_titles(session, s) + self.ui.matchList.clear() + if matches: + for track in matches: + t = QListWidgetItem() + t.setText( + f"{track.title} - {track.artist} " + f"[{helpers.ms_to_mmss(track.duration)}]" + ) + t.setData(Qt.UserRole, track.id) + self.ui.matchList.addItem(t) def double_click(self, entry): track_id = entry.data(Qt.UserRole) @@ -681,8 +683,9 @@ class DbDialog(QDialog): self.select_searchtext() def add_track(self, track_id): - track = Tracks.track_from_id(self.session, track_id) - self.parent().visible_playlist()._add_to_playlist(track) + with Session() as session: + track = Tracks.track_from_id(session, track_id) + self.parent().visible_playlist().add_to_playlist(session, track) # Select search text to make it easier for next search self.select_searchtext() @@ -696,7 +699,8 @@ class DbDialog(QDialog): item = self.ui.matchList.currentItem() track_id = item.data(Qt.UserRole) - self.ui.dbPath.setText(Tracks.get_path(self.session, track_id)) + with Session() as session: + self.ui.dbPath.setText(Tracks.get_path(session, track_id)) class SelectPlaylistDialog(QDialog): diff --git a/app/playlists.py b/app/playlists.py index 99e7d0b..982f8c1 100644 --- a/app/playlists.py +++ b/app/playlists.py @@ -141,6 +141,8 @@ class Playlist(QTableWidget): f"Moved row(s) {rows} to become row {drop_row}" ) + with Session() as session: + self._save_playlist(session) self._repaint() def eventFilter(self, source, event): @@ -165,7 +167,7 @@ class Playlist(QTableWidget): # ########## Externally called functions ########## - def add_note(self, note, repaint=True): + def add_note(self, session, note, repaint=True): """ Add note to playlist @@ -211,9 +213,23 @@ class Playlist(QTableWidget): self.scrollToItem(titleitem, QAbstractItemView.PositionAtCenter) if repaint: + self._save_playlist(session) self._repaint(clear_selection=False) - def add_track(self, track, repaint=True): + def add_to_playlist(self, session, data, repaint=True): + """ + Add data to playlist. Data may be either a Tracks object or a + Notes object. + """ + + DEBUG(f"add_to_playlist(session={session}, data={data})") + + if isinstance(data, Tracks): + self.add_track(session, data, repaint=repaint) + elif isinstance(data, Notes): + self.add_note(session, data, repaint=repaint) + + def add_track(self, session, track, repaint=True): """ Add track to playlist @@ -250,6 +266,7 @@ class Playlist(QTableWidget): self.scrollToItem(titleitem, QAbstractItemView.PositionAtCenter) if repaint: + self._save_playlist(session) self._repaint(clear_selection=False) def clear_current(self): @@ -353,12 +370,13 @@ class Playlist(QTableWidget): # Now add data in row order for item in sorted(data, key=lambda x: x[0]): - self._add_to_playlist(item[1], repaint=False) + self.add_to_playlist(session, item[1], repaint=False) # Scroll to top scroll_to = self.item(0, self.COL_INDEX) self.scrollToItem(scroll_to, QAbstractItemView.PositionAtTop) + self._save_playlist(session) self._repaint() def repaint(self): @@ -442,19 +460,6 @@ class Playlist(QTableWidget): # ########## Internally called functions ########## - def _add_to_playlist(self, data, repaint=True): - """ - Add data to playlist. Data may be either a Tracks object or a - Notes object. - """ - - DEBUG(f"_add_to_playlist(data={data})") - - if isinstance(data, Tracks): - self.add_track(data, repaint=repaint) - elif isinstance(data, Notes): - self.add_note(data, repaint=repaint) - def _calculate_next_start_time(self, session, row, start): "Return this row's end time given its start time" @@ -490,7 +495,7 @@ class Playlist(QTableWidget): DEBUG("playlist._delete_row(): Can't delete next track") return - else: + with Session() as session: title = self.item(row, self.COL_TITLE).text() msg = QMessageBox(self) @@ -500,14 +505,14 @@ class Playlist(QTableWidget): msg.setDefaultButton(QMessageBox.Cancel) msg.setWindowTitle("Delete row") if msg.exec() == QMessageBox.Yes: - with Session() as session: - id = self._get_row_id(row) - if row in self._meta_get_notes(): - Notes.delete_note(session, id) - else: - PlaylistTracks.remove_track(session, self.db.id, row) - self.removeRow(row) + id = self._get_row_id(row) + if row in self._meta_get_notes(): + Notes.delete_note(session, id) + else: + PlaylistTracks.remove_track(session, self.db.id, row) + self.removeRow(row) + self._save_playlist(session) self._repaint() def _drop_on(self, event): @@ -716,20 +721,17 @@ class Playlist(QTableWidget): self._repaint(save_playlist=False) self.master_process.set_next_track(track_id) - def _repaint(self, clear_selection=True, save_playlist=True): - "Set row colours, fonts, etc, and save playlist" + def _repaint(self, clear_selection=True): + "Set row colours, fonts, etc" DEBUG( f"playlist[{self.db.id}:{self.db.name}]." - f"_repaint(clear_selection={clear_selection}, " - f"save_playlist={save_playlist})" + f"_repaint(clear_selection={clear_selection}" ) with Session() as session: if clear_selection: self.clearSelection() - if save_playlist: - self._save_playlist(session) current = self._meta_get_current() next = self._meta_get_next()