Compare commits

..

No commits in common. "1abe377b4cca308d96c6668eb4a2e81a51478394" and "ad717aeb2cc7f930b3aa4d2340ffa8ceb662b220" have entirely different histories.

2 changed files with 59 additions and 69 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(session, track) self.visible_playlist()._add_to_playlist(track)
def set_main_window_size(self): def set_main_window_size(self):
@ -470,7 +470,6 @@ class Window(QMainWindow, Ui_MainWindow):
DEBUG("musicmuster.stop()") DEBUG("musicmuster.stop()")
self.stop_playing(fade=False) self.stop_playing(fade=False)
self.enable_play_next_controls()
def stop_playing(self, fade=True): def stop_playing(self, fade=True):
"Stop playing current track" "Stop playing current track"
@ -623,6 +622,7 @@ 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)
@ -632,22 +632,20 @@ 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)
with Session() as session: record = Settings.get_int(self.session, "dbdialog_width")
record = Settings.get_int(session, "dbdialog_width")
width = record.f_int or 800 width = record.f_int or 800
record = Settings.get_int(session, "dbdialog_height") record = Settings.get_int(self.session, "dbdialog_height")
height = record.f_int or 600 height = record.f_int or 600
self.resize(width, height) self.resize(width, height)
def __del__(self): def __del__(self):
with Session() as session: record = Settings.get_int(self.session, "dbdialog_height")
record = Settings.get_int(session, "dbdialog_height")
if record.f_int != self.height(): if record.f_int != self.height():
record.update(session, {'f_int': self.height()}) record.update(self.session, {'f_int': self.height()})
record = Settings.get_int(session, "dbdialog_width") record = Settings.get_int(self.session, "dbdialog_width")
if record.f_int != self.width(): if record.f_int != self.width():
record.update(session, {'f_int': self.width()}) record.update(self.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():
@ -662,9 +660,8 @@ class DbDialog(QDialog):
self.close() self.close()
def chars_typed(self, s): def chars_typed(self, s):
if len(s) > 0: if len(s) >= 3:
with Session() as session: matches = Tracks.search_titles(self.session, s)
matches = Tracks.search_titles(session, s)
self.ui.matchList.clear() self.ui.matchList.clear()
if matches: if matches:
for track in matches: for track in matches:
@ -683,9 +680,8 @@ class DbDialog(QDialog):
self.select_searchtext() self.select_searchtext()
def add_track(self, track_id): def add_track(self, track_id):
with Session() as session: track = Tracks.track_from_id(self.session, track_id)
track = Tracks.track_from_id(session, track_id) self.parent().visible_playlist()._add_to_playlist(track)
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()
@ -699,8 +695,7 @@ 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)
with Session() as session: self.ui.dbPath.setText(Tracks.get_path(self.session, track_id))
self.ui.dbPath.setText(Tracks.get_path(session, track_id))
class SelectPlaylistDialog(QDialog): class SelectPlaylistDialog(QDialog):

View File

@ -141,8 +141,6 @@ 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):
@ -167,7 +165,7 @@ class Playlist(QTableWidget):
# ########## Externally called functions ########## # ########## Externally called functions ##########
def add_note(self, session, note, repaint=True): def add_note(self, note, repaint=True):
""" """
Add note to playlist Add note to playlist
@ -213,23 +211,9 @@ 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_to_playlist(self, session, data, repaint=True): def add_track(self, track, 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
@ -266,7 +250,6 @@ 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):
@ -370,13 +353,12 @@ 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(session, item[1], repaint=False) self._add_to_playlist(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):
@ -460,6 +442,19 @@ 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"
@ -495,7 +490,7 @@ class Playlist(QTableWidget):
DEBUG("playlist._delete_row(): Can't delete next track") DEBUG("playlist._delete_row(): Can't delete next track")
return return
with Session() as session: else:
title = self.item(row, self.COL_TITLE).text() title = self.item(row, self.COL_TITLE).text()
msg = QMessageBox(self) msg = QMessageBox(self)
@ -505,6 +500,7 @@ 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)
@ -512,7 +508,6 @@ class Playlist(QTableWidget):
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):
@ -721,17 +716,20 @@ 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): def _repaint(self, clear_selection=True, save_playlist=True):
"Set row colours, fonts, etc" "Set row colours, fonts, etc, and save playlist"
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()
@ -876,10 +874,7 @@ class Playlist(QTableWidget):
playlist_tracks[row] = self._get_row_id(row) playlist_tracks[row] = self._get_row_id(row)
# Database # Database
# Workaround for issue #10 for track in self.db.tracks:
# for track in self.db.tracks:
for track in session.query(PlaylistTracks).filter(
PlaylistTracks.playlist_id == self.db.id).all():
database_tracks[track.row] = track.track_id database_tracks[track.row] = track.track_id
# Tracks rows to add to database # Tracks rows to add to database