QGIS API Documentation
4.3.0-Master (16eddb2670e)
Toggle main menu visibility
Loading...
Searching...
No Matches
src
gui
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
6
email : sunilraj.kiran@kcubeconsulting.com
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
"
25
#include "
qgslonglongvalidator.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
37
using namespace
Qt::StringLiterals;
38
39
QgsFieldValidator::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
111
mNullValue =
QgsApplication::nullRepresentation
();
112
}
113
114
QgsFieldValidator::~QgsFieldValidator
()
115
{}
116
117
QValidator::State
QgsFieldValidator::validate
( QString &s,
int
&i )
const
118
{
119
// empty values are considered NULL for numbers and dates and are acceptable
120
if
( s.isEmpty() && ( mField.type() == QMetaType::Type::Double || mField.type() == QMetaType::Type::Int || mField.type() == QMetaType::Type::LongLong || mField.type() == QMetaType::Type::QDate ) )
121
{
122
return
Acceptable;
123
}
124
125
if
( s == mDefaultValue )
126
return
Acceptable;
127
128
// delegate to the child validator if any
129
if
( mValidator )
130
{
131
const
QValidator::State result = mValidator->validate( s, i );
132
return
result;
133
}
134
else
if
( mField.type() == QMetaType::Type::QString )
135
{
136
if
( s == mNullValue )
137
return
Acceptable;
138
139
// allow entering the NULL representation, which might be longer than the actual field
140
if
( mField.length() > 0 && s.size() > mField.length() )
141
{
142
if
( !mNullValue.isEmpty() && !s.isEmpty() && s.size() < mNullValue.size() && s == mNullValue.left( s.size() ) )
143
return
Intermediate;
144
145
if
( !mDefaultValue.isEmpty() && !s.isEmpty() && s.size() < mDefaultValue.size() && s == mDefaultValue.left( s.size() ) )
146
return
Intermediate;
147
148
return
Invalid;
149
}
150
}
151
else
if
( mField.type() == QMetaType::Type::QDate )
152
{
153
return
QDate::fromString( s, mDateFormat ).isValid() ? Acceptable : Intermediate;
154
}
155
else
if
( mField.type() == QMetaType::Type::QVariantMap )
156
{
157
return
Acceptable;
158
}
159
else
if
( mField.type() == QMetaType::Type::User && mField.typeName().compare(
"geometry"
_L1, Qt::CaseInsensitive ) == 0 )
160
{
161
return
Acceptable;
162
}
163
else
164
{
165
QgsDebugError
( u
"unsupported type %1 (%2) for validation"
_s.arg( mField.type() ).arg( mField.typeName() ) );
166
return
Invalid;
167
}
168
169
return
Acceptable;
170
}
171
172
void
QgsFieldValidator::fixup
( QString &s )
const
173
{
174
if
( mValidator )
175
{
176
mValidator->fixup( s );
177
}
178
else
if
( mField.type() == QMetaType::Type::QString && mField.length() > 0 && s.size() > mField.length() && s != mDefaultValue )
179
{
180
// if the value is longer, this must be a partial NULL representation
181
s = mNullValue;
182
}
183
else
if
( mField.type() == QMetaType::Type::QDate )
184
{
185
// invalid dates will also translate to NULL
186
s = QString();
187
}
188
}
QgsApplication::nullRepresentation
static QString nullRepresentation()
Returns the string used to represent the value NULL throughout QGIS.
Definition
qgsapplication.cpp:2227
QgsFieldValidator::validate
State validate(QString &s, int &i) const override
Definition
qgsfieldvalidator.cpp:117
QgsFieldValidator::QgsFieldValidator
QgsFieldValidator(QObject *parent, const QgsField &field, const QString &defaultValue, const QString &dateFormat="yyyy-MM-dd")
Definition
qgsfieldvalidator.cpp:39
QgsFieldValidator::~QgsFieldValidator
~QgsFieldValidator() override
Definition
qgsfieldvalidator.cpp:114
QgsFieldValidator::dateFormat
QString dateFormat() const
Definition
qgsfieldvalidator.h:46
QgsFieldValidator::fixup
void fixup(QString &s) const override
Definition
qgsfieldvalidator.cpp:172
QgsField
Encapsulate a field in an attribute table or data source.
Definition
qgsfield.h:56
QgsLongLongValidator
A QValidator which allows validation of long long values.
Definition
qgslonglongvalidator.h:39
qgsapplication.h
qgsfields.h
qgsfieldvalidator.h
qgslogger.h
QgsDebugError
#define QgsDebugError(str)
Definition
qgslogger.h:71
qgslonglongvalidator.h
qgssettings.h
Generated on
for QGIS API Documentation by
1.17.0