Skip to content

Commit

Permalink
Adding support to reset all settings (#104)
Browse files Browse the repository at this point in the history
  • Loading branch information
DasAmpharos authored Apr 20, 2024
1 parent dc44f71 commit 9b60b89
Show file tree
Hide file tree
Showing 9 changed files with 82 additions and 3 deletions.
2 changes: 1 addition & 1 deletion eon_timer/resources/main.scss
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ AppWindow {
border-image: url("${background}") 0 0 0 0 stretch stretch;
}

SettingsDialog {
QDialog {
background-color: $neutral-a10;
}

Expand Down
7 changes: 7 additions & 0 deletions eon_timer/settings/action/widget.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Empty file.
33 changes: 33 additions & 0 deletions eon_timer/settings/advanced/widget.py
Original file line number Diff line number Diff line change
@@ -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()
18 changes: 16 additions & 2 deletions eon_timer/settings/dialog.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,29 @@
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


@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:
Expand All @@ -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')
Expand All @@ -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)
7 changes: 7 additions & 0 deletions eon_timer/settings/theme/widget.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
7 changes: 7 additions & 0 deletions eon_timer/settings/timer/widget.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
4 changes: 4 additions & 0 deletions eon_timer/util/properties/property.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
7 changes: 7 additions & 0 deletions eon_timer/util/properties/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand All @@ -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:
Expand Down

0 comments on commit 9b60b89

Please sign in to comment.