Support editing integer arrays in settings interface (font sizes)

This commit is contained in:
Dan Paulat 2022-12-21 00:52:48 -06:00
parent b2dbcfefb0
commit 9b5b841903
4 changed files with 110 additions and 23 deletions

View file

@ -3,6 +3,8 @@
#include <scwx/qt/settings/settings_interface.hpp> #include <scwx/qt/settings/settings_interface.hpp>
#include <scwx/qt/settings/settings_variable.hpp> #include <scwx/qt/settings/settings_variable.hpp>
#include <boost/tokenizer.hpp>
#include <fmt/ranges.h>
#include <QAbstractButton> #include <QAbstractButton>
#include <QCheckBox> #include <QCheckBox>
#include <QComboBox> #include <QComboBox>
@ -41,8 +43,8 @@ public:
QWidget* editWidget_ {nullptr}; QWidget* editWidget_ {nullptr};
QAbstractButton* resetButton_ {nullptr}; QAbstractButton* resetButton_ {nullptr};
std::function<T(const T&)> mapFromValue_ {nullptr}; std::function<std::string(const T&)> mapFromValue_ {nullptr};
std::function<T(const T&)> mapToValue_ {nullptr}; std::function<T(const std::string&)> mapToValue_ {nullptr};
}; };
template<class T> template<class T>
@ -99,6 +101,52 @@ void SettingsInterface<T>::SetEditWidget(QWidget* widget)
// TODO: Display invalid status // TODO: Display invalid status
}); });
} }
else if constexpr (std::is_same_v<T, std::vector<std::int64_t>>)
{
// If the line is edited (not programatically changed), stage the new
// value
QObject::connect(
lineEdit,
&QLineEdit::textEdited,
p->context_.get(),
[this](const QString& text)
{
// Map to value if required
T value {};
if (p->mapToValue_ != nullptr)
{
// User-defined map to value
value = p->mapToValue_(text.toStdString());
}
else
{
// Tokenize string to parse each element
const std::string str {text.toStdString()};
boost::tokenizer tokens(str);
for (auto it = tokens.begin(); it != tokens.end(); ++it)
{
try
{
// Good value
value.push_back(
static_cast<T::value_type>(std::stoll(*it)));
}
catch (const std::exception&)
{
// Error value
value.push_back(
std::numeric_limits<T::value_type>::min());
}
}
}
// Attempt to stage the value
p->stagedValid_ = p->variable_->StageValue(value);
p->UpdateResetButton();
// TODO: Display invalid status
});
}
} }
else if (QCheckBox* checkBox = dynamic_cast<QCheckBox*>(widget)) else if (QCheckBox* checkBox = dynamic_cast<QCheckBox*>(widget))
{ {
@ -228,14 +276,14 @@ void SettingsInterface<T>::SetResetButton(QAbstractButton* button)
template<class T> template<class T>
void SettingsInterface<T>::SetMapFromValueFunction( void SettingsInterface<T>::SetMapFromValueFunction(
std::function<T(const T&)> function) std::function<std::string(const T&)> function)
{ {
p->mapFromValue_ = function; p->mapFromValue_ = function;
} }
template<class T> template<class T>
void SettingsInterface<T>::SetMapToValueFunction( void SettingsInterface<T>::SetMapToValueFunction(
std::function<T(const T&)> function) std::function<T(const std::string&)> function)
{ {
p->mapToValue_ = function; p->mapToValue_ = function;
} }
@ -266,6 +314,19 @@ void SettingsInterface<T>::Impl::UpdateEditWidget()
lineEdit->setText(QString::fromStdString(currentValue)); 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, ", "))));
}
}
} }
else if (QCheckBox* checkBox = dynamic_cast<QCheckBox*>(editWidget_)) else if (QCheckBox* checkBox = dynamic_cast<QCheckBox*>(editWidget_))
{ {

View file

@ -60,7 +60,7 @@ public:
* *
* @param function Map from settings value function * @param function Map from settings value function
*/ */
void SetMapFromValueFunction(std::function<T(const T&)> function); void SetMapFromValueFunction(std::function<std::string(const T&)> function);
/** /**
* If the edit widget displays a different value than what is stored in the * If the edit widget displays a different value than what is stored in the
@ -69,7 +69,7 @@ public:
* *
* @param function Map to settings value function * @param function Map to settings value function
*/ */
void SetMapToValueFunction(std::function<T(const T&)> function); void SetMapToValueFunction(std::function<T(const std::string&)> function);
private: private:
class Impl; class Impl;

View file

@ -55,11 +55,12 @@ public:
SettingsDialog* self_; SettingsDialog* self_;
settings::SettingsInterface<std::string> defaultRadarSite_ {}; settings::SettingsInterface<std::string> defaultRadarSite_ {};
settings::SettingsInterface<int64_t> gridWidth_ {}; settings::SettingsInterface<std::vector<std::int64_t>> fontSizes_ {};
settings::SettingsInterface<int64_t> gridHeight_ {}; settings::SettingsInterface<std::int64_t> gridWidth_ {};
settings::SettingsInterface<std::string> mapboxApiKey_ {}; settings::SettingsInterface<std::int64_t> gridHeight_ {};
settings::SettingsInterface<bool> debugEnabled_ {}; settings::SettingsInterface<std::string> mapboxApiKey_ {};
settings::SettingsInterface<bool> debugEnabled_ {};
std::unordered_map<std::string, settings::SettingsInterface<std::string>> std::unordered_map<std::string, settings::SettingsInterface<std::string>>
colorTables_ {}; colorTables_ {};
@ -150,6 +151,10 @@ void SettingsDialogImpl::SetupGeneralTab()
defaultRadarSite_.SetEditWidget(self_->ui->radarSiteComboBox); defaultRadarSite_.SetEditWidget(self_->ui->radarSiteComboBox);
defaultRadarSite_.SetResetButton(self_->ui->resetRadarSiteButton); defaultRadarSite_.SetResetButton(self_->ui->resetRadarSiteButton);
fontSizes_.SetSettingsVariable(generalSettings.font_sizes());
fontSizes_.SetEditWidget(self_->ui->fontSizesLineEdit);
fontSizes_.SetResetButton(self_->ui->resetFontSizesButton);
gridWidth_.SetSettingsVariable(generalSettings.grid_width()); gridWidth_.SetSettingsVariable(generalSettings.grid_width());
gridWidth_.SetEditWidget(self_->ui->gridWidthSpinBox); gridWidth_.SetEditWidget(self_->ui->gridWidthSpinBox);
gridWidth_.SetResetButton(self_->ui->resetGridWidthButton); gridWidth_.SetResetButton(self_->ui->resetGridWidthButton);

View file

@ -116,17 +116,17 @@
</property> </property>
</widget> </widget>
</item> </item>
<item row="2" column="0"> <item row="3" column="0">
<widget class="QLabel" name="label_3"> <widget class="QLabel" name="label_3">
<property name="text"> <property name="text">
<string>Grid Height</string> <string>Grid Height</string>
</property> </property>
</widget> </widget>
</item> </item>
<item row="2" column="1"> <item row="3" column="2">
<widget class="QSpinBox" name="gridHeightSpinBox"/> <widget class="QSpinBox" name="gridHeightSpinBox"/>
</item> </item>
<item row="1" column="2"> <item row="2" column="3">
<widget class="QToolButton" name="resetGridWidthButton"> <widget class="QToolButton" name="resetGridWidthButton">
<property name="text"> <property name="text">
<string>...</string> <string>...</string>
@ -137,10 +137,7 @@
</property> </property>
</widget> </widget>
</item> </item>
<item row="3" column="1"> <item row="4" column="3">
<widget class="QLineEdit" name="mapboxApiKeyLineEdit"/>
</item>
<item row="3" column="2">
<widget class="QToolButton" name="resetMapboxApiKeyButton"> <widget class="QToolButton" name="resetMapboxApiKeyButton">
<property name="text"> <property name="text">
<string>...</string> <string>...</string>
@ -151,7 +148,7 @@
</property> </property>
</widget> </widget>
</item> </item>
<item row="0" column="2"> <item row="0" column="3">
<widget class="QToolButton" name="resetRadarSiteButton"> <widget class="QToolButton" name="resetRadarSiteButton">
<property name="text"> <property name="text">
<string>...</string> <string>...</string>
@ -162,13 +159,13 @@
</property> </property>
</widget> </widget>
</item> </item>
<item row="1" column="1"> <item row="2" column="2">
<widget class="QSpinBox" name="gridWidthSpinBox"/> <widget class="QSpinBox" name="gridWidthSpinBox"/>
</item> </item>
<item row="0" column="1"> <item row="0" column="2">
<widget class="QComboBox" name="radarSiteComboBox"/> <widget class="QComboBox" name="radarSiteComboBox"/>
</item> </item>
<item row="2" column="2"> <item row="3" column="3">
<widget class="QToolButton" name="resetGridHeightButton"> <widget class="QToolButton" name="resetGridHeightButton">
<property name="text"> <property name="text">
<string>...</string> <string>...</string>
@ -179,20 +176,44 @@
</property> </property>
</widget> </widget>
</item> </item>
<item row="3" column="0"> <item row="4" column="0">
<widget class="QLabel" name="label_4"> <widget class="QLabel" name="label_4">
<property name="text"> <property name="text">
<string>Mapbox API Key</string> <string>Mapbox API Key</string>
</property> </property>
</widget> </widget>
</item> </item>
<item row="4" column="2">
<widget class="QLineEdit" name="mapboxApiKeyLineEdit"/>
</item>
<item row="1" column="0"> <item row="1" column="0">
<widget class="QLabel" name="label_5">
<property name="text">
<string>Font Sizes</string>
</property>
</widget>
</item>
<item row="2" column="0">
<widget class="QLabel" name="label_2"> <widget class="QLabel" name="label_2">
<property name="text"> <property name="text">
<string>Grid Width</string> <string>Grid Width</string>
</property> </property>
</widget> </widget>
</item> </item>
<item row="1" column="2">
<widget class="QLineEdit" name="fontSizesLineEdit"/>
</item>
<item row="1" column="3">
<widget class="QToolButton" name="resetFontSizesButton">
<property name="text">
<string>...</string>
</property>
<property name="icon">
<iconset resource="../../../../scwx-qt.qrc">
<normaloff>:/res/icons/font-awesome-6/rotate-left-solid.svg</normaloff>:/res/icons/font-awesome-6/rotate-left-solid.svg</iconset>
</property>
</widget>
</item>
</layout> </layout>
</widget> </widget>
</item> </item>