QGIS API Documentation 3.99.0-Master (d270888f95f)
Loading...
Searching...
No Matches
qgsfieldvalidator.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsfieldvalidator.cpp - description
3 -------------------
4 begin : March 2011
5 copyright : (C) 2011 by SunilRajKiran-kCube
7
8 adapted version of QValidator for QgsField
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 "qgsfieldvalidator.h"
21
22#include "qgsapplication.h"
23#include "qgsfields.h"
24#include "qgslogger.h"
26#include "qgssettings.h"
27
28#include <QDate>
29#include <QRegularExpression>
30#include <QRegularExpressionValidator>
31#include <QString>
32#include <QValidator>
33#include <QVariant>
34
35#include "moc_qgsfieldvalidator.cpp"
36
37using namespace Qt::StringLiterals;
38
39QgsFieldValidator::QgsFieldValidator( QObject *parent, const QgsField &field, const QString &defaultValue, const QString &dateFormat )
40 : QValidator( parent )
41 , mField( field )
42 , mDefaultValue( defaultValue )
43 , mDateFormat( dateFormat )
44{
45 switch ( mField.type() )
46 {
47 case QMetaType::Type::Int:
48 {
49 if ( mField.length() > 0 )
50 {
51 const QString re = u"-?\\d{0,%1}"_s.arg( mField.length() );
52 mValidator = new QRegularExpressionValidator( QRegularExpression( re ), parent );
53 }
54 else
55 {
56 mValidator = new QIntValidator( parent );
57 }
58 }
59 break;
60
61 case QMetaType::Type::Double:
62 {
63 if ( mField.length() > 0 && mField.precision() > 0 )
64 {
65 QString re;
66 // Also accept locale's decimalPoint if it's not a dot
67 if ( QLocale().decimalPoint() != '.' )
68 {
69 re = u"-?\\d{0,%1}([\\.%2]\\d{0,%3})?"_s.arg( mField.length() - mField.precision() ).arg( QLocale().decimalPoint() ).arg( mField.precision() );
70 }
71 else
72 {
73 re = u"-?\\d{0,%1}([\\.,]\\d{0,%2})?"_s.arg( mField.length() - mField.precision() ).arg( mField.precision() );
74 }
75 mValidator = new QRegularExpressionValidator( QRegularExpression( re ), parent );
76 }
77 else if ( mField.length() > 0 && mField.precision() == 0 )
78 {
79 const QString re = u"-?\\d{0,%1}"_s.arg( mField.length() );
80 mValidator = new QRegularExpressionValidator( QRegularExpression( re ), parent );
81 }
82 else if ( mField.precision() > 0 )
83 {
84 QString re;
85 // Also accept locale's decimalPoint if it's not a dot
86 if ( QLocale().decimalPoint() != '.' )
87 {
88 re = u"-?\\d*([\\.%1]\\d{0,%2})?"_s.arg( QLocale().decimalPoint(), mField.precision() );
89 }
90 else
91 {
92 re = u"-?\\d*([\\.]\\d{0,%1})?"_s.arg( mField.precision() );
93 }
94 mValidator = new QRegularExpressionValidator( QRegularExpression( re ), parent );
95 }
96 else
97 {
98 mValidator = new QDoubleValidator( parent );
99 }
100 }
101 break;
102
103 case QMetaType::Type::LongLong:
104 mValidator = new QgsLongLongValidator( parent );
105 break;
106
107 default:
108 mValidator = nullptr;
109 }
110
112}
113
115{
116 delete mValidator;
117}
118
119QValidator::State QgsFieldValidator::validate( QString &s, int &i ) const
120{
121 // empty values are considered NULL for numbers and dates and are acceptable
122 if ( s.isEmpty() && ( mField.type() == QMetaType::Type::Double || mField.type() == QMetaType::Type::Int || mField.type() == QMetaType::Type::LongLong || mField.type() == QMetaType::Type::QDate ) )
123 {
124 return Acceptable;
125 }
126
127 if ( s == mDefaultValue )
128 return Acceptable;
129
130 // delegate to the child validator if any
131 if ( mValidator )
132 {
133 const QValidator::State result = mValidator->validate( s, i );
134 return result;
135 }
136 else if ( mField.type() == QMetaType::Type::QString )
137 {
138 if ( s == mNullValue )
139 return Acceptable;
140
141 // allow entering the NULL representation, which might be longer than the actual field
142 if ( mField.length() > 0 && s.size() > mField.length() )
143 {
144 if ( !mNullValue.isEmpty() && !s.isEmpty() && s.size() < mNullValue.size() && s == mNullValue.left( s.size() ) )
145 return Intermediate;
146
147 if ( !mDefaultValue.isEmpty() && !s.isEmpty() && s.size() < mDefaultValue.size() && s == mDefaultValue.left( s.size() ) )
148 return Intermediate;
149
150 return Invalid;
151 }
152 }
153 else if ( mField.type() == QMetaType::Type::QDate )
154 {
155 return QDate::fromString( s, mDateFormat ).isValid() ? Acceptable : Intermediate;
156 }
157 else if ( mField.type() == QMetaType::Type::QVariantMap )
158 {
159 return Acceptable;
160 }
161 else if ( mField.type() == QMetaType::Type::User && mField.typeName().compare( "geometry"_L1, Qt::CaseInsensitive ) == 0 )
162 {
163 return Acceptable;
164 }
165 else
166 {
168 u"unsupported type %1 (%2) for validation"_s
169 .arg( mField.type() )
170 .arg( mField.typeName() )
171 );
172 return Invalid;
173 }
174
175 return Acceptable;
176}
177
178void QgsFieldValidator::fixup( QString &s ) const
179{
180 if ( mValidator )
181 {
182 mValidator->fixup( s );
183 }
184 else if ( mField.type() == QMetaType::Type::QString && mField.length() > 0 && s.size() > mField.length() && s != mDefaultValue )
185 {
186 // if the value is longer, this must be a partial NULL representation
187 s = mNullValue;
188 }
189 else if ( mField.type() == QMetaType::Type::QDate )
190 {
191 // invalid dates will also translate to NULL
192 s = QString();
193 }
194}
static QString nullRepresentation()
Returns the string used to represent the value NULL throughout QGIS.
State validate(QString &s, int &i) const override
QgsFieldValidator(QObject *parent, const QgsField &field, const QString &defaultValue, const QString &dateFormat="yyyy-MM-dd")
QString dateFormat() const
void fixup(QString &s) const override
Encapsulate a field in an attribute table or data source.
Definition qgsfield.h:56
A QValidator which allows validation of long long values.
#define QgsDebugError(str)
Definition qgslogger.h:59