Index: src/gui/CMakeLists.txt =================================================================== --- src/gui/CMakeLists.txt (revision 15556) +++ src/gui/CMakeLists.txt (working copy) @@ -89,6 +89,7 @@ qgslegendinterface.h qgisinterface.h qgsencodingfiledialog.h +qgsfieldvalidator.h qgsformannotationitem.h qgsgenericprojectionselector.h qgsmapcanvas.h Index: src/gui/qgsattributeeditor.cpp =================================================================== --- src/gui/qgsattributeeditor.cpp (revision 15556) +++ src/gui/qgsattributeeditor.cpp (working copy) @@ -23,6 +23,7 @@ #include #include #include +#include #include #include @@ -357,20 +358,9 @@ le->setCompleter( c ); } - if ( myFieldType == QVariant::Int ) - { - le->setValidator( new QIntValidator( le ) ); - } - else if ( myFieldType == QVariant::LongLong ) - { - le->setValidator( new QgsLongLongValidator( le ) ); - } - else if ( myFieldType == QVariant::Double ) - { - le->setValidator( new QDoubleValidator( le ) ); - } - + le->setValidator( new QgsFieldValidator( le, field ) ); myWidget = le; + } if ( te ) @@ -460,7 +450,7 @@ { text = le->text(); modified = le->isModified(); - if ( text == nullValue ) + if ( text == nullValue || text == "NULL" || text == "null" ) { text = QString::null; } Index: qgsfieldvalidator.h =================================================================== --- qgsfieldvalidator.h (revision 0) +++ qgsfieldvalidator.h (revision 0) @@ -0,0 +1,137 @@ +/*************************************************************************** + qgsfieldvalidator.h - description + ------------------- + begin : March 2011 + copyright : (C) 2011 by SunilRajKiran-kCube + email : sunilraj.kiran@kcubeconsulting.com + + adapted version of QValidator for QgsField + ***************************************************************************/ + +/*************************************************************************** + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation; either version 2 of the License, or * + * (at your option) any later version. * + * * + ***************************************************************************/ +/* $Id$ */ + +#ifndef QGSFIELDVALIDATOR_H +#define QGSFIELDVALIDATOR_H + +#include +#include +#include +#include + +#include "qgslogger.h" +#include "qgslonglongvalidator.h" +#include "qgsfield.h" + +class GUI_EXPORT QgsFieldValidator : public QValidator +{ + Q_OBJECT + +public: +QgsFieldValidator(QObject *parent, const QgsField &field, int t, int b):QValidator(parent) +{ + fieldLength = field.length(); + fieldPrecision = field.precision(); + fieldType = field.type(); + + switch(fieldType) + { + case QVariant::Int : + validator = new QIntValidator(b, t, parent); + break; + case QVariant::Double : + validator = new QDoubleValidator(parent); + break; + case QVariant::LongLong : + validator = new QgsLongLongValidator(b, t, parent); + break; + default: + validator = 0; + } + +} + +QgsFieldValidator(QObject *parent, const QgsField &field):QValidator(parent) +{ + fieldLength = field.length(); + fieldPrecision = field.precision(); + fieldType = field.type(); + + switch(fieldType) + { + case QVariant::Int : + validator = new QIntValidator(parent); + break; + case QVariant::Double : + validator = new QDoubleValidator(parent); + break; + case QVariant::LongLong : + validator = new QgsLongLongValidator(parent); + break; + default: + validator = 0; + } +} + +~QgsFieldValidator() +{ + delete validator; +} + +QValidator::State validate(QString &s, int &i) const +{ + + if(validator && validator->validate(s, i) == Invalid) + { + return Invalid; + } + + if(validator && validator->validate(s, i) == Intermediate) + { + if(s.length() > fieldLength) + return Invalid; + return Intermediate; + } + + if ( fieldType == QVariant::String ) + { + if (s.length() > 1 ) + { + if ( s == "NU" || s == "NUL" || s == "NULL" || s == "nu" || s == "nul" || s == "null") + return Intermediate; + } + } + + if(s.length() > fieldLength) + return Invalid; + + if(fieldType == QVariant::Double) + { + int len = s.length(); + int prec = s.indexOf(QChar('.')); + QgsDebugMsg(tr("length %1 %2") . arg(len). arg(prec)); + if(s.indexOf(QChar('.')) != -1 && s.length() - (s.indexOf(QChar('.')) + 1) > fieldPrecision) + return Invalid; + } + + return Acceptable; +} + +private: + // Disables copy constructing + Q_DISABLE_COPY( QgsFieldValidator ) + + QValidator *validator; + int fieldLength; + QVariant::Type fieldType; + int fieldPrecision; +}; + +#endif // QGSFIELDVALIDATOR_H