Wobuzz/wobuzz/gui_communication/track_control.py

114 lines
3.8 KiB
Python
Raw Permalink Normal View History

2024-12-22 19:42:48 +01:00
#!/usr/bin/python3
from PyQt6.QtCore import QTimer
from PyQt6.QtGui import QIcon
2024-12-22 19:42:48 +01:00
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
2024-12-22 19:42:48 +01:00
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)
2024-12-22 19:42:48 +01:00
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)
2024-12-22 19:42:48 +01:00
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):
2024-12-24 17:22:30 +01:00
if self.app.player.current_playlist.has_tracks():
2024-12-22 19:42:48 +01:00
self.app.player.previous_track()
def stop(self):
2024-12-24 17:22:30 +01:00
if self.app.player.current_playlist.has_tracks():
2024-12-22 19:42:48 +01:00
self.app.player.stop()
def next_track(self):
2024-12-24 17:22:30 +01:00
if self.app.player.current_playlist.has_tracks():
2024-12-22 19:42:48 +01:00
self.app.player.next_track()
def on_slider_release(self):
2024-12-24 17:22:30 +01:00
if self.app.player.current_playlist.has_tracks():
2024-12-22 19:42:48 +01:00
self.app.player.seek(self.track_control.track_progress_slider.value())
def on_track_change(self, previous_track, track):
2024-12-22 19:42:48 +01:00
if self.app.player.playing:
duration = 0
if track:
duration = track.duration
2024-12-22 19:42:48 +01:00
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
2024-12-24 17:22:30 +01:00
track_duration = self.app.player.current_playlist.current_track.duration
2024-12-22 19:42:48 +01:00
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
2024-12-22 19:42:48 +01:00
self.app.player.unpause()
elif self.app.player.playing: # playing
2024-12-22 19:42:48 +01:00
self.app.player.pause()
2024-12-24 17:22:30 +01:00
elif self.app.player.current_playlist.has_tracks(): # stopped but tracks in the current playlist
2024-12-22 19:42:48 +01:00
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)
2024-12-22 19:42:48 +01:00