23 #include <QRegExpValidator> 34 : QValidator( parent )
36 , mDefaultValue( defaultValue )
37 , mDateFormat( dateFormat )
39 switch ( mField.
type() )
45 QString re = QStringLiteral(
"-?\\d{0,%1}" ).arg( mField.
length() );
46 mValidator =
new QRegExpValidator( QRegExp( re ), parent );
50 mValidator =
new QIntValidator( parent );
55 case QVariant::Double:
59 QString re = QStringLiteral(
"-?\\d{0,%1}(\\.\\d{0,%2})?" ).arg( mField.
length() - mField.
precision() ).arg( mField.
precision() );
60 mValidator =
new QRegExpValidator( QRegExp( re ), parent );
64 QString re = QStringLiteral(
"-?\\d{0,%1}" ).arg( mField.
length() );
65 mValidator =
new QRegExpValidator( QRegExp( re ), parent );
69 QString re = QStringLiteral(
"-?\\d*(\\.\\d{0,%1})?" ).arg( mField.
precision() );
70 mValidator =
new QRegExpValidator( QRegExp( re ), parent );
74 mValidator =
new QDoubleValidator( parent );
79 case QVariant::LongLong :
99 ( mField.
type() == QVariant::Double
100 || mField.
type() == QVariant::Int
101 || mField.
type() == QVariant::LongLong
102 || mField.
type() == QVariant::Date
109 if ( s == mDefaultValue )
119 if ( mField.
type() == QVariant::Double )
120 s = s.replace(
',',
'.' );
121 QValidator::State result = mValidator->validate( s, i );
124 else if ( mField.
type() == QVariant::String )
128 if ( !mNullValue.isEmpty() && !s.isEmpty() && s.size() < mNullValue.size() && s == mNullValue.left( s.size() ) )
131 if ( !mDefaultValue.isEmpty() && !s.isEmpty() && s.size() < mDefaultValue.size() && s == mDefaultValue.left( s.size() ) )
134 if ( s == mNullValue )
137 if ( mField.
length() > 0 && s.size() > mField.
length() )
140 else if ( mField.
type() == QVariant::Date )
142 return QDate::fromString( s, mDateFormat ).isValid() ? Acceptable : Intermediate;
146 QgsDebugMsg( QString(
"unsupported type %1 for validation" ).arg( mField.
type() ) );
157 mValidator->fixup( s );
159 else if ( mField.
type() == QVariant::String && mField.
length() > 0 && s.size() > mField.
length() && s != mDefaultValue )
164 else if ( mField.
type() == QVariant::Date )
167 s = QLatin1String(
"" );
State validate(QString &s, int &i) const override
QgsFieldValidator(QObject *parent, const QgsField &field, const QString &defaultValue, const QString &dateFormat="yyyy-MM-dd")
~QgsFieldValidator() override
static QString nullRepresentation()
This string is used to represent the value NULL throughout QGIS.
Encapsulate a field in an attribute table or data source.
void fixup(QString &s) const override