Differentiate between playlist tabs and db objects. Fixes #17
This commit is contained in:
parent
37ccf7c325
commit
20bd178cf1
@ -27,7 +27,7 @@ import music
|
||||
from config import Config
|
||||
from model import (Notes, Playdates, Playlists, PlaylistTracks,
|
||||
Session, Settings, Tracks)
|
||||
from playlists import Playlist
|
||||
from playlists import PlaylistTab
|
||||
from songdb import create_track_from_file
|
||||
from ui.dlg_search_database_ui import Ui_Dialog
|
||||
from ui.dlg_SelectPlaylist_ui import Ui_dlgSelectPlaylist
|
||||
@ -47,9 +47,9 @@ class Window(QMainWindow, Ui_MainWindow):
|
||||
|
||||
self.music = music.Music()
|
||||
self.current_track = None
|
||||
self.current_track_playlist = None
|
||||
self.current_track_playlist_tab = None
|
||||
self.next_track = None
|
||||
self.next_track_playlist = None
|
||||
self.next_track_playlist_tab = None
|
||||
self.previous_track = None
|
||||
self.previous_track_position = None
|
||||
self.spnVolume.setValue(Config.VOLUME_VLC_DEFAULT)
|
||||
@ -57,7 +57,7 @@ class Window(QMainWindow, Ui_MainWindow):
|
||||
self.menuTest.menuAction().setVisible(Config.TESTMODE)
|
||||
self.set_main_window_size()
|
||||
|
||||
self.visible_playlist = self.tabPlaylist.currentWidget
|
||||
self.visible_playlist_tab = self.tabPlaylist.currentWidget
|
||||
|
||||
self.load_last_playlists()
|
||||
self.enable_play_next_controls()
|
||||
@ -77,7 +77,7 @@ class Window(QMainWindow, Ui_MainWindow):
|
||||
# Add to playlist on screen
|
||||
# If we don't specify "repaint=False", playlist will
|
||||
# also be saved to database
|
||||
self.visible_playlist().insert_track(session, track)
|
||||
self.visible_playlist_tab().insert_track(session, track)
|
||||
|
||||
def set_main_window_size(self):
|
||||
"Set size of window from database"
|
||||
@ -94,8 +94,8 @@ class Window(QMainWindow, Ui_MainWindow):
|
||||
self.setGeometry(x, y, width, height)
|
||||
|
||||
def clear_selection(self):
|
||||
if self.visible_playlist():
|
||||
self.visible_playlist().clearSelection()
|
||||
if self.visible_playlist_tab():
|
||||
self.visible_playlist_tab().clearSelection()
|
||||
|
||||
def closeEvent(self, event):
|
||||
"Don't allow window to close when a track is playing"
|
||||
@ -129,8 +129,8 @@ class Window(QMainWindow, Ui_MainWindow):
|
||||
def connect_signals_slots(self):
|
||||
self.actionAdd_file.triggered.connect(self.add_file)
|
||||
self.action_Clear_selection.triggered.connect(self.clear_selection)
|
||||
self.actionClosePlaylist.triggered.connect(self.close_playlist)
|
||||
self.actionExport_playlist.triggered.connect(self.export_playlist)
|
||||
self.actionClosePlaylist.triggered.connect(self.close_playlist_tab)
|
||||
self.actionExport_playlist.triggered.connect(self.export_playlist_tab)
|
||||
self.actionFade.triggered.connect(self.fade)
|
||||
self.actionMoveSelected.triggered.connect(self.move_selected)
|
||||
self.actionNewPlaylist.triggered.connect(self.create_playlist)
|
||||
@ -155,7 +155,6 @@ class Window(QMainWindow, Ui_MainWindow):
|
||||
self.btnSongInfo.clicked.connect(self.song_info_search)
|
||||
self.btnStop.clicked.connect(self.stop)
|
||||
self.spnVolume.valueChanged.connect(self.change_volume)
|
||||
self.tabPlaylist.currentChanged.connect(self.tab_change)
|
||||
|
||||
self.timer.timeout.connect(self.tick)
|
||||
|
||||
@ -169,8 +168,8 @@ class Window(QMainWindow, Ui_MainWindow):
|
||||
ok = dlg.exec()
|
||||
if ok:
|
||||
with Session() as session:
|
||||
playlist = Playlists.new(session, dlg.textValue())
|
||||
self.load_playlist(session, playlist)
|
||||
playlist_db = Playlists.new(session, dlg.textValue())
|
||||
self.load_playlist(session, playlist_db)
|
||||
|
||||
def change_volume(self, volume):
|
||||
"Change player maximum volume"
|
||||
@ -179,10 +178,10 @@ class Window(QMainWindow, Ui_MainWindow):
|
||||
|
||||
self.music.set_volume(volume)
|
||||
|
||||
def close_playlist(self):
|
||||
def close_playlist_tab(self):
|
||||
with Session() as session:
|
||||
playlist_db = session.query(Playlists).filter(
|
||||
Playlists.id == self.visible_playlist().id).one()
|
||||
Playlists.id == self.visible_playlist_tab().id).one()
|
||||
playlist_db.close(session)
|
||||
index = self.tabPlaylist.currentIndex()
|
||||
self.tabPlaylist.removeTab(index)
|
||||
@ -197,13 +196,13 @@ class Window(QMainWindow, Ui_MainWindow):
|
||||
Return note.
|
||||
"""
|
||||
|
||||
row = self.visible_playlist().get_selected_row()
|
||||
row = self.visible_playlist_tab().get_selected_row()
|
||||
if row is None:
|
||||
row = self.visible_playlist().rowCount()
|
||||
row = self.visible_playlist_tab().rowCount()
|
||||
DEBUG(f"musicmuster.create_note(text={text}): row={row}")
|
||||
|
||||
note = Notes.add_note(
|
||||
session, self.visible_playlist().id, row, text)
|
||||
session, self.visible_playlist_tab().id, row, text)
|
||||
|
||||
return note
|
||||
|
||||
@ -215,16 +214,16 @@ class Window(QMainWindow, Ui_MainWindow):
|
||||
DEBUG("enable_play_next_controls()")
|
||||
self.actionPlay_next.setEnabled(True)
|
||||
|
||||
def export_playlist(self):
|
||||
def export_playlist_tab(self):
|
||||
"Export the current playlist to an m3u file"
|
||||
|
||||
if not self.visible_playlist():
|
||||
if not self.visible_playlist_tab():
|
||||
return
|
||||
|
||||
# Get output filename
|
||||
pathspec = QFileDialog.getSaveFileName(
|
||||
self, 'Save Playlist',
|
||||
directory=f"{self.visible_playlist().name}.m3u",
|
||||
directory=f"{self.visible_playlist_tab().name}.m3u",
|
||||
filter="M3U files (*.m3u);;All files (*.*)"
|
||||
)
|
||||
if not pathspec:
|
||||
@ -237,11 +236,12 @@ class Window(QMainWindow, Ui_MainWindow):
|
||||
|
||||
# Get playlist db object
|
||||
with Session() as session:
|
||||
p = Playlists.get_playlist(session, self.visible_playlist().id)
|
||||
playlist_db = Playlists.get_playlist(
|
||||
session, self.visible_playlist_tab().id)
|
||||
with open(path, "w") as f:
|
||||
# Required directive on first line
|
||||
f.write("#EXTM3U\n")
|
||||
for track in p.get_tracks():
|
||||
for track in playlist_db.get_tracks():
|
||||
f.write(
|
||||
"#EXTINF:"
|
||||
f"{int(track.duration / 1000)},"
|
||||
@ -273,17 +273,14 @@ class Window(QMainWindow, Ui_MainWindow):
|
||||
if ok:
|
||||
with Session() as session:
|
||||
note = self.create_note(session, dlg.textValue())
|
||||
self.visible_playlist().insert_note(session, note)
|
||||
self.visible_playlist_tab().insert_note(session, note)
|
||||
|
||||
def load_last_playlists(self):
|
||||
"Load the playlists that we loaded at end of last session"
|
||||
|
||||
with Session() as session:
|
||||
for playlist in Playlists.get_last_used(session):
|
||||
DEBUG(
|
||||
f"load_last_playlists(), playlist.name={playlist.name}, "
|
||||
f"playlist.id={playlist.id}")
|
||||
self.load_playlist(session, playlist)
|
||||
for playlist_db in Playlists.get_last_used(session):
|
||||
self.load_playlist(session, playlist_db)
|
||||
|
||||
def load_playlist(self, session, playlist_db):
|
||||
"""
|
||||
@ -291,18 +288,18 @@ class Window(QMainWindow, Ui_MainWindow):
|
||||
the database object, get it populated and then add tab.
|
||||
"""
|
||||
|
||||
playlist_table = Playlist(self)
|
||||
playlist_table.populate(session, playlist_db)
|
||||
idx = self.tabPlaylist.addTab(playlist_table, playlist_db.name)
|
||||
playlist_tab = PlaylistTab(self)
|
||||
playlist_tab.populate(session, playlist_db)
|
||||
idx = self.tabPlaylist.addTab(playlist_tab, playlist_db.name)
|
||||
self.tabPlaylist.setCurrentIndex(idx)
|
||||
|
||||
def move_selected(self):
|
||||
"Move selected rows to another playlist"
|
||||
|
||||
with Session() as session:
|
||||
playlists = [p for p in Playlists.get_all_playlists(session)
|
||||
if p.id != self.visible_playlist().id]
|
||||
dlg = SelectPlaylistDialog(self, playlists=playlists)
|
||||
playlist_dbs = [p for p in Playlists.get_all_playlists(session)
|
||||
if p.id != self.visible_playlist_tab().id]
|
||||
dlg = SelectPlaylistDialog(self, playlists=playlist_dbs)
|
||||
dlg.exec()
|
||||
if not dlg.plid:
|
||||
return
|
||||
@ -319,7 +316,8 @@ class Window(QMainWindow, Ui_MainWindow):
|
||||
|
||||
rows = []
|
||||
for (row, track_id) in (
|
||||
self.visible_playlist().get_selected_rows_and_tracks()):
|
||||
self.visible_playlist_tab().get_selected_rows_and_tracks()
|
||||
):
|
||||
rows.append(row)
|
||||
track = Tracks.track_from_id(session, track_id)
|
||||
if destination_visible_playlist_tab:
|
||||
@ -329,14 +327,14 @@ class Window(QMainWindow, Ui_MainWindow):
|
||||
|
||||
# Update database
|
||||
PlaylistTracks.move_track(
|
||||
session, self.visible_playlist().id, row, dlg.plid)
|
||||
session, self.visible_playlist_tab().id, row, dlg.plid)
|
||||
|
||||
# Update destination playlist if visible
|
||||
if destination_visible_playlist_tab:
|
||||
destination_visible_playlist_tab.repaint()
|
||||
|
||||
# Update source playlist
|
||||
self.visible_playlist().remove_rows(rows)
|
||||
self.visible_playlist_tab().remove_rows(rows)
|
||||
|
||||
def play_next(self):
|
||||
"""
|
||||
@ -370,24 +368,24 @@ class Window(QMainWindow, Ui_MainWindow):
|
||||
|
||||
# Play next track
|
||||
self.current_track = self.next_track
|
||||
self.current_track_playlist = self.next_track_playlist
|
||||
self.current_track_playlist_tab = self.next_track_playlist_tab
|
||||
self.next_track = None
|
||||
self.next_track_playlist = None
|
||||
self.next_track_playlist_tab = None
|
||||
self.music.play(self.current_track.path)
|
||||
|
||||
# Update metadata
|
||||
next_track_id = self.current_track_playlist.play_started()
|
||||
next_track_id = self.current_track_playlist_tab.play_started()
|
||||
|
||||
if next_track_id is not None:
|
||||
self.next_track = Tracks.get_track(session, next_track_id)
|
||||
self.next_track_playlist = self.current_track_playlist
|
||||
self.next_track_playlist_tab = self.current_track_playlist_tab
|
||||
# Check we can read it
|
||||
if not os.access(self.next_track.path, os.R_OK):
|
||||
self.show_warning(
|
||||
"Can't read next track",
|
||||
self.next_track.path)
|
||||
else:
|
||||
self.next_track = self.next_track_playlist = None
|
||||
self.next_track = self.next_track_playlist_tab = None
|
||||
|
||||
# Tell database to record it as played
|
||||
self.current_track.update_lastplayed()
|
||||
@ -419,34 +417,35 @@ class Window(QMainWindow, Ui_MainWindow):
|
||||
|
||||
def open_playlist(self):
|
||||
with Session() as session:
|
||||
playlists = Playlists.get_all_closed_playlists(session)
|
||||
dlg = SelectPlaylistDialog(self, playlists=playlists)
|
||||
playlist_dbs = Playlists.get_all_closed_playlists(session)
|
||||
dlg = SelectPlaylistDialog(self, playlist_dbs=playlist_dbs)
|
||||
dlg.exec()
|
||||
if dlg.plid:
|
||||
playlist = Playlists.open(session, dlg.plid)
|
||||
self.load_playlist(session, playlist)
|
||||
playlist_db = Playlists.open(session, dlg.plid)
|
||||
self.load_playlist(session, playlist_db)
|
||||
|
||||
def select_next_track(self):
|
||||
"Select next or first track in playlist"
|
||||
|
||||
self.visible_playlist().select_next_track()
|
||||
self.visible_playlist_tab().select_next_track()
|
||||
|
||||
def select_previous_track(self):
|
||||
"Select previous or first track in playlist"
|
||||
|
||||
self.visible_playlist().select_previous_track()
|
||||
self.visible_playlist_tab().select_previous_track()
|
||||
|
||||
def set_next_track(self, next_track_id=None):
|
||||
"Set selected track as next"
|
||||
|
||||
with Session() as session:
|
||||
if not next_track_id:
|
||||
next_track_id = self.visible_playlist().set_selected_as_next()
|
||||
next_track_id = (
|
||||
self.visible_playlist_tab().set_selected_as_next())
|
||||
if next_track_id:
|
||||
if self.next_track_playlist != self.visible_playlist():
|
||||
if self.next_track_playlist:
|
||||
self.next_track_playlist.clear_next()
|
||||
self.next_track_playlist = self.visible_playlist()
|
||||
if self.next_track_playlist_tab != self.visible_playlist_tab():
|
||||
if self.next_track_playlist_tab:
|
||||
self.next_track_playlist_tab.clear_next()
|
||||
self.next_track_playlist_tab = self.visible_playlist_tab()
|
||||
self.next_track = Tracks.get_track(session, next_track_id)
|
||||
self.update_headers()
|
||||
|
||||
@ -464,7 +463,7 @@ class Window(QMainWindow, Ui_MainWindow):
|
||||
- next track
|
||||
"""
|
||||
|
||||
title = self.visible_playlist().get_selected_title()
|
||||
title = self.visible_playlist_tab().get_selected_title()
|
||||
if not title:
|
||||
if self.current_track:
|
||||
title = self.current_track.title
|
||||
@ -502,18 +501,13 @@ class Window(QMainWindow, Ui_MainWindow):
|
||||
self.music.fade()
|
||||
else:
|
||||
self.music.stop()
|
||||
self.current_track_playlist.clear_current()
|
||||
self.current_track_playlist_tab.clear_current()
|
||||
|
||||
# Shuffle tracks along
|
||||
self.previous_track = self.current_track
|
||||
|
||||
self.update_headers()
|
||||
|
||||
def tab_change(self):
|
||||
"User has changed tabs"
|
||||
|
||||
pass
|
||||
|
||||
def test_function(self):
|
||||
"Placeholder for test function"
|
||||
|
||||
@ -599,11 +593,11 @@ class Window(QMainWindow, Ui_MainWindow):
|
||||
if self.playing:
|
||||
self.label_end_timer.setText("00:00")
|
||||
self.frame_silent.setStyleSheet("")
|
||||
self.current_track_playlist.play_stopped()
|
||||
self.current_track_playlist_tab.play_stopped()
|
||||
self.playing = False
|
||||
self.previous_track = self.current_track
|
||||
self.current_track = None
|
||||
self.current_track_playlist = None
|
||||
self.current_track_playlist_tab = None
|
||||
self.previous_track_position = 0
|
||||
self.update_headers()
|
||||
# Release player
|
||||
@ -702,7 +696,7 @@ class DbDialog(QDialog):
|
||||
# Add to playlist on screen
|
||||
# If we don't specify "repaint=False", playlist will
|
||||
# also be saved to database
|
||||
self.parent().visible_playlist().insert_track(
|
||||
self.parent().visible_playlist_tab().insert_track(
|
||||
self.session, track)
|
||||
# Select search text to make it easier for next search
|
||||
self.select_searchtext()
|
||||
@ -721,10 +715,10 @@ class DbDialog(QDialog):
|
||||
|
||||
|
||||
class SelectPlaylistDialog(QDialog):
|
||||
def __init__(self, parent=None, playlists=None):
|
||||
def __init__(self, parent=None, playlist_dbs=None):
|
||||
super().__init__(parent)
|
||||
|
||||
if playlists is None:
|
||||
if playlist_dbs is None:
|
||||
return
|
||||
self.ui = Ui_dlgSelectPlaylist()
|
||||
self.ui.setupUi(self)
|
||||
@ -740,7 +734,7 @@ class SelectPlaylistDialog(QDialog):
|
||||
height = record.f_int or 600
|
||||
self.resize(width, height)
|
||||
|
||||
for (plid, plname) in [(a.id, a.name) for a in playlists]:
|
||||
for (plid, plname) in [(a.id, a.name) for a in playlist_dbs]:
|
||||
p = QListWidgetItem()
|
||||
p.setText(plname)
|
||||
p.setData(Qt.UserRole, plid)
|
||||
|
||||
@ -20,7 +20,7 @@ from log import DEBUG, ERROR
|
||||
from model import Notes, Playlists, PlaylistTracks, Session, Settings, Tracks
|
||||
|
||||
|
||||
class Playlist(QTableWidget):
|
||||
class PlaylistTab(QTableWidget):
|
||||
# Column names
|
||||
COL_INDEX = 0
|
||||
COL_MSS = 1
|
||||
@ -163,7 +163,7 @@ class Playlist(QTableWidget):
|
||||
act_delete = self.menu.addAction('Delete')
|
||||
act_delete.triggered.connect(lambda: self._delete_row(row))
|
||||
|
||||
return super(Playlist, self).eventFilter(source, event)
|
||||
return super(PlaylistTab, self).eventFilter(source, event)
|
||||
|
||||
# ########## Externally called functions ##########
|
||||
|
||||
@ -841,7 +841,7 @@ class Playlist(QTableWidget):
|
||||
database_notes = {}
|
||||
notes_rows = self._meta_get_notes()
|
||||
|
||||
# Playlist
|
||||
# PlaylistTab
|
||||
for row in notes_rows:
|
||||
note_id = self._get_row_id(row)
|
||||
if not note_id:
|
||||
|
||||
Loading…
Reference in New Issue
Block a user