musicmuster/app/musicmuster.py
2021-04-04 12:57:43 +01:00

252 lines
7.9 KiB
Python
Executable File

#!/usr/bin/python3
import sys
import music
from datetime import datetime, timedelta
# from log import DEBUG, ERROR
from PyQt5.QtCore import Qt, QTimer
from PyQt5.QtWidgets import (
QApplication,
QDialog,
QFileDialog,
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.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.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.btnPrevious.clicked.connect(self.play_previous)
self.btnSearchDatabase.clicked.connect(self.search_database)
self.btnSetNextTrack.clicked.connect(self.playlist.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 play_next(self):
self.playlist.play_next()
self.disable_play_next_controls()
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()}"
)
# 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):
# TODO
pass
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():
playtime = self.music.get_playtime()
self.label_elapsed_timer.setText(helpers.ms_to_mmss(playtime))
self.label_fade_timer.setText(
helpers.ms_to_mmss(
self.playlist.get_current_fade_at() - playtime)
)
time_to_silence = (
self.playlist.get_current_silence_at() - playtime
)
if time_to_silence < 500:
self.label_silent_timer.setText("00:00")
self.enable_play_next_controls()
else:
self.label_silent_timer.setText(
helpers.ms_to_mmss(time_to_silence)
)
self.label_end_timer.setText(
helpers.ms_to_mmss(
self.playlist.get_current_duration() - playtime))
else:
# When music ends, update playlist display
self.playlist.update_playlist_colours()
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()