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

View file

@ -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()

View file

@ -446,7 +446,7 @@
<property name="frameShadow">
<enum>QFrame::Plain</enum>
</property>
<layout class="QGridLayout" name="gridLayout_8" columnstretch="1,1">
<layout class="QGridLayout" name="gridLayout_8" columnstretch="2,3">
<item row="0" column="0">
<widget class="QFrame" name="frame_5">
<property name="frameShape">