2024-12-20 18:02:59 +01:00
|
|
|
#!/usr/bin/python3
|
|
|
|
|
|
|
|
from PyQt6.QtGui import QIcon
|
2024-12-23 16:02:22 +01:00
|
|
|
from PyQt6.QtWidgets import QToolBar, QLabel
|
2024-12-22 19:09:37 +01:00
|
|
|
from .track_progress_slider import TrackProgressSlider
|
2024-12-20 18:02:59 +01:00
|
|
|
|
|
|
|
|
|
|
|
class TrackControl(QToolBar):
|
2025-01-25 16:02:03 +01:00
|
|
|
def __init__(self, app, parent=None):
|
2024-12-20 18:02:59 +01:00
|
|
|
super().__init__(parent)
|
|
|
|
|
2025-01-25 16:02:03 +01:00
|
|
|
self.app = app
|
|
|
|
|
2025-01-25 21:46:48 +01:00
|
|
|
self.setWindowTitle("Track Control")
|
|
|
|
|
2025-01-25 16:02:03 +01:00
|
|
|
self.play_icon = QIcon.fromTheme(QIcon.ThemeIcon.MediaPlaybackStart)
|
|
|
|
self.pause_icon = QIcon.fromTheme(QIcon.ThemeIcon.MediaPlaybackPause)
|
|
|
|
|
2024-12-20 18:02:59 +01:00
|
|
|
icon = QIcon.fromTheme(QIcon.ThemeIcon.MediaSkipBackward)
|
|
|
|
self.previous_button = self.addAction(icon, "Previous")
|
|
|
|
|
2025-01-25 16:02:03 +01:00
|
|
|
self.toggle_play_button = self.addAction(self.play_icon, "Play/Pause")
|
2024-12-20 18:02:59 +01:00
|
|
|
|
|
|
|
icon = QIcon.fromTheme(QIcon.ThemeIcon.MediaPlaybackStop)
|
|
|
|
self.stop_button = self.addAction(icon, "Stop")
|
|
|
|
|
|
|
|
icon = QIcon.fromTheme(QIcon.ThemeIcon.MediaSkipForward)
|
|
|
|
self.next_button = self.addAction(icon, "Next")
|
|
|
|
|
2024-12-21 20:50:09 +01:00
|
|
|
self.progress_indicator = QLabel("0:00")
|
|
|
|
self.addWidget(self.progress_indicator)
|
2024-12-20 18:11:25 +01:00
|
|
|
|
2025-01-25 16:02:03 +01:00
|
|
|
self.track_progress_slider = TrackProgressSlider(app, self)
|
2024-12-21 19:00:06 +01:00
|
|
|
self.addWidget(self.track_progress_slider)
|
2024-12-20 18:02:59 +01:00
|
|
|
|
2024-12-21 16:07:27 +01:00
|
|
|
self.track_length_indicator = QLabel("0:00")
|
2024-12-20 18:11:25 +01:00
|
|
|
self.addWidget(self.track_length_indicator)
|
|
|
|
|
2025-01-25 16:02:03 +01:00
|
|
|
self.connect()
|
|
|
|
self.on_track_change(None, None)
|
|
|
|
|
|
|
|
def connect(self):
|
|
|
|
self.previous_button.triggered.connect(self.previous_track)
|
|
|
|
self.toggle_play_button.triggered.connect(self.toggle_playing)
|
|
|
|
self.stop_button.triggered.connect(self.stop)
|
|
|
|
self.next_button.triggered.connect(self.next_track)
|
|
|
|
|
|
|
|
def previous_track(self):
|
2025-02-04 11:17:35 +01:00
|
|
|
if self.app.player.current_playlist is not None and self.app.player.current_playlist.has_tracks():
|
2025-01-25 16:02:03 +01:00
|
|
|
self.app.player.previous_track()
|
|
|
|
|
|
|
|
def stop(self):
|
2025-02-04 11:17:35 +01:00
|
|
|
if self.app.player.current_playlist is not None and self.app.player.current_playlist.has_tracks():
|
2025-01-25 16:02:03 +01:00
|
|
|
self.app.player.stop()
|
|
|
|
|
|
|
|
def next_track(self):
|
2025-02-04 11:17:35 +01:00
|
|
|
if self.app.player.current_playlist is not None and self.app.player.current_playlist.has_tracks():
|
2025-01-25 16:02:03 +01:00
|
|
|
self.app.player.next_track()
|
|
|
|
|
|
|
|
def on_track_change(self, previous_track, track):
|
|
|
|
if self.app.player.playing:
|
|
|
|
duration = 0
|
|
|
|
|
|
|
|
if track:
|
|
|
|
duration = track.duration
|
|
|
|
|
|
|
|
self.track_progress_slider.setRange(
|
|
|
|
0,
|
|
|
|
duration
|
|
|
|
)
|
|
|
|
|
|
|
|
self.track_length_indicator.setText(self.app.utils.format_time(duration))
|
|
|
|
|
|
|
|
self.track_progress_slider.update_progress()
|
|
|
|
|
|
|
|
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()
|
|
|
|
|
2025-02-03 17:53:35 +01:00
|
|
|
# stopped but tracks in the current playlist
|
|
|
|
elif self.app.player.current_playlist is not None and self.app.player.current_playlist.has_tracks():
|
2025-01-25 16:02:03 +01:00
|
|
|
self.app.player.start_playing()
|
|
|
|
|
2025-02-03 17:53:35 +01:00
|
|
|
elif self.app.player.current_playlist is None:
|
2025-02-03 17:59:37 +01:00
|
|
|
if self.app.settings.latest_playlist is not None:
|
2025-02-03 18:00:42 +01:00
|
|
|
for playlist in self.app.library.playlists: # get loaded playlist by the path of the latest playlist
|
2025-02-03 17:59:37 +01:00
|
|
|
if playlist.path == self.app.settings.latest_playlist:
|
|
|
|
self.app.player.start_playlist(playlist)
|
|
|
|
|
|
|
|
break
|
2025-01-26 16:49:09 +01:00
|
|
|
|
2025-01-25 16:02:03 +01:00
|
|
|
def on_playstate_update(self):
|
|
|
|
if self.app.player.playing:
|
|
|
|
if self.app.player.paused:
|
|
|
|
self.toggle_play_button.setIcon(self.play_icon)
|
|
|
|
|
|
|
|
else:
|
|
|
|
self.toggle_play_button.setIcon(self.pause_icon)
|
|
|
|
|
|
|
|
else:
|
|
|
|
self.toggle_play_button.setIcon(self.play_icon)
|
|
|
|
|