QGIS API Documentation  2.0.1-Dufour
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
qgsfieldvalidator.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsfieldvalidator.cpp - description
3  -------------------
4  begin : March 2011
5  copyright : (C) 2011 by SunilRajKiran-kCube
6  email : [email protected]
7 
8  adapted version of QValidator for QgsField
9  ***************************************************************************/
10 
11 /***************************************************************************
12  * *
13  * This program is free software; you can redistribute it and/or modify *
14  * it under the terms of the GNU General Public License as published by *
15  * the Free Software Foundation; either version 2 of the License, or *
16  * (at your option) any later version. *
17  * *
18  ***************************************************************************/
19 
20 #include "qgsfieldvalidator.h"
21 
22 #include <QValidator>
23 #include <QRegExpValidator>
24 #include <QDate>
25 #include <QVariant>
26 #include <QSettings>
27 
28 #include "qgslogger.h"
29 #include "qgslonglongvalidator.h"
30 #include "qgsfield.h"
31 
32 QgsFieldValidator::QgsFieldValidator( QObject *parent, const QgsField &field, QString dateFormat )
33  : QValidator( parent )
34  , mField( field )
35  , mDateFormat( dateFormat )
36 {
37  switch ( mField.type() )
38  {
39  case QVariant::Int:
40  {
41  if ( mField.length() > 0 )
42  {
43  QString re = QString( "-?\\d{0,%1}" ).arg( mField.length() );
44  mValidator = new QRegExpValidator( QRegExp( re ), parent );
45  }
46  else
47  {
48  mValidator = new QIntValidator( parent );
49  }
50  }
51  break;
52 
53  case QVariant::Double:
54  {
55  if ( mField.length() > 0 && mField.precision() > 0 )
56  {
57  QString re = QString( "-?\\d{0,%1}(\\.\\d{0,%2})?" ).arg( mField.length() - mField.precision() ).arg( mField.precision() );
58  mValidator = new QRegExpValidator( QRegExp( re ), parent );
59  }
60  else if ( mField.length() > 0 && mField.precision() == 0 )
61  {
62  QString re = QString( "-?\\d{0,%1}" ).arg( mField.length() );
63  mValidator = new QRegExpValidator( QRegExp( re ), parent );
64  }
65  else if ( mField.precision() > 0 )
66  {
67  QString re = QString( "-?\\d*(\\.\\d{0,%1})?" ).arg( mField.precision() );
68  mValidator = new QRegExpValidator( QRegExp( re ), parent );
69  }
70  else
71  {
72  mValidator = new QDoubleValidator( parent );
73  }
74  }
75  break;
76 
77  case QVariant::LongLong :
78  mValidator = new QgsLongLongValidator( parent );
79  break;
80 
81  default:
82  mValidator = 0;
83  }
84 
85  QSettings settings;
86  mNullValue = settings.value( "qgis/nullValue", "NULL" ).toString();
87 }
88 
90 {
91  delete mValidator;
92 }
93 
94 QValidator::State QgsFieldValidator::validate( QString &s, int &i ) const
95 {
96  // empty values are considered NULL for numbers and dates and are acceptable
97  if ( s.isEmpty() &&
98  ( mField.type() == QVariant::Double
99  || mField.type() == QVariant::Int
100  || mField.type() == QVariant::LongLong
101  || mField.type() == QVariant::Date
102  )
103  )
104  {
105  return Acceptable;
106  }
107 
108  // delegate to the child validator if any
109  if ( mValidator )
110  {
111  QValidator::State result = mValidator->validate( s, i );
112  return result;
113  }
114  else if ( mField.type() == QVariant::String )
115  {
116  // allow to enter the NULL representation, which might be
117  // longer than the actual field
118  if ( mNullValue.size() > 0 &&
119  s.size() > 0 &&
120  s.size() < mNullValue.size() &&
121  s == mNullValue.left( s.size() ) )
122  return Intermediate;
123 
124  if ( s == mNullValue )
125  return Acceptable;
126 
127  if ( mField.length() > 0 && s.size() > mField.length() )
128  return Invalid;
129  }
130  else if ( mField.type() == QVariant::Date )
131  {
132  return QDate::fromString( s, mDateFormat ).isValid() ? Acceptable : Intermediate;
133  }
134  else
135  {
136  QgsDebugMsg( QString( "unsupported type %1 for validation" ).arg( mField.type() ) );
137  return Invalid;
138  }
139 
140  return Acceptable;
141 }
142 
143 void QgsFieldValidator::fixup( QString &s ) const
144 {
145  if ( mValidator )
146  {
147  mValidator->fixup( s );
148  }
149  else if ( mField.type() == QVariant::String && mField.length() > 0 && s.size() > mField.length() )
150  {
151  // if the value is longer, this must be a partial NULL representation
152  s = mNullValue;
153  }
154  else if ( mField.type() == QVariant::Date )
155  {
156  // invalid dates will also translate to NULL
157  s = "";
158  }
159 }