Quantum GIS API Documentation  1.7.5-Wroclaw
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator 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 : sunilraj.kiran@kcubeconsulting.com
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 /* $Id$ */
20 
21 #include "qgsfieldvalidator.h"
22 
23 #include <QValidator>
24 #include <QRegExpValidator>
25 #include <QDate>
26 #include <QVariant>
27 #include <QSettings>
28 
29 #include "qgslogger.h"
30 #include "qgslonglongvalidator.h"
31 #include "qgsfield.h"
32 
33 QgsFieldValidator::QgsFieldValidator( QObject *parent, const QgsField &field )
34  : QValidator( parent )
35  , mField( field )
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.precision() > 0 )
61  {
62  QString re = QString( "-?\\d*(\\.\\d{0,%1))?" ).arg( mField.precision() );
63  mValidator = new QRegExpValidator( QRegExp( re ), parent );
64  }
65  else
66  {
67  mValidator = new QDoubleValidator( parent );
68  }
69  }
70  break;
71 
72  case QVariant::LongLong :
73  mValidator = new QgsLongLongValidator( parent );
74  break;
75 
76  default:
77  mValidator = 0;
78  }
79 
80  QSettings settings;
81  mNullValue = settings.value( "qgis/nullValue", "NULL" ).toString();
82 }
83 
85 {
86  delete mValidator;
87 }
88 
89 QValidator::State QgsFieldValidator::validate( QString &s, int &i ) const
90 {
91  // empty values are considered NULL for numbers and dates and are acceptable
92  if ( s.isEmpty() &&
93  ( mField.type() == QVariant::Double
94  || mField.type() == QVariant::Int
95  || mField.type() == QVariant::LongLong
96  || mField.type() == QVariant::Date
97  )
98  )
99  {
100  return Acceptable;
101  }
102 
103  // delegate to the child validator if any
104  if ( mValidator )
105  {
106  QValidator::State result = mValidator->validate( s, i );
107  return result;
108  }
109  else if ( mField.type() == QVariant::String )
110  {
111  // allow to enter the NULL representation, which might be
112  // longer than the actual field
113  if ( mNullValue.size() > 0 &&
114  s.size() > 0 &&
115  s.size() < mNullValue.size() &&
116  s == mNullValue.left( s.size() ) )
117  return Intermediate;
118 
119  if ( s == mNullValue )
120  return Acceptable;
121 
122  if ( mField.length() > 0 && s.size() > mField.length() )
123  return Invalid;
124  }
125  else if ( mField.type() == QVariant::Date )
126  {
127  return QDate::fromString( s ).isValid() ? Acceptable : Intermediate;
128  }
129  else
130  {
131  QgsDebugMsg( "unsupported type for validation" );
132  return Invalid;
133  }
134 
135  return Acceptable;
136 }
137 
138 void QgsFieldValidator::fixup( QString &s ) const
139 {
140  if ( mValidator )
141  {
142  mValidator->fixup( s );
143  }
144  else if ( mField.type() == QVariant::String && mField.length() > 0 && s.size() > mField.length() )
145  {
146  // if the value is longer, this must be a partial NULL representation
147  s = mNullValue;
148  }
149  else if ( mField.type() == QVariant::Date )
150  {
151  // invalid dates will also translate to NULL
152  s = "";
153  }
154 }