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