From 2b55d0cd69b99252013ecc511395498bcf05b7e9 Mon Sep 17 00:00:00 2001 From: Dan Paulat Date: Sun, 25 Dec 2022 00:57:57 -0600 Subject: [PATCH] Adding alert colors to settings --- scwx-qt/scwx-qt.cmake | 6 +- .../scwx/qt/settings/palette_settings.cpp | 124 +++++++++++++++--- .../scwx/qt/settings/palette_settings.hpp | 5 + scwx-qt/source/scwx/qt/util/color.cpp | 25 ++++ scwx-qt/source/scwx/qt/util/color.hpp | 19 +++ test/data | 2 +- 6 files changed, 161 insertions(+), 20 deletions(-) create mode 100644 scwx-qt/source/scwx/qt/util/color.cpp create mode 100644 scwx-qt/source/scwx/qt/util/color.hpp diff --git a/scwx-qt/scwx-qt.cmake b/scwx-qt/scwx-qt.cmake index df9d37fd..b6732109 100644 --- a/scwx-qt/scwx-qt.cmake +++ b/scwx-qt/scwx-qt.cmake @@ -155,12 +155,14 @@ set(UI_UI source/scwx/qt/ui/alert_dialog.ui source/scwx/qt/ui/imgui_debug_dialog.ui source/scwx/qt/ui/radar_site_dialog.ui source/scwx/qt/ui/settings_dialog.ui) -set(HDR_UTIL source/scwx/qt/util/font.hpp +set(HDR_UTIL source/scwx/qt/util/color.hpp + source/scwx/qt/util/font.hpp source/scwx/qt/util/font_buffer.hpp source/scwx/qt/util/json.hpp source/scwx/qt/util/streams.hpp source/scwx/qt/util/texture_atlas.hpp) -set(SRC_UTIL source/scwx/qt/util/font.cpp +set(SRC_UTIL source/scwx/qt/util/color.cpp + source/scwx/qt/util/font.cpp source/scwx/qt/util/font_buffer.cpp source/scwx/qt/util/json.cpp source/scwx/qt/util/texture_atlas.cpp) diff --git a/scwx-qt/source/scwx/qt/settings/palette_settings.cpp b/scwx-qt/source/scwx/qt/settings/palette_settings.cpp index 4fb378d5..f747233e 100644 --- a/scwx-qt/source/scwx/qt/settings/palette_settings.cpp +++ b/scwx-qt/source/scwx/qt/settings/palette_settings.cpp @@ -1,5 +1,11 @@ #include #include +#include + +#include +#include + +#include namespace scwx { @@ -10,7 +16,7 @@ namespace settings static const std::string logPrefix_ = "scwx::qt::settings::palette_settings"; -static const std::vector paletteNames_ = { +static const std::vector kPaletteNames_ = { // Level 2 / Common Products "BR", "BV", @@ -31,36 +37,85 @@ static const std::vector paletteNames_ = { "VIL", "???"}; -static const std::string kDefaultKey = "???"; -static const std::string kDefaultPalette = ""; +static const std::map< + awips::Phenomenon, + std::pair> + kAlertColors_ { + {awips::Phenomenon::Marine, {{255, 127, 0, 255}, {127, 63, 0, 255}}}, + {awips::Phenomenon::FlashFlood, {{0, 255, 0, 255}, {0, 127, 0, 255}}}, + {awips::Phenomenon::SevereThunderstorm, + {{255, 255, 0, 255}, {127, 127, 0, 255}}}, + {awips::Phenomenon::SnowSquall, {{0, 255, 255, 255}, {0, 127, 127, 255}}}, + {awips::Phenomenon::Tornado, {{255, 0, 0, 255}, {127, 0, 0, 255}}}}; + +static const std::string kDefaultKey_ {"???"}; +static const std::string kDefaultPalette_ {""}; +static const awips::Phenomenon kDefaultPhenomenon_ {awips::Phenomenon::Marine}; class PaletteSettingsImpl { public: explicit PaletteSettingsImpl() { - std::for_each(paletteNames_.cbegin(), - paletteNames_.cend(), - [&](const std::string& name) - { - auto result = palette_.emplace( - name, SettingsVariable {name}); + for (const std::string& name : kPaletteNames_) + { + auto result = + palette_.emplace(name, SettingsVariable {name}); - SettingsVariable& settingsVariable = - result.first->second; + SettingsVariable& settingsVariable = result.first->second; - settingsVariable.SetDefault(kDefaultPalette); + settingsVariable.SetDefault(kDefaultPalette_); - variables_.push_back(&settingsVariable); - }); + variables_.push_back(&settingsVariable); + }; + + for (auto& alert : kAlertColors_) + { + std::string phenomenonCode = awips::GetPhenomenonCode(alert.first); + std::string activeName = std::format("{}-active", phenomenonCode); + std::string inactiveName = std::format("{}-inactive", phenomenonCode); + + auto activeResult = activeAlertColor_.emplace( + alert.first, SettingsVariable {activeName}); + auto inactiveResult = inactiveAlertColor_.emplace( + alert.first, SettingsVariable {inactiveName}); + + SettingsVariable& activeVariable = + activeResult.first->second; + SettingsVariable& inactiveVariable = + inactiveResult.first->second; + + activeVariable.SetDefault( + util::color::ToArgbString(alert.second.first)); + inactiveVariable.SetDefault( + util::color::ToArgbString(alert.second.second)); + + activeVariable.SetValidator(&ValidateColor); + inactiveVariable.SetValidator(&ValidateColor); + + variables_.push_back(&activeVariable); + variables_.push_back(&inactiveVariable); + } } ~PaletteSettingsImpl() {} - std::unordered_map> palette_; - std::vector variables_; + static bool ValidateColor(const std::string& value); + + std::unordered_map> palette_ {}; + std::unordered_map> + activeAlertColor_ {}; + std::unordered_map> + inactiveAlertColor_ {}; + std::vector variables_ {}; }; +bool PaletteSettingsImpl::ValidateColor(const std::string& value) +{ + static const std::regex re {"#[0-9A-Za-z]{8}"}; + return std::regex_match(value, re); +} + PaletteSettings::PaletteSettings() : SettingsCategory("palette"), p(std::make_unique()) { @@ -82,12 +137,47 @@ PaletteSettings::palette(const std::string& name) const if (palette == p->palette_.cend()) { - palette = p->palette_.find(kDefaultKey); + palette = p->palette_.find(kDefaultKey_); } return palette->second; } +SettingsVariable& +PaletteSettings::alert_color(awips::Phenomenon phenomenon, bool active) const +{ + if (active) + { + auto alert = p->activeAlertColor_.find(phenomenon); + if (alert == p->activeAlertColor_.cend()) + { + alert = p->activeAlertColor_.find(kDefaultPhenomenon_); + } + return alert->second; + } + else + { + auto alert = p->inactiveAlertColor_.find(phenomenon); + if (alert == p->inactiveAlertColor_.cend()) + { + alert = p->inactiveAlertColor_.find(kDefaultPhenomenon_); + } + return alert->second; + } +} + +const std::vector& PaletteSettings::alert_phenomena() +{ + static const std::vector kAlertPhenomena_ { + awips::Phenomenon::Marine, + awips::Phenomenon::FlashFlood, + awips::Phenomenon::SevereThunderstorm, + awips::Phenomenon::SnowSquall, + awips::Phenomenon::Tornado}; + + return kAlertPhenomena_; +} + bool operator==(const PaletteSettings& lhs, const PaletteSettings& rhs) { return lhs.p->palette_ == rhs.p->palette_; diff --git a/scwx-qt/source/scwx/qt/settings/palette_settings.hpp b/scwx-qt/source/scwx/qt/settings/palette_settings.hpp index 0edcc4ab..948decf5 100644 --- a/scwx-qt/source/scwx/qt/settings/palette_settings.hpp +++ b/scwx-qt/source/scwx/qt/settings/palette_settings.hpp @@ -2,6 +2,7 @@ #include #include +#include #include #include @@ -28,6 +29,10 @@ public: PaletteSettings& operator=(PaletteSettings&&) noexcept; SettingsVariable& palette(const std::string& name) const; + SettingsVariable& alert_color(awips::Phenomenon phenomenon, + bool active) const; + + static const std::vector& alert_phenomena(); friend bool operator==(const PaletteSettings& lhs, const PaletteSettings& rhs); diff --git a/scwx-qt/source/scwx/qt/util/color.cpp b/scwx-qt/source/scwx/qt/util/color.cpp new file mode 100644 index 00000000..b4955918 --- /dev/null +++ b/scwx-qt/source/scwx/qt/util/color.cpp @@ -0,0 +1,25 @@ +#include + +#include + +namespace scwx +{ +namespace qt +{ +namespace util +{ +namespace color +{ + +static const std::string logPrefix_ = "scwx::qt::util::color"; + +std::string ToArgbString(const boost::gil::rgba8_pixel_t& color) +{ + return std::format( + "#{:02x}{:02x}{:02x}{:02x}", color[3], color[0], color[1], color[2]); +} + +} // namespace color +} // namespace util +} // namespace qt +} // namespace scwx diff --git a/scwx-qt/source/scwx/qt/util/color.hpp b/scwx-qt/source/scwx/qt/util/color.hpp new file mode 100644 index 00000000..cbd95660 --- /dev/null +++ b/scwx-qt/source/scwx/qt/util/color.hpp @@ -0,0 +1,19 @@ +#pragma once + +#include + +namespace scwx +{ +namespace qt +{ +namespace util +{ +namespace color +{ + +std::string ToArgbString(const boost::gil::rgba8_pixel_t& color); + +} // namespace color +} // namespace util +} // namespace qt +} // namespace scwx diff --git a/test/data b/test/data index b20f1c30..cf7b1e86 160000 --- a/test/data +++ b/test/data @@ -1 +1 @@ -Subproject commit b20f1c3015ae88d0883734cb51ecc01372ede749 +Subproject commit cf7b1e86a0755da801040fdf24961a1e9eb0821b