label_slider_widget.cpp
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 
26 #include <QHBoxLayout>
27 
28 namespace corbo {
29 namespace gui {
30 
31 LabelSliderWidget::LabelSliderWidget(const QString& label, double default_value, int resolution, int display_decimal_places, QWidget* parent)
32  : QWidget(parent)
33 {
34  setSizePolicy(QSizePolicy::Policy::Preferred, QSizePolicy::Policy::Preferred);
35 
36  QHBoxLayout* box = new QHBoxLayout(this);
37  box->setContentsMargins(5, 0, 0, 0);
38  box->setAlignment(Qt::AlignLeft);
39  _label = new QLabel(label);
40  box->addWidget(_label);
41  _display = new QLabel;
42  _display->setMinimumWidth(15);
43  _slider = new QSlider;
44  _slider->setOrientation(Qt::Horizontal);
45  _resolution = resolution;
46  _decimal_places = display_decimal_places;
47  setValue(default_value); // display is updated as well, must be initialized first
48  box->addWidget(_slider);
49  connect(_slider, SIGNAL(valueChanged(int)), this, SLOT(updateDisplay(int)));
50 
51  _label->setBuddy(_slider);
52  box->addWidget(_display, 0, Qt::AlignRight);
53 }
54 
55 QSize LabelSliderWidget::sizeHint() const { return QSize(250, 20); }
56 
57 void LabelSliderWidget::setValue(double value)
58 {
59  int act_value = int(value * _resolution);
60  _slider->setValue(act_value);
61  updateDisplay(act_value);
62 }
63 
64 void LabelSliderWidget::setMinMax(double min, double max)
65 {
66  _min_dbl_value = min;
67  _max_dbl_value = max;
68  _slider->setMinimum(int(min * _resolution));
69  _slider->setMaximum(int(max * _resolution));
70 }
71 
72 void LabelSliderWidget::updateDisplay(int value)
73 {
74  double double_val = (double)value / (double)_resolution;
75  // bound value, since there could be some rounding errors due to the resolution
76  if (double_val < _min_dbl_value)
77  double_val = _min_dbl_value;
78  else if (double_val > _max_dbl_value)
79  double_val = _max_dbl_value;
80 
81  _display->setText(QString::number(double_val, 'f', _decimal_places));
82  emit valueChanged(double_val);
83 }
84 
85 } // namespace gui
86 } // namespace corbo
Eigen::Horizontal
@ Horizontal
Definition: Constants.h:268
corbo
Definition: communication/include/corbo-communication/utilities.h:37
label_slider_widget.h
int
return int(ret)+1
min
#define min(a, b)
Definition: datatypes.h:19
max
#define max(a, b)
Definition: datatypes.h:20
corbo::gui::LabelSliderWidget::LabelSliderWidget
LabelSliderWidget(QWidget *parent=0)
Definition: label_slider_widget.h:107


control_box_rst
Author(s): Christoph Rösmann
autogenerated on Wed Mar 2 2022 00:05:51