QGIS API Documentation  3.16.0-Hannover (43b64b13f3)
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 <QLocale>
23 #include "qgis_gui.h"
24 
25 #include "qgsdoublevalidator.h"
26 
27 const QString PERMISSIVE_DOUBLE = R"(-?[\d]{0,1000}([\.%1][\d]{0,1000})?(e[+-]?[\d]{0,%2})?)";
28 
30  : QRegularExpressionValidator( parent )
31  , mMinimum( std::numeric_limits<qreal>::min() )
32  , mMaximum( std::numeric_limits<qreal>::max() )
33 {
34  // The regular expression accept double with point as decimal point but also the locale decimal point
35  QRegularExpression reg( PERMISSIVE_DOUBLE.arg( locale().decimalPoint() ).arg( 1000 ) );
36  setRegularExpression( reg );
37 }
38 
39 QgsDoubleValidator::QgsDoubleValidator( const QRegularExpression &expression, double bottom, double top, QObject *parent )
40  : QRegularExpressionValidator( parent )
41  , mMinimum( bottom )
42  , mMaximum( top )
43 {
44  setRegularExpression( expression );
45 }
46 
47 QgsDoubleValidator::QgsDoubleValidator( double bottom, double top, QObject *parent )
48  : QRegularExpressionValidator( parent )
49  , mMinimum( bottom )
50  , mMaximum( top )
51 {
52  // The regular expression accept double with point as decimal point but also the locale decimal point
53  QRegularExpression reg( PERMISSIVE_DOUBLE.arg( locale().decimalPoint() ).arg( 1000 ) );
54  setRegularExpression( reg );
55 }
56 
57 QgsDoubleValidator::QgsDoubleValidator( double bottom, double top, int decimal, QObject *parent )
58  : QRegularExpressionValidator( parent )
59  , mMinimum( bottom )
60  , mMaximum( top )
61 {
62  // The regular expression accept double with point as decimal point but also the locale decimal point
63  QRegularExpression reg( PERMISSIVE_DOUBLE.arg( locale().decimalPoint() ).arg( QString::number( decimal ) ) );
64  setRegularExpression( reg );
65 }
66 
67 QValidator::State QgsDoubleValidator::validate( QString &input, int & ) const
68 {
69  if ( input.isEmpty() )
70  return Intermediate;
71 
72 
73  bool ok = false;
74  const double entered = QgsDoubleValidator::toDouble( input, &ok );
75  if ( ! ok )
76  {
77  if ( regularExpression().match( input ).captured( 0 ) == input )
78  return Intermediate;
79  else
80  return Invalid;
81  }
82 
83  if ( entered >= mMinimum && entered <= mMaximum && regularExpression().match( input ).captured( 0 ) == input )
84  return Acceptable;
85  else
86  return Intermediate;
87 }
88 
89 QValidator::State QgsDoubleValidator::validate( QString &input ) const
90 {
91  if ( input.isEmpty() )
92  return Intermediate;
93 
94 
95  bool ok = false;
96  const double entered = QgsDoubleValidator::toDouble( input, &ok );
97  if ( ! ok )
98  {
99  if ( regularExpression().match( input ).captured( 0 ) == input )
100  return Intermediate;
101  else
102  return Invalid;
103  }
104 
105  if ( entered >= mMinimum && entered <= mMaximum && regularExpression().match( input ).captured( 0 ) == input )
106  return Acceptable;
107  else
108  return Intermediate;
109 }
110 
111 double QgsDoubleValidator::toDouble( const QString &input )
112 {
113  bool ok = false;
114  double value = QLocale().toDouble( input, &ok );
115  if ( ! ok )
116  {
117  value = QLocale( QLocale::C ).toDouble( input, &ok );
118  }
119  return value;
120 }
121 
122 double QgsDoubleValidator::toDouble( const QString &input, bool *ok )
123 {
124  double value = QLocale().toDouble( input, ok );
125 
126  if ( ! *ok )
127  {
128  value = QLocale( QLocale::C ).toDouble( input, ok );
129  }
130  return value ;
131 }
QgsDoubleValidator::QgsDoubleValidator
QgsDoubleValidator(QObject *parent)
Constructor for QgsDoubleValidator.
Definition: qgsdoublevalidator.cpp:29
QgsDoubleValidator::validate
QValidator::State validate(QString &input, int &) const override
Definition: qgsdoublevalidator.cpp:67
PERMISSIVE_DOUBLE
const QString PERMISSIVE_DOUBLE
Definition: qgsdoublevalidator.cpp:27
QgsDoubleValidator::toDouble
static double toDouble(const QString &input, bool *ok)
Converts input string to double value.
Definition: qgsdoublevalidator.cpp:122
qgsdoublevalidator.h