Developer Documentation
Loading...
Searching...
No Matches
numWidget.cc
1/*===========================================================================*\
2* *
3* OpenFlipper *
4 * Copyright (c) 2001-2015, RWTH-Aachen University *
5 * Department of Computer Graphics and Multimedia *
6 * All rights reserved. *
7 * www.openflipper.org *
8 * *
9 *---------------------------------------------------------------------------*
10 * This file is part of OpenFlipper. *
11 *---------------------------------------------------------------------------*
12 * *
13 * Redistribution and use in source and binary forms, with or without *
14 * modification, are permitted provided that the following conditions *
15 * are met: *
16 * *
17 * 1. Redistributions of source code must retain the above copyright notice, *
18 * this list of conditions and the following disclaimer. *
19 * *
20 * 2. Redistributions in binary form must reproduce the above copyright *
21 * notice, this list of conditions and the following disclaimer in the *
22 * documentation and/or other materials provided with the distribution. *
23 * *
24 * 3. Neither the name of the copyright holder nor the names of its *
25 * contributors may be used to endorse or promote products derived from *
26 * this software without specific prior written permission. *
27 * *
28 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS *
29 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED *
30 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A *
31 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER *
32 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, *
33 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, *
34 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR *
35 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF *
36 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING *
37 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS *
38 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *
39* *
40\*===========================================================================*/
41
42
43
44//== INCLUDES =================================================================
45#include <cmath>
46
47#include <QHBoxLayout>
48#include <QSlider>
49#include <QPushButton>
50#include <QDoubleSpinBox>
51
52#include "numWidget.hh"
53
54//== NAMESPACES ===============================================================
55namespace VSI {
56
57//=============================================================================
58//
59// CLASS VSI::NumWidget - IMPLEMENTATION
60//
61//=============================================================================
62
64NumWidget::NumWidget(QMap< QString, QString > &_hints, const QString& _typeName, QWidget *_parent) :
65 TypeWidget (_hints, _typeName, _parent),
66 precision_ (1.0),
67 isFloat_ (false),
68 numDecimals_ (0),
69 sliderMul_ (1.0),
70 slider_ (0),
71 spin_ (0)
72{
73 bool ok;
74
75 QHBoxLayout *hL = new QHBoxLayout;
76
77 if (_hints.contains ("precision"))
78 {
79 double v = _hints["precision"].toDouble (&ok);
80 if (ok)
81 precision_ = v;
82 }
83
84
85
86 while (precision_ * sliderMul_ != floor(precision_ * sliderMul_) && numDecimals_ <= 10)
87 {
88 sliderMul_ *= 10.0;
89 numDecimals_++;
90 }
91
92 if (_typeName == "Float")
93 isFloat_ = true;
94 else if (_typeName == "Integer")
95 isFloat_ = false;
96 else if (precision_ != int(precision_))
97 isFloat_ = true;
98
99
100 if (_hints.contains ("min") && _hints.contains ("max"))
101 {
102 double min = _hints["min"].toDouble (&ok);
103 if (!ok)
104 min = -65535;
105 if (!isFloat_)
106 min = int (min);
107
108 double max = _hints["max"].toDouble (&ok);
109 if (!ok)
110 max = 65535;
111 if (!isFloat_)
112 max = int (max);
113
114 if (min > max)
115 max = min + precision_;
116
117 max *= sliderMul_;
118 min *= sliderMul_;
119
120 slider_ = new QSlider;
121 slider_->setRange (min, max);
122 slider_->setSingleStep (precision_ * sliderMul_);
123 slider_->setOrientation (Qt::Horizontal);
124
125 connect (slider_, SIGNAL (valueChanged(int)), this, SLOT (sliderValueChanged(int)));
126
127 hL->addWidget(slider_);
128 }
129 else
130 {
131 hL->addStretch ();
132 }
133
134 spin_ = new QDoubleSpinBox;
135
136 if (_hints.contains ("min") && _hints.contains ("max"))
137 {
138 double min = _hints["min"].toDouble (&ok);
139 if (!ok)
140 min = -65535;
141 if (!isFloat_)
142 min = int (min);
143
144 double max = _hints["max"].toDouble (&ok);
145 if (!ok)
146 max = 65535;
147 if (!isFloat_)
148 max = int (max);
149
150 if (min > max)
151 max = min + precision_;
152
153 spin_->setRange (min, max);
154 }
155 else
156 spin_->setRange (-1000000, 1000000);
157
158 spin_->setSingleStep (precision_);
159 spin_->setDecimals (numDecimals_);
160 connect (spin_, SIGNAL (valueChanged(double)), this, SLOT (spinValueChanged(double)));
161
162 ok = false;
163 if (_hints.contains ("default"))
164 default_ = _hints["default"].toFloat (&ok);
165
166 if (!ok)
167 default_ = (spin_->minimum () + spin_->maximum ()) / 2;
168
169 spin_->setValue (default_);
170
171 hL->addWidget (spin_);
172
173 setLayout (hL);
174}
175
177NumWidget::~ NumWidget()
178{
179}
180
181//------------------------------------------------------------------------------
182
185{
186 return QString::number (spin_->value ());
187}
188
189//------------------------------------------------------------------------------
190
192void NumWidget::fromValue(QString _from)
193{
194 spin_->setValue (_from.toDouble());
195}
196
197//------------------------------------------------------------------------------
198
199// handle slider changes
200void VSI::NumWidget::sliderValueChanged(int _value)
201{
202 double v = _value / sliderMul_;
203
204 if (v != spin_->value ())
205 spin_->setValue (v);
206}
207
208//------------------------------------------------------------------------------
209
210// handle spinbox changes
211void VSI::NumWidget::spinValueChanged(double _value)
212{
213 double v = _value * sliderMul_;
214
215 if (slider_ && v != slider_->value ())
216 slider_->setValue (v);
217}
218
219//------------------------------------------------------------------------------
220
223{
224 spin_->setValue (default_);
225}
226
227//------------------------------------------------------------------------------
228}
229
NumWidget(QMap< QString, QString > &_hints, const QString &_typeName, QWidget *_parent=NULL)
Constructor.
Definition numWidget.cc:64
QString toValue() override
Convert current value to string.
Definition numWidget.cc:184
void toDefault() override
Reset to default.
Definition numWidget.cc:222
void fromValue(QString _from) override
Read value from string.
Definition numWidget.cc:192