mirror of
https://github.com/ciphervance/supercell-wx.git
synced 2025-11-01 08:20:06 +00:00
added full unit support for radius field, implemented such that future fields needing units can be done easily
This commit is contained in:
parent
c53836e91a
commit
146055fb99
7 changed files with 103 additions and 24 deletions
|
|
@ -40,6 +40,7 @@ public:
|
|||
|
||||
void UpdateEditWidget();
|
||||
void UpdateResetButton();
|
||||
void UpdateUnitLabel();
|
||||
|
||||
SettingsInterface<T>* self_;
|
||||
|
||||
|
|
@ -49,9 +50,14 @@ public:
|
|||
std::unique_ptr<QObject> context_ {std::make_unique<QObject>()};
|
||||
QWidget* editWidget_ {nullptr};
|
||||
QAbstractButton* resetButton_ {nullptr};
|
||||
QLabel* unitLabel_ {nullptr};
|
||||
|
||||
std::function<std::string(const T&)> mapFromValue_ {nullptr};
|
||||
std::function<T(const std::string&)> mapToValue_ {nullptr};
|
||||
|
||||
double unitScale_ {1};
|
||||
const std::string * unitAbbreiation_ {nullptr};
|
||||
bool unitEnabled_ {false};
|
||||
};
|
||||
|
||||
template<class T>
|
||||
|
|
@ -381,6 +387,11 @@ void SettingsInterface<T>::SetEditWidget(QWidget* widget)
|
|||
p->context_.get(),
|
||||
[this](double d)
|
||||
{
|
||||
if (p->unitEnabled_)
|
||||
{
|
||||
d = d / p->unitScale_;
|
||||
}
|
||||
|
||||
const T value = p->variable_->GetValue();
|
||||
const std::optional<T> staged = p->variable_->GetStaged();
|
||||
|
||||
|
|
@ -448,6 +459,11 @@ void SettingsInterface<T>::SetResetButton(QAbstractButton* button)
|
|||
p->UpdateResetButton();
|
||||
}
|
||||
}
|
||||
template<class T>
|
||||
void SettingsInterface<T>::SetUnitLabel(QLabel* label)
|
||||
{
|
||||
p->unitLabel_ = label;
|
||||
}
|
||||
|
||||
template<class T>
|
||||
void SettingsInterface<T>::SetMapFromValueFunction(
|
||||
|
|
@ -463,6 +479,17 @@ void SettingsInterface<T>::SetMapToValueFunction(
|
|||
p->mapToValue_ = function;
|
||||
}
|
||||
|
||||
template<class T>
|
||||
void SettingsInterface<T>::SetUnit(const double& scale,
|
||||
const std::string& abbreviation)
|
||||
{
|
||||
p->unitScale_ = scale;
|
||||
p->unitAbbreiation_ = &abbreviation;
|
||||
p->unitEnabled_ = true;
|
||||
p->UpdateEditWidget();
|
||||
p->UpdateUnitLabel();
|
||||
}
|
||||
|
||||
template<class T>
|
||||
template<class U>
|
||||
void SettingsInterface<T>::Impl::SetWidgetText(U* widget, const T& currentValue)
|
||||
|
|
@ -559,11 +586,27 @@ void SettingsInterface<T>::Impl::UpdateEditWidget()
|
|||
{
|
||||
if constexpr (std::is_floating_point_v<T>)
|
||||
{
|
||||
doubleSpinBox->setValue(static_cast<double>(currentValue));
|
||||
double doubleValue = static_cast<double>(currentValue);
|
||||
if (unitEnabled_)
|
||||
{
|
||||
doubleValue = doubleValue * unitScale_;
|
||||
}
|
||||
doubleSpinBox->setValue(doubleValue);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
template<class T>
|
||||
void SettingsInterface<T>::Impl::UpdateUnitLabel()
|
||||
{
|
||||
if (unitLabel_ == nullptr || !unitEnabled_)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
unitLabel_->setText(QString::fromStdString(*unitAbbreiation_));
|
||||
}
|
||||
|
||||
template<class T>
|
||||
void SettingsInterface<T>::Impl::UpdateResetButton()
|
||||
{
|
||||
|
|
|
|||
|
|
@ -7,6 +7,8 @@
|
|||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
class QLabel;
|
||||
|
||||
namespace scwx
|
||||
{
|
||||
namespace qt
|
||||
|
|
@ -91,6 +93,13 @@ public:
|
|||
*/
|
||||
void SetResetButton(QAbstractButton* button) override;
|
||||
|
||||
/**
|
||||
* Sets the label for units from the settings dialog.
|
||||
*
|
||||
* @param label Unit label
|
||||
*/
|
||||
void SetUnitLabel(QLabel* label);
|
||||
|
||||
/**
|
||||
* If the edit widget displays a different value than what is stored in the
|
||||
* settings variable, a mapping function must be provided in order to convert
|
||||
|
|
@ -109,6 +118,14 @@ public:
|
|||
*/
|
||||
void SetMapToValueFunction(std::function<T(const std::string&)> function);
|
||||
|
||||
/**
|
||||
* Sets the unit to be used by this setting.
|
||||
*
|
||||
* @param scale The radio of the current unit to the base unit
|
||||
* @param abbreviation The abreviation to be displayed
|
||||
*/
|
||||
void SetUnit(const double& scale, const std::string& abbreviation);
|
||||
|
||||
private:
|
||||
class Impl;
|
||||
std::unique_ptr<Impl> p;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue