diff --git a/wobuzz/gui.py b/wobuzz/gui.py index 1490efc..4d44f21 100644 --- a/wobuzz/gui.py +++ b/wobuzz/gui.py @@ -1,6 +1,5 @@ #!/usr/bin/python3 -from PyQt6.QtCore import QTimer from ui.main_window import MainWindow @@ -15,8 +14,5 @@ class GUI: self.window.show() def connect(self): - self.window.main_container.track_control.previous_button.triggered.connect(self.app.player.previous_track) - 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) + pass diff --git a/wobuzz/gui_communication/gui_communication.py b/wobuzz/gui_communication/gui_communication.py index f350bf1..22abad5 100644 --- a/wobuzz/gui_communication/gui_communication.py +++ b/wobuzz/gui_communication/gui_communication.py @@ -1,9 +1,6 @@ #!/usr/bin/python3 -from PyQt6.QtCore import QTimer - -PROGRESS_UPDATE_RATE = 60 -PROGRESS_UPDATE_INTERVAL = 1000 // PROGRESS_UPDATE_RATE +from .track_control import TrackControl class GUICommunication: @@ -14,60 +11,13 @@ class GUICommunication: def __init__(self, app): self.app = app - self.window = self.app.gui.window - self.track_control = self.window.main_container.track_control - - self.track_progress_slider_dragged = False + self.track_control = TrackControl(self.app) 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): - self.track_control.track_progress_slider.sliderPressed.connect(self.on_progress_slider_press) - 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()) + pass 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) + self.track_control.on_track_start() diff --git a/wobuzz/gui_communication/track_control.py b/wobuzz/gui_communication/track_control.py new file mode 100644 index 0000000..ce42a0d --- /dev/null +++ b/wobuzz/gui_communication/track_control.py @@ -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() + diff --git a/wobuzz/player/player.py b/wobuzz/player/player.py index c40344a..715a65b 100644 --- a/wobuzz/player/player.py +++ b/wobuzz/player/player.py @@ -113,16 +113,6 @@ class Player: 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): self.music_channel.stop() self.track_progress.stop() diff --git a/wobuzz/ui/track_progress_slider.py b/wobuzz/ui/track_progress_slider.py index cf28a66..5ee04d9 100644 --- a/wobuzz/ui/track_progress_slider.py +++ b/wobuzz/ui/track_progress_slider.py @@ -6,6 +6,14 @@ from PyQt6.QtWidgets import 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): if event.button() == Qt.MouseButton.LeftButton: event.accept() @@ -17,3 +25,9 @@ class TrackProgressSlider(QSlider): return super().mousePressEvent(event) + def on_press(self): + self.dragged = True + + def on_release(self): + self.dragged = False +