From bf267432931ba87af325fa11207086b72bfd300c 2017-07-27 14:39:40 From: Alexandre Leroux Date: 2017-07-27 14:39:40 Subject: [PATCH] Settings binding (2) Makes widget for general settings a ISqpSettingsBindable and handles load and save --- diff --git a/gui/include/Settings/SqpSettingsGeneralWidget.h b/gui/include/Settings/SqpSettingsGeneralWidget.h index ae00854..02f3f96 100644 --- a/gui/include/Settings/SqpSettingsGeneralWidget.h +++ b/gui/include/Settings/SqpSettingsGeneralWidget.h @@ -1,6 +1,8 @@ #ifndef SCIQLOP_SQPSETTINGSGENERALWIDGET_H #define SCIQLOP_SQPSETTINGSGENERALWIDGET_H +#include "Settings/ISqpSettingsBindable.h" + #include namespace Ui { @@ -10,13 +12,19 @@ class SqpSettingsGeneralWidget; /** * @brief The SqpSettingsGeneralWidget class represents the general settings of SciQlop */ -class SqpSettingsGeneralWidget : public QWidget { +class SqpSettingsGeneralWidget : public QWidget, public ISqpSettingsBindable { Q_OBJECT public: explicit SqpSettingsGeneralWidget(QWidget *parent = 0); virtual ~SqpSettingsGeneralWidget() noexcept; + /// @sa ISqpSettingsBindable::loadSettings() + void loadSettings() override final; + + /// @sa ISqpSettingsBindable::saveSettings() + void saveSettings() const override final; + private: Ui::SqpSettingsGeneralWidget *ui; }; diff --git a/gui/src/Settings/SqpSettingsGeneralWidget.cpp b/gui/src/Settings/SqpSettingsGeneralWidget.cpp index 9754237..f4f7bcf 100644 --- a/gui/src/Settings/SqpSettingsGeneralWidget.cpp +++ b/gui/src/Settings/SqpSettingsGeneralWidget.cpp @@ -1,5 +1,7 @@ #include "Settings/SqpSettingsGeneralWidget.h" +#include "Settings/SqpSettingsDefs.h" + #include "ui_SqpSettingsGeneralWidget.h" SqpSettingsGeneralWidget::SqpSettingsGeneralWidget(QWidget *parent) @@ -12,3 +14,32 @@ SqpSettingsGeneralWidget::~SqpSettingsGeneralWidget() noexcept { delete ui; } + +void SqpSettingsGeneralWidget::loadSettings() +{ + QSettings settings{}; + + auto loadTolerance = [&settings](const QString &key, double defaultValue) { + // Tolerance is converted to percent + auto toleranceValue = settings.value(key, defaultValue).toDouble(); + return toleranceValue * 100.; + }; + + ui->toleranceInitSpinBox->setValue( + loadTolerance(GENERAL_TOLERANCE_AT_INIT_KEY, GENERAL_TOLERANCE_AT_INIT_DEFAULT_VALUE)); + ui->toleranceUpdateSpinBox->setValue( + loadTolerance(GENERAL_TOLERANCE_AT_UPDATE_KEY, GENERAL_TOLERANCE_AT_UPDATE_DEFAULT_VALUE)); +} + +void SqpSettingsGeneralWidget::saveSettings() const +{ + QSettings settings{}; + + auto saveTolerance = [&settings](const QString &key, double value) { + // Tolerance is converted from percent + settings.setValue(key, value * 0.01); + }; + + saveTolerance(GENERAL_TOLERANCE_AT_INIT_KEY, ui->toleranceInitSpinBox->value()); + saveTolerance(GENERAL_TOLERANCE_AT_UPDATE_KEY, ui->toleranceUpdateSpinBox->value()); +}