#!/usr/bin/python3 from PyQt6.QtCore import QTimer PROGRESS_UPDATE_RATE = 60 PROGRESS_UPDATE_INTERVAL = 1000 // PROGRESS_UPDATE_RATE class TrackControl: """ Handles events for the track_control widget. """ def __init__(self, app): self.app = app self.track_control = self.app.gui.window.track_control self.track_progress_slider = self.track_control.track_progress_slider self.progress_update_timer = QTimer() self.progress_update_timer.timeout.connect(self.update_progress) self.progress_update_timer.start(PROGRESS_UPDATE_INTERVAL) self.connect() self.on_track_start() def connect(self): self.track_control.previous_button.triggered.connect(self.previous_track) self.track_control.toggle_play_button.triggered.connect(self.toggle_playing) self.track_control.stop_button.triggered.connect(self.stop) self.track_control.next_button.triggered.connect(self.next_track) self.track_control.track_progress_slider.sliderReleased.connect(self.on_slider_release) def previous_track(self): if len(self.app.player.current_playlist) > 0: self.app.player.previous_track() def stop(self): if len(self.app.player.current_playlist) > 0: self.app.player.stop() def next_track(self): if len(self.app.player.current_playlist) > 0: self.app.player.next_track() def on_slider_release(self): if len(self.app.player.current_playlist) > 0: self.app.player.seek(self.track_control.track_progress_slider.value()) def on_track_start(self): if self.app.player.playing: duration = self.app.player.current_track.duration self.track_control.track_progress_slider.setRange( 0, duration ) self.track_control.track_length_indicator.setText(self.app.utils.format_time(duration)) self.update_progress() def update_progress(self): if not self.track_progress_slider.dragged: if self.app.player.playing: remaining = self.app.player.track_progress.timer.remainingTime() if remaining == -1: remaining = self.app.player.track_progress.remaining_time track_duration = self.app.player.current_track.duration progress = track_duration - remaining self.track_control.progress_indicator.setText(self.app.utils.format_time(progress)) self.track_control.track_progress_slider.setValue(progress) else: self.track_control.progress_indicator.setText(self.app.utils.format_time(0)) self.track_control.track_progress_slider.setValue(0) def toggle_playing(self): if self.app.player.playing and self.app.player.paused: self.app.player.unpause() elif self.app.player.playing: self.app.player.pause() elif len(self.app.player.current_playlist) > 0: self.app.player.start_playing()