From 7d2635608df297c52aa3929b5f7c46cd86577835 Mon Sep 17 00:00:00 2001 From: AdenKoperczak Date: Fri, 28 Mar 2025 11:28:11 -0400 Subject: [PATCH] Fixes to add_custom_layer_dialog suggested on GitHub --- .../scwx/qt/settings/settings_interface.cpp | 155 ++++++++---------- .../source/scwx/qt/ui/custom_layer_dialog.hpp | 5 +- scwx-qt/source/scwx/qt/ui/settings_dialog.cpp | 12 +- scwx-qt/source/scwx/qt/ui/settings_dialog.hpp | 4 +- 4 files changed, 77 insertions(+), 99 deletions(-) diff --git a/scwx-qt/source/scwx/qt/settings/settings_interface.cpp b/scwx-qt/source/scwx/qt/settings/settings_interface.cpp index 3fbeccf3..e2c280b2 100644 --- a/scwx-qt/source/scwx/qt/settings/settings_interface.cpp +++ b/scwx-qt/source/scwx/qt/settings/settings_interface.cpp @@ -44,6 +44,7 @@ public: void UpdateEditWidget(); void UpdateResetButton(); void UpdateUnitLabel(); + void UpdateValidityDisplay(); SettingsInterface* self_; @@ -173,24 +174,19 @@ void SettingsInterface::SetEditWidget(QWidget* widget) { if constexpr (std::is_same_v) { - QObject::connect( - hotkeyEdit, - &ui::HotkeyEdit::KeySequenceChanged, - p->context_.get(), - [this, hotkeyEdit](const QKeySequence& sequence) - { - const std::string value {sequence.toString().toStdString()}; + QObject::connect(hotkeyEdit, + &ui::HotkeyEdit::KeySequenceChanged, + p->context_.get(), + [this](const QKeySequence& sequence) + { + const std::string value { + sequence.toString().toStdString()}; - // Attempt to stage the value - p->stagedValid_ = p->variable_->StageValue(value); - p->UpdateResetButton(); - - hotkeyEdit->setStyleSheet(p->stagedValid_ ? kValidStyleSheet_ : - kInvalidStyleSheet_); - hotkeyEdit->setToolTip(p->invalidTooltip_ && !p->stagedValid_ ? - p->invalidTooltip_->c_str() : - ""); - }); + // Attempt to stage the value + p->stagedValid_ = p->variable_->StageValue(value); + p->UpdateResetButton(); + p->UpdateValidityDisplay(); + }); } } else if (QLineEdit* lineEdit = dynamic_cast(widget)) @@ -199,64 +195,54 @@ void SettingsInterface::SetEditWidget(QWidget* widget) { // If the line is edited (not programatically changed), stage the new // value - QObject::connect( - lineEdit, - &QLineEdit::textEdited, - p->context_.get(), - [this, lineEdit](const QString& text) - { - const QString trimmedText = - p->trimmingEnabled_ ? text.trimmed() : text; + QObject::connect(lineEdit, + &QLineEdit::textEdited, + p->context_.get(), + [this](const QString& text) + { + const QString trimmedText = + p->trimmingEnabled_ ? text.trimmed() : text; - // Map to value if required - std::string value {trimmedText.toStdString()}; - if (p->mapToValue_ != nullptr) - { - value = p->mapToValue_(value); - } + // Map to value if required + std::string value {trimmedText.toStdString()}; + if (p->mapToValue_ != nullptr) + { + value = p->mapToValue_(value); + } - // Attempt to stage the value - p->stagedValid_ = p->variable_->StageValue(value); - p->UpdateResetButton(); - - lineEdit->setStyleSheet(p->stagedValid_ ? kValidStyleSheet_ : - kInvalidStyleSheet_); - lineEdit->setToolTip(p->invalidTooltip_ && !p->stagedValid_ ? - p->invalidTooltip_->c_str() : - ""); - }); + // Attempt to stage the value + p->stagedValid_ = p->variable_->StageValue(value); + p->UpdateResetButton(); + p->UpdateValidityDisplay(); + }); } else if constexpr (std::is_same_v) { // If the line is edited (not programatically changed), stage the new // value - QObject::connect( - lineEdit, - &QLineEdit::textEdited, - p->context_.get(), - [this, lineEdit](const QString& text) - { - // Convert to a double - bool ok = false; - const double value = text.toDouble(&ok); - if (ok) - { - // Attempt to stage the value - p->stagedValid_ = p->variable_->StageValue(value); - p->UpdateResetButton(); - } - else - { - p->stagedValid_ = false; - p->UpdateResetButton(); - } + QObject::connect(lineEdit, + &QLineEdit::textEdited, + p->context_.get(), + [this](const QString& text) + { + // Convert to a double + bool ok = false; + const double value = text.toDouble(&ok); + if (ok) + { + // Attempt to stage the value + p->stagedValid_ = + p->variable_->StageValue(value); + p->UpdateResetButton(); + } + else + { + p->stagedValid_ = false; + p->UpdateResetButton(); + } - lineEdit->setStyleSheet(p->stagedValid_ ? kValidStyleSheet_ : - kInvalidStyleSheet_); - lineEdit->setToolTip(p->invalidTooltip_ && !p->stagedValid_ ? - p->invalidTooltip_->c_str() : - ""); - }); + p->UpdateValidityDisplay(); + }); } else if constexpr (std::is_same_v>) { @@ -266,7 +252,7 @@ void SettingsInterface::SetEditWidget(QWidget* widget) lineEdit, &QLineEdit::textEdited, p->context_.get(), - [this, lineEdit](const QString& text) + [this](const QString& text) { // Map to value if required T value {}; @@ -300,12 +286,7 @@ void SettingsInterface::SetEditWidget(QWidget* widget) // Attempt to stage the value p->stagedValid_ = p->variable_->StageValue(value); p->UpdateResetButton(); - - lineEdit->setStyleSheet(p->stagedValid_ ? kValidStyleSheet_ : - kInvalidStyleSheet_); - lineEdit->setToolTip(p->invalidTooltip_ && !p->stagedValid_ ? - p->invalidTooltip_->c_str() : - ""); + p->UpdateValidityDisplay(); }); } } @@ -368,7 +349,7 @@ void SettingsInterface::SetEditWidget(QWidget* widget) spinBox, &QSpinBox::valueChanged, p->context_.get(), - [this, spinBox](int i) + [this](int i) { const T value = p->variable_->GetValue(); const std::optional staged = p->variable_->GetStaged(); @@ -390,11 +371,7 @@ void SettingsInterface::SetEditWidget(QWidget* widget) } // Otherwise, don't process an unchanged value - spinBox->setStyleSheet(p->stagedValid_ ? kValidStyleSheet_ : - kInvalidStyleSheet_); - spinBox->setToolTip(p->invalidTooltip_ && !p->stagedValid_ ? - p->invalidTooltip_->c_str() : - ""); + p->UpdateValidityDisplay(); }); } } @@ -420,7 +397,7 @@ void SettingsInterface::SetEditWidget(QWidget* widget) doubleSpinBox, &QDoubleSpinBox::valueChanged, p->context_.get(), - [this, doubleSpinBox](double d) + [this](double d) { if (p->unitEnabled_) { @@ -447,12 +424,7 @@ void SettingsInterface::SetEditWidget(QWidget* widget) } // Otherwise, don't process an unchanged value - doubleSpinBox->setStyleSheet( - p->stagedValid_ ? kValidStyleSheet_ : kInvalidStyleSheet_); - doubleSpinBox->setToolTip(p->invalidTooltip_ && - !p->stagedValid_ ? - p->invalidTooltip_->c_str() : - ""); + p->UpdateValidityDisplay(); }); } } @@ -662,6 +634,15 @@ void SettingsInterface::Impl::UpdateUnitLabel() unitLabel_->setText(QString::fromStdString(unitAbbreviation_.value_or(""))); } +template +void SettingsInterface::Impl::UpdateValidityDisplay() +{ + editWidget_->setStyleSheet(stagedValid_ ? kValidStyleSheet_ : + kInvalidStyleSheet_); + editWidget_->setToolTip( + invalidTooltip_ && !stagedValid_ ? invalidTooltip_->c_str() : ""); +} + template void SettingsInterface::Impl::UpdateResetButton() { diff --git a/scwx-qt/source/scwx/qt/ui/custom_layer_dialog.hpp b/scwx-qt/source/scwx/qt/ui/custom_layer_dialog.hpp index 8c03cf71..ad0a2526 100644 --- a/scwx-qt/source/scwx/qt/ui/custom_layer_dialog.hpp +++ b/scwx-qt/source/scwx/qt/ui/custom_layer_dialog.hpp @@ -17,15 +17,12 @@ class CustomLayerDialogImpl; class CustomLayerDialog : public QDialog { Q_OBJECT + Q_DISABLE_COPY_MOVE(CustomLayerDialog) public: explicit CustomLayerDialog(const QMapLibre::Settings& settings, QWidget* parent = nullptr); ~CustomLayerDialog() override; - CustomLayerDialog(const CustomLayerDialog&) = delete; - CustomLayerDialog(CustomLayerDialog&&) = delete; - CustomLayerDialog& operator=(const CustomLayerDialog&) = delete; - CustomLayerDialog& operator=(CustomLayerDialog&&) = delete; std::string selected_layer(); diff --git a/scwx-qt/source/scwx/qt/ui/settings_dialog.cpp b/scwx-qt/source/scwx/qt/ui/settings_dialog.cpp index bf5c9180..c1e6d501 100644 --- a/scwx-qt/source/scwx/qt/ui/settings_dialog.cpp +++ b/scwx-qt/source/scwx/qt/ui/settings_dialog.cpp @@ -108,8 +108,8 @@ static const std::unordered_map class SettingsDialogImpl { public: - explicit SettingsDialogImpl(SettingsDialog* self, - QMapLibre::Settings mapSettings) : + explicit SettingsDialogImpl(SettingsDialog* self, + QMapLibre::Settings& mapSettings) : self_ {self}, radarSiteDialog_ {new RadarSiteDialog(self)}, alertAudioRadarSiteDialog_ {new RadarSiteDialog(self)}, @@ -117,7 +117,7 @@ public: countyDialog_ {new CountyDialog(self)}, wfoDialog_ {new WFODialog(self)}, fontDialog_ {new QFontDialog(self)}, - mapSettings_ {std::move(mapSettings)}, + mapSettings_ {mapSettings}, fontCategoryModel_ {new QStandardItemModel(self)}, settings_ {std::initializer_list { &defaultRadarSite_, @@ -223,7 +223,7 @@ public: WFODialog* wfoDialog_; QFontDialog* fontDialog_; - QMapLibre::Settings mapSettings_; + QMapLibre::Settings& mapSettings_; QStandardItemModel* fontCategoryModel_; @@ -298,8 +298,8 @@ public: std::vector settings_; }; -SettingsDialog::SettingsDialog(const QMapLibre::Settings& mapSettings, - QWidget* parent) : +SettingsDialog::SettingsDialog(QMapLibre::Settings& mapSettings, + QWidget* parent) : QDialog(parent), p {std::make_unique(this, mapSettings)}, ui(new Ui::SettingsDialog) diff --git a/scwx-qt/source/scwx/qt/ui/settings_dialog.hpp b/scwx-qt/source/scwx/qt/ui/settings_dialog.hpp index b767aa0b..866e1ea8 100644 --- a/scwx-qt/source/scwx/qt/ui/settings_dialog.hpp +++ b/scwx-qt/source/scwx/qt/ui/settings_dialog.hpp @@ -25,8 +25,8 @@ private: Q_DISABLE_COPY(SettingsDialog) public: - explicit SettingsDialog(const QMapLibre::Settings& mapSettings, - QWidget* parent = nullptr); + explicit SettingsDialog(QMapLibre::Settings& mapSettings, + QWidget* parent = nullptr); ~SettingsDialog(); private: