1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
|
/***
Olive - Non-Linear Video Editor
Copyright (C) 2019 Olive Team
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
***/
#include "floatslider.h"
#include <QAudio>
OLIVE_NAMESPACE_ENTER
FloatSlider::FloatSlider(QWidget *parent) :
SliderBase(kFloat, parent),
display_type_(kNormal),
decimal_places_(1),
autotrim_decimal_places_(false)
{
connect(this, SIGNAL(ValueChanged(QVariant)), this, SLOT(ConvertValue(QVariant)));
}
double FloatSlider::GetValue()
{
return Value().toDouble();
}
void FloatSlider::SetValue(const double &d)
{
SliderBase::SetValue(d);
}
void FloatSlider::SetMinimum(const double &d)
{
SetMinimumInternal(d);
}
void FloatSlider::SetMaximum(const double &d)
{
SetMaximumInternal(d);
}
void FloatSlider::SetDecimalPlaces(int i)
{
decimal_places_ = i;
ForceLabelUpdate();
}
void FloatSlider::SetDisplayType(const FloatSlider::DisplayType &type)
{
display_type_ = type;
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)
{
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
val = QAudio::convertVolume(val, QAudio::LinearVolumeScale, QAudio::DecibelVolumeScale);
break;
case kPercentage:
// Multiply value by 100 for user-friendly percentage
val *= 100.0;
break;
}
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)
{
switch (display_type_) {
case kNormal:
// Do nothing, skip to the return string at the end
break;
case kDecibel:
{
bool valid;
// See if we can get a decimal number out of this
qreal decibels = s.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;
// Try to get double value
double val = s.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;
}
case kPercentage:
return SliderBase::AdjustDragDistanceInternal(start, drag * 0.01);
}
return SliderBase::AdjustDragDistanceInternal(start, drag);
}
void FloatSlider::ConvertValue(QVariant v)
{
emit ValueChanged(v.toDouble());
}
OLIVE_NAMESPACE_EXIT
|