slider_center_stuck_widget.h
Go to the documentation of this file.
1 /*********************************************************************
2  *
3  * Software License Agreement
4  *
5  * Copyright (c) 2020,
6  * TU Dortmund - Institute of Control Theory and Systems Engineering.
7  * All rights reserved.
8  *
9  * This program is free software: you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation, either version 3 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program. If not, see <https://www.gnu.org/licenses/>.
21  *
22  * Authors: Christoph Rösmann
23  *********************************************************************/
24 
25 #ifndef SRC_GUI_INCLUDE_CORBO_GUI_SLIDER_CENTER_STUCK_WIDGET_H_
26 #define SRC_GUI_INCLUDE_CORBO_GUI_SLIDER_CENTER_STUCK_WIDGET_H_
27 
28 #include <QSlider>
29 
30 #include <cmath>
31 
32 namespace corbo {
33 namespace gui {
34 
35 class SliderCenterStuck : public QSlider
36 {
37  Q_OBJECT
38  public:
39  explicit SliderCenterStuck(QWidget* parent = nullptr) : QSlider(parent) { connect(this, SIGNAL(sliderReleased()), this, SLOT(onRelease())); }
40 
41  explicit SliderCenterStuck(Qt::Orientation orientation, QWidget* parent = nullptr) : QSlider(orientation, parent)
42  {
43  connect(this, SIGNAL(sliderReleased()), this, SLOT(onRelease()));
44  }
45 
46  int getCenter() { return std::round(0.5 * (double)(maximum() + minimum())); }
47  void setToCenter() { this->setSliderPosition(getCenter()); }
48 
49  void setStuckToCenter(int abs_distance) { _stuck_center_dist = abs_distance; }
50 
51  protected slots:
52  void onRelease()
53  {
54  if (abs(value() - getCenter()) < _stuck_center_dist)
55  {
56  setToCenter();
57  }
58  }
59 
60  private:
62 };
63 
64 /*
65 class NonWrappingDial : public QDial
66 {
67  Q_OBJECT
68  public:
69  explicit NonWrappingDial(QWidget* parent = NULL) : QDial(parent)
70  {
71  connect(this, SIGNAL(actionTriggered(int)), this, SLOT(onAction(int)));
72  connect(this, SIGNAL(sliderReleased()), this, SLOT(onRelease()));
73  }
74 
75  void setStuckToCenter(int abs_distance) { _stuck_center_dist = abs_distance; }
76 
77  void setToCenter() { this->setSliderPosition(getCenter()); }
78 
79  int getCenter() { return std::round(0.5 * (double)(maximum() + minimum())); }
80 
81  protected slots:
82  void onAction(int val)
83  {
84  static const int minDistance = 1;
85  if (val == QAbstractSlider::SliderMove)
86  {
87  if (value() == maximum() && sliderPosition() < maximum() - minDistance)
88  {
89  this->setSliderPosition(maximum());
90  }
91  else if (value() == minimum() && sliderPosition() > minimum() + minDistance)
92  {
93  this->setSliderPosition(minimum());
94  }
95  }
96  }
97  void onRelease()
98  {
99  if (abs(value() - getCenter()) < _stuck_center_dist)
100  {
101  setToCenter();
102  }
103  }
104 
105  private:
106  int _stuck_center_dist = 0;
107 };
108 */
109 
110 } // namespace gui
111 } // namespace corbo
112 
113 #endif // SRC_GUI_INCLUDE_CORBO_GUI_SLIDER_CENTER_STUCK_WIDGET_H_
SliderCenterStuck(QWidget *parent=nullptr)
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const AbsReturnType abs() const
SliderCenterStuck(Qt::Orientation orientation, QWidget *parent=nullptr)
EIGEN_DEVICE_FUNC const RoundReturnType round() const


control_box_rst
Author(s): Christoph Rösmann
autogenerated on Mon Feb 28 2022 22:07:21