Quantum GIS API Documentation  1.8
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 
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 )
00033     : QValidator( parent )
00034     , mField( field )
00035 {
00036   switch ( mField.type() )
00037   {
00038     case QVariant::Int:
00039     {
00040       if ( mField.length() > 0 )
00041       {
00042         QString re = QString( "-?\\d{0,%1}" ).arg( mField.length() );
00043         mValidator = new QRegExpValidator( QRegExp( re ), parent );
00044       }
00045       else
00046       {
00047         mValidator = new QIntValidator( parent );
00048       }
00049     }
00050     break;
00051 
00052     case QVariant::Double:
00053     {
00054       if ( mField.length() > 0 && mField.precision() > 0 )
00055       {
00056         QString re = QString( "-?\\d{0,%1}(\\.\\d{0,%2})?" ).arg( mField.length() - mField.precision() ).arg( mField.precision() );
00057         mValidator = new QRegExpValidator( QRegExp( re ), parent );
00058       }
00059       else if ( mField.length() > 0 && mField.precision() == 0 )
00060       {
00061         QString re = QString( "-?\\d{0,%1}" ).arg( mField.length() );
00062         mValidator = new QRegExpValidator( QRegExp( re ), parent );
00063       }
00064       else if ( mField.precision() > 0 )
00065       {
00066         QString re = QString( "-?\\d*(\\.\\d{0,%1})?" ).arg( mField.precision() );
00067         mValidator = new QRegExpValidator( QRegExp( re ), parent );
00068       }
00069       else
00070       {
00071         mValidator = new QDoubleValidator( parent );
00072       }
00073     }
00074     break;
00075 
00076     case QVariant::LongLong :
00077       mValidator = new QgsLongLongValidator( parent );
00078       break;
00079 
00080     default:
00081       mValidator = 0;
00082   }
00083 
00084   QSettings settings;
00085   mNullValue = settings.value( "qgis/nullValue", "NULL" ).toString();
00086 }
00087 
00088 QgsFieldValidator::~QgsFieldValidator()
00089 {
00090   delete mValidator;
00091 }
00092 
00093 QValidator::State QgsFieldValidator::validate( QString &s, int &i ) const
00094 {
00095   // empty values are considered NULL for numbers and dates and are acceptable
00096   if ( s.isEmpty() &&
00097        ( mField.type() == QVariant::Double
00098          || mField.type() == QVariant::Int
00099          || mField.type() == QVariant::LongLong
00100          || mField.type() == QVariant::Date
00101        )
00102      )
00103   {
00104     return Acceptable;
00105   }
00106 
00107   // delegate to the child validator if any
00108   if ( mValidator )
00109   {
00110     QValidator::State result = mValidator->validate( s, i );
00111     return result;
00112   }
00113   else if ( mField.type() == QVariant::String )
00114   {
00115     // allow to enter the NULL representation, which might be
00116     // longer than the actual field
00117     if ( mNullValue.size() > 0 &&
00118          s.size() > 0 &&
00119          s.size() < mNullValue.size() &&
00120          s == mNullValue.left( s.size() ) )
00121       return Intermediate;
00122 
00123     if ( s == mNullValue )
00124       return Acceptable;
00125 
00126     if ( mField.length() > 0 && s.size() > mField.length() )
00127       return Invalid;
00128   }
00129   else if ( mField.type() == QVariant::Date )
00130   {
00131     return QDate::fromString( s ).isValid() ? Acceptable : Intermediate;
00132   }
00133   else
00134   {
00135     QgsDebugMsg( "unsupported type for validation" );
00136     return Invalid;
00137   }
00138 
00139   return Acceptable;
00140 }
00141 
00142 void QgsFieldValidator::fixup( QString &s ) const
00143 {
00144   if ( mValidator )
00145   {
00146     mValidator->fixup( s );
00147   }
00148   else if ( mField.type() == QVariant::String && mField.length() > 0 && s.size() > mField.length() )
00149   {
00150     // if the value is longer, this must be a partial NULL representation
00151     s = mNullValue;
00152   }
00153   else if ( mField.type() == QVariant::Date )
00154   {
00155     // invalid dates will also translate to NULL
00156     s = "";
00157   }
00158 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines