319 lines
9.8 KiB
Python
Executable File
319 lines
9.8 KiB
Python
Executable File
#!/usr/bin/env python
|
|
|
|
import sys
|
|
|
|
|
|
from datetime import datetime, timedelta
|
|
from log import DEBUG, EXCEPTION
|
|
|
|
from PyQt5.QtCore import QTimer
|
|
from PyQt5.QtWidgets import (
|
|
QApplication,
|
|
QFileDialog,
|
|
QInputDialog,
|
|
QLabel,
|
|
QMainWindow,
|
|
)
|
|
|
|
import helpers
|
|
|
|
from config import Config
|
|
from model import Settings
|
|
from songdb import add_path_to_db
|
|
from ui.main_window_ui import Ui_MainWindow
|
|
|
|
|
|
class Window(QMainWindow, Ui_MainWindow):
|
|
def __init__(self, parent=None):
|
|
super().__init__(parent)
|
|
self.setupUi(self)
|
|
self.timer = QTimer()
|
|
self.even_tick = True
|
|
self.playing = False
|
|
self.connect_signals_slots()
|
|
self.disable_play_next_controls()
|
|
|
|
self.menuTest.menuAction().setVisible(Config.TESTMODE)
|
|
|
|
record = Settings.get_int("mainwindow_x")
|
|
x = record.f_int or 1
|
|
record = Settings.get_int("mainwindow_y")
|
|
y = record.f_int or 1
|
|
record = Settings.get_int("mainwindow_width")
|
|
width = record.f_int or 1599
|
|
record = Settings.get_int("mainwindow_height")
|
|
height = record.f_int or 981
|
|
self.setGeometry(x, y, width, height)
|
|
|
|
self.playlist.set_column_widths()
|
|
|
|
# Hard code to the first playlist for now
|
|
# TODO
|
|
if self.playlist.load_playlist(1):
|
|
self.update_headers()
|
|
self.enable_play_next_controls()
|
|
|
|
self.plLabel = QLabel(f"Playlist: {self.playlist.playlist_name}")
|
|
self.statusbar.addPermanentWidget(self.plLabel)
|
|
|
|
self.timer.start(Config.TIMER_MS)
|
|
|
|
def add_file(self):
|
|
dlg = QFileDialog()
|
|
dlg.setFileMode(QFileDialog.ExistingFile)
|
|
dlg.setViewMode(QFileDialog.Detail)
|
|
dlg.setDirectory(Config.ROOT)
|
|
dlg.setNameFilter("Music files (*.flac *.mp3)")
|
|
|
|
if dlg.exec_():
|
|
for fname in dlg.selectedFiles():
|
|
track = add_path_to_db(fname)
|
|
self.playlist.add_to_playlist(track)
|
|
|
|
def closeEvent(self, event):
|
|
"Don't allow window to close when a track is playing"
|
|
|
|
if self.playlist.music.playing():
|
|
DEBUG("closeEvent() ignored as music is playing")
|
|
event.ignore()
|
|
# TODO notify user
|
|
else:
|
|
DEBUG("closeEvent() accepted")
|
|
|
|
record = Settings.get_int("mainwindow_height")
|
|
if record.f_int != self.height():
|
|
record.update({'f_int': self.height()})
|
|
|
|
record = Settings.get_int("mainwindow_width")
|
|
if record.f_int != self.width():
|
|
record.update({'f_int': self.width()})
|
|
|
|
record = Settings.get_int("mainwindow_x")
|
|
if record.f_int != self.x():
|
|
record.update({'f_int': self.x()})
|
|
|
|
record = Settings.get_int("mainwindow_y")
|
|
if record.f_int != self.y():
|
|
record.update({'f_int': self.y()})
|
|
|
|
event.accept()
|
|
|
|
def connect_signals_slots(self):
|
|
self.actionAdd_file.triggered.connect(self.add_file)
|
|
self.action_Clear_selection.triggered.connect(
|
|
self.playlist.clearSelection)
|
|
self.actionFade.triggered.connect(self.fade)
|
|
self.actionNewPlaylist.triggered.connect(self.create_playlist)
|
|
self.actionPlay_next.triggered.connect(self.play_next)
|
|
self.actionSearch_database.triggered.connect(self.search_database)
|
|
self.actionSelectPlaylist.triggered.connect(self.select_playlist)
|
|
self.actionSkip_next.triggered.connect(self.play_next)
|
|
self.actionSkipToEnd.triggered.connect(self.test_skip_to_end)
|
|
self.actionSkipToFade.triggered.connect(self.test_skip_to_fade)
|
|
self.actionTestFunction.triggered.connect(self.test_function)
|
|
self.btnAddFile.clicked.connect(self.add_file)
|
|
self.btnAddNote.clicked.connect(self.insert_note)
|
|
self.btnDatabase.clicked.connect(self.search_database)
|
|
self.btnFade.clicked.connect(self.fade)
|
|
self.btnPlay.clicked.connect(self.play_next)
|
|
self.btnPrevious.clicked.connect(self.play_previous)
|
|
self.btnSetNext.clicked.connect(self.set_next_track)
|
|
self.btnSkipNext.clicked.connect(self.play_next)
|
|
|
|
self.timer.timeout.connect(self.tick)
|
|
|
|
def create_playlist(self):
|
|
"Create new playlist"
|
|
|
|
dlg = QInputDialog(self)
|
|
dlg.setInputMode(QInputDialog.TextInput)
|
|
dlg.setLabelText("Playlist name:")
|
|
dlg.resize(500, 100)
|
|
ok = dlg.exec()
|
|
if ok:
|
|
self.playlist.create_playlist(dlg.textValue())
|
|
|
|
def disable_play_next_controls(self):
|
|
DEBUG("disable_play_next_controls()")
|
|
self.actionPlay_next.setEnabled(False)
|
|
|
|
def enable_play_next_controls(self):
|
|
DEBUG("enable_play_next_controls()")
|
|
self.actionPlay_next.setEnabled(True)
|
|
|
|
def fade(self):
|
|
self.playlist.fade()
|
|
self.enable_play_next_controls()
|
|
|
|
def insert_note(self):
|
|
"Add non-track row to playlist"
|
|
|
|
dlg = QInputDialog(self)
|
|
dlg.setInputMode(QInputDialog.TextInput)
|
|
dlg.setLabelText("Note:")
|
|
dlg.resize(500, 100)
|
|
ok = dlg.exec()
|
|
if ok:
|
|
self.playlist.add_note(dlg.textValue())
|
|
|
|
def play_next(self):
|
|
self.playlist.play_next()
|
|
self.disable_play_next_controls()
|
|
self.update_headers()
|
|
|
|
# Set time clocks
|
|
now = datetime.now()
|
|
self.label_start_tod.setText(now.strftime("%H:%M:%S"))
|
|
silence_at = self.playlist.get_current_silence_at()
|
|
silence_time = now + timedelta(milliseconds=silence_at)
|
|
self.label_silent_tod.setText(silence_time.strftime("%H:%M:%S"))
|
|
self.label_fade_length.setText(helpers.ms_to_mmss(
|
|
silence_at - self.playlist.get_current_fade_at()
|
|
))
|
|
|
|
def play_previous(self):
|
|
"Resume playing last track"
|
|
|
|
# TODO
|
|
pass
|
|
|
|
def search_database(self):
|
|
self.playlist.search_database()
|
|
|
|
def select_playlist(self):
|
|
self.playlist.select_playlist()
|
|
|
|
def set_next_track(self):
|
|
"Set selected track as next"
|
|
|
|
self.playlist.set_selected_as_next()
|
|
self.update_headers()
|
|
|
|
def test_function(self):
|
|
"Placeholder for test function"
|
|
|
|
import ipdb
|
|
ipdb.set_trace()
|
|
|
|
def test_skip_to_end(self):
|
|
"Skip current track to 1 second before silence"
|
|
|
|
if not self.playlist.music.playing():
|
|
return
|
|
|
|
self.playlist.music.set_position(
|
|
self.playlist.get_current_silence_at() - 1000
|
|
)
|
|
|
|
def test_skip_to_fade(self):
|
|
"Skip current track to 1 second before fade"
|
|
|
|
if not self.playlist.music.playing():
|
|
return
|
|
|
|
self.playlist.music.set_position(
|
|
self.playlist.get_current_fade_at() - 1000
|
|
)
|
|
|
|
def tick(self):
|
|
"""
|
|
Update screen
|
|
|
|
The Time of Day clock is updated every tick (500ms).
|
|
|
|
All other timers are updated every second. As the timers have a
|
|
one-second resolution, updating every 500ms can result in some
|
|
timers updating and then, 500ms later, other timers updating. That
|
|
looks odd.
|
|
"""
|
|
|
|
now = datetime.now()
|
|
|
|
self.lblTOD.setText(now.strftime("%H:%M:%S"))
|
|
|
|
self.even_tick = not self.even_tick
|
|
if not self.even_tick:
|
|
return
|
|
|
|
if self.playlist.music.playing():
|
|
self.playing = True
|
|
playtime = self.playlist.music.get_playtime()
|
|
time_to_fade = (self.playlist.get_current_fade_at() - playtime)
|
|
time_to_silence = (
|
|
self.playlist.get_current_silence_at() - playtime
|
|
)
|
|
time_to_end = (self.playlist.get_current_duration() - playtime)
|
|
|
|
# Elapsed time
|
|
if time_to_end < 500:
|
|
self.label_elapsed_timer.setText(
|
|
helpers.ms_to_mmss(self.playlist.get_current_duration())
|
|
)
|
|
else:
|
|
self.label_elapsed_timer.setText(helpers.ms_to_mmss(playtime))
|
|
|
|
# Time to fade
|
|
self.label_fade_timer.setText(helpers.ms_to_mmss(time_to_fade))
|
|
|
|
# Time to silence
|
|
if time_to_silence < 5000:
|
|
self.frame_silent.setStyleSheet(
|
|
f"background: {Config.COLOUR_ENDING_TIMER}"
|
|
)
|
|
self.enable_play_next_controls()
|
|
elif time_to_fade < 500:
|
|
self.frame_silent.setStyleSheet(
|
|
f"background: {Config.COLOUR_WARNING_TIMER}"
|
|
)
|
|
self.enable_play_next_controls()
|
|
else:
|
|
self.frame_silent.setStyleSheet("")
|
|
|
|
self.label_silent_timer.setText(
|
|
helpers.ms_to_mmss(time_to_silence)
|
|
)
|
|
|
|
# Time to end
|
|
self.label_end_timer.setText(helpers.ms_to_mmss(time_to_end))
|
|
|
|
else:
|
|
if self.playing:
|
|
self.label_end_timer.setText("00:00")
|
|
self.frame_silent.setStyleSheet("")
|
|
self.playing = False
|
|
self.playlist.music_ended()
|
|
self.update_headers()
|
|
|
|
def update_headers(self):
|
|
"Update last / current / next track headers"
|
|
|
|
self.previous_track.setText(
|
|
f"{self.playlist.get_previous_title()} - "
|
|
f"{self.playlist.get_previous_artist()}"
|
|
)
|
|
self.current_track.setText(
|
|
f"{self.playlist.get_current_title()} - "
|
|
f"{self.playlist.get_current_artist()}"
|
|
)
|
|
self.next_track.setText(
|
|
f"{self.playlist.get_next_title()} - "
|
|
f"{self.playlist.get_next_artist()}"
|
|
)
|
|
|
|
def update_statusbar(self):
|
|
pass
|
|
|
|
|
|
def main():
|
|
try:
|
|
app = QApplication(sys.argv)
|
|
win = Window()
|
|
win.show()
|
|
sys.exit(app.exec())
|
|
except Exception:
|
|
EXCEPTION("Unhandled Exception caught by musicmuster.main()")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|