1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
3 // File: ModuleBase_DoubleSpinBox.cxx
4 // Author: Sergey TELKOV
6 #include "ModuleBase_DoubleSpinBox.h"
7 #include "ModuleBase_Tools.h"
10 #include <QDoubleValidator>
16 const double PSEUDO_ZERO = 1.e-20;
19 \class ModuleBase_DoubleSpinBox
20 \brief Enhanced version of the Qt's double spin box.
22 The ModuleBase_DoubleSpinBox class represents the widget for entering the
23 floating point values. In addition to the functionality provided by
24 QDoubleSpinBox, this class supports "cleared" state - this is the
25 state corresponding to "None" (or empty) entered value.
27 To set "cleared" state use setCleared() method. To check if the spin
28 box stores "cleared" state, use isCleared() method.
31 if (myDblSpinBox->isCleared()) {
32 ... // process "None" state
35 double value = myDblSpinBox->value();
36 ... // process entered value
40 Another useful feature is possibility to use scientific notation (e.g. 1.234e+18)
41 for the widget text. To enable this, negative precision should be specified either
42 through a constructor or using setPrecision() method.
44 Note that "decimals" property of QDoubleSpinBox is almost completely substituted
45 by "myPrecision" field of ModuleBase_DoubleSpinBox class. "decimals" is still used
46 for proper size hint calculation and for rounding minimum and maximum bounds of
53 Constructs a spin box with 0.0 as minimum value and 99.99 as maximum value,
54 a step value of 1.0 and a precision of 2 decimal places.
55 The value is initially set to 0.00.
57 \param theParent parent object
58 \param thePrecision precision of values input
60 ModuleBase_DoubleSpinBox::ModuleBase_DoubleSpinBox(QWidget* theParent, int thePrecision)
61 : QDoubleSpinBox(theParent),
63 myIsEmitKeyPressEvent(false)
65 setLocale(ModuleBase_Tools::doubleLocale());
67 // MPV 15/09/2014: this must be set before setDecimals;
68 // otherwise in release mode setDecimals may crash
69 myPrecision = thePrecision;
71 // Use precision equal to default Qt decimals
72 // it's necessary to set decimals before the range setting,
73 // by default Qt rounds boundaries to 2 decimals at setRange
74 setDecimals(qAbs(myPrecision));
76 connect(lineEdit(), SIGNAL(textChanged( const QString& )), this,
77 SLOT(onTextChanged( const QString& )));
79 myEnabledBaseColor = palette().color(QPalette::Active, QPalette::Base);
85 ModuleBase_DoubleSpinBox::~ModuleBase_DoubleSpinBox()
90 \brief Check if spin box is in the "cleared" state.
91 \return \c true if spin box is cleared
94 bool ModuleBase_DoubleSpinBox::isCleared() const
100 \brief Change "cleared" status of the spin box.
101 \param on new "cleared" status
104 void ModuleBase_DoubleSpinBox::setCleared(const bool on)
110 setSpecialValueText(specialValueText());
114 \brief Set precision of the spin box
116 If precision value is less than 0, the 'g' format is used for value output,
117 otherwise 'f' format is used.
119 \param prec new precision value.
122 void ModuleBase_DoubleSpinBox::setPrecision(const int prec)
124 int newPrec = qAbs(prec);
125 int oldPrec = qAbs(myPrecision);
127 if (newPrec != oldPrec)
132 \brief Get precision value of the spin box
133 \return current precision value
136 int ModuleBase_DoubleSpinBox::getPrecision() const
142 \brief Interpret text entered by the user as a value.
143 \param text text entered by the user
147 double ModuleBase_DoubleSpinBox::valueFromText(const QString& text) const
150 return text.toDouble();
152 return QDoubleSpinBox::valueFromText(text);
156 \brief This function is used by the spin box whenever it needs to display
159 \param val spin box value
160 \return text representation of the value
163 QString ModuleBase_DoubleSpinBox::textFromValue(double val) const
165 QString s = locale().toString(val, myPrecision >= 0 ? 'f' : 'g', qAbs(myPrecision));
166 return removeTrailingZeroes(s);
170 \brief Return source string with removed leading and trailing zeros.
171 \param src source string
172 \return resulting string
174 QString ModuleBase_DoubleSpinBox::removeTrailingZeroes(const QString& src) const
176 QString delim(locale().decimalPoint());
178 int idx = src.lastIndexOf(delim);
182 QString iPart = src.left(idx);
183 QString fPart = src.mid(idx + 1);
185 int idx1 = fPart.lastIndexOf(QRegExp("e[+|-]?[0-9]+"));
187 ePart = fPart.mid(idx1);
188 fPart = fPart.left(idx1);
191 fPart.remove(QRegExp("0+$"));
194 if (!fPart.isEmpty())
195 res += delim + fPart;
201 void ModuleBase_DoubleSpinBox::keyPressEvent(QKeyEvent* theEvent)
203 switch (theEvent->key()) {
205 case Qt::Key_Return: {
206 // do not react to the Enter key, the property panel processes it
207 if (!myIsEmitKeyPressEvent)
214 QDoubleSpinBox::keyPressEvent(theEvent);
217 void ModuleBase_DoubleSpinBox::keyReleaseEvent(QKeyEvent* theEvent)
219 switch (theEvent->key()) {
221 case Qt::Key_Return: {
222 // the enter has already been processed when key is pressed,
223 // key release should not be processed in operation manager
224 if (myIsEmitKeyPressEvent) {
226 emit enterReleased();
234 QDoubleSpinBox::keyReleaseEvent(theEvent);
238 \brief Perform \a steps increment/decrement steps.
240 The \a steps value can be any integer number. If it is > 0,
241 the value incrementing is done, otherwise value is decremented
244 \param steps number of increment/decrement steps
246 void ModuleBase_DoubleSpinBox::stepBy(int steps)
250 QDoubleSpinBox::stepBy(steps);
251 double tmpval = value();
252 if (qAbs(tmpval) < PSEUDO_ZERO)
254 if (tmpval < minimum())
256 else if (tmpval > maximum())
262 \brief This function is used to determine whether input is valid.
263 \param str currently entered value
264 \param pos cursor position in the string
265 \return validating operation result
267 QValidator::State ModuleBase_DoubleSpinBox::validate(QString& str, int& pos) const
269 QString pref = this->prefix();
270 QString suff = this->suffix();
271 uint overhead = pref.length() + suff.length();
272 QValidator::State state = QValidator::Invalid;
274 QDoubleValidator v(NULL);
276 // If 'g' format is used (myPrecision < 0), then
277 // myPrecision - 1 digits are allowed after the decimal point.
278 // Otherwise, expect myPrecision digits after the decimal point.
279 int decs = myPrecision < 0 ? qAbs(myPrecision) - 1 : myPrecision;
281 v.setLocale(this->locale());
283 v.setBottom(minimum());
286 myPrecision >= 0 ? QDoubleValidator::StandardNotation : QDoubleValidator::ScientificNotation);
289 state = v.validate(str, pos);
291 if ((uint)(str.length()) >= overhead &&
292 str.startsWith(pref) &&
293 str.right(suff.length()) == suff) {
294 QString core = str.mid(pref.length(), str.length() - overhead);
295 int corePos = pos - pref.length();
296 state = v.validate(core, corePos);
297 pos = corePos + pref.length();
298 str.replace(pref.length(), str.length() - overhead, core);
300 state = v.validate(str, pos);
301 if (state == QValidator::Invalid) {
302 QString special = this->specialValueText().trimmed();
303 QString candidate = str.trimmed();
304 if (special.startsWith(candidate)) {
305 if (candidate.length() == special.length())
306 state = QValidator::Acceptable;
308 state = QValidator::Intermediate;
314 // Treat values outside (min; max) range as Invalid
315 // This check is enabled by assigning "strict_validity_check" dynamic property
316 // with value "true" to the spin box instance.
317 if (state == QValidator::Intermediate) {
319 double val = str.toDouble(&isOk);
321 QVariant propVal = property("strict_validity_check");
322 if (propVal.isValid() && propVal.canConvert(QVariant::Bool) && propVal.toBool()) {
323 if (val < minimum() || val > maximum())
324 state = QValidator::Invalid;
326 } else if (myPrecision < 0) {
327 // Consider too large negative exponent as Invalid
328 QChar e(locale().exponential());
329 int epos = str.indexOf(e, 0, Qt::CaseInsensitive);
331 epos++; // Skip exponential symbol itself
332 QString exponent = str.right(str.length() - epos);
333 int expValue = exponent.toInt(&isOk);
334 if (isOk && expValue < std::numeric_limits<double>::min_exponent10)
335 state = QValidator::Invalid;
344 \brief Called when user enters the text in the spin box.
346 void ModuleBase_DoubleSpinBox::onTextChanged(const QString& )
351 bool ModuleBase_DoubleSpinBox::enableKeyPressEvent(const bool& theEnable)
353 bool aPreviousValue = myIsEmitKeyPressEvent;
354 myIsEmitKeyPressEvent = theEnable;
356 return aPreviousValue;
359 void ModuleBase_DoubleSpinBox::setValueEnabled(const bool& theEnable)
361 setReadOnly(!theEnable);
363 QPalette aPal = palette();
364 aPal.setColor(QPalette::All, QPalette::Base,
365 theEnable? myEnabledBaseColor : aPal.color(QPalette::Disabled, QPalette::Base));