QGIS API Documentation  3.20.0-Odense (decaadbb31)
qgsdoublevalidator.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsdoublevalidator.cpp - description
3  -------------------
4  begin : June 2020
5  copyright : (C) 2020 by Sebastien Peillet
6  email : [email protected]
7 
8  adapted version of Qgslonglongvalidator + QgsFieldValidator
9  ***************************************************************************/
10 
11 /***************************************************************************
12  * *
13  * This program is free software; you can redistribute it and/or modify *
14  * it under the terms of the GNU General Public License as published by *
15  * the Free Software Foundation; either version 2 of the License, or *
16  * (at your option) any later version. *
17  * *
18  ***************************************************************************/
19 
20 #include <limits>
21 #include <QRegExpValidator>
22 #include <QRegularExpression>
23 #include <QLocale>
24 #include "qgis_gui.h"
25 
26 #include "qgsdoublevalidator.h"
27 
28 const QString PERMISSIVE_DOUBLE = R"(-?[\d]{0,1000}([\.%1][\d]{0,1000})?(e[+-]?[\d]{0,%2})?)";
29 
31  : QRegularExpressionValidator( parent )
32  , mMinimum( std::numeric_limits<qreal>::lowest() )
33  , mMaximum( std::numeric_limits<qreal>::max() )
34 {
35  // The regular expression accept double with point as decimal point but also the locale decimal point
36  QRegularExpression reg( PERMISSIVE_DOUBLE.arg( locale().decimalPoint() ).arg( 1000 ) );
37  setRegularExpression( reg );
38 }
39 
40 QgsDoubleValidator::QgsDoubleValidator( const QRegularExpression &expression, double bottom, double top, QObject *parent )
41  : QRegularExpressionValidator( parent )
42  , mMinimum( bottom )
43  , mMaximum( top )
44 {
45  setRegularExpression( expression );
46 }
47 
48 QgsDoubleValidator::QgsDoubleValidator( double bottom, double top, QObject *parent )
49  : QRegularExpressionValidator( parent )
50  , mMinimum( bottom )
51  , mMaximum( top )
52 {
53  // The regular expression accept double with point as decimal point but also the locale decimal point
54  QRegularExpression reg( PERMISSIVE_DOUBLE.arg( locale().decimalPoint() ).arg( 1000 ) );
55  setRegularExpression( reg );
56 }
57 
58 QgsDoubleValidator::QgsDoubleValidator( double bottom, double top, int decimal, QObject *parent )
59  : QRegularExpressionValidator( parent )
60  , mMinimum( bottom )
61  , mMaximum( top )
62 {
63  // The regular expression accept double with point as decimal point but also the locale decimal point
64  QRegularExpression reg( PERMISSIVE_DOUBLE.arg( locale().decimalPoint() ).arg( QString::number( decimal ) ) );
65  setRegularExpression( reg );
66 }
67 
68 QgsDoubleValidator::QgsDoubleValidator( int decimal, QObject *parent )
69  : QRegularExpressionValidator( parent )
70  , mMinimum( std::numeric_limits<qreal>::lowest() )
71  , mMaximum( std::numeric_limits<qreal>::max() )
72 {
73  // The regular expression accept double with point as decimal point but also the locale decimal point
74  QRegularExpression reg( PERMISSIVE_DOUBLE.arg( locale().decimalPoint() ).arg( QString::number( decimal ) ) );
75  setRegularExpression( reg );
76 }
77 
78 QValidator::State QgsDoubleValidator::validate( QString &input, int & ) const
79 {
80  if ( input.isEmpty() )
81  return Intermediate;
82 
83 
84  bool ok = false;
85  const double entered = QgsDoubleValidator::toDouble( input, &ok );
86  if ( ! ok )
87  {
88  if ( regularExpression().match( input ).captured( 0 ) == input )
89  return Intermediate;
90  else
91  return Invalid;
92  }
93 
94  if ( entered >= mMinimum && entered <= mMaximum && regularExpression().match( input ).captured( 0 ) == input )
95  return Acceptable;
96  else
97  return Intermediate;
98 }
99 
100 QValidator::State QgsDoubleValidator::validate( QString &input ) const
101 {
102  if ( input.isEmpty() )
103  return Intermediate;
104 
105 
106  bool ok = false;
107  const double entered = QgsDoubleValidator::toDouble( input, &ok );
108  if ( ! ok )
109  {
110  if ( regularExpression().match( input ).captured( 0 ) == input )
111  return Intermediate;
112  else
113  return Invalid;
114  }
115 
116  if ( entered >= mMinimum && entered <= mMaximum && regularExpression().match( input ).captured( 0 ) == input )
117  return Acceptable;
118  else
119  return Intermediate;
120 }
121 
122 double QgsDoubleValidator::toDouble( const QString &input )
123 {
124  bool ok = false;
125  double value = QLocale().toDouble( input, &ok );
126  if ( ! ok )
127  {
128  value = QLocale( QLocale::C ).toDouble( input, &ok );
129  }
130  return value;
131 }
132 
133 double QgsDoubleValidator::toDouble( const QString &input, bool *ok )
134 {
135  double value = QLocale().toDouble( input, ok );
136 
137  if ( ! *ok )
138  {
139  value = QLocale( QLocale::C ).toDouble( input, ok );
140  }
141  return value ;
142 }
static double toDouble(const QString &input, bool *ok)
Converts input string to double value.
QgsDoubleValidator(QObject *parent)
Constructor for QgsDoubleValidator.
QValidator::State validate(QString &input, int &) const override
const QString PERMISSIVE_DOUBLE