diff --git a/scwx-qt/source/scwx/qt/settings/settings_interface.cpp b/scwx-qt/source/scwx/qt/settings/settings_interface.cpp index 1db40dcf..41a81c9a 100644 --- a/scwx-qt/source/scwx/qt/settings/settings_interface.cpp +++ b/scwx-qt/source/scwx/qt/settings/settings_interface.cpp @@ -4,6 +4,7 @@ #include #include +#include #include #include #include @@ -38,6 +39,9 @@ public: std::unique_ptr context_ {std::make_unique()}; QWidget* editWidget_ {nullptr}; QAbstractButton* resetButton_ {nullptr}; + + std::function mapFromValue_ {nullptr}; + std::function mapToValue_ {nullptr}; }; template @@ -80,9 +84,37 @@ void SettingsInterface::SetEditWidget(QWidget* widget) p->context_.get(), [this](const QString& text) { + // Map to value if required + std::string value {text.toStdString()}; + if (p->mapToValue_ != nullptr) + { + value = p->mapToValue_(value); + } + // Attempt to stage the value - p->stagedValid_ = - p->variable_->StageValue(text.toStdString()); + p->stagedValid_ = p->variable_->StageValue(value); + p->UpdateResetButton(); + }); + } + } + else if (QComboBox* comboBox = dynamic_cast(widget)) + { + if constexpr (std::is_same_v) + { + QObject::connect(comboBox, + &QComboBox::currentTextChanged, + p->context_.get(), + [this](const QString& text) + { + // Map to value if required + std::string value {text.toStdString()}; + if (p->mapToValue_ != nullptr) + { + value = p->mapToValue_(value); + } + + // Attempt to stage the value + p->stagedValid_ = p->variable_->StageValue(value); p->UpdateResetButton(); }); } @@ -175,30 +207,67 @@ void SettingsInterface::SetResetButton(QAbstractButton* button) p->UpdateResetButton(); } +template +void SettingsInterface::SetMapFromValueFunction( + std::function function) +{ + p->mapFromValue_ = function; +} + +template +void SettingsInterface::SetMapToValueFunction( + std::function function) +{ + p->mapToValue_ = function; +} + template void SettingsInterface::Impl::UpdateEditWidget() { // Use the staged value if present, otherwise the current value const std::optional staged = variable_->GetStaged(); const T value = variable_->GetValue(); - const T& displayValue = staged.has_value() ? *staged : value; + const T& currentValue = staged.has_value() ? *staged : value; if (QLineEdit* lineEdit = dynamic_cast(editWidget_)) { if constexpr (std::is_integral_v) { - lineEdit->setText(QString::number(displayValue)); + lineEdit->setText(QString::number(currentValue)); } else if constexpr (std::is_same_v) { - lineEdit->setText(QString::fromStdString(displayValue)); + if (mapFromValue_ != nullptr) + { + lineEdit->setText( + QString::fromStdString(mapFromValue_(currentValue))); + } + else + { + lineEdit->setText(QString::fromStdString(currentValue)); + } + } + } + else if (QComboBox* comboBox = dynamic_cast(editWidget_)) + { + if constexpr (std::is_same_v) + { + if (mapFromValue_ != nullptr) + { + comboBox->setCurrentText( + QString::fromStdString(mapFromValue_(currentValue))); + } + else + { + comboBox->setCurrentText(QString::fromStdString(currentValue)); + } } } else if (QSpinBox* spinBox = dynamic_cast(editWidget_)) { if constexpr (std::is_integral_v) { - spinBox->setValue(static_cast(displayValue)); + spinBox->setValue(static_cast(currentValue)); } } } diff --git a/scwx-qt/source/scwx/qt/settings/settings_interface.hpp b/scwx-qt/source/scwx/qt/settings/settings_interface.hpp index 82e60d66..c47fbe9b 100644 --- a/scwx-qt/source/scwx/qt/settings/settings_interface.hpp +++ b/scwx-qt/source/scwx/qt/settings/settings_interface.hpp @@ -1,5 +1,6 @@ #pragma once +#include #include #include #include @@ -52,6 +53,24 @@ public: */ void SetResetButton(QAbstractButton* button); + /** + * If the edit widget displays a different value than what is stored in the + * settings variable, a mapping function must be provided in order to convert + * the value used by the edit widget from the settings value. + * + * @param function Map from settings value function + */ + void SetMapFromValueFunction(std::function function); + + /** + * If the edit widget displays a different value than what is stored in the + * settings variable, a mapping function must be provided in order to convert + * the value used by the edit widget to the settings value. + * + * @param function Map to settings value function + */ + void SetMapToValueFunction(std::function function); + private: class Impl; std::unique_ptr p; diff --git a/scwx-qt/source/scwx/qt/ui/settings_dialog.cpp b/scwx-qt/source/scwx/qt/ui/settings_dialog.cpp index 23ee6cbf..60250cfe 100644 --- a/scwx-qt/source/scwx/qt/ui/settings_dialog.cpp +++ b/scwx-qt/source/scwx/qt/ui/settings_dialog.cpp @@ -6,6 +6,8 @@ #include #include +#include + #include namespace scwx @@ -101,10 +103,8 @@ void SettingsDialogImpl::SetupGeneralTab() // Add sorted radar sites for (std::shared_ptr& radarSite : radarSites) { - QString text = - QString("%1 (%2)") - .arg(QString::fromStdString(radarSite->id())) - .arg(QString::fromStdString(radarSite->location_name())); + QString text = QString::fromStdString( + std::format("{} ({})", radarSite->id(), radarSite->location_name())); self_->ui->radarSiteComboBox->addItem(text); } @@ -112,6 +112,38 @@ void SettingsDialogImpl::SetupGeneralTab() manager::SettingsManager::general_settings(); defaultRadarSite_.SetSettingsVariable(generalSettings.default_radar_site()); + defaultRadarSite_.SetMapFromValueFunction( + [](const std::string& id) -> std::string + { + // Get the radar site associated with the ID + std::shared_ptr radarSite = + config::RadarSite::Get(id); + + if (radarSite == nullptr) + { + // No radar site found, just return the ID + return id; + } + + // Add location details to the radar site + return std::format( + "{} ({})", radarSite->id(), radarSite->location_name()); + }); + defaultRadarSite_.SetMapToValueFunction( + [](const std::string& text) -> std::string + { + // Find the position of location details + size_t pos = text.rfind(" ("); + + if (pos == std::string::npos) + { + // No location details found, just return the text + return text; + } + + // Remove location details from the radar site + return text.substr(0, pos); + }); defaultRadarSite_.SetEditWidget(self_->ui->radarSiteComboBox); defaultRadarSite_.SetResetButton(self_->ui->resetRadarSiteButton);