From e8e3032dcdc899b0d50aec99d3892914adeebebe Mon Sep 17 00:00:00 2001 From: Dan Paulat Date: Wed, 14 Jun 2023 22:56:26 -0500 Subject: [PATCH] Save loop delay, speed, and time to settings on shutdown --- scwx-qt/source/scwx/qt/main/main.cpp | 1 + .../scwx/qt/manager/settings_manager.cpp | 12 +++++ .../scwx/qt/manager/settings_manager.hpp | 1 + .../scwx/qt/manager/timeline_manager.cpp | 17 +++++-- .../scwx/qt/settings/general_settings.cpp | 45 +++++++++++++++++++ .../scwx/qt/settings/general_settings.hpp | 5 +++ .../scwx/qt/settings/settings_variable.cpp | 3 +- .../scwx/qt/settings/settings_variable.hpp | 1 + .../scwx/qt/ui/animation_dock_widget.cpp | 40 +++++++++++------ test/data | 2 +- .../qt/settings/settings_variable.test.cpp | 30 +++++++++++++ 11 files changed, 138 insertions(+), 19 deletions(-) diff --git a/scwx-qt/source/scwx/qt/main/main.cpp b/scwx-qt/source/scwx/qt/main/main.cpp index b360aa1e..400909d7 100644 --- a/scwx-qt/source/scwx/qt/main/main.cpp +++ b/scwx-qt/source/scwx/qt/main/main.cpp @@ -82,6 +82,7 @@ int main(int argc, char* argv[]) // Shutdown application scwx::qt::manager::ResourceManager::Shutdown(); + scwx::qt::manager::SettingsManager::Shutdown(); // Shutdown AWS SDK Aws::ShutdownAPI(awsSdkOptions); diff --git a/scwx-qt/source/scwx/qt/manager/settings_manager.cpp b/scwx-qt/source/scwx/qt/manager/settings_manager.cpp index c11d4f71..1ff772f0 100644 --- a/scwx-qt/source/scwx/qt/manager/settings_manager.cpp +++ b/scwx-qt/source/scwx/qt/manager/settings_manager.cpp @@ -90,6 +90,18 @@ void SaveSettings() } } +void Shutdown() +{ + bool dataChanged = false; + + dataChanged |= general_settings().Shutdown(); + + if (dataChanged) + { + SaveSettings(); + } +} + settings::GeneralSettings& general_settings() { static settings::GeneralSettings generalSettings_; diff --git a/scwx-qt/source/scwx/qt/manager/settings_manager.hpp b/scwx-qt/source/scwx/qt/manager/settings_manager.hpp index 75a10e87..ed05ca1e 100644 --- a/scwx-qt/source/scwx/qt/manager/settings_manager.hpp +++ b/scwx-qt/source/scwx/qt/manager/settings_manager.hpp @@ -16,6 +16,7 @@ namespace SettingsManager void Initialize(); void ReadSettings(const std::string& settingsPath); void SaveSettings(); +void Shutdown(); settings::GeneralSettings& general_settings(); settings::MapSettings& map_settings(); diff --git a/scwx-qt/source/scwx/qt/manager/timeline_manager.cpp b/scwx-qt/source/scwx/qt/manager/timeline_manager.cpp index 7bcb9b64..66820167 100644 --- a/scwx-qt/source/scwx/qt/manager/timeline_manager.cpp +++ b/scwx-qt/source/scwx/qt/manager/timeline_manager.cpp @@ -1,5 +1,6 @@ #include #include +#include #include #include #include @@ -33,7 +34,15 @@ static constexpr std::chrono::seconds kRadarSweepMonitorTimeout_ {5}; class TimelineManager::Impl { public: - explicit Impl(TimelineManager* self) : self_ {self} {} + explicit Impl(TimelineManager* self) : self_ {self} + { + auto& generalSettings = SettingsManager::general_settings(); + + loopDelay_ = + std::chrono::milliseconds(generalSettings.loop_delay().GetValue()); + loopSpeed_ = generalSettings.loop_speed().GetValue(); + loopTime_ = std::chrono::minutes(generalSettings.loop_time().GetValue()); + } ~Impl() { @@ -72,9 +81,9 @@ public: std::chrono::system_clock::time_point adjustedTime_ {}; std::chrono::system_clock::time_point selectedTime_ {}; types::MapTime viewType_ {types::MapTime::Live}; - std::chrono::minutes loopTime_ {30}; - double loopSpeed_ {5.0}; - std::chrono::milliseconds loopDelay_ {2500}; + std::chrono::minutes loopTime_; + double loopSpeed_; + std::chrono::milliseconds loopDelay_; bool radarSweepMonitorActive_ {false}; std::mutex radarSweepMonitorMutex_ {}; diff --git a/scwx-qt/source/scwx/qt/settings/general_settings.cpp b/scwx-qt/source/scwx/qt/settings/general_settings.cpp index 0375ecc4..8f2d3e74 100644 --- a/scwx-qt/source/scwx/qt/settings/general_settings.cpp +++ b/scwx-qt/source/scwx/qt/settings/general_settings.cpp @@ -33,6 +33,9 @@ public: defaultAlertAction_.SetDefault(defaultDefaultAlertActionValue); defaultRadarSite_.SetDefault("KLSX"); fontSizes_.SetDefault({16}); + loopDelay_.SetDefault(2500); + loopSpeed_.SetDefault(5.0); + loopTime_.SetDefault(30); gridWidth_.SetDefault(1); gridHeight_.SetDefault(1); mapProvider_.SetDefault(defaultMapProviderValue); @@ -48,6 +51,12 @@ public: gridWidth_.SetMaximum(2); gridHeight_.SetMinimum(1); gridHeight_.SetMaximum(2); + loopDelay_.SetMinimum(0); + loopDelay_.SetMaximum(15000); + loopSpeed_.SetMinimum(1.0); + loopSpeed_.SetMaximum(99.99); + loopTime_.SetMinimum(1); + loopTime_.SetMaximum(1440); defaultAlertAction_.SetValidator( [](const std::string& value) @@ -101,6 +110,9 @@ public: SettingsContainer> fontSizes_ {"font_sizes"}; SettingsVariable gridWidth_ {"grid_width"}; SettingsVariable gridHeight_ {"grid_height"}; + SettingsVariable loopDelay_ {"loop_delay"}; + SettingsVariable loopSpeed_ {"loop_speed"}; + SettingsVariable loopTime_ {"loop_time"}; SettingsVariable mapProvider_ {"map_provider"}; SettingsVariable mapboxApiKey_ {"mapbox_api_key"}; SettingsVariable maptilerApiKey_ {"maptiler_api_key"}; @@ -116,6 +128,9 @@ GeneralSettings::GeneralSettings() : &p->fontSizes_, &p->gridWidth_, &p->gridHeight_, + &p->loopDelay_, + &p->loopSpeed_, + &p->loopTime_, &p->mapProvider_, &p->mapboxApiKey_, &p->maptilerApiKey_, @@ -159,6 +174,21 @@ SettingsVariable& GeneralSettings::grid_width() const return p->gridWidth_; } +SettingsVariable& GeneralSettings::loop_delay() const +{ + return p->loopDelay_; +} + +SettingsVariable& GeneralSettings::loop_speed() const +{ + return p->loopSpeed_; +} + +SettingsVariable& GeneralSettings::loop_time() const +{ + return p->loopTime_; +} + SettingsVariable& GeneralSettings::map_provider() const { return p->mapProvider_; @@ -179,6 +209,18 @@ SettingsVariable& GeneralSettings::update_notifications_enabled() const return p->updateNotificationsEnabled_; } +bool GeneralSettings::Shutdown() +{ + bool dataChanged = false; + + // Commit settings that are managed separate from the settings dialog + dataChanged |= p->loopDelay_.Commit(); + dataChanged |= p->loopSpeed_.Commit(); + dataChanged |= p->loopTime_.Commit(); + + return dataChanged; +} + bool operator==(const GeneralSettings& lhs, const GeneralSettings& rhs) { return (lhs.p->debugEnabled_ == rhs.p->debugEnabled_ && @@ -187,6 +229,9 @@ bool operator==(const GeneralSettings& lhs, const GeneralSettings& rhs) lhs.p->fontSizes_ == rhs.p->fontSizes_ && lhs.p->gridWidth_ == rhs.p->gridWidth_ && lhs.p->gridHeight_ == rhs.p->gridHeight_ && + lhs.p->loopDelay_ == rhs.p->loopDelay_ && + lhs.p->loopSpeed_ == rhs.p->loopSpeed_ && + lhs.p->loopTime_ == rhs.p->loopTime_ && lhs.p->mapProvider_ == rhs.p->mapProvider_ && lhs.p->mapboxApiKey_ == rhs.p->mapboxApiKey_ && lhs.p->maptilerApiKey_ == rhs.p->maptilerApiKey_ && diff --git a/scwx-qt/source/scwx/qt/settings/general_settings.hpp b/scwx-qt/source/scwx/qt/settings/general_settings.hpp index 4d54074d..375d887e 100644 --- a/scwx-qt/source/scwx/qt/settings/general_settings.hpp +++ b/scwx-qt/source/scwx/qt/settings/general_settings.hpp @@ -33,6 +33,9 @@ public: SettingsContainer>& font_sizes() const; SettingsVariable& grid_height() const; SettingsVariable& grid_width() const; + SettingsVariable& loop_delay() const; + SettingsVariable& loop_speed() const; + SettingsVariable& loop_time() const; SettingsVariable& map_provider() const; SettingsVariable& mapbox_api_key() const; SettingsVariable& maptiler_api_key() const; @@ -41,6 +44,8 @@ public: friend bool operator==(const GeneralSettings& lhs, const GeneralSettings& rhs); + bool Shutdown(); + private: std::unique_ptr p; }; diff --git a/scwx-qt/source/scwx/qt/settings/settings_variable.cpp b/scwx-qt/source/scwx/qt/settings/settings_variable.cpp index 1dac32bd..270a2eed 100644 --- a/scwx-qt/source/scwx/qt/settings/settings_variable.cpp +++ b/scwx-qt/source/scwx/qt/settings/settings_variable.cpp @@ -51,7 +51,8 @@ SettingsVariable::operator=(SettingsVariable&&) noexcept = default; template inline auto FormatParameter(const T& value) { - if constexpr (std::is_integral_v || std::is_same_v) + if constexpr (std::is_integral_v || std::is_floating_point_v || + std::is_same_v) { return value; } diff --git a/scwx-qt/source/scwx/qt/settings/settings_variable.hpp b/scwx-qt/source/scwx/qt/settings/settings_variable.hpp index 3d825189..c7999c0d 100644 --- a/scwx-qt/source/scwx/qt/settings/settings_variable.hpp +++ b/scwx-qt/source/scwx/qt/settings/settings_variable.hpp @@ -208,6 +208,7 @@ private: #ifdef SETTINGS_VARIABLE_IMPLEMENTATION template class SettingsVariable; +template class SettingsVariable; template class SettingsVariable; template class SettingsVariable; diff --git a/scwx-qt/source/scwx/qt/ui/animation_dock_widget.cpp b/scwx-qt/source/scwx/qt/ui/animation_dock_widget.cpp index 5bfc5a89..dc567d63 100644 --- a/scwx-qt/source/scwx/qt/ui/animation_dock_widget.cpp +++ b/scwx-qt/source/scwx/qt/ui/animation_dock_widget.cpp @@ -1,6 +1,7 @@ #include "animation_dock_widget.hpp" #include "ui_animation_dock_widget.h" +#include #include #include @@ -86,9 +87,11 @@ AnimationDockWidget::AnimationDockWidget(QWidget* parent) : maxDateTimer->start(15000); // Set loop defaults - ui->loopTimeSpinBox->setValue(30); - ui->loopSpeedSpinBox->setValue(5.0); - ui->loopDelaySpinBox->setValue(2.5); + auto& generalSettings = manager::SettingsManager::general_settings(); + ui->loopTimeSpinBox->setValue(generalSettings.loop_time().GetValue()); + ui->loopSpeedSpinBox->setValue(generalSettings.loop_speed().GetValue()); + ui->loopDelaySpinBox->setValue(generalSettings.loop_delay().GetValue() * + 0.001); // Connect widget signals p->ConnectSignals(); @@ -152,22 +155,33 @@ void AnimationDockWidgetImpl::ConnectSignals() }); // Loop controls - QObject::connect(self_->ui->loopTimeSpinBox, - &QSpinBox::valueChanged, - self_, - [this](int i) { - Q_EMIT self_->LoopTimeChanged(std::chrono::minutes(i)); - }); - QObject::connect(self_->ui->loopSpeedSpinBox, - &QDoubleSpinBox::valueChanged, - self_, - [this](double d) { Q_EMIT self_->LoopSpeedChanged(d); }); + QObject::connect( + self_->ui->loopTimeSpinBox, + &QSpinBox::valueChanged, + self_, + [this](int i) + { + manager::SettingsManager::general_settings().loop_time().StageValue(i); + Q_EMIT self_->LoopTimeChanged(std::chrono::minutes(i)); + }); + QObject::connect( + self_->ui->loopSpeedSpinBox, + &QDoubleSpinBox::valueChanged, + self_, + [this](double d) + { + manager::SettingsManager::general_settings().loop_speed().StageValue( + d); + Q_EMIT self_->LoopSpeedChanged(d); + }); QObject::connect( self_->ui->loopDelaySpinBox, &QDoubleSpinBox::valueChanged, self_, [this](double d) { + manager::SettingsManager::general_settings().loop_delay().StageValue( + static_cast(d * 1000.0)); Q_EMIT self_->LoopDelayChanged(std::chrono::milliseconds( static_cast(d * 1000.0))); }); diff --git a/test/data b/test/data index 93eb0d15..98ee5891 160000 --- a/test/data +++ b/test/data @@ -1 +1 @@ -Subproject commit 93eb0d154ce70675812c5569a51c4fdadedbc24d +Subproject commit 98ee58919fc2d616519e514f70308658c5a737e5 diff --git a/test/source/scwx/qt/settings/settings_variable.test.cpp b/test/source/scwx/qt/settings/settings_variable.test.cpp index bb7b4e9a..5f24c746 100644 --- a/test/source/scwx/qt/settings/settings_variable.test.cpp +++ b/test/source/scwx/qt/settings/settings_variable.test.cpp @@ -23,6 +23,36 @@ TEST(SettingsVariableTest, Boolean) EXPECT_EQ(boolVariable.GetValue(), false); } +TEST(SettingsVariableTest, Double) +{ + SettingsVariable doubleVariable {"double"}; + doubleVariable.SetDefault(4.2); + doubleVariable.SetMinimum(1.0); + doubleVariable.SetMaximum(9.9); + doubleVariable.SetValue(5.0); + + EXPECT_EQ(doubleVariable.name(), "double"); + EXPECT_EQ(doubleVariable.GetValue(), 5.0); + EXPECT_EQ(doubleVariable.SetValue(0), false); + EXPECT_EQ(doubleVariable.GetValue(), 5.0); + EXPECT_EQ(doubleVariable.SetValueOrDefault(0.0), false); // < Minimum + EXPECT_EQ(doubleVariable.GetValue(), 1.0); + EXPECT_EQ(doubleVariable.SetValueOrDefault(10.0), false); // > Maximum + EXPECT_EQ(doubleVariable.GetValue(), 9.9); + doubleVariable.SetValueToDefault(); + EXPECT_EQ(doubleVariable.GetValue(), 4.2); + EXPECT_EQ(doubleVariable.SetValue(4.3), true); + EXPECT_EQ(doubleVariable.GetValue(), 4.3); + EXPECT_EQ(doubleVariable.SetValueOrDefault(5.7), true); + EXPECT_EQ(doubleVariable.GetValue(), 5.7); + + EXPECT_EQ(doubleVariable.StageValue(0.0), false); + EXPECT_EQ(doubleVariable.StageValue(5.0), true); + EXPECT_EQ(doubleVariable.GetValue(), 5.7); + doubleVariable.Commit(); + EXPECT_EQ(doubleVariable.GetValue(), 5.0); +} + TEST(SettingsVariableTest, Integer) { SettingsVariable intVariable {"int64_t"};