From f94ff084119b0fabd83090e2c2ee588988954cde Mon Sep 17 00:00:00 2001 From: jupfi Date: Sun, 26 May 2024 16:21:28 +0200 Subject: [PATCH] Implemented default_settings. --- .../base_spectrometer_controller.py | 7 +++++++ .../base_spectrometer_model.py | 20 +++++++++++++++++++ .../base_spectrometer_view.py | 5 +++++ src/nqrduck_spectrometer/controller.py | 2 ++ 4 files changed, 34 insertions(+) diff --git a/src/nqrduck_spectrometer/base_spectrometer_controller.py b/src/nqrduck_spectrometer/base_spectrometer_controller.py index 6eb0f9a..35bf3e7 100644 --- a/src/nqrduck_spectrometer/base_spectrometer_controller.py +++ b/src/nqrduck_spectrometer/base_spectrometer_controller.py @@ -1,8 +1,10 @@ """Base class for all spectrometer controllers.""" +import logging import ast from nqrduck.module.module_controller import ModuleController +logger = logging.getLogger(__name__) class BaseSpectrometerController(ModuleController): """The base class for all spectrometer controllers.""" @@ -11,6 +13,11 @@ class BaseSpectrometerController(ModuleController): """Initializes the spectrometer controller.""" super().__init__(module) + def on_loading(self): + """Called when the module is loading.""" + logger.debug("Loading spectrometer controller") + self.module.model.load_default_settings() + def save_settings(self, path: str) -> None: """Saves the settings of the spectrometer.""" # We get the different settings objects from the model diff --git a/src/nqrduck_spectrometer/base_spectrometer_model.py b/src/nqrduck_spectrometer/base_spectrometer_model.py index 688ddee..2934f06 100644 --- a/src/nqrduck_spectrometer/base_spectrometer_model.py +++ b/src/nqrduck_spectrometer/base_spectrometer_model.py @@ -2,6 +2,7 @@ import logging from collections import OrderedDict +from PyQt6.QtCore import QSettings from PyQt6.QtGui import QPixmap from nqrduck.module.module_model import ModuleModel from .settings import Setting @@ -101,6 +102,25 @@ class BaseSpectrometerModel(ModuleModel): super().__init__(module) self.settings = OrderedDict() self.pulse_parameter_options = OrderedDict() + self.default_settings = QSettings("nqrduck-spectrometer", "nqrduck") + + def set_default_settings(self) -> None: + """Sets the default settings of the spectrometer.""" + self.default_settings.clear() + for category in self.settings.keys(): + for setting in self.settings[category]: + setting_string = f"{self.module.model.name},{setting.name}" + self.default_settings.setValue(setting_string, setting.value) + logger.debug(f"Setting default value for {setting_string} to {setting.value}") + + def load_default_settings(self) -> None: + """Load the default settings of the spectrometer.""" + for category in self.settings.keys(): + for setting in self.settings[category]: + setting_string = f"{self.module.model.name},{setting.name}" + if self.default_settings.contains(setting_string): + logger.debug(f"Loading default value for {setting_string}") + setting.value = self.default_settings.value(setting_string) def add_setting(self, setting: Setting, category: str) -> None: """Adds a setting to the spectrometer. diff --git a/src/nqrduck_spectrometer/base_spectrometer_view.py b/src/nqrduck_spectrometer/base_spectrometer_view.py index 599b7f6..2193544 100644 --- a/src/nqrduck_spectrometer/base_spectrometer_view.py +++ b/src/nqrduck_spectrometer/base_spectrometer_view.py @@ -96,6 +96,11 @@ class BaseSpectrometerView(ModuleView): # Now we add a save and load button to the widget self.button_layout = QHBoxLayout() + # Default Settings Button + self.default_button = QPushButton("Default Settings") + self.default_button.clicked.connect(self.module.model.set_default_settings) + self.button_layout.addWidget(self.default_button) + # Save Button self.save_button = QPushButton("Save Settings") self.save_button.setIcon(Logos.Save16x16()) diff --git a/src/nqrduck_spectrometer/controller.py b/src/nqrduck_spectrometer/controller.py index 717de66..93a27c3 100644 --- a/src/nqrduck_spectrometer/controller.py +++ b/src/nqrduck_spectrometer/controller.py @@ -43,6 +43,8 @@ class SpectrometerController(ModuleController): logger.debug("Adding spectrometer to spectrometer model: %s", module_name) self._module.model.add_spectrometers(module_name, module) + module.controller.on_loading() + self._module.view.create_menu_entry() def process_signals(self, key: str, value: object) -> None: