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.
This commit is contained in:
Keith Edmunds 2021-06-04 12:37:37 +01:00
parent 70f693a86b
commit 9eac5caf09
2 changed files with 64 additions and 58 deletions

View File

@ -74,7 +74,7 @@ class Window(QMainWindow, Ui_MainWindow):
with Session() as session: with Session() as session:
for fname in dlg.selectedFiles(): for fname in dlg.selectedFiles():
track = add_path_to_db(session, fname) 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): def set_main_window_size(self):
@ -623,7 +623,6 @@ class Window(QMainWindow, Ui_MainWindow):
class DbDialog(QDialog): class DbDialog(QDialog):
def __init__(self, parent=None): def __init__(self, parent=None):
super().__init__(parent) super().__init__(parent)
self.session = Session()
self.ui = Ui_Dialog() self.ui = Ui_Dialog()
self.ui.setupUi(self) self.ui.setupUi(self)
self.ui.searchString.textEdited.connect(self.chars_typed) self.ui.searchString.textEdited.connect(self.chars_typed)
@ -633,20 +632,22 @@ class DbDialog(QDialog):
self.ui.btnClose.clicked.connect(self.close) self.ui.btnClose.clicked.connect(self.close)
self.ui.matchList.itemSelectionChanged.connect(self.selection_changed) self.ui.matchList.itemSelectionChanged.connect(self.selection_changed)
record = Settings.get_int(self.session, "dbdialog_width") with Session() as session:
width = record.f_int or 800 record = Settings.get_int(session, "dbdialog_width")
record = Settings.get_int(self.session, "dbdialog_height") width = record.f_int or 800
height = record.f_int or 600 record = Settings.get_int(session, "dbdialog_height")
self.resize(width, height) height = record.f_int or 600
self.resize(width, height)
def __del__(self): def __del__(self):
record = Settings.get_int(self.session, "dbdialog_height") with Session() as session:
if record.f_int != self.height(): record = Settings.get_int(session, "dbdialog_height")
record.update(self.session, {'f_int': self.height()}) if record.f_int != self.height():
record.update(session, {'f_int': self.height()})
record = Settings.get_int(self.session, "dbdialog_width") record = Settings.get_int(session, "dbdialog_width")
if record.f_int != self.width(): if record.f_int != self.width():
record.update(self.session, {'f_int': self.width()}) record.update(session, {'f_int': self.width()})
def add_selected(self): def add_selected(self):
if not self.ui.matchList.selectedItems(): if not self.ui.matchList.selectedItems():
@ -661,18 +662,19 @@ class DbDialog(QDialog):
self.close() self.close()
def chars_typed(self, s): def chars_typed(self, s):
if len(s) >= 3: if len(s) > 0:
matches = Tracks.search_titles(self.session, s) with Session() as session:
self.ui.matchList.clear() matches = Tracks.search_titles(session, s)
if matches: self.ui.matchList.clear()
for track in matches: if matches:
t = QListWidgetItem() for track in matches:
t.setText( t = QListWidgetItem()
f"{track.title} - {track.artist} " t.setText(
f"[{helpers.ms_to_mmss(track.duration)}]" f"{track.title} - {track.artist} "
) f"[{helpers.ms_to_mmss(track.duration)}]"
t.setData(Qt.UserRole, track.id) )
self.ui.matchList.addItem(t) t.setData(Qt.UserRole, track.id)
self.ui.matchList.addItem(t)
def double_click(self, entry): def double_click(self, entry):
track_id = entry.data(Qt.UserRole) track_id = entry.data(Qt.UserRole)
@ -681,8 +683,9 @@ class DbDialog(QDialog):
self.select_searchtext() self.select_searchtext()
def add_track(self, track_id): def add_track(self, track_id):
track = Tracks.track_from_id(self.session, track_id) with Session() as session:
self.parent().visible_playlist()._add_to_playlist(track) 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 # Select search text to make it easier for next search
self.select_searchtext() self.select_searchtext()
@ -696,7 +699,8 @@ class DbDialog(QDialog):
item = self.ui.matchList.currentItem() item = self.ui.matchList.currentItem()
track_id = item.data(Qt.UserRole) 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): class SelectPlaylistDialog(QDialog):

View File

