Engauge Digitizer  2
DlgValidatorNumber.cpp
1 /******************************************************************************************************
2  * (C) 2014 markummitchell@github.com. This file is part of Engauge Digitizer, which is released *
3  * under GNU General Public License version 2 (GPLv2) or (at your option) any later version. See file *
4  * LICENSE or go to gnu.org/licenses for details. Distribution requires prior written permission. *
5  ******************************************************************************************************/
6 
7 #include "DlgValidatorNumber.h"
8 #include "Logger.h"
9 #include <QDoubleValidator>
10 #include <QLocale>
11 
13  const QLocale &locale,
14  QObject *parent) :
15  DlgValidatorAbstract(parent),
16  m_coordScale (coordScale),
17  m_locale (locale)
18 {
19  LOG4CPP_INFO_S ((*mainCat)) << "DlgValidatorNumber::DlgValidatorNumber";
20 }
21 
22 QValidator::State DlgValidatorNumber::validate (QString &input,
23  int &pos) const
24 {
25  // First do standard check
26  QDoubleValidator validator;
27  validator.setLocale (m_locale);
28  QValidator::State state = validator.validate (input,
29  pos);
30  if (state == QValidator::Acceptable) {
31 
32  if (m_coordScale == COORD_SCALE_LOG) {
33  if (m_locale.toDouble (input) < 0.0) {
34 
35  // Cannot allow negative number
36  state = QValidator::Invalid;
37 
38  } if (m_locale.toDouble (input) == 0.0) {
39 
40  // Treat as a leading zero, which is legal
41  state = QValidator::Intermediate;
42  }
43  }
44  }
45 
46  return state;
47 }
Abstract validator for all numeric formats.
DlgValidatorNumber(CoordScale coordScale, const QLocale &locale, QObject *parent=0)
Single constructor.
virtual QValidator::State validate(QString &input, int &pos) const
Apply the standard validation with 0 as the exclusive minimum. Call setCoordScale just before calling...