Developer Documentation
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 * $Revision$ *
45 * $LastChangedBy$ *
46 * $Date$ *
47 * *
48 \*===========================================================================*/
49 
50 //== INCLUDES =================================================================
51 #include <cmath>
52 
53 #include <QHBoxLayout>
54 #include <QSlider>
55 #include <QPushButton>
56 #include <QDoubleSpinBox>
57 
58 #include "numWidget.hh"
59 
60 //== NAMESPACES ===============================================================
61 namespace VSI {
62 
63 //=============================================================================
64 //
65 // CLASS VSI::NumWidget - IMPLEMENTATION
66 //
67 //=============================================================================
68 
70 NumWidget::NumWidget(QMap< QString, QString > &_hints, QString _typeName, QWidget *_parent) :
71  TypeWidget (_hints, _typeName, _parent),
72  precision_ (1.0),
73  isFloat_ (false),
74  numDecimals_ (0),
75  sliderMul_ (1.0),
76  slider_ (0),
77  spin_ (0)
78 {
79  bool ok;
80 
81  QHBoxLayout *hL = new QHBoxLayout;
82 
83  if (_hints.contains ("precision"))
84  {
85  double v = _hints["precision"].toDouble (&ok);
86  if (ok)
87  precision_ = v;
88  }
89 
90 
91 
92  while (precision_ * sliderMul_ != floor(precision_ * sliderMul_) && numDecimals_ <= 10)
93  {
94  sliderMul_ *= 10.0;
95  numDecimals_++;
96  }
97 
98  if (_typeName == "Float")
99  isFloat_ = true;
100  else if (_typeName == "Integer")
101  isFloat_ = false;
102  else if (precision_ != int(precision_))
103  isFloat_ = true;
104 
105 
106  if (_hints.contains ("min") && _hints.contains ("max"))
107  {
108  double min = _hints["min"].toDouble (&ok);
109  if (!ok)
110  min = -65535;
111  if (!isFloat_)
112  min = int (min);
113 
114  double max = _hints["max"].toDouble (&ok);
115  if (!ok)
116  max = 65535;
117  if (!isFloat_)
118  max = int (max);
119 
120  if (min > max)
121  max = min + precision_;
122 
123  max *= sliderMul_;
124  min *= sliderMul_;
125 
126  slider_ = new QSlider;
127  slider_->setRange (min, max);
128  slider_->setSingleStep (precision_ * sliderMul_);
129  slider_->setOrientation (Qt::Horizontal);
130 
131  connect (slider_, SIGNAL (valueChanged(int)), this, SLOT (sliderValueChanged(int)));
132 
133  hL->addWidget(slider_);
134  }
135  else
136  {
137  hL->addStretch ();
138  }
139 
140  spin_ = new QDoubleSpinBox;
141 
142  if (_hints.contains ("min") && _hints.contains ("max"))
143  {
144  double min = _hints["min"].toDouble (&ok);
145  if (!ok)
146  min = -65535;
147  if (!isFloat_)
148  min = int (min);
149 
150  double max = _hints["max"].toDouble (&ok);
151  if (!ok)
152  max = 65535;
153  if (!isFloat_)
154  max = int (max);
155 
156  if (min > max)
157  max = min + precision_;
158 
159  spin_->setRange (min, max);
160  }
161  else
162  spin_->setRange (-1000000, 1000000);
163 
164  spin_->setSingleStep (precision_);
165  spin_->setDecimals (numDecimals_);
166  connect (spin_, SIGNAL (valueChanged(double)), this, SLOT (spinValueChanged(double)));
167 
168  ok = false;
169  if (_hints.contains ("default"))
170  default_ = _hints["default"].toFloat (&ok);
171 
172  if (!ok)
173  default_ = (spin_->minimum () + spin_->maximum ()) / 2;
174 
175  spin_->setValue (default_);
176 
177  hL->addWidget (spin_);
178 
179  setLayout (hL);
180 }
181 
184 {
185 }
186 
187 //------------------------------------------------------------------------------
188 
191 {
192  return QString::number (spin_->value ());
193 }
194 
195 //------------------------------------------------------------------------------
196 
198 void NumWidget::fromValue(QString _from)
199 {
200  spin_->setValue (_from.toDouble());
201 }
202 
203 //------------------------------------------------------------------------------
204 
205 // handle slider changes
206 void VSI::NumWidget::sliderValueChanged(int _value)
207 {
208  double v = _value / sliderMul_;
209 
210  if (v != spin_->value ())
211  spin_->setValue (v);
212 }
213 
214 //------------------------------------------------------------------------------
215 
216 // handle spinbox changes
217 void VSI::NumWidget::spinValueChanged(double _value)
218 {
219  double v = _value * sliderMul_;
220 
221  if (slider_ && v != slider_->value ())
222  slider_->setValue (v);
223 }
224 
225 //------------------------------------------------------------------------------
226 
229 {
230  spin_->setValue (default_);
231 }
232 
233 //------------------------------------------------------------------------------
234 }
235 
void fromValue(QString _from)
Read value from string.
Definition: numWidget.cc:198
NumWidget(QMap< QString, QString > &_hints, QString _typeName, QWidget *_parent=NULL)
Constructor.
Definition: numWidget.cc:70
~NumWidget()
Destructor.
Definition: numWidget.cc:183
QString toValue()
Convert current value to string.
Definition: numWidget.cc:190
void toDefault()
Reset to default.
Definition: numWidget.cc:228