From db117e0780b2eb21df2bc6eac339d3938de04399 Mon Sep 17 00:00:00 2001 From: itsmattkc Date: Wed, 22 Apr 2020 17:49:32 +1000 Subject: [PATCH] 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. --- .../preferences/tabs/preferencesdisktab.cpp | 6 +- app/node/math/math/math.cpp | 4 ++ .../nodeparamviewwidgetbridge.cpp | 12 ++++ app/widget/slider/floatslider.cpp | 55 +++++++++++++------ app/widget/slider/floatslider.h | 7 +++ app/widget/slider/sliderbase.cpp | 33 +++++++---- app/widget/slider/sliderbase.h | 12 ++-- 7 files changed, 90 insertions(+), 39 deletions(-) diff --git a/app/dialog/preferences/tabs/preferencesdisktab.cpp b/app/dialog/preferences/tabs/preferencesdisktab.cpp index df9708b7f..ea93e102d 100644 --- a/app/dialog/preferences/tabs/preferencesdisktab.cpp +++ b/app/dialog/preferences/tabs/preferencesdisktab.cpp @@ -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().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().toDouble()); cache_behavior_layout->addWidget(cache_behind_slider_, row, 3); diff --git a/app/node/math/math/math.cpp b/app/node/math/math/math.cpp index 4a2d6f14a..103166581 100644 --- a/app/node/math/math/math.cpp +++ b/app/node/math/math/math.cpp @@ -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_); } diff --git a/app/widget/nodeparamview/nodeparamviewwidgetbridge.cpp b/app/widget/nodeparamview/nodeparamviewwidgetbridge.cpp index 37da1ba05..8bb24106a 100644 --- a/app/widget/nodeparamview/nodeparamviewwidgetbridge.cpp +++ b/app/widget/nodeparamview/nodeparamviewwidgetbridge.cpp @@ -662,6 +662,18 @@ void NodeParamViewWidgetBridge::PropertyChanged(const QString &key, const QVaria foreach (QWidget* w, widgets_) { static_cast(w)->SetDisplayType(display_type); } + } else if (key == QStringLiteral("decimalplaces")) { + int dec_places = value.toInt(); + + foreach (QWidget* w, widgets_) { + static_cast(w)->SetDecimalPlaces(dec_places); + } + } else if (key == QStringLiteral("autotrim")) { + bool autotrim = value.toBool(); + + foreach (QWidget* w, widgets_) { + static_cast(w)->SetAutoTrimDecimalPlaces(autotrim); + } } } } diff --git a/app/widget/slider/floatslider.cpp b/app/widget/slider/floatslider.cpp index 71e119258..2793fcf17 100644 --- a/app/widget/slider/floatslider.cpp +++ b/app/widget/slider/floatslider.cpp @@ -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; diff --git a/app/widget/slider/floatslider.h b/app/widget/slider/floatslider.h index 15dfb84ee..27b6aa007 100644 --- a/app/widget/slider/floatslider.h +++ b/app/widget/slider/floatslider.h @@ -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 diff --git a/app/widget/slider/sliderbase.cpp b/app/widget/slider/sliderbase.cpp index 2a6420683..4e41119bf 100644 --- a/app/widget/slider/sliderbase.cpp +++ b/app/widget/slider/sliderbase.cpp @@ -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))); } } diff --git a/app/widget/slider/sliderbase.h b/app/widget/slider/sliderbase.h index 06a50bbfe..2a4b8899f 100644 --- a/app/widget/slider/sliderbase.h +++ b/app/widget/slider/sliderbase.h @@ -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();