Go to the documentation of this file.00001 #ifndef REALSLIDER_H
00002 #define REALSLIDER_H
00003
00004 #include <QSlider>
00005
00006
00007 class RealSlider: public QSlider
00008 {
00009 Q_OBJECT
00010 public:
00011 RealSlider(QWidget* parent = nullptr);
00012
00013 void setLimits(double min, double max, int steps);
00014 double getValue() const;
00015 void setRealValue(double val);
00016
00017 double getMaximum() const { return _max_value; }
00018
00019 double getMinimum() const { return _min_value; }
00020
00021 private slots:
00022 void onValueChanged(int value);
00023
00024 signals:
00025 void realValueChanged(double);
00026
00027 private:
00028 double _min_value;
00029 double _max_value;
00030 };
00031
00032
00033 inline RealSlider::RealSlider(QWidget *parent):
00034 QSlider(parent)
00035 {
00036 setLimits(0.0, 1.0, 1);
00037 connect(this, &QSlider::valueChanged,
00038 this, &RealSlider::onValueChanged);
00039 }
00040
00041 inline void RealSlider::setLimits(double min, double max,int steps)
00042 {
00043 _min_value = min;
00044 _max_value = max;
00045 QSlider::setRange(0,steps);
00046 }
00047
00048 inline void RealSlider::setRealValue(double val)
00049 {
00050 val = std::max( val, _min_value) ;
00051 val = std::min( val, _max_value) ;
00052 const double ratio = (val -_min_value) / (_max_value - _min_value );
00053 int pos = std::round( (double)(maximum() - minimum()) * ratio + minimum()) ;
00054 QSlider::setValue(pos);
00055 }
00056
00057 inline void RealSlider::onValueChanged(int value)
00058 {
00059 int min = minimum() ;
00060 int max = maximum() ;
00061 const double ratio = (double)value / (double)(max - min );
00062 double posX = (_max_value - _min_value) * ratio + _min_value;
00063 emit realValueChanged(posX);
00064 }
00065
00066 #endif // REALSLIDER_H