mathnode: increased decimal places

Also includes general improvements to the slider widget such as an optional
"autotrim" function to cut off trailing zeroes, a "format" string that's easier
to internationalize, and general tidiness.
This commit is contained in:
itsmattkc
2020-04-22 17:49:32 +10:00
parent 0f825cbc44
commit db117e0780
7 changed files with 90 additions and 39 deletions
@@ -58,7 +58,7 @@ PreferencesDiskTab::PreferencesDiskTab()
disk_management_layout->addWidget(new QLabel(tr("Maximum Disk Cache:")), row, 0);
maximum_cache_slider_ = new FloatSlider();
maximum_cache_slider_->SetSuffix(QStringLiteral(" GB"));
maximum_cache_slider_->SetFormat(tr("%1 GB"));
maximum_cache_slider_->SetMinimum(1.0);
maximum_cache_slider_->SetValue(Config::Current()["DiskCacheSize"].toDouble());
disk_management_layout->addWidget(maximum_cache_slider_, row, 1, 1, 2);
@@ -84,14 +84,14 @@ PreferencesDiskTab::PreferencesDiskTab()
cache_behavior_layout->addWidget(new QLabel(tr("Cache Ahead:")), row, 0);
cache_ahead_slider_ = new FloatSlider();
cache_ahead_slider_->SetSuffix(QStringLiteral(" seconds"));
cache_ahead_slider_->SetFormat(tr("%1 seconds"));
cache_ahead_slider_->SetValue(Config::Current()["DiskCacheAhead"].value<rational>().toDouble());
cache_behavior_layout->addWidget(cache_ahead_slider_, row, 1);
cache_behavior_layout->addWidget(new QLabel(tr("Cache Behind:")), row, 2);
cache_behind_slider_ = new FloatSlider();
cache_behind_slider_->SetSuffix(QStringLiteral(" seconds"));
cache_behind_slider_->SetFormat(tr("%1 seconds"));
cache_behind_slider_->SetValue(Config::Current()["DiskCacheBehind"].value<rational>().toDouble());
cache_behavior_layout->addWidget(cache_behind_slider_, row, 3);
+4
View File
@@ -36,9 +36,13 @@ MathNode::MathNode()
AddInput(method_in_);
param_a_in_ = new NodeInput(QStringLiteral("param_a_in"), NodeParam::kFloat);
param_a_in_->set_property(QStringLiteral("decimalplaces"), 8);
param_a_in_->set_property(QStringLiteral("autotrim"), true);
AddInput(param_a_in_);
param_b_in_ = new NodeInput(QStringLiteral("param_b_in"), NodeParam::kFloat);
param_b_in_->set_property(QStringLiteral("decimalplaces"), 8);
param_b_in_->set_property(QStringLiteral("autotrim"), true);
AddInput(param_b_in_);
}
@@ -662,6 +662,18 @@ void NodeParamViewWidgetBridge::PropertyChanged(const QString &key, const QVaria
foreach (QWidget* w, widgets_) {
static_cast<FloatSlider*>(w)->SetDisplayType(display_type);
}
} else if (key == QStringLiteral("decimalplaces")) {
int dec_places = value.toInt();
foreach (QWidget* w, widgets_) {
static_cast<FloatSlider*>(w)->SetDecimalPlaces(dec_places);
}
} else if (key == QStringLiteral("autotrim")) {
bool autotrim = value.toBool();
foreach (QWidget* w, widgets_) {
static_cast<FloatSlider*>(w)->SetAutoTrimDecimalPlaces(autotrim);
}
}
}
}
+37 -18
View File
@@ -26,7 +26,9 @@ OLIVE_NAMESPACE_ENTER
FloatSlider::FloatSlider(QWidget *parent) :
SliderBase(kFloat, parent),
display_type_(kNormal)
display_type_(kNormal),
decimal_places_(1),
autotrim_decimal_places_(false)
{
connect(this, SIGNAL(ValueChanged(QVariant)), this, SLOT(ConvertValue(QVariant)));
}
@@ -55,14 +57,31 @@ void FloatSlider::SetDecimalPlaces(int i)
{
decimal_places_ = i;
UpdateLabel(Value());
ForceLabelUpdate();
}
void FloatSlider::SetDisplayType(const FloatSlider::DisplayType &type)
{
display_type_ = type;
UpdateLabel(Value());
switch (display_type_) {
case kNormal:
ClearFormat();
break;
case kDecibel:
SetFormat(tr("%1 dB"));
break;
case kPercentage:
SetFormat(tr("%1%"));
break;
}
}
void FloatSlider::SetAutoTrimDecimalPlaces(bool e)
{
autotrim_decimal_places_ = e;
ForceLabelUpdate();
}
QString FloatSlider::ValueToString(const QVariant &v)
@@ -74,18 +93,25 @@ QString FloatSlider::ValueToString(const QVariant &v)
// Do nothing, skip to the return string at the end
break;
case kDecibel:
{
// Convert to decibels and return dB formatted string
qreal decibels = QAudio::convertVolume(val, QAudio::LinearVolumeScale, QAudio::DecibelVolumeScale);
return QStringLiteral("%1 dB").arg(QString::number(decibels, 'f', decimal_places_));
}
val = QAudio::convertVolume(val, QAudio::LinearVolumeScale, QAudio::DecibelVolumeScale);
break;
case kPercentage:
// Multiply value by 100 for user-friendly percentage
val *= 100.0;
return QStringLiteral("%1%").arg(QString::number(val, 'f', decimal_places_));
break;
}
return QString::number(val, 'f', decimal_places_);
QString s = QString::number(val, 'f', decimal_places_);
if (autotrim_decimal_places_) {
while (s.endsWith('0')
&& s.at(s.size() - 2).isDigit()) {
s = s.left(s.size() - 1);
}
}
return s;
}
QVariant FloatSlider::StringToValue(const QString &s, bool *ok)
@@ -98,12 +124,8 @@ QVariant FloatSlider::StringToValue(const QString &s, bool *ok)
{
bool valid;
// Remove any instance of "dB" in the string
QString decibel_number = s;
decibel_number.replace("dB", "", Qt::CaseInsensitive);
// See if we can get a decimal number out of this
qreal decibels = decibel_number.toDouble(&valid);
qreal decibels = s.toDouble(&valid);
if (ok) *ok = valid;
@@ -118,11 +140,8 @@ QVariant FloatSlider::StringToValue(const QString &s, bool *ok)
{
bool valid;
QString percent_number = s;
percent_number.replace("%", "", Qt::CaseInsensitive);
// Try to get double value
double val = percent_number.toDouble(&valid);
double val = s.toDouble(&valid);
if (ok) *ok = valid;
+7
View File
@@ -49,6 +49,8 @@ public:
void SetDisplayType(const DisplayType& type);
void SetAutoTrimDecimalPlaces(bool e);
protected:
virtual QString ValueToString(const QVariant& v) override;
@@ -64,6 +66,11 @@ private slots:
private:
DisplayType display_type_;
int decimal_places_;
bool autotrim_decimal_places_;
};
OLIVE_NAMESPACE_EXIT
+21 -12
View File
@@ -28,7 +28,6 @@ OLIVE_NAMESPACE_ENTER
SliderBase::SliderBase(Mode mode, QWidget *parent) :
QStackedWidget(parent),
decimal_places_(1),
drag_multiplier_(1.0),
has_min_(false),
has_max_(false),
@@ -98,16 +97,21 @@ bool SliderBase::IsDragging() const
return dragged_;
}
void SliderBase::SetPrefix(const QString &s)
void SliderBase::SetFormat(const QString &s)
{
prefix_ = s;
UpdateLabel(value_);
custom_format_ = s;
ForceLabelUpdate();
}
void SliderBase::SetSuffix(const QString &s)
void SliderBase::ClearFormat()
{
suffix_ = s;
UpdateLabel(value_);
custom_format_.clear();
ForceLabelUpdate();
}
void SliderBase::ForceLabelUpdate()
{
UpdateLabel(Value());
}
const QVariant &SliderBase::Value()
@@ -176,16 +180,21 @@ const QVariant &SliderBase::ClampValue(const QVariant &v)
return v;
}
QString SliderBase::GetFormat() const
{
if (custom_format_.isEmpty()) {
return QStringLiteral("%1");
} else {
return custom_format_;
}
}
void SliderBase::UpdateLabel(const QVariant &v)
{
if (tristate_) {
label_->setText("---");
} else {
QString comp = prefix_;
comp.append(ValueToString(v));
comp.append(suffix_);
label_->setText(comp);
label_->setText(GetFormat().arg(ValueToString(v)));
}
}
+6 -6
View File
@@ -50,8 +50,8 @@ public:
bool IsDragging() const;
void SetPrefix(const QString& s);
void SetSuffix(const QString& s);
void SetFormat(const QString& s);
void ClearFormat();
signals:
void ValueChanged(QVariant v);
@@ -75,13 +75,15 @@ protected:
virtual void changeEvent(QEvent* e) override;
int decimal_places_;
void ForceLabelUpdate();
double drag_multiplier_;
private:
const QVariant& ClampValue(const QVariant& v);
QString GetFormat() const;
SliderLabel* label_;
FocusableLineEdit* editor_;
@@ -106,9 +108,7 @@ private:
bool tristate_;
QString prefix_;
QString suffix_;
QString custom_format_;
private slots:
void LabelPressed();