@ -141,6 +141,8 @@ class Playlist(QTableWidget):
f"Moved row(s) {rows} to become row {drop_row}" f"Moved row(s) {rows} to become row {drop_row}"
) )
with Session() as session:
self._save_playlist(session)
self._repaint() self._repaint()
def eventFilter(self, source, event): def eventFilter(self, source, event):
@ -165,7 +167,7 @@ class Playlist(QTableWidget):
# ########## Externally called functions ########## # ########## Externally called functions ##########
def add_note(self, note, repaint=True): def add_note(self, session, note, repaint=True):
""" """
Add note to playlist Add note to playlist
@ -211,9 +213,23 @@ class Playlist(QTableWidget):
self.scrollToItem(titleitem, QAbstractItemView.PositionAtCenter) self.scrollToItem(titleitem, QAbstractItemView.PositionAtCenter)
if repaint: if repaint:
self._save_playlist(session)
self._repaint(clear_selection=False) 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 Add track to playlist
@ -250,6 +266,7 @@ class Playlist(QTableWidget):
self.scrollToItem(titleitem, QAbstractItemView.PositionAtCenter) self.scrollToItem(titleitem, QAbstractItemView.PositionAtCenter)
if repaint: if repaint:
self._save_playlist(session)
self._repaint(clear_selection=False) self._repaint(clear_selection=False)
def clear_current(self): def clear_current(self):
@ -353,12 +370,13 @@ class Playlist(QTableWidget):
# Now add data in row order # Now add data in row order
for item in sorted(data, key=lambda x: x[0]): 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 top
scroll_to = self.item(0, self.COL_INDEX) scroll_to = self.item(0, self.COL_INDEX)
self.scrollToItem(scroll_to, QAbstractItemView.PositionAtTop) self.scrollToItem(scroll_to, QAbstractItemView.PositionAtTop)
self._save_playlist(session)
self._repaint() self._repaint()
def repaint(self): def repaint(self):
@ -442,19 +460,6 @@ class Playlist(QTableWidget):
# ########## Internally called functions ########## # ########## 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): def _calculate_next_start_time(self, session, row, start):
"Return this row's end time given its start time" "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") DEBUG("playlist._delete_row(): Can't delete next track")
return return
else: with Session() as session:
title = self.item(row, self.COL_TITLE).text() title = self.item(row, self.COL_TITLE).text()
msg = QMessageBox(self) msg = QMessageBox(self)
@ -500,14 +505,14 @@ class Playlist(QTableWidget):
msg.setDefaultButton(QMessageBox.Cancel) msg.setDefaultButton(QMessageBox.Cancel)
msg.setWindowTitle("Delete row") msg.setWindowTitle("Delete row")
if msg.exec() == QMessageBox.Yes: if msg.exec() == QMessageBox.Yes:
with Session() as session: id = self._get_row_id(row)
id = self._get_row_id(row) if row in self._meta_get_notes():
if row in self._meta_get_notes(): Notes.delete_note(session, id)
Notes.delete_note(session, id) else:
else: PlaylistTracks.remove_track(session, self.db.id, row)
PlaylistTracks.remove_track(session, self.db.id, row) self.removeRow(row)
self.removeRow(row)
self._save_playlist(session)
self._repaint() self._repaint()
def _drop_on(self, event): def _drop_on(self, event):
@ -716,20 +721,17 @@ class Playlist(QTableWidget):
self._repaint(save_playlist=False) self._repaint(save_playlist=False)
self.master_process.set_next_track(track_id) self.master_process.set_next_track(track_id)
def _repaint(self, clear_selection=True, save_playlist=True): def _repaint(self, clear_selection=True):
"Set row colours, fonts, etc, and save playlist" "Set row colours, fonts, etc"
DEBUG( DEBUG(
f"playlist[{self.db.id}:{self.db.name}]." f"playlist[{self.db.id}:{self.db.name}]."
f"_repaint(clear_selection={clear_selection}, " f"_repaint(clear_selection={clear_selection}"
f"save_playlist={save_playlist})"
) )
with Session() as session: with Session() as session:
if clear_selection: if clear_selection:
self.clearSelection() self.clearSelection()
if save_playlist:
self._save_playlist(session)
current = self._meta_get_current() current = self._meta_get_current()
next = self._meta_get_next() next = self._meta_get_next()