27#include <QRegularExpression>
28#include <QRegularExpressionValidator>
30#include "moc_qgsdoublevalidator.cpp"
32const QString
PERMISSIVE_DOUBLE = R
"(^\s*[+\-%3]?[\d]{0,1000}([\.%1][\d]{0,1000})?([eE%4][+\-%3]?[\d]{0,%2})?\s*$)";
35 : QRegularExpressionValidator( parent )
36 , mMinimum( std::numeric_limits<qreal>::lowest() )
37 , mMaximum( std::numeric_limits<qreal>::max() )
39 setRegularExpression( createExpression( 1000 ) );
43 : QRegularExpressionValidator( parent )
47 setRegularExpression( expression );
51 : QRegularExpressionValidator( parent )
55 setRegularExpression( createExpression( 1000 ) );
59 : QRegularExpressionValidator( parent )
63 setRegularExpression( createExpression( decimal ) );
67 : QRegularExpressionValidator( parent )
68 , mMinimum( std::numeric_limits<qreal>::lowest() )
69 , mMaximum( std::numeric_limits<qreal>::max() )
72 setRegularExpression( createExpression( decimal ) );
77 setRegularExpression( createExpression( maxDecimals ) );
82 if ( input.isEmpty() )
90 if ( regularExpression().match( input ).captured( 0 ) == input )
96 if ( entered >= mMinimum && entered <= mMaximum && regularExpression().match( input ).captured( 0 ) == input )
104 if ( input.isEmpty() )
112 if ( regularExpression().match( input ).captured( 0 ) == input )
118 if ( entered >= mMinimum && entered <= mMaximum && regularExpression().match( input ).captured( 0 ) == input )
130QRegularExpression QgsDoubleValidator::createExpression(
int decimals )
132 const QString localeDecimalPoint = QLocale().decimalPoint();
133 const QString localeNegativeSign = QLocale().negativeSign();
134 const QString localeExponential = QLocale().exponential();
136 localeDecimalPoint ==
'.' ? QString() : QRegularExpression::escape( localeDecimalPoint ),
137 QString::number( decimals ),
138 localeNegativeSign ==
'-' ? QString() : QRegularExpression::escape( localeNegativeSign ),
139 localeExponential ==
'E' ? QString() : localeExponential
145 double value = QLocale().toDouble( input, ok );
149 value = QLocale( QLocale::C ).toDouble( input, ok );
152 if ( !*ok && !( QLocale().numberOptions() & QLocale::NumberOption::OmitGroupSeparator ) )
154 value = QLocale().toDouble( QString( input ).replace( QLocale().groupSeparator(), QString() ), ok );
static double toDouble(const QString &input, bool *ok)
Converts input string to double value.
QgsDoubleValidator(QObject *parent)
Constructor for QgsDoubleValidator.
double top() const
Returns top range limit.
QValidator::State validate(QString &input, int &) const override
void setMaxDecimals(int maxDecimals)
Sets the number of decimals accepted by the validator to maxDecimals.
double bottom() const
Returns top range limit.
const QString PERMISSIVE_DOUBLE