307 lines
9.5 KiB
Python
Executable File
307 lines
9.5 KiB
Python
Executable File
#!/usr/bin/python3
|
|
|
|
import sys
|
|
import music
|
|
|
|
|
|
from datetime import datetime, timedelta
|
|
from log import DEBUG
|
|
|
|
from PyQt5.QtCore import Qt, QTimer
|
|
from PyQt5.QtWidgets import (
|
|
QApplication,
|
|
QDialog,
|
|
QFileDialog,
|
|
QInputDialog,
|
|
QListWidgetItem,
|
|
QMainWindow,
|
|
)
|
|
|
|
import helpers
|
|
|
|
from config import Config
|
|
from model import Playlists, Settings, Tracks
|
|
from ui.dlg_search_database_ui import Ui_Dialog
|
|
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.music = music.Music()
|
|
self.playing = False
|
|
self.playlist.music = self.music
|
|
self.connect_signals_slots()
|
|
self.disable_play_next_controls()
|
|
|
|
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)
|
|
|
|
# Hard code to the only playlist we have for now
|
|
if self.playlist.load_playlist("Default"):
|
|
self.update_headers()
|
|
self.enable_play_next_controls()
|
|
self.timer.start(Config.TIMER_MS)
|
|
|
|
def __del__(self):
|
|
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()})
|
|
|
|
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_():
|
|
pass
|
|
# TODO Add to database
|
|
# for fname in dlg.selectedFiles():
|
|
# trackwest = Track(fname)
|
|
# self.add_to_playlist(track)
|
|
|
|
def clear_selection(self):
|
|
self.playlist.clearSelection()
|
|
|
|
def connect_signals_slots(self):
|
|
self.action_Clear_selection.triggered.connect(self.clear_selection)
|
|
self.actionFade.triggered.connect(self.fade)
|
|
self.actionPlay_next.triggered.connect(self.play_next)
|
|
self.actionSearch_database.triggered.connect(self.search_database)
|
|
self.actionSkip_next.triggered.connect(self.play_next)
|
|
self.btnInsertNote.clicked.connect(self.insert_note)
|
|
self.btnPrevious.clicked.connect(self.play_previous)
|
|
self.btnSearchDatabase.clicked.connect(self.search_database)
|
|
self.btnSetNextTrack.clicked.connect(self.set_next_track)
|
|
self.btnSkipNext.clicked.connect(self.play_next)
|
|
self.btnStop.clicked.connect(self.fade)
|
|
|
|
self.timer.timeout.connect(self.tick)
|
|
|
|
def disable_play_next_controls(self):
|
|
self.actionPlay_next.setEnabled(False)
|
|
|
|
def enable_play_next_controls(self):
|
|
self.actionPlay_next.setEnabled(True)
|
|
|
|
def fade(self):
|
|
self.playlist.fade()
|
|
|
|
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):
|
|
dlg = DbDialog(self)
|
|
dlg.exec()
|
|
|
|
def set_next_track(self):
|
|
"Set selected track as next"
|
|
|
|
self.playlist.set_selected_as_next()
|
|
self.update_headers()
|
|
|
|
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.music.playing():
|
|
self.playing = True
|
|
playtime = self.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()}"
|
|
)
|
|
|
|
|
|
class AddNoteDialog(QDialog):
|
|
def __init__(self, parent=None):
|
|
super().__init__(parent)
|
|
self.ui = Ui_dlgAddNote()
|
|
self.ui.setupUi(self)
|
|
|
|
|
|
class DbDialog(QDialog):
|
|
def __init__(self, parent=None):
|
|
super().__init__(parent)
|
|
self.ui = Ui_Dialog()
|
|
self.ui.setupUi(self)
|
|
self.ui.searchString.textEdited.connect(self.chars_typed)
|
|
self.ui.matchList.itemDoubleClicked.connect(self.listdclick)
|
|
|
|
record = Settings.get_int("dbdialog_width")
|
|
width = record.f_int or 800
|
|
record = Settings.get_int("dbdialog_height")
|
|
height = record.f_int or 600
|
|
self.resize(width, height)
|
|
|
|
def __del__(self):
|
|
record = Settings.get_int("dbdialog_height")
|
|
if record.f_int != self.height():
|
|
record.update({'f_int': self.height()})
|
|
|
|
record = Settings.get_int("dbdialog_width")
|
|
if record.f_int != self.width():
|
|
record.update({'f_int': self.width()})
|
|
|
|
def chars_typed(self, s):
|
|
if len(s) >= 3:
|
|
matches = Tracks.search_titles(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 listdclick(self, entry):
|
|
track_id = entry.data(Qt.UserRole)
|
|
track = Tracks.track_from_id(track_id)
|
|
|
|
# Store in current playlist in database
|
|
db_playlist = Playlists.get_only_playlist()
|
|
# TODO: hack position to be at end of list
|
|
db_playlist.add_track(track, 99999)
|
|
|
|
# Add to on-screen playlist
|
|
self.parent().add_to_playlist(track)
|
|
|
|
|
|
def main():
|
|
app = QApplication(sys.argv)
|
|
win = Window()
|
|
win.show()
|
|
sys.exit(app.exec())
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|