diff --git a/scwx-qt/source/scwx/qt/settings/settings_interface.cpp b/scwx-qt/source/scwx/qt/settings/settings_interface.cpp index 49fe7513..b7133537 100644 --- a/scwx-qt/source/scwx/qt/settings/settings_interface.cpp +++ b/scwx-qt/source/scwx/qt/settings/settings_interface.cpp @@ -27,7 +27,7 @@ template class SettingsInterface::Impl { public: - explicit Impl() + explicit Impl(SettingsInterface* self) : self_ {self} { context_->moveToThread(QCoreApplication::instance()->thread()); } @@ -40,6 +40,8 @@ public: void UpdateEditWidget(); void UpdateResetButton(); + SettingsInterface* self_; + SettingsVariable* variable_ {nullptr}; bool stagedValid_ {true}; @@ -53,17 +55,27 @@ public: template SettingsInterface::SettingsInterface() : - SettingsInterfaceBase(), p(std::make_unique()) + SettingsInterfaceBase(), p(std::make_unique(this)) { } template SettingsInterface::~SettingsInterface() = default; template -SettingsInterface::SettingsInterface(SettingsInterface&&) noexcept = default; +SettingsInterface::SettingsInterface(SettingsInterface&& o) noexcept : + p {std::move(o.p)} +{ + p->self_ = this; +} + template SettingsInterface& -SettingsInterface::operator=(SettingsInterface&&) noexcept = default; +SettingsInterface::operator=(SettingsInterface&& o) noexcept +{ + p = std::move(o.p); + p->self_ = this; + return *this; +} template void SettingsInterface::SetSettingsVariable(SettingsVariable& variable) @@ -77,6 +89,27 @@ SettingsVariable* SettingsInterface::GetSettingsVariable() const return p->variable_; } +template +bool SettingsInterface::IsDefault() +{ + bool isDefault = false; + + const std::optional staged = p->variable_->GetStaged(); + const T defaultValue = p->variable_->GetDefault(); + const T value = p->variable_->GetValue(); + + if (staged.has_value()) + { + isDefault = (p->stagedValid_ && *staged == defaultValue); + } + else + { + isDefault = (value == defaultValue); + } + + return isDefault; +} + template bool SettingsInterface::Commit() { @@ -99,6 +132,14 @@ void SettingsInterface::StageDefault() p->UpdateResetButton(); } +template +void SettingsInterface::StageValue(const T& value) +{ + p->variable_->StageValue(value); + p->UpdateEditWidget(); + p->UpdateResetButton(); +} + template void SettingsInterface::SetEditWidget(QWidget* widget) { @@ -412,20 +453,9 @@ void SettingsInterface::Impl::UpdateEditWidget() template void SettingsInterface::Impl::UpdateResetButton() { - const std::optional staged = variable_->GetStaged(); - const T defaultValue = variable_->GetDefault(); - const T value = variable_->GetValue(); - if (resetButton_ != nullptr) { - if (staged.has_value()) - { - resetButton_->setVisible(!stagedValid_ || *staged != defaultValue); - } - else - { - resetButton_->setVisible(value != defaultValue); - } + resetButton_->setVisible(!self_->IsDefault()); } } diff --git a/scwx-qt/source/scwx/qt/settings/settings_interface.hpp b/scwx-qt/source/scwx/qt/settings/settings_interface.hpp index 4ba1ea0e..f5f5bb4a 100644 --- a/scwx-qt/source/scwx/qt/settings/settings_interface.hpp +++ b/scwx-qt/source/scwx/qt/settings/settings_interface.hpp @@ -45,6 +45,14 @@ public: */ SettingsVariable* GetSettingsVariable() const; + /** + * Gets whether the staged value (or current value, if none staged) is + * set to the default value. + * + * @return true if the settings variable is set to default, otherwise false. + */ + bool IsDefault() override; + /** * Sets the current value of the associated settings variable to the staged * value. @@ -64,6 +72,11 @@ public: */ void StageDefault() override; + /** + * Stages a value to the associated settings variable. + */ + void StageValue(const T& value); + /** * Sets the edit widget from the settings dialog. * diff --git a/scwx-qt/source/scwx/qt/settings/settings_interface_base.hpp b/scwx-qt/source/scwx/qt/settings/settings_interface_base.hpp index 97ae442d..d0dc2ff2 100644 --- a/scwx-qt/source/scwx/qt/settings/settings_interface_base.hpp +++ b/scwx-qt/source/scwx/qt/settings/settings_interface_base.hpp @@ -24,10 +24,18 @@ public: SettingsInterfaceBase(SettingsInterfaceBase&&) noexcept; SettingsInterfaceBase& operator=(SettingsInterfaceBase&&) noexcept; + /** + * Gets whether the staged value (or current value, if none staged) is + * set to the default value. + * + * @return true if the settings variable is set to default, otherwise false. + */ + virtual bool IsDefault() = 0; + /** * Sets the current value of the associated settings variable to the staged * value. - * + * * @return true if the staged value was committed, false if no staged value * is present. */ diff --git a/scwx-qt/source/scwx/qt/ui/settings_dialog.cpp b/scwx-qt/source/scwx/qt/ui/settings_dialog.cpp index c175fe7b..32464ecf 100644 --- a/scwx-qt/source/scwx/qt/ui/settings_dialog.cpp +++ b/scwx-qt/source/scwx/qt/ui/settings_dialog.cpp @@ -312,18 +312,24 @@ void SettingsDialogImpl::ConnectSignals() self_, [this](const QFont& font) { - logger_->debug("Selected font: {}", - font.toString().toStdString()); - fontFamilies_.at(selectedFontCategory_) - .GetSettingsVariable() - ->StageValue(font.family().toStdString()); + .StageValue(font.family().toStdString()); fontStyles_.at(selectedFontCategory_) - .GetSettingsVariable() - ->StageValue(font.styleName().toStdString()); + .StageValue(font.styleName().toStdString()); fontPointSizes_.at(selectedFontCategory_) - .GetSettingsVariable() - ->StageValue(font.pointSizeF()); + .StageValue(font.pointSizeF()); + + UpdateFontDisplayData(); + }); + + QObject::connect(self_->ui->resetFontButton, + &QAbstractButton::clicked, + self_, + [this]() + { + fontFamilies_.at(selectedFontCategory_).StageDefault(); + fontStyles_.at(selectedFontCategory_).StageDefault(); + fontPointSizes_.at(selectedFontCategory_).StageDefault(); UpdateFontDisplayData(); }); @@ -943,37 +949,6 @@ QFont SettingsDialogImpl::GetSelectedFont() void SettingsDialogImpl::SelectFontCategory(types::FontCategory fontCategory) { - if (selectedFontCategory_ != types::FontCategory::Unknown && - selectedFontCategory_ != fontCategory) - { - auto& fontFamily = fontFamilies_.at(selectedFontCategory_); - auto& fontStyle = fontStyles_.at(selectedFontCategory_); - auto& fontSize = fontPointSizes_.at(selectedFontCategory_); - - fontFamily.SetResetButton(nullptr); - fontStyle.SetResetButton(nullptr); - fontSize.SetResetButton(nullptr); - - fontFamily.SetEditWidget(nullptr); - fontStyle.SetEditWidget(nullptr); - fontSize.SetEditWidget(nullptr); - } - - if (selectedFontCategory_ != fontCategory) - { - auto& fontFamily = fontFamilies_.at(fontCategory); - auto& fontStyle = fontStyles_.at(fontCategory); - auto& fontSize = fontPointSizes_.at(fontCategory); - - fontFamily.SetResetButton(self_->ui->resetFontButton); - fontStyle.SetResetButton(self_->ui->resetFontButton); - fontSize.SetResetButton(self_->ui->resetFontButton); - - fontFamily.SetEditWidget(self_->ui->fontNameLabel); - fontStyle.SetEditWidget(self_->ui->fontStyleLabel); - fontSize.SetEditWidget(self_->ui->fontSizeLabel); - } - selectedFontCategory_ = fontCategory; } @@ -986,6 +961,17 @@ void SettingsDialogImpl::UpdateFontDisplayData() self_->ui->fontSizeLabel->setText(QString::number(font.pointSizeF())); self_->ui->fontPreviewLabel->setFont(font); + + if (selectedFontCategory_ != types::FontCategory::Unknown) + { + auto& fontFamily = fontFamilies_.at(selectedFontCategory_); + auto& fontStyle = fontStyles_.at(selectedFontCategory_); + auto& fontSize = fontPointSizes_.at(selectedFontCategory_); + + self_->ui->resetFontButton->setVisible(!fontFamily.IsDefault() || + !fontStyle.IsDefault() || + !fontSize.IsDefault()); + } } void SettingsDialogImpl::ApplyChanges() diff --git a/scwx-qt/source/scwx/qt/ui/settings_dialog.ui b/scwx-qt/source/scwx/qt/ui/settings_dialog.ui index de695341..8ed4a76f 100644 --- a/scwx-qt/source/scwx/qt/ui/settings_dialog.ui +++ b/scwx-qt/source/scwx/qt/ui/settings_dialog.ui @@ -446,7 +446,7 @@ QFrame::Plain - +