musicmuster/app/musicmuster.py
2021-04-03 22:45:30 +01:00

251 lines
7.9 KiB
Python
Executable File

#!/usr/bin/python3
import sys
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.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
self.playlist.load_playlist("Default")
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.actionPlay_selected.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_selected.setEnabled(False)
self.actionPlay_next.setEnabled(False)
def enable_play_next_controls(self):
self.actionPlay_selected.setEnabled(True)
self.actionPlay_next.setEnabled(True)
def fade(self):
self.playlist.fade()
def play_next(self):
self.music.play_next()
self.current_track.setText(
f"{self.music.get_current_title()} - "
f"{self.music.get_current_artist()}"
)
self.previous_track.setText(
f"{self.music.get_previous_title()} - "
f"{self.music.get_previous_artist()}"
)
self.set_next_track()
# Set time clocks
now = datetime.now()
self.label_start_tod.setText(now.strftime("%H:%M:%S"))
silence_at = self.music.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.music.get_current_fade_at()))
self.set_playlist_colours()
def play_previous(self):
"Resume playing last track"
# TODO
pass
def search_database(self):
dlg = DbDialog(self)
dlg.exec()
def tick(self):
pass
# self.current_time.setText(now.strftime("%H:%M:%S"))
# if self.music.playing():
# playtime = self.music.get_current_playtime()
# self.label_elapsed_timer.setText(helpers.ms_to_mmss(playtime))
# self.label_fade_timer.setText(
# helpers.ms_to_mmss(self.music.get_current_fade_at() - playtime)
# )
# self.label_silent_timer.setText(
# helpers.ms_to_mmss(
# self.music.get_current_silence_at() - playtime))
# self.label_end_timer.setText(
# helpers.ms_to_mmss(
# self.music.get_current_duration() - playtime))
# else:
# # When music ends, ensure next track is selected
# if self.playlist.selectionModel().hasSelection():
# row = self.playlist.currentRow()
# track_id = int(self.playlist.item(row, 0).text())
# if track_id == self.music.get_current_track_id():
# # Current track highlighted: select next
# try:
# self.playlist.selectRow(row + 1)
# except AttributeError:
# # TODO
# pass
def update_tod_clock(self):
"Update time of day clock"
# TODO
pass
def update_track_headers(self):
"Update last/current/next track header"
# TODO
pass
def update_track_clocks(self):
"Update started at, silent at, clock times"
# TODO
pass
def update_track_timers(self):
"Update elapsed time, etc, timers"
# TODO
pass
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()