Refacto en sous fichiers pour la maintenabilité
This commit is contained in:
85
src/controllers/audio_controller.py
Normal file
85
src/controllers/audio_controller.py
Normal file
@@ -0,0 +1,85 @@
|
||||
from PySide6.QtCore import QFile, QBuffer, QByteArray, QIODevice
|
||||
from PySide6.QtMultimedia import QMediaPlayer, QAudioOutput
|
||||
|
||||
from config.config_manager import ConfigManager, VOLUME_KEY
|
||||
|
||||
from src.constants import MP3_PATH
|
||||
|
||||
class AudioController:
|
||||
# Encapsule toute la logique audio : lecture, volume, mute.
|
||||
|
||||
def __init__(self, config: ConfigManager, slider, mute_btn):
|
||||
self._config = config
|
||||
self._slider = slider
|
||||
self._mute_btn = mute_btn
|
||||
|
||||
# Lecteur
|
||||
self._player = QMediaPlayer()
|
||||
self._output = QAudioOutput()
|
||||
self._player.setAudioOutput(self._output)
|
||||
self._player.setLoops(-1)
|
||||
|
||||
# Chargement du MP3 depuis les ressources Qt
|
||||
mp3file = QFile(MP3_PATH)
|
||||
mp3file.open(QFile.ReadOnly)
|
||||
mp3data = mp3file.readAll()
|
||||
mp3file.close()
|
||||
|
||||
self._buffer = QBuffer()
|
||||
self._buffer.setData(QByteArray(mp3data))
|
||||
self._buffer.open(QIODevice.ReadOnly)
|
||||
self._player.setSourceDevice(self._buffer)
|
||||
|
||||
# État initial
|
||||
volume = config.get_volume()
|
||||
self._is_muted = volume == 0
|
||||
self._previous_volume = volume if volume != 0 else config.get_default(VOLUME_KEY)
|
||||
|
||||
self._apply_volume(volume, save=False)
|
||||
self._refresh_mute_btn()
|
||||
self._player.play()
|
||||
|
||||
# Connexions
|
||||
self._slider.valueChanged.connect(self._on_slider_changed)
|
||||
self._mute_btn.clicked.connect(self.toggle_mute)
|
||||
|
||||
# ------------------------------------------------------------------
|
||||
# Public API
|
||||
# ------------------------------------------------------------------
|
||||
|
||||
def toggle_mute(self) -> None:
|
||||
if not self._is_muted:
|
||||
self._previous_volume = self._slider.value()
|
||||
self._apply_volume(0)
|
||||
self._is_muted = True
|
||||
else:
|
||||
self._apply_volume(self._previous_volume)
|
||||
self._is_muted = False
|
||||
|
||||
# ------------------------------------------------------------------
|
||||
# Private helpers
|
||||
# ------------------------------------------------------------------
|
||||
|
||||
def _on_slider_changed(self, value: int) -> None:
|
||||
self._is_muted = value == 0
|
||||
self._output.setVolume(value / 100.0)
|
||||
self._config.set_volume(value)
|
||||
self._refresh_mute_btn()
|
||||
|
||||
def _apply_volume(self, value: int, save: bool = True) -> None:
|
||||
self._slider.blockSignals(True)
|
||||
self._slider.setValue(value)
|
||||
self._slider.blockSignals(False)
|
||||
|
||||
self._output.setVolume(value / 100.0)
|
||||
|
||||
if save:
|
||||
self._config.set_volume(value)
|
||||
|
||||
self._refresh_mute_btn()
|
||||
|
||||
def _refresh_mute_btn(self) -> None:
|
||||
muted = self._slider.value() == 0
|
||||
self._mute_btn.setProperty("muted", muted)
|
||||
self._mute_btn.style().unpolish(self._mute_btn)
|
||||
self._mute_btn.style().polish(self._mute_btn)
|
||||
32
src/controllers/glow_animator.py
Normal file
32
src/controllers/glow_animator.py
Normal file
@@ -0,0 +1,32 @@
|
||||
from PySide6.QtCore import QPropertyAnimation, QEasingCurve
|
||||
from PySide6.QtWidgets import QGraphicsDropShadowEffect
|
||||
|
||||
from src.constants import GLOW_COLOR, GLOW_BLUR_BASE, GLOW_BLUR_PEAK, GLOW_ANIM_DURATION
|
||||
|
||||
|
||||
class GlowAnimator:
|
||||
# Gère l'effet de lueur pulsée sur un widget.
|
||||
|
||||
def __init__(self, widget):
|
||||
self._widget = widget
|
||||
|
||||
self._effect = QGraphicsDropShadowEffect(widget)
|
||||
self._effect.setBlurRadius(GLOW_BLUR_BASE)
|
||||
self._effect.setOffset(0, 0)
|
||||
self._effect.setColor(GLOW_COLOR)
|
||||
|
||||
self._anim = QPropertyAnimation(self._effect, b"blurRadius")
|
||||
self._anim.setDuration(GLOW_ANIM_DURATION)
|
||||
self._anim.setStartValue(GLOW_BLUR_BASE)
|
||||
self._anim.setKeyValueAt(0.5, GLOW_BLUR_PEAK)
|
||||
self._anim.setEndValue(GLOW_BLUR_BASE)
|
||||
self._anim.setEasingCurve(QEasingCurve.InOutQuad)
|
||||
self._anim.setLoopCount(-1)
|
||||
|
||||
def start(self) -> None:
|
||||
self._widget.setGraphicsEffect(self._effect)
|
||||
self._anim.start()
|
||||
|
||||
def stop(self) -> None:
|
||||
self._anim.stop()
|
||||
self._widget.setGraphicsEffect(None)
|
||||
25
src/controllers/window_dragger.py
Normal file
25
src/controllers/window_dragger.py
Normal file
@@ -0,0 +1,25 @@
|
||||
from PySide6 import QtGui
|
||||
from PySide6.QtCore import Qt
|
||||
from PySide6.QtWidgets import QMainWindow
|
||||
|
||||
|
||||
class WindowDragger:
|
||||
# Permet de déplacer une fenêtre sans barre de titre.
|
||||
|
||||
def __init__(self, window: QMainWindow):
|
||||
self._window = window
|
||||
self._drag_pos = None
|
||||
|
||||
def mouse_press(self, event: QtGui.QMouseEvent) -> None:
|
||||
if event.button() == Qt.MouseButton.LeftButton:
|
||||
self._drag_pos = (
|
||||
event.globalPosition().toPoint()
|
||||
- self._window.frameGeometry().topLeft()
|
||||
)
|
||||
|
||||
def mouse_move(self, event: QtGui.QMouseEvent) -> None:
|
||||
if event.buttons() & Qt.MouseButton.LeftButton and self._drag_pos is not None:
|
||||
self._window.move(event.globalPosition().toPoint() - self._drag_pos)
|
||||
|
||||
def mouse_release(self, _event) -> None:
|
||||
self._drag_pos = None
|
||||
Reference in New Issue
Block a user