#!/usr/bin/python3 from PyQt6.QtCore import QTimer from PyQt6.QtGui import QIcon 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.play_icon = QIcon.fromTheme(QIcon.ThemeIcon.MediaPlaybackStart) self.pause_icon = QIcon.fromTheme(QIcon.ThemeIcon.MediaPlaybackPause) 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_change(None, None) 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 self.app.player.current_playlist.has_tracks(): self.app.player.previous_track() def stop(self): if self.app.player.current_playlist.has_tracks(): self.app.player.stop() def next_track(self): if self.app.player.current_playlist.has_tracks(): self.app.player.next_track() def on_slider_release(self): if self.app.player.current_playlist.has_tracks(): self.app.player.seek(self.track_control.track_progress_slider.value()) def on_track_change(self, previous_track, track): if self.app.player.playing: duration = 0 if track: duration = 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_playlist.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: # paused self.app.player.unpause() elif self.app.player.playing: # playing self.app.player.pause() elif self.app.player.current_playlist.has_tracks(): # stopped but tracks in the current playlist self.app.player.start_playing() def on_playstate_update(self): if self.app.player.playing: if self.app.player.paused: self.track_control.toggle_play_button.setIcon(self.play_icon) else: self.track_control.toggle_play_button.setIcon(self.pause_icon) else: self.track_control.toggle_play_button.setIcon(self.play_icon)