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:
61 if ( QLocale().decimalPoint() !=
'.' )
63 re = QStringLiteral(
"-?\\d{0,%1}([\\.%2]\\d{0,%3})?" ).arg( mField.
length() - mField.
precision() ).arg( QLocale().decimalPoint() ).arg( mField.
precision() );
67 re = QStringLiteral(
"-?\\d{0,%1}([\\.,]\\d{0,%2})?" ).arg( mField.
length() - mField.
precision() ).arg( mField.
precision() );
69 mValidator =
new QRegExpValidator( QRegExp( re ), parent );
73 QString re = QStringLiteral(
"-?\\d{0,%1}" ).arg( mField.
length() );
74 mValidator =
new QRegExpValidator( QRegExp( re ), parent );
80 if ( QLocale().decimalPoint() !=
'.' )
82 re = QStringLiteral(
"-?\\d*([\\.%1]\\d{0,%2})?" ).arg( QLocale().decimalPoint(), mField.
precision() );
86 re = QStringLiteral(
"-?\\d*([\\.]\\d{0,%1})?" ).arg( mField.
precision() );
88 mValidator =
new QRegExpValidator( QRegExp( re ), parent );
92 mValidator =
new QDoubleValidator( parent );
97 case QVariant::LongLong :
102 mValidator =
nullptr;
117 ( mField.
type() == QVariant::Double
118 || mField.
type() == QVariant::Int
119 || mField.
type() == QVariant::LongLong
120 || mField.
type() == QVariant::Date
127 if ( s == mDefaultValue )
133 QValidator::State result = mValidator->validate( s, i );
136 else if ( mField.
type() == QVariant::String )
140 if ( !mNullValue.isEmpty() && !s.isEmpty() && s.size() < mNullValue.size() && s == mNullValue.left( s.size() ) )
143 if ( !mDefaultValue.isEmpty() && !s.isEmpty() && s.size() < mDefaultValue.size() && s == mDefaultValue.left( s.size() ) )
146 if ( s == mNullValue )
149 if ( mField.
length() > 0 && s.size() > mField.
length() )
152 else if ( mField.
type() == QVariant::Date )
154 return QDate::fromString( s, mDateFormat ).isValid() ? Acceptable : Intermediate;
158 QgsDebugMsg( QString(
"unsupported type %1 for validation" ).arg( mField.
type() ) );
169 mValidator->fixup( s );
171 else if ( mField.
type() == QVariant::String && mField.
length() > 0 && s.size() > mField.
length() && s != mDefaultValue )
176 else if ( mField.
type() == QVariant::Date )
179 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