#!/usr/bin/python3 import sys from datetime import datetime, timedelta from log import 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() 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 __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_(): for fname in dlg.selectedFiles(): track = add_path_to_db(fname) self.playlist.add_to_playlist(track) 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.playlist.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.btnAddFile.clicked.connect(self.add_file) 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.playlist.fade) 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): self.actionPlay_next.setEnabled(False) def enable_play_next_controls(self): self.actionPlay_next.setEnabled(True) 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 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()