23 #include <QRegExpValidator> 33 : QValidator( parent )
35 , mDefaultValue( defaultValue )
36 , mDateFormat( dateFormat )
38 switch ( mField.
type() )
44 QString re = QString(
"-?\\d{0,%1}" ).arg( mField.
length() );
45 mValidator =
new QRegExpValidator( QRegExp( re ), parent );
49 mValidator =
new QIntValidator( parent );
54 case QVariant::Double:
58 QString re = QString(
"-?\\d{0,%1}(\\.\\d{0,%2})?" ).arg( mField.
length() - mField.
precision() ).arg( mField.
precision() );
59 mValidator =
new QRegExpValidator( QRegExp( re ), parent );
63 QString re = QString(
"-?\\d{0,%1}" ).arg( mField.
length() );
64 mValidator =
new QRegExpValidator( QRegExp( re ), parent );
68 QString re = QString(
"-?\\d*(\\.\\d{0,%1})?" ).arg( mField.
precision() );
69 mValidator =
new QRegExpValidator( QRegExp( re ), parent );
73 mValidator =
new QDoubleValidator( parent );
78 case QVariant::LongLong :
87 mNullValue = settings.value(
"qgis/nullValue",
"NULL" ).toString();
99 ( mField.
type() == QVariant::Double
100 || mField.
type() == QVariant::Int
101 || mField.
type() == QVariant::LongLong
102 || mField.
type() == QVariant::Date
109 if ( s == mDefaultValue )
115 QValidator::State result = mValidator->validate( s, i );
118 else if ( mField.
type() == QVariant::String )
122 if ( mNullValue.size() > 0 && s.size() > 0 && s.size() < mNullValue.size() && s == mNullValue.left( s.size() ) )
125 if ( mDefaultValue.size() > 0 && s.size() > 0 && s.size() < mDefaultValue.size() && s == mDefaultValue.left( s.size() ) )
128 if ( s == mNullValue )
131 if ( mField.
length() > 0 && s.size() > mField.
length() )
134 else if ( mField.
type() == QVariant::Date )
136 return QDate::fromString( s, mDateFormat ).isValid() ? Acceptable : Intermediate;
140 QgsDebugMsg( QString(
"unsupported type %1 for validation" ).arg( mField.
type() ) );
151 mValidator->fixup( s );
153 else if ( mField.
type() == QVariant::String && mField.
length() > 0 && s.size() > mField.
length() && s != mDefaultValue )
158 else if ( mField.
type() == QVariant::Date )
virtual State validate(QString &, int &) const override
int precision() const
Gets the precision of the field.
virtual void fixup(QString &) const override
Encapsulate a field in an attribute table or data source.
int length() const
Gets the length of the field.
QgsFieldValidator(QObject *parent, const QgsField &field, QString defaultValue, QString dateFormat="yyyy-MM-dd")
QVariant::Type type() const
Gets variant type of the field as it will be retrieved from data source.