diff --git a/eon_timer/resources/main.scss b/eon_timer/resources/main.scss index a59ed7a3..939dc04d 100644 --- a/eon_timer/resources/main.scss +++ b/eon_timer/resources/main.scss @@ -35,7 +35,7 @@ AppWindow { border-image: url("${background}") 0 0 0 0 stretch stretch; } -SettingsDialog { +QDialog { background-color: $neutral-a10; } diff --git a/eon_timer/settings/action/widget.py b/eon_timer/settings/action/widget.py index 1d6a238f..58e20834 100644 --- a/eon_timer/settings/action/widget.py +++ b/eon_timer/settings/action/widget.py @@ -105,6 +105,13 @@ def on_accepted(self): self.model.settings_changed.emit() def on_rejected(self): + self.__reset_properties() + + def on_reset(self): + self.model.reset() + self.__reset_properties() + + def __reset_properties(self): self.mode.update(self.model.mode) self.sound.update(self.model.sound) self.custom_sound.update(self.model.custom_sound) diff --git a/eon_timer/settings/advanced/__init__.py b/eon_timer/settings/advanced/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/eon_timer/settings/advanced/widget.py b/eon_timer/settings/advanced/widget.py new file mode 100644 index 00000000..17f368dd --- /dev/null +++ b/eon_timer/settings/advanced/widget.py @@ -0,0 +1,33 @@ +from typing import Final + +from PySide6.QtCore import Signal +from PySide6.QtGui import Qt +from PySide6.QtWidgets import QWidget, QPushButton, QVBoxLayout, QMessageBox + +from eon_timer.util.injector import component + + +@component() +class AdvancedSettingsWidget(QWidget): + on_reset: Final[Signal] = Signal() + + def __init__(self): + super().__init__() + self.__init_components() + + def __init_components(self): + layout = QVBoxLayout(self) + layout.setSpacing(10) + # ----- reset button ----- + button = QPushButton('Reset Settings') + button.clicked.connect(self.__on_reset) + layout.addWidget(button, alignment=Qt.AlignmentFlag.AlignTop) + + def __on_reset(self): + reply = QMessageBox.warning(self, + 'Warning', + 'Are you sure you want to reset all settings? This operation cannot be undone.', + QMessageBox.StandardButton.Yes | QMessageBox.StandardButton.No, + QMessageBox.StandardButton.No) + if reply == QMessageBox.StandardButton.Yes: + self.on_reset.emit() diff --git a/eon_timer/settings/dialog.py b/eon_timer/settings/dialog.py index 8d67ceb7..5d147f62 100644 --- a/eon_timer/settings/dialog.py +++ b/eon_timer/settings/dialog.py @@ -1,10 +1,11 @@ from typing import Final -from PySide6.QtCore import Qt +from PySide6.QtCore import Qt, QSettings from PySide6.QtWidgets import (QDialog, QGridLayout, QPushButton, QTabWidget) from eon_timer import component from eon_timer.settings.action.widget import ActionSettingsWidget +from eon_timer.settings.advanced.widget import AdvancedSettingsWidget from eon_timer.settings.theme.widget import ThemeSettingsWidget from eon_timer.settings.timer.widget import TimerSettingsWidget @@ -12,13 +13,17 @@ @component() class SettingsDialog(QDialog): def __init__(self, + settings: QSettings, action_settings_widget: ActionSettingsWidget, timer_settings_widget: TimerSettingsWidget, - theme_settings_widget: ThemeSettingsWidget) -> None: + theme_settings_widget: ThemeSettingsWidget, + advanced_settings_widget: AdvancedSettingsWidget) -> None: super().__init__() + self.settings: Final[QSettings] = settings self.action_settings_widget: Final[ActionSettingsWidget] = action_settings_widget self.timer_settings_widget: Final[TimerSettingsWidget] = timer_settings_widget self.theme_settings_widget: Final[ThemeSettingsWidget] = theme_settings_widget + self.advanced_settings_widget: Final[AdvancedSettingsWidget] = advanced_settings_widget self.__init_components() def __init_components(self) -> None: @@ -35,6 +40,8 @@ def __init_components(self) -> None: tabs.addTab(self.action_settings_widget, 'Action') tabs.addTab(self.timer_settings_widget, 'Timer') tabs.addTab(self.theme_settings_widget, 'Theme') + tabs.addTab(self.advanced_settings_widget, 'Advanced') + self.advanced_settings_widget.on_reset.connect(self.__on_reset) layout.addWidget(tabs, 0, 0, 1, 2) # ----- cancel button ----- button = QPushButton('Cancel') @@ -57,3 +64,10 @@ def __on_cancelled(self) -> None: self.timer_settings_widget.on_rejected() self.theme_settings_widget.on_rejected() self.done(QDialog.DialogCode.Rejected) + + def __on_reset(self): + self.settings.clear() + self.action_settings_widget.on_reset() + self.timer_settings_widget.on_reset() + self.theme_settings_widget.on_reset() + self.done(QDialog.DialogCode.Accepted) diff --git a/eon_timer/settings/theme/widget.py b/eon_timer/settings/theme/widget.py index d3349e45..a463cf07 100644 --- a/eon_timer/settings/theme/widget.py +++ b/eon_timer/settings/theme/widget.py @@ -34,4 +34,11 @@ def on_accepted(self): self.model.theme.update(self.theme) def on_rejected(self): + self.__reset_properties() + + def on_reset(self): + self.model.reset() + self.__reset_properties() + + def __reset_properties(self): self.theme.update(self.model.theme) diff --git a/eon_timer/settings/timer/widget.py b/eon_timer/settings/timer/widget.py index 022d0668..f0719e9d 100644 --- a/eon_timer/settings/timer/widget.py +++ b/eon_timer/settings/timer/widget.py @@ -54,6 +54,13 @@ def on_accepted(self): self.model.precision_calibration.update(self.precision_calibration) def on_rejected(self): + self.__reset_properties() + + def on_reset(self): + self.model.reset() + self.__reset_properties() + + def __reset_properties(self): self.console.update(self.model.console) self.refresh_interval.update(self.model.refresh_interval) self.precision_calibration.update(self.model.precision_calibration) diff --git a/eon_timer/util/properties/property.py b/eon_timer/util/properties/property.py index 1a42f274..62078923 100644 --- a/eon_timer/util/properties/property.py +++ b/eon_timer/util/properties/property.py @@ -12,6 +12,7 @@ def __init__(self, transient: bool = False): if value_type is None and initial_value is None: raise ValueError('value_type must be specified if initial_value is None') + self.initial_value: Final[T | None] = initial_value self.value_type: Final[Type[T]] = value_type or type(initial_value) self.__change_listeners: list[PropertyChangeListener] = [] self.__transient = transient @@ -34,6 +35,9 @@ def set(self, new_value: T) -> None: for listener in self.__change_listeners: listener(event) + def reset(self): + self.set(self.initial_value) + @property def transient(self) -> bool: return self.__transient diff --git a/eon_timer/util/properties/settings.py b/eon_timer/util/properties/settings.py index e5665582..3989296b 100644 --- a/eon_timer/util/properties/settings.py +++ b/eon_timer/util/properties/settings.py @@ -15,12 +15,14 @@ def __init__(self, settings: QSettings): self_type = type(self) self.settings: Final[QSettings] = settings + self.__properties: Final[list[Property]] = [] settings.beginGroup(self.group) for name, value in self_type.__dict__.items(): if isinstance(value, Property): if settings.contains(name): new_value = settings.value(name, None, value.value_type) value.set(new_value) + self.__properties.append(value) setattr(self, name, value) settings.endGroup() @@ -33,6 +35,11 @@ def _on_close(self): self.settings.setValue(name, value.get()) self.settings.endGroup() + def reset(self): + for prop in self.__properties: + prop.reset() + self.settings_changed.emit() + @property @abstractmethod def group(self) -> str: