Quantum GIS API Documentation  1.7.4
src/gui/qgsfieldvalidator.cpp
Go to the documentation of this file.
00001 /***************************************************************************
00002                          qgsfieldvalidator.cpp  -  description
00003                              -------------------
00004     begin                : March 2011
00005     copyright            : (C) 2011 by SunilRajKiran-kCube
00006     email                : [email protected]
00007 
00008   adapted version of QValidator for QgsField
00009  ***************************************************************************/
00010 
00011 /***************************************************************************
00012  *                                                                         *
00013  *   This program is free software; you can redistribute it and/or modify  *
00014  *   it under the terms of the GNU General Public License as published by  *
00015  *   the Free Software Foundation; either version 2 of the License, or     *
00016  *   (at your option) any later version.                                   *
00017  *                                                                         *
00018  ***************************************************************************/
00019 /* $Id$ */
00020 
00021 #include "qgsfieldvalidator.h"
00022 
00023 #include <QValidator>
00024 #include <QRegExpValidator>
00025 #include <QDate>
00026 #include <QVariant>
00027 #include <QSettings>
00028 
00029 #include "qgslogger.h"
00030 #include "qgslonglongvalidator.h"
00031 #include "qgsfield.h"
00032 
00033 QgsFieldValidator::QgsFieldValidator( QObject *parent, const QgsField &field )
00034     : QValidator( parent )
00035     , mField( field )
00036 {
00037   switch ( mField.type() )
00038   {
00039     case QVariant::Int:
00040     {
00041       if ( mField.length() > 0 )
00042       {
00043         QString re = QString( "-?\\d{0,%1}" ).arg( mField.length() );
00044         mValidator = new QRegExpValidator( QRegExp( re ), parent );
00045       }
00046       else
00047       {
00048         mValidator = new QIntValidator( parent );
00049       }
00050     }
00051     break;
00052 
00053     case QVariant::Double:
00054     {
00055       if ( mField.length() > 0 && mField.precision() > 0 )
00056       {
00057         QString re = QString( "-?\\d{0,%1}(\\.\\d{0,%2})?" ).arg( mField.length() - mField.precision() ).arg( mField.precision() );
00058         mValidator = new QRegExpValidator( QRegExp( re ), parent );
00059       }
00060       else if ( mField.precision() > 0 )
00061       {
00062         QString re = QString( "-?\\d*(\\.\\d{0,%1))?" ).arg( mField.precision() );
00063         mValidator = new QRegExpValidator( QRegExp( re ), parent );
00064       }
00065       else
00066       {
00067         mValidator = new QDoubleValidator( parent );
00068       }
00069     }
00070     break;
00071 
00072     case QVariant::LongLong :
00073       mValidator = new QgsLongLongValidator( parent );
00074       break;
00075 
00076     default:
00077       mValidator = 0;
00078   }
00079 
00080   QSettings settings;
00081   mNullValue = settings.value( "qgis/nullValue", "NULL" ).toString();
00082 }
00083 
00084 QgsFieldValidator::~QgsFieldValidator()
00085 {
00086   delete mValidator;
00087 }
00088 
00089 QValidator::State QgsFieldValidator::validate( QString &s, int &i ) const
00090 {
00091   // empty values are considered NULL for numbers and dates and are acceptable
00092   if ( s.isEmpty() &&
00093        ( mField.type() == QVariant::Double
00094          || mField.type() == QVariant::Int
00095          || mField.type() == QVariant::LongLong
00096          || mField.type() == QVariant::Date
00097        )
00098      )
00099   {
00100     return Acceptable;
00101   }
00102 
00103   // delegate to the child validator if any
00104   if ( mValidator )
00105   {
00106     QValidator::State result = mValidator->validate( s, i );
00107     return result;
00108   }
00109   else if ( mField.type() == QVariant::String )
00110   {
00111     // allow to enter the NULL representation, which might be
00112     // longer than the actual field
00113     if ( mNullValue.size() > 0 &&
00114          s.size() > 0 &&
00115          s.size() < mNullValue.size() &&
00116          s == mNullValue.left( s.size() ) )
00117       return Intermediate;
00118 
00119     if ( s == mNullValue )
00120       return Acceptable;
00121 
00122     if ( mField.length() > 0 && s.size() > mField.length() )
00123       return Invalid;
00124   }
00125   else if ( mField.type() == QVariant::Date )
00126   {
00127     return QDate::fromString( s ).isValid() ? Acceptable : Intermediate;
00128   }
00129   else
00130   {
00131     QgsDebugMsg( "unsupported type for validation" );
00132     return Invalid;
00133   }
00134 
00135   return Acceptable;
00136 }
00137 
00138 void QgsFieldValidator::fixup( QString &s ) const
00139 {
00140   if ( mValidator )
00141   {
00142     mValidator->fixup( s );
00143   }
00144   else if ( mField.type() == QVariant::String && mField.length() > 0 && s.size() > mField.length() )
00145   {
00146     // if the value is longer, this must be a partial NULL representation
00147     s = mNullValue;
00148   }
00149   else if ( mField.type() == QVariant::Date )
00150   {
00151     // invalid dates will also translate to NULL
00152     s = "";
00153   }
00154 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines