from PyQt6.QtWidgets import QWidget, QVBoxLayout, QComboBox, QLabel, QHBoxLayout, QSizePolicy from PyQt6.QtCore import Qt from app.core.main_manager import MainManager, NotificationType from typing import Optional class SettingsWindow(QWidget): def __init__(self, parent: Optional[QWidget] = None) -> None: super().__init__(parent) self.main_manager: MainManager = MainManager.get_instance() self.language_manager = self.main_manager.get_language_manager() self.settings_manager = self.main_manager.get_settings_manager() self.theme_manager = self.main_manager.get_theme_manager() self.observer_manager = self.main_manager.get_observer_manager() self.observer_manager.subscribe(NotificationType.LANGUAGE, self.update_language) # Type hints for UI elements self.language_layout: QHBoxLayout self.languageLabel: QLabel self.languageCombo: QComboBox self.theme_layout: QHBoxLayout self.themeLabel: QLabel self.themeCombo: QComboBox self.audio_format_layout: QHBoxLayout self.audioFormatLabel: QLabel self.audioFormatCombo: QComboBox self.audio_quality_layout: QHBoxLayout self.audioQualityLabel: QLabel self.audioQualityCombo: QComboBox self.setup_ui() def setup_ui(self) -> None: layout: QVBoxLayout = QVBoxLayout(self) layout.setAlignment(Qt.AlignmentFlag.AlignCenter) layout.setSpacing(20) layout.setContentsMargins(20, 20, 20, 20) layout.addStretch(1) self.language_layout = QHBoxLayout() # Paramètres de langue self.languageLabel = QLabel(self.language_manager.get_text("language"),self) self.languageLabel.setMinimumWidth(100) self.languageLabel.setSizePolicy(QSizePolicy.Policy.Minimum, QSizePolicy.Policy.Fixed) self.language_layout.addWidget(self.languageLabel) self.languageCombo = self.createLanguageSelector() self.languageCombo.setSizePolicy(QSizePolicy.Policy.Expanding, QSizePolicy.Policy.Fixed) self.language_layout.addWidget(self.languageCombo) layout.addLayout(self.language_layout) layout.addStretch(1) # Paramètres de thème self.theme_layout = QHBoxLayout() self.themeLabel = QLabel(self.language_manager.get_text("theme"), self) self.themeLabel.setMinimumWidth(100) self.themeLabel.setSizePolicy(QSizePolicy.Policy.Minimum, QSizePolicy.Policy.Fixed) self.theme_layout.addWidget(self.themeLabel) self.themeCombo = self.createThemeSelector() self.themeCombo.setSizePolicy(QSizePolicy.Policy.Expanding, QSizePolicy.Policy.Fixed) self.theme_layout.addWidget(self.themeCombo) layout.addLayout(self.theme_layout) layout.addStretch(1) self.audio_format_layout = QHBoxLayout() self.audioFormatLabel = QLabel(self.language_manager.get_text("audio_format_setting"), self) self.audioFormatLabel.setMinimumWidth(100) self.audioFormatLabel.setSizePolicy(QSizePolicy.Policy.Minimum, QSizePolicy.Policy.Fixed) self.audio_format_layout.addWidget(self.audioFormatLabel) self.audioFormatCombo = self.createAudioFormatSelector() self.audioFormatCombo.setSizePolicy(QSizePolicy.Policy.Expanding, QSizePolicy.Policy.Fixed) self.audio_format_layout.addWidget(self.audioFormatCombo) layout.addLayout(self.audio_format_layout) layout.addStretch(1) self.audio_quality_layout = QHBoxLayout() self.audioQualityLabel = QLabel(self.language_manager.get_text("audio_quality_setting"), self) self.audioQualityLabel.setMinimumWidth(100) self.audioQualityLabel.setSizePolicy(QSizePolicy.Policy.Minimum, QSizePolicy.Policy.Fixed) self.audio_quality_layout.addWidget(self.audioQualityLabel) self.audioQualityCombo = self.createAudioQualitySelector() self.audioQualityCombo.setSizePolicy(QSizePolicy.Policy.Expanding, QSizePolicy.Policy.Fixed) self.audio_quality_layout.addWidget(self.audioQualityCombo) layout.addLayout(self.audio_quality_layout) layout.addStretch(1) self._update_audio_quality_enabled_state() def createLanguageSelector(self) -> QComboBox: combo: QComboBox = QComboBox() # Ajouter toutes les langues disponibles for langCode, langData in self.language_manager.translations.items(): combo.addItem(langData["lang_name"], langCode) # Sélectionner la langue actuelle currentIndex = combo.findData(self.settings_manager.get_language()) combo.setCurrentIndex(currentIndex) combo.currentIndexChanged.connect(self.change_language) return combo def createThemeSelector(self) -> QComboBox: combo: QComboBox = QComboBox() # Ajouter toutes les options de thème disponibles for theme in self.theme_manager.get_themes(): combo.addItem(self.language_manager.get_text(theme.name+"_theme"), theme.name) # Sélectionner le thème actuel currentIndex = combo.findData(self.settings_manager.get_theme()) combo.setCurrentIndex(currentIndex) combo.currentIndexChanged.connect(self.change_theme) return combo def change_language(self, index: int) -> None: self.settings_manager.set_language(self.languageCombo.itemData(index)) def change_theme(self, index: int) -> None: theme: str = self.themeCombo.itemData(index) self.settings_manager.set_theme(theme) def createAudioFormatSelector(self) -> QComboBox: combo: QComboBox = QComboBox() combo.addItem(self.language_manager.get_text("audio_format_mp3"), "mp3") combo.addItem(self.language_manager.get_text("audio_format_best"), "best") current_index = combo.findData(self.settings_manager.get_audio_format()) combo.setCurrentIndex(current_index if current_index >= 0 else 0) combo.currentIndexChanged.connect(self.change_audio_format) return combo def createAudioQualitySelector(self) -> QComboBox: combo: QComboBox = QComboBox() combo.addItem("320 kbps", "320") combo.addItem("192 kbps", "192") combo.addItem("128 kbps", "128") current_index = combo.findData(self.settings_manager.get_audio_quality()) combo.setCurrentIndex(current_index if current_index >= 0 else 0) combo.currentIndexChanged.connect(self.change_audio_quality) return combo def change_audio_format(self, index: int) -> None: audio_format = self.audioFormatCombo.itemData(index) self.settings_manager.set_audio_format(audio_format) self._update_audio_quality_enabled_state() def change_audio_quality(self, index: int) -> None: quality = self.audioQualityCombo.itemData(index) self.settings_manager.set_audio_quality(quality) def _update_audio_quality_enabled_state(self) -> None: self.audioQualityCombo.setEnabled(self.settings_manager.get_audio_format() == "mp3") def update_language(self) -> None: self.languageLabel.setText(self.language_manager.get_text("language")) self.themeLabel.setText(self.language_manager.get_text("theme")) self.audioFormatLabel.setText(self.language_manager.get_text("audio_format_setting")) self.audioQualityLabel.setText(self.language_manager.get_text("audio_quality_setting")) # Mettre à jour les textes dans la combo de thème for i in range(self.themeCombo.count()): self.themeCombo.setItemText(i, self.language_manager.get_text(self.themeCombo.itemData(i)+ "_theme")) current_audio_format = self.audioFormatCombo.currentData() self.audioFormatCombo.setItemText(0, self.language_manager.get_text("audio_format_mp3")) self.audioFormatCombo.setItemText(1, self.language_manager.get_text("audio_format_best")) format_index = self.audioFormatCombo.findData(current_audio_format) if format_index >= 0: self.audioFormatCombo.setCurrentIndex(format_index)