OOPed everything a little more.

This commit is contained in:
The Wobbler 2024-12-22 19:42:48 +01:00
parent 19b6f4dcaa
commit 8811ba7ab4
5 changed files with 113 additions and 69 deletions

View file

@ -1,6 +1,5 @@
#!/usr/bin/python3 #!/usr/bin/python3
from PyQt6.QtCore import QTimer
from ui.main_window import MainWindow from ui.main_window import MainWindow
@ -15,8 +14,5 @@ class GUI:
self.window.show() self.window.show()
def connect(self): def connect(self):
self.window.main_container.track_control.previous_button.triggered.connect(self.app.player.previous_track) pass
self.window.main_container.track_control.toggle_play_button.triggered.connect(self.app.player.toggle_playing)
self.window.main_container.track_control.stop_button.triggered.connect(self.app.player.stop)
self.window.main_container.track_control.next_button.triggered.connect(self.app.player.next_track)

View file

@ -1,9 +1,6 @@
#!/usr/bin/python3 #!/usr/bin/python3
from PyQt6.QtCore import QTimer from .track_control import TrackControl
PROGRESS_UPDATE_RATE = 60
PROGRESS_UPDATE_INTERVAL = 1000 // PROGRESS_UPDATE_RATE
class GUICommunication: class GUICommunication:
@ -14,60 +11,13 @@ class GUICommunication:
def __init__(self, app): def __init__(self, app):
self.app = app self.app = app
self.window = self.app.gui.window self.track_control = TrackControl(self.app)
self.track_control = self.window.main_container.track_control
self.track_progress_slider_dragged = False
self.connect() self.connect()
self.progress_update_timer = QTimer()
self.progress_update_timer.timeout.connect(self.update_progress)
self.progress_update_timer.start(PROGRESS_UPDATE_INTERVAL)
self.on_track_start()
def connect(self): def connect(self):
self.track_control.track_progress_slider.sliderPressed.connect(self.on_progress_slider_press) pass
self.track_control.track_progress_slider.sliderReleased.connect(self.on_progress_slider_release)
def on_progress_slider_press(self):
self.track_progress_slider_dragged = True
def on_progress_slider_release(self):
self.track_progress_slider_dragged = False
self.app.player.seek(self.track_control.track_progress_slider.value())
def on_track_start(self): def on_track_start(self):
if self.app.player.playing: self.track_control.on_track_start()
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)

View file

@ -0,0 +1,94 @@
#!/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.main_container.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()

View file

@ -113,16 +113,6 @@ class Player:
self.app.gui_communication.on_track_start() self.app.gui_communication.on_track_start()
def toggle_playing(self):
if self.playing and self.paused:
self.unpause()
elif self.playing:
self.pause()
else:
self.start_playing()
def stop(self): def stop(self):
self.music_channel.stop() self.music_channel.stop()
self.track_progress.stop() self.track_progress.stop()

View file

@ -6,6 +6,14 @@ from PyQt6.QtWidgets import QSlider
class TrackProgressSlider(QSlider): class TrackProgressSlider(QSlider):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.dragged = False
self.sliderPressed.connect(self.on_press)
self.sliderReleased.connect(self.on_release)
def mousePressEvent(self, event: QMouseEvent): def mousePressEvent(self, event: QMouseEvent):
if event.button() == Qt.MouseButton.LeftButton: if event.button() == Qt.MouseButton.LeftButton:
event.accept() event.accept()
@ -17,3 +25,9 @@ class TrackProgressSlider(QSlider):
return super().mousePressEvent(event) return super().mousePressEvent(event)
def on_press(self):
self.dragged = True
def on_release(self):
self.dragged = False