Connecting font selection to settings dialog, in-work

This commit is contained in:
Dan Paulat 2023-09-27 23:47:37 -05:00
parent d82fb666f9
commit 4e5aa7b5e1
6 changed files with 245 additions and 95 deletions

View file

@ -9,6 +9,7 @@
#include <QCheckBox>
#include <QComboBox>
#include <QCoreApplication>
#include <QLabel>
#include <QLineEdit>
#include <QSpinBox>
#include <QWidget>
@ -33,6 +34,9 @@ public:
~Impl() {}
template<class U>
void SetWidgetText(U* widget, const T& currentValue);
void UpdateEditWidget();
void UpdateResetButton();
@ -105,6 +109,11 @@ void SettingsInterface<T>::SetEditWidget(QWidget* widget)
p->editWidget_ = widget;
if (widget == nullptr)
{
return;
}
if (QLineEdit* lineEdit = dynamic_cast<QLineEdit*>(widget))
{
if constexpr (std::is_same_v<T, std::string>)
@ -274,33 +283,36 @@ void SettingsInterface<T>::SetResetButton(QAbstractButton* button)
p->resetButton_ = button;
QObject::connect(p->resetButton_,
&QAbstractButton::clicked,
p->context_.get(),
[this]()
{
T defaultValue = p->variable_->GetDefault();
if (p->variable_->GetValue() == defaultValue)
if (p->resetButton_ != nullptr)
{
QObject::connect(p->resetButton_,
&QAbstractButton::clicked,
p->context_.get(),
[this]()
{
// If the current value is default, reset the staged
// value
p->variable_->Reset();
p->stagedValid_ = true;
p->UpdateEditWidget();
p->UpdateResetButton();
}
else
{
// Stage the default value
p->stagedValid_ =
p->variable_->StageValue(defaultValue);
p->UpdateEditWidget();
p->UpdateResetButton();
}
});
T defaultValue = p->variable_->GetDefault();
p->UpdateResetButton();
if (p->variable_->GetValue() == defaultValue)
{
// If the current value is default, reset the
// staged value
p->variable_->Reset();
p->stagedValid_ = true;
p->UpdateEditWidget();
p->UpdateResetButton();
}
else
{
// Stage the default value
p->stagedValid_ =
p->variable_->StageValue(defaultValue);
p->UpdateEditWidget();
p->UpdateResetButton();
}
});
p->UpdateResetButton();
}
}
template<class T>
@ -317,6 +329,39 @@ void SettingsInterface<T>::SetMapToValueFunction(
p->mapToValue_ = function;
}
template<class T>
template<class U>
void SettingsInterface<T>::Impl::SetWidgetText(U* widget, const T& currentValue)
{
if constexpr (std::is_integral_v<T>)
{
widget->setText(QString::number(currentValue));
}
else if constexpr (std::is_same_v<T, std::string>)
{
if (mapFromValue_ != nullptr)
{
widget->setText(QString::fromStdString(mapFromValue_(currentValue)));
}
else
{
widget->setText(QString::fromStdString(currentValue));
}
}
else if constexpr (std::is_same_v<T, std::vector<std::int64_t>>)
{
if (mapFromValue_ != nullptr)
{
widget->setText(QString::fromStdString(mapFromValue_(currentValue)));
}
else
{
widget->setText(QString::fromStdString(
fmt::format("{}", fmt::join(currentValue, ", "))));
}
}
}
template<class T>
void SettingsInterface<T>::Impl::UpdateEditWidget()
{
@ -327,35 +372,11 @@ void SettingsInterface<T>::Impl::UpdateEditWidget()
if (QLineEdit* lineEdit = dynamic_cast<QLineEdit*>(editWidget_))
{
if constexpr (std::is_integral_v<T>)
{
lineEdit->setText(QString::number(currentValue));
}
else if constexpr (std::is_same_v<T, std::string>)
{
if (mapFromValue_ != nullptr)
{
lineEdit->setText(
QString::fromStdString(mapFromValue_(currentValue)));
}
else
{
lineEdit->setText(QString::fromStdString(currentValue));
}
}
else if constexpr (std::is_same_v<T, std::vector<std::int64_t>>)
{
if (mapFromValue_ != nullptr)
{
lineEdit->setText(
QString::fromStdString(mapFromValue_(currentValue)));
}
else
{
lineEdit->setText(QString::fromStdString(
fmt::format("{}", fmt::join(currentValue, ", "))));
}
}
SetWidgetText(lineEdit, currentValue);
}
else if (QLabel* label = dynamic_cast<QLabel*>(editWidget_))
{
SetWidgetText(label, currentValue);
}
else if (QCheckBox* checkBox = dynamic_cast<QCheckBox*>(editWidget_))
{

View file

@ -239,6 +239,12 @@ std::optional<T> SettingsVariable<T>::GetStaged() const
return p->staged_;
}
template<class T>
T SettingsVariable<T>::GetStagedOrValue() const
{
return p->staged_.value_or(GetValue());
}
template<class T>
T SettingsVariable<T>::GetDefault() const
{

View file

@ -103,6 +103,14 @@ public:
*/
std::optional<T> GetStaged() const;
/**
* Gets the staged value of the settings variable, if defined, otherwise the
* current value.
*
* @return Staged value or current value
*/
T GetStagedOrValue() const;
/**
* Validate the value against the defined parameters of the settings
* variable.

View file

@ -79,6 +79,13 @@ public:
void InitializeFontVariables();
friend bool operator==(const FontData& lhs, const FontData& rhs)
{
return (lhs.fontFamily_ == rhs.fontFamily_ &&
lhs.fontStyle_ == rhs.fontStyle_ &&
lhs.fontPointSize_ == rhs.fontPointSize_);
}
TextSettings* self_;
std::unordered_map<types::FontCategory, FontData> fontData_ {};
@ -168,7 +175,8 @@ TextSettings& TextSettings::Instance()
bool operator==(const TextSettings& lhs, const TextSettings& rhs)
{
return (lhs.p->hoverTextWrap_ == rhs.p->hoverTextWrap_ &&
return (lhs.p->fontData_ == rhs.p->fontData_ &&
lhs.p->hoverTextWrap_ == rhs.p->hoverTextWrap_ &&
lhs.p->tooltipMethod_ == rhs.p->tooltipMethod_);
}

View file

@ -9,6 +9,7 @@
#include <scwx/qt/settings/settings_interface.hpp>
#include <scwx/qt/settings/text_settings.hpp>
#include <scwx/qt/types/alert_types.hpp>
#include <scwx/qt/types/font_types.hpp>
#include <scwx/qt/types/text_types.hpp>
#include <scwx/qt/ui/placefile_settings_widget.hpp>
#include <scwx/qt/ui/radar_site_dialog.hpp>
@ -119,7 +120,10 @@ public:
}
// Configure font dialog
fontDialog_->setOptions(QFontDialog::FontDialogOption::ScalableFonts);
fontDialog_->setOptions(
QFontDialog::FontDialogOption::DontUseNativeDialog |
QFontDialog::FontDialogOption::ScalableFonts);
fontDialog_->setWindowModality(Qt::WindowModality::WindowModal);
}
~SettingsDialogImpl() = default;
@ -133,6 +137,10 @@ public:
void ShowColorDialog(QLineEdit* lineEdit, QFrame* frame = nullptr);
void UpdateRadarDialogLocation(const std::string& id);
QFont GetSelectedFont();
void SelectFontCategory(types::FontCategory fontCategory);
void UpdateFontDisplayData();
void ApplyChanges();
void DiscardChanges();
void ResetToDefault();
@ -157,6 +165,8 @@ public:
QStandardItemModel* fontCategoryModel_;
types::FontCategory selectedFontCategory_ {types::FontCategory::Unknown};
settings::SettingsInterface<std::string> defaultRadarSite_ {};
settings::SettingsInterface<std::vector<std::int64_t>> fontSizes_ {};
settings::SettingsInterface<std::int64_t> gridWidth_ {};
@ -251,18 +261,6 @@ void SettingsDialogImpl::ConnectSignals()
}
});
QObject::connect(self_->ui->fontSelectButton,
&QAbstractButton::clicked,
self_,
[this]() { fontDialog_->show(); });
QObject::connect(
fontDialog_,
&QFontDialog::fontSelected,
self_,
[this](const QFont& font)
{ logger_->debug("Selected font: {}", font.toString().toStdString()); });
// Update the Radar Site dialog "map" location with the currently selected
// radar site
auto& defaultRadarSite = *defaultRadarSite_.GetSettingsVariable();
@ -270,6 +268,66 @@ void SettingsDialogImpl::ConnectSignals()
[this](const std::string& newValue)
{ UpdateRadarDialogLocation(newValue); });
QObject::connect(
self_->ui->fontListView->selectionModel(),
&QItemSelectionModel::selectionChanged,
self_,
[this](const QItemSelection& selected, const QItemSelection& deselected)
{
if (selected.size() == 0 && deselected.size() == 0)
{
// Items which stay selected but change their index are not
// included in selected and deselected. Thus, this signal might
// be emitted with both selected and deselected empty, if only
// the indices of selected items change.
return;
}
if (selected.size() > 0)
{
QModelIndex selectedIndex = selected[0].indexes()[0];
QVariant variantData =
self_->ui->fontListView->model()->data(selectedIndex);
if (variantData.typeId() == QMetaType::QString)
{
types::FontCategory fontCategory =
types::GetFontCategory(variantData.toString().toStdString());
SelectFontCategory(fontCategory);
UpdateFontDisplayData();
}
}
});
QObject::connect(self_->ui->fontSelectButton,
&QAbstractButton::clicked,
self_,
[this]()
{
fontDialog_->setCurrentFont(GetSelectedFont());
fontDialog_->show();
});
QObject::connect(fontDialog_,
&QFontDialog::fontSelected,
self_,
[this](const QFont& font)
{
logger_->debug("Selected font: {}",
font.toString().toStdString());
fontFamilies_.at(selectedFontCategory_)
.GetSettingsVariable()
->StageValue(font.family().toStdString());
fontStyles_.at(selectedFontCategory_)
.GetSettingsVariable()
->StageValue(font.styleName().toStdString());
fontPointSizes_.at(selectedFontCategory_)
.GetSettingsVariable()
->StageValue(font.pointSizeF());
UpdateFontDisplayData();
});
QObject::connect(
self_->ui->buttonBox,
&QDialogButtonBox::clicked,
@ -706,6 +764,8 @@ void SettingsDialogImpl::SetupTextTab()
fontSize.SetSettingsVariable(textSettings.font_point_size(fontCategory));
}
self_->ui->fontListView->setCurrentIndex(fontCategoryModel_->index(0, 0));
SelectFontCategory(*types::FontCategoryIterator().begin());
UpdateFontDisplayData();
hoverTextWrap_.SetSettingsVariable(textSettings.hover_text_wrap());
hoverTextWrap_.SetEditWidget(self_->ui->hoverTextWrapSpinBox);
@ -861,6 +921,73 @@ void SettingsDialogImpl::UpdateRadarDialogLocation(const std::string& id)
}
}
QFont SettingsDialogImpl::GetSelectedFont()
{
std::string fontFamily = fontFamilies_.at(selectedFontCategory_)
.GetSettingsVariable()
->GetStagedOrValue();
std::string fontStyle = fontStyles_.at(selectedFontCategory_)
.GetSettingsVariable()
->GetStagedOrValue();
units::font_size::points<double> fontSize {
fontPointSizes_.at(selectedFontCategory_)
.GetSettingsVariable()
->GetStagedOrValue()};
QFont font(QString::fromStdString(fontFamily));
font.setStyleName(QString::fromStdString(fontStyle));
font.setPointSizeF(fontSize.value());
return font;
}
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;
}
void SettingsDialogImpl::UpdateFontDisplayData()
{
QFont font = GetSelectedFont();
self_->ui->fontNameLabel->setText(font.family());
self_->ui->fontStyleLabel->setText(font.styleName());
self_->ui->fontSizeLabel->setText(QString::number(font.pointSizeF()));
self_->ui->fontPreviewLabel->setFont(font);
}
void SettingsDialogImpl::ApplyChanges()
{
logger_->info("Applying settings changes");

View file

@ -102,7 +102,7 @@
</sizepolicy>
</property>
<property name="currentIndex">
<number>0</number>
<number>2</number>
</property>
<widget class="QWidget" name="general">
<layout class="QVBoxLayout" name="verticalLayout_2">
@ -468,36 +468,16 @@
<property name="bottomMargin">
<number>0</number>
</property>
<item row="2" column="1">
<spacer name="horizontalSpacer_2">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
<item row="2" column="0">
<widget class="QPushButton" name="resetAllFontsButton">
<property name="text">
<string>Reset All Fonts</string>
</property>
</widget>
</item>
<item row="1" column="0" colspan="3">
<widget class="QListView" name="fontListView"/>
</item>
<item row="0" column="0" colspan="3">
<item row="0" column="0" colspan="2">
<widget class="QLabel" name="label_10">
<property name="text">
<string>Display Item:</string>
</property>
</widget>
</item>
<item row="1" column="0" colspan="2">
<widget class="QListView" name="fontListView"/>
</item>
</layout>
</widget>
</item>
@ -538,10 +518,10 @@
<item row="7" column="0" colspan="5">
<widget class="QFrame" name="frame_7">
<property name="frameShape">
<enum>QFrame::StyledPanel</enum>
<enum>QFrame::Panel</enum>
</property>
<property name="frameShadow">
<enum>QFrame::Raised</enum>
<enum>QFrame::Plain</enum>
</property>
<layout class="QVBoxLayout" name="verticalLayout_7">
<item>