QGIS API Documentation  master-59fd5e0
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                : sunilraj.kiran@kcubeconsulting.com
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 
00020 #include "qgsfieldvalidator.h"
00021 
00022 #include <QValidator>
00023 #include <QRegExpValidator>
00024 #include <QDate>
00025 #include <QVariant>
00026 #include <QSettings>
00027 
00028 #include "qgslogger.h"
00029 #include "qgslonglongvalidator.h"
00030 #include "qgsfield.h"
00031 
00032 QgsFieldValidator::QgsFieldValidator( QObject *parent, const QgsField &field, QString dateFormat )
00033     : QValidator( parent )
00034     , mField( field )
00035     , mDateFormat( dateFormat )
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.length() > 0 && mField.precision() == 0 )
00061       {
00062         QString re = QString( "-?\\d{0,%1}" ).arg( mField.length() );
00063         mValidator = new QRegExpValidator( QRegExp( re ), parent );
00064       }
00065       else if ( mField.precision() > 0 )
00066       {
00067         QString re = QString( "-?\\d*(\\.\\d{0,%1})?" ).arg( mField.precision() );
00068         mValidator = new QRegExpValidator( QRegExp( re ), parent );
00069       }
00070       else
00071       {
00072         mValidator = new QDoubleValidator( parent );
00073       }
00074     }
00075     break;
00076 
00077     case QVariant::LongLong :
00078       mValidator = new QgsLongLongValidator( parent );
00079       break;
00080 
00081     default:
00082       mValidator = 0;
00083   }
00084 
00085   QSettings settings;
00086   mNullValue = settings.value( "qgis/nullValue", "NULL" ).toString();
00087 }
00088 
00089 QgsFieldValidator::~QgsFieldValidator()
00090 {
00091   delete mValidator;
00092 }
00093 
00094 QValidator::State QgsFieldValidator::validate( QString &s, int &i ) const
00095 {
00096   // empty values are considered NULL for numbers and dates and are acceptable
00097   if ( s.isEmpty() &&
00098        ( mField.type() == QVariant::Double
00099          || mField.type() == QVariant::Int
00100          || mField.type() == QVariant::LongLong
00101          || mField.type() == QVariant::Date
00102        )
00103      )
00104   {
00105     return Acceptable;
00106   }
00107 
00108   // delegate to the child validator if any
00109   if ( mValidator )
00110   {
00111     QValidator::State result = mValidator->validate( s, i );
00112     return result;
00113   }
00114   else if ( mField.type() == QVariant::String )
00115   {
00116     // allow to enter the NULL representation, which might be
00117     // longer than the actual field
00118     if ( mNullValue.size() > 0 &&
00119          s.size() > 0 &&
00120          s.size() < mNullValue.size() &&
00121          s == mNullValue.left( s.size() ) )
00122       return Intermediate;
00123 
00124     if ( s == mNullValue )
00125       return Acceptable;
00126 
00127     if ( mField.length() > 0 && s.size() > mField.length() )
00128       return Invalid;
00129   }
00130   else if ( mField.type() == QVariant::Date )
00131   {
00132     return QDate::fromString( s, mDateFormat ).isValid() ? Acceptable : Intermediate;
00133   }
00134   else
00135   {
00136     QgsDebugMsg( QString( "unsupported type %1 for validation" ).arg( mField.type() ) );
00137     return Invalid;
00138   }
00139 
00140   return Acceptable;
00141 }
00142 
00143 void QgsFieldValidator::fixup( QString &s ) const
00144 {
00145   if ( mValidator )
00146   {
00147     mValidator->fixup( s );
00148   }
00149   else if ( mField.type() == QVariant::String && mField.length() > 0 && s.size() > mField.length() )
00150   {
00151     // if the value is longer, this must be a partial NULL representation
00152     s = mNullValue;
00153   }
00154   else if ( mField.type() == QVariant::Date )
00155   {
00156     // invalid dates will also translate to NULL
00157     s = "";
00158   }
00159 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Defines