From 2f47f0c2296a4b72e5e684b07c979e8a9d79f877 Mon Sep 17 00:00:00 2001 From: Dan Paulat Date: Thu, 18 Nov 2021 22:00:31 -0600 Subject: [PATCH] Grid width and grid height --- .../scwx/qt/settings/general_settings.cpp | 44 +++++++++++++- .../scwx/qt/settings/general_settings.hpp | 2 + scwx-qt/source/scwx/qt/util/json.cpp | 60 ++++++++++++++++++- scwx-qt/source/scwx/qt/util/json.hpp | 8 +++ test/data | 2 +- .../scwx/qt/manager/settings_manager.test.cpp | 34 +++++++++++ 6 files changed, 144 insertions(+), 6 deletions(-) diff --git a/scwx-qt/source/scwx/qt/settings/general_settings.cpp b/scwx-qt/source/scwx/qt/settings/general_settings.cpp index 746d206f..e1a41dbf 100644 --- a/scwx-qt/source/scwx/qt/settings/general_settings.cpp +++ b/scwx-qt/source/scwx/qt/settings/general_settings.cpp @@ -13,6 +13,13 @@ namespace settings static const std::string logPrefix_ = "[scwx::qt::settings::general_settings] "; static const std::string DEFAULT_DEFAULT_RADAR_SITE = "KLSX"; +static const int64_t DEFAULT_GRID_WIDTH = 1; +static const int64_t DEFAULT_GRID_HEIGHT = 1; + +static const int64_t GRID_WIDTH_MINIMUM = 1; +static const int64_t GRID_WIDTH_MAXIMUM = 2; +static const int64_t GRID_HEIGHT_MINIMUM = 1; +static const int64_t GRID_HEIGHT_MAXIMUM = 2; class GeneralSettingsImpl { @@ -21,9 +28,16 @@ public: ~GeneralSettingsImpl() {} - void SetDefaults() { defaultRadarSite_ = DEFAULT_DEFAULT_RADAR_SITE; } + void SetDefaults() + { + defaultRadarSite_ = DEFAULT_DEFAULT_RADAR_SITE; + gridWidth_ = DEFAULT_GRID_WIDTH; + gridHeight_ = DEFAULT_GRID_HEIGHT; + } std::string defaultRadarSite_; + int64_t gridWidth_; + int64_t gridHeight_; }; GeneralSettings::GeneralSettings() : p(std::make_unique()) @@ -40,11 +54,23 @@ const std::string& GeneralSettings::default_radar_site() const return p->defaultRadarSite_; } +int64_t GeneralSettings::grid_height() const +{ + return p->gridHeight_; +} + +int64_t GeneralSettings::grid_width() const +{ + return p->gridWidth_; +} + boost::json::value GeneralSettings::ToJson() const { boost::json::object json; json["default_radar_site"] = p->defaultRadarSite_; + json["grid_width"] = p->gridWidth_; + json["grid_height"] = p->gridHeight_; return json; } @@ -72,6 +98,18 @@ GeneralSettings::Load(const boost::json::value* json, bool& jsonDirty) "default_radar_site", generalSettings->p->defaultRadarSite_, DEFAULT_DEFAULT_RADAR_SITE); + jsonDirty |= !util::json::FromJsonInt64(json->as_object(), + "grid_width", + generalSettings->p->gridWidth_, + DEFAULT_GRID_WIDTH, + GRID_WIDTH_MINIMUM, + GRID_WIDTH_MAXIMUM); + jsonDirty |= !util::json::FromJsonInt64(json->as_object(), + "grid_height", + generalSettings->p->gridHeight_, + DEFAULT_GRID_HEIGHT, + GRID_HEIGHT_MINIMUM, + GRID_HEIGHT_MAXIMUM); } else { @@ -95,7 +133,9 @@ GeneralSettings::Load(const boost::json::value* json, bool& jsonDirty) bool operator==(const GeneralSettings& lhs, const GeneralSettings& rhs) { - return lhs.p->defaultRadarSite_ == rhs.p->defaultRadarSite_; + return (lhs.p->defaultRadarSite_ == rhs.p->defaultRadarSite_ && + lhs.p->gridWidth_ == rhs.p->gridWidth_ && + lhs.p->gridHeight_ == rhs.p->gridHeight_); } } // namespace settings diff --git a/scwx-qt/source/scwx/qt/settings/general_settings.hpp b/scwx-qt/source/scwx/qt/settings/general_settings.hpp index 6f7391a5..a2595fbb 100644 --- a/scwx-qt/source/scwx/qt/settings/general_settings.hpp +++ b/scwx-qt/source/scwx/qt/settings/general_settings.hpp @@ -27,6 +27,8 @@ public: GeneralSettings& operator=(GeneralSettings&&) noexcept; const std::string& default_radar_site() const; + int64_t grid_height() const; + int64_t grid_width() const; boost::json::value ToJson() const; diff --git a/scwx-qt/source/scwx/qt/util/json.cpp b/scwx-qt/source/scwx/qt/util/json.cpp index 2ad60651..97c5ebb6 100644 --- a/scwx-qt/source/scwx/qt/util/json.cpp +++ b/scwx-qt/source/scwx/qt/util/json.cpp @@ -27,20 +27,74 @@ static void PrettyPrintJson(std::ostream& os, boost::json::value const& jv, std::string* indent = nullptr); +bool FromJsonInt64(const boost::json::object& json, + const std::string& key, + int64_t& value, + const int64_t defaultValue, + std::optional minValue, + std::optional maxValue) +{ + const boost::json::value* jv = json.if_contains(key); + bool dirty = true; + + if (jv != nullptr) + { + if (jv->is_int64()) + { + value = boost::json::value_to(*jv); + + if (minValue.has_value() && value < *minValue) + { + BOOST_LOG_TRIVIAL(warning) + << logPrefix_ << key << " less than minimum (" << value << " < " + << *minValue << "), setting to: " << *minValue; + value = *minValue; + } + else if (maxValue.has_value() && value > *maxValue) + { + BOOST_LOG_TRIVIAL(warning) + << logPrefix_ << key << " greater than maximum (" << value + << " > " << *maxValue << "), setting to: " << *maxValue; + value = *maxValue; + } + else + { + dirty = false; + } + } + else + { + BOOST_LOG_TRIVIAL(warning) + << logPrefix_ << key << " is not an int64 (" << jv->kind() + << "), setting to default:" << defaultValue; + value = defaultValue; + } + } + else + { + BOOST_LOG_TRIVIAL(debug) + << logPrefix_ << key + << " is not present, setting to default: " << defaultValue; + value = defaultValue; + } + + return !dirty; +} + bool FromJsonString(const boost::json::object& json, const std::string& key, std::string& value, const std::string& defaultValue) { const boost::json::value* jv = json.if_contains(key); - bool found = false; + bool dirty = true; if (jv != nullptr) { if (jv->is_string()) { value = boost::json::value_to(*jv); - found = true; + dirty = false; } else { @@ -58,7 +112,7 @@ bool FromJsonString(const boost::json::object& json, value = defaultValue; } - return found; + return !dirty; } boost::json::value ReadJsonFile(const std::string& path) diff --git a/scwx-qt/source/scwx/qt/util/json.hpp b/scwx-qt/source/scwx/qt/util/json.hpp index c72d62d6..5a57891a 100644 --- a/scwx-qt/source/scwx/qt/util/json.hpp +++ b/scwx-qt/source/scwx/qt/util/json.hpp @@ -1,5 +1,7 @@ #pragma once +#include + #include namespace scwx @@ -11,6 +13,12 @@ namespace util namespace json { +bool FromJsonInt64(const boost::json::object& json, + const std::string& key, + int64_t& value, + const int64_t defaultValue, + std::optional minValue, + std::optional maxValue); bool FromJsonString(const boost::json::object& json, const std::string& key, std::string& value, diff --git a/test/data b/test/data index dd31c585..8bdbd9aa 160000 --- a/test/data +++ b/test/data @@ -1 +1 @@ -Subproject commit dd31c5857e2747e8ff5e0628aaf038f3eb45c2bb +Subproject commit 8bdbd9aaa97187fb1291298bafb638ced4806ed3 diff --git a/test/source/scwx/qt/manager/settings_manager.test.cpp b/test/source/scwx/qt/manager/settings_manager.test.cpp index a1304e0b..5082441d 100644 --- a/test/source/scwx/qt/manager/settings_manager.test.cpp +++ b/test/source/scwx/qt/manager/settings_manager.test.cpp @@ -95,6 +95,40 @@ INSTANTIATE_TEST_SUITE_P(SettingsManager, "settings-empty-groups.json", "settings-empty-object.json")); +TEST(SettingsManager, SettingsBadMinimum) +{ + std::string minimumFile(std::string(SCWX_TEST_DATA_DIR) + + "/json/settings/settings-minimum.json"); + std::string sourceFile(std::string(SCWX_TEST_DATA_DIR) + + "/json/settings/settings-bad-minimum.json"); + std::string filename {TEMP_SETTINGS_FILE}; + + std::filesystem::copy_file(sourceFile, filename); + + SettingsManager::ReadSettings(filename); + + CompareFiles(filename, minimumFile); + + std::filesystem::remove(filename); +} + +TEST(SettingsManager, SettingsBadMaximum) +{ + std::string maximumFile(std::string(SCWX_TEST_DATA_DIR) + + "/json/settings/settings-maximum.json"); + std::string sourceFile(std::string(SCWX_TEST_DATA_DIR) + + "/json/settings/settings-bad-maximum.json"); + std::string filename {TEMP_SETTINGS_FILE}; + + std::filesystem::copy_file(sourceFile, filename); + + SettingsManager::ReadSettings(filename); + + CompareFiles(filename, maximumFile); + + std::filesystem::remove(filename); +} + } // namespace manager } // namespace qt } // namespace scwx