implemented percentage and decibel view modes for slider

A slider can now hold a float value and display it in the UI converted to
either a percentage or a decibel (still returning a linear float value
programmatically for the rest of the code).
This commit is contained in:
itsmattkc
2019-12-18 02:09:53 +11:00
parent be0d857e38
commit cde668b590
5 changed files with 131 additions and 10 deletions
+97 -3
View File
@@ -20,8 +20,11 @@
#include "floatslider.h"
#include <QAudio>
FloatSlider::FloatSlider(QWidget *parent) :
SliderBase(kFloat, parent)
SliderBase(kFloat, parent),
display_type_(kNormal)
{
connect(this, SIGNAL(ValueChanged(QVariant)), this, SLOT(ConvertValue(QVariant)));
}
@@ -53,17 +56,108 @@ void FloatSlider::SetDecimalPlaces(int i)
UpdateLabel(Value());
}
void FloatSlider::SetDisplayType(const FloatSlider::DisplayType &type)
{
display_type_ = type;
UpdateLabel(Value());
}
QString FloatSlider::ValueToString(const QVariant &v)
{
return QString::number(v.toDouble(), 'f', decimal_places_);
double val = v.toDouble();
switch (display_type_) {
case kNormal:
// 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_));
}
case kPercentage:
// Multiply value by 100 for user-friendly percentage
val *= 100.0;
return QStringLiteral("%1%").arg(QString::number(val, 'f', decimal_places_));
}
return QString::number(val, 'f', decimal_places_);
}
QVariant FloatSlider::StringToValue(const QString &s, bool *ok)
{
// Allow both floats and integers for either modes
switch (display_type_) {
case kNormal:
// Do nothing, skip to the return string at the end
break;
case kDecibel:
{
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);
if (ok) *ok = valid;
if (valid) {
// Convert from decibel scale to linear decimal
return QAudio::convertVolume(decibels, QAudio::DecibelVolumeScale, QAudio::LinearVolumeScale);
}
break;
}
case kPercentage:
{
bool valid;
QString percent_number = s;
percent_number.replace("%", "", Qt::CaseInsensitive);
// Try to get double value
double val = percent_number.toDouble(&valid);
if (ok) *ok = valid;
// If we could get it, convert back to a 0.0 - 1.0 value and return
if (valid) {
return val * 0.01;
}
break;
}
}
// Just try to convert the string to a double
return s.toDouble(ok);
}
double FloatSlider::AdjustDragDistanceInternal(const double &start, const double &drag)
{
switch (display_type_) {
case kNormal:
// No change here
break;
case kDecibel:
{
qreal current_db = QAudio::convertVolume(start, QAudio::LinearVolumeScale, QAudio::DecibelVolumeScale);
current_db += drag;
qreal adjusted_linear = QAudio::convertVolume(current_db, QAudio::DecibelVolumeScale, QAudio::LinearVolumeScale);
return adjusted_linear - start;
}
case kPercentage:
return drag * 0.01;
}
return SliderBase::AdjustDragDistanceInternal(start, drag);
}
void FloatSlider::ConvertValue(QVariant v)
{
emit ValueChanged(v.toDouble());