Cleaning up font selection in settings dialog

This commit is contained in:
Dan Paulat 2023-09-28 23:11:19 -05:00
parent 4e5aa7b5e1
commit d3a3c3db36
5 changed files with 95 additions and 58 deletions

View file

@ -27,7 +27,7 @@ template<class T>
class SettingsInterface<T>::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<T>* self_;
SettingsVariable<T>* variable_ {nullptr};
bool stagedValid_ {true};
@ -53,17 +55,27 @@ public:
template<class T>
SettingsInterface<T>::SettingsInterface() :
SettingsInterfaceBase(), p(std::make_unique<Impl>())
SettingsInterfaceBase(), p(std::make_unique<Impl>(this))
{
}
template<class T>
SettingsInterface<T>::~SettingsInterface() = default;
template<class T>
SettingsInterface<T>::SettingsInterface(SettingsInterface&&) noexcept = default;
SettingsInterface<T>::SettingsInterface(SettingsInterface&& o) noexcept :
p {std::move(o.p)}
{
p->self_ = this;
}
template<class T>
SettingsInterface<T>&
SettingsInterface<T>::operator=(SettingsInterface&&) noexcept = default;
SettingsInterface<T>::operator=(SettingsInterface&& o) noexcept
{
p = std::move(o.p);
p->self_ = this;
return *this;
}
template<class T>
void SettingsInterface<T>::SetSettingsVariable(SettingsVariable<T>& variable)
@ -77,6 +89,27 @@ SettingsVariable<T>* SettingsInterface<T>::GetSettingsVariable() const
return p->variable_;
}
template<class T>
bool SettingsInterface<T>::IsDefault()
{
bool isDefault = false;
const std::optional<T> 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<class T>
bool SettingsInterface<T>::Commit()
{
@ -99,6 +132,14 @@ void SettingsInterface<T>::StageDefault()
p->UpdateResetButton();
}
template<class T>
void SettingsInterface<T>::StageValue(const T& value)
{
p->variable_->StageValue(value);
p->UpdateEditWidget();
p->UpdateResetButton();
}
template<class T>
void SettingsInterface<T>::SetEditWidget(QWidget* widget)
{
@ -412,20 +453,9 @@ void SettingsInterface<T>::Impl::UpdateEditWidget()
template<class T>
void SettingsInterface<T>::Impl::UpdateResetButton()
{
const std::optional<T> 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());
}
}

View file

@ -45,6 +45,14 @@ public:
*/
SettingsVariable<T>* 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.
*

View file

@ -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.
*/