realslider.h
Go to the documentation of this file.
1 /*
2  * This Source Code Form is subject to the terms of the Mozilla Public
3  * License, v. 2.0. If a copy of the MPL was not distributed with this
4  * file, You can obtain one at https://mozilla.org/MPL/2.0/.
5  */
6 
7 #ifndef REALSLIDER_H
8 #define REALSLIDER_H
9 
10 #include <QSlider>
11 
12 class RealSlider : public QSlider
13 {
14  Q_OBJECT
15 public:
16  RealSlider(QWidget* parent = nullptr);
17 
18  void setLimits(double min, double max, int steps);
19 
20  double getValue() const;
21 
22  void setRealValue(double val);
23 
24  double getMaximum() const
25  {
26  return _max_value;
27  }
28 
29  double getMinimum() const
30  {
31  return _min_value;
32  }
33 
34  void setRealStepValue(double step);
35 
36 private slots:
37  void onValueChanged(int value);
38 
39 signals:
40  void realValueChanged(double);
41 
42 private:
43  double _min_value;
44  double _max_value;
45 };
46 //-------------------------------------------------------------
47 
48 inline RealSlider::RealSlider(QWidget* parent) : QSlider(parent)
49 {
50  setLimits(0.0, 1.0, 1);
51  connect(this, &QSlider::valueChanged, this, &RealSlider::onValueChanged);
52 }
53 
54 inline void RealSlider::setLimits(double min, double max, int steps)
55 {
56  _min_value = min;
57  _max_value = max;
58  QSlider::setRange(0, steps);
59 }
60 
61 inline void RealSlider::setRealValue(double val)
62 {
63  val = std::max(val, _min_value);
64  val = std::min(val, _max_value);
65  const double ratio = (val - _min_value) / (_max_value - _min_value);
66  long pos = std::round((double)(maximum() - minimum()) * ratio + minimum());
67  QSlider::setValue(pos);
68 }
69 
70 inline void RealSlider::setRealStepValue(double step)
71 {
72  const double ratio = (_max_value - _min_value) / (double)(maximum() - minimum());
73  int new_step = std::max(1, static_cast<int>(std::round(step / ratio)));
74  QSlider::setSingleStep(new_step);
75 }
76 
77 inline void RealSlider::onValueChanged(int value)
78 {
79  int min = minimum();
80  int max = maximum();
81  const double ratio = (double)value / (double)(max - min);
82  double posX = (_max_value - _min_value) * ratio + _min_value;
83  emit realValueChanged(posX);
84 }
85 
86 #endif // REALSLIDER_H
RealSlider::setRealValue
void setRealValue(double val)
Definition: realslider.h:61
RealSlider::setRealStepValue
void setRealStepValue(double step)
Definition: realslider.h:70
RealSlider::getMinimum
double getMinimum() const
Definition: realslider.h:29
RealSlider
Definition: realslider.h:12
RealSlider::getMaximum
double getMaximum() const
Definition: realslider.h:24
RealSlider::RealSlider
RealSlider(QWidget *parent=nullptr)
Definition: realslider.h:48
RealSlider::onValueChanged
void onValueChanged(int value)
Definition: realslider.h:77
RealSlider::_max_value
double _max_value
Definition: realslider.h:44
RealSlider::getValue
double getValue() const
RealSlider::_min_value
double _min_value
Definition: realslider.h:43
RealSlider::setLimits
void setLimits(double min, double max, int steps)
Definition: realslider.h:54
RealSlider::realValueChanged
void realValueChanged(double)


plotjuggler
Author(s): Davide Faconti
autogenerated on Mon Nov 11 2024 03:23:46