Fixed loading and saving of settings.

This commit is contained in:
jupfi 2024-05-29 16:33:16 +02:00
parent c1208103ca
commit 5100ee203c
3 changed files with 32 additions and 25 deletions

View file

@ -16,19 +16,18 @@ class BaseSpectrometerController(ModuleController):
def on_loading(self):
"""Called when the module is loading."""
logger.debug("Loading spectrometer controller")
#self.module.model.load_default_settings()
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
settings = self.module.model.settings
settings = self.module.model.quackseq_model.settings
json = {}
json["name"] = self.module.model.name
for category in settings.keys():
for setting in settings[category]:
json[setting.name] = setting.value
for setting in settings.values():
json[setting.name] = setting.value
with open(path, "w") as f:
f.write(str(json))
@ -50,14 +49,13 @@ class BaseSpectrometerController(ModuleController):
self.module.nqrduck_signal.emit("notification", ["Error", message])
return
settings = self.module.model.settings
for category in settings.keys():
for setting in settings[category]:
if setting.name in json:
setting.value = json[setting.name]
else:
message = f"Setting {setting.name} not found in settings file. A change in settings might have broken compatibility."
self.module.nqrduck_signal.emit("notification", ["Error", message])
visual_settings = self.module.model.quackseq_visuals
for visual_setting in visual_settings.values():
if visual_setting.setting.name in json:
visual_setting.value = json[visual_setting.setting.name]
else:
message = f"Setting {visual_setting.setting.name} not found in settings file. A change in settings might have broken compatibility."
self.module.nqrduck_signal.emit("notification", ["Error", message])
def start_measurement(self):
"""Starts the measurement.

View file

@ -18,8 +18,10 @@ class BaseSpectrometerModel(ModuleModel):
module (Module) : The module that the spectrometer is connected to
Attributes:
settings (OrderedDict) : The settings of the spectrometer
pulse_parameter_options (OrderedDict) : The pulse parameter options of the spectrometer
SETTING_FILE_EXTENSION (str) : The file extension for the settings file
default_settings (QSettings) : The default settings of the spectrometer
quackseq_model (QuackseqModel) : The quackseq model of the spectrometer
quackseq_visuals (dict) : The visual settings of the spectrometer
"""
SETTING_FILE_EXTENSION = "setduck"
@ -62,18 +64,24 @@ class BaseSpectrometerModel(ModuleModel):
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}")
settings = self.quackseq_model.settings
for setting in settings.values():
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 setting in self.quackseq_model.settings.values():
setting_string = f"{self.module.model.name},{setting.name}"
setting.value = self.default_settings.value(setting_string)
logger.debug(f"Setting {setting_string} to {setting.value}")
visual_settings = self.quackseq_visuals
for visual_setting in visual_settings.values():
setting_string = f"{self.module.model.name},{visual_setting.setting.name}"
value = self.default_settings.value(setting_string)
if value is None:
logger.debug(f"Setting {setting_string} not found in default settings")
continue
visual_setting.value = value
def clear_default_settings(self) -> None:
"""Clear the default settings of the spectrometer."""

View file

@ -97,7 +97,8 @@ class VisualFloatSetting(VisualSetting):
if self.spin_box:
self.widget.spin_box.setValue(self.setting.value)
else:
self.widget.setText(str(self.setting.value))
value = float(value)
self.widget.set_value(value)
class VisualIntSetting(VisualSetting):