-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcustomslider.cpp
42 lines (36 loc) · 1.32 KB
/
customslider.cpp
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
#include "customslider.h"
#include <math.h>
CustomSlider::CustomSlider(QWidget *parent) :
QSlider(parent)
{
}
double CustomSlider::getCurrentCustomValue()
{
double percent = ((double)(value() - minimum())) / (maximum() - minimum());
double newValue = ((percent * (customMax - customMin)) + customMin);
double scale = 0.01; // i.e. round to nearest one-hundreth
double roundedValue = floor(newValue / scale + 0.5) * scale;
return roundedValue;
}
void CustomSlider::setCurrentCustomValue(double v) {
if (v >= customMin && v <= customMax) {
double percent = (double)(v - customMin) / (customMax - customMin);
double newValue = percent * (maximum() - minimum()) + minimum();
double scale = 0.01; // i.e. round to nearest one-hundreth
double roundedValue = floor(newValue / scale + 0.5) * scale;
setValue(roundedValue);
}
}
void CustomSlider::sliderChange(QAbstractSlider::SliderChange change)
{
QSlider::sliderChange(change);
if (change == SliderValueChange) {
emit customValueChanged(QString::number(getCurrentCustomValue()));
emit customValueChanged(getCurrentCustomValue());
}
}
void CustomSlider::setCustomValues(double min, double max, double initialValue) {
customMin = min;
customMax = max;
setCurrentCustomValue(initialValue);
}