QGIS API Documentation  2.99.0-Master (a18066b)
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 "qgsfields.h"
31 #include "qgsapplication.h"
32 
33 QgsFieldValidator::QgsFieldValidator( QObject *parent, const QgsField &field, const QString& defaultValue, const QString& dateFormat )
34  : QValidator( parent )
35  , mField( field )
36  , mDefaultValue( defaultValue )
37  , mDateFormat( dateFormat )
38 {
39  switch ( mField.type() )
40  {
41  case QVariant::Int:
42  {
43  if ( mField.length() > 0 )
44  {
45  QString re = QStringLiteral( "-?\\d{0,%1}" ).arg( mField.length() );
46  mValidator = new QRegExpValidator( QRegExp( re ), parent );
47  }
48  else
49  {
50  mValidator = new QIntValidator( parent );
51  }
52  }
53  break;
54 
55  case QVariant::Double:
56  {
57  if ( mField.length() > 0 && mField.precision() > 0 )
58  {
59  QString re = QStringLiteral( "-?\\d{0,%1}(\\.\\d{0,%2})?" ).arg( mField.length() - mField.precision() ).arg( mField.precision() );
60  mValidator = new QRegExpValidator( QRegExp( re ), parent );
61  }
62  else if ( mField.length() > 0 && mField.precision() == 0 )
63  {
64  QString re = QStringLiteral( "-?\\d{0,%1}" ).arg( mField.length() );
65  mValidator = new QRegExpValidator( QRegExp( re ), parent );
66  }
67  else if ( mField.precision() > 0 )
68  {
69  QString re = QStringLiteral( "-?\\d*(\\.\\d{0,%1})?" ).arg( mField.precision() );
70  mValidator = new QRegExpValidator( QRegExp( re ), parent );
71  }
72  else
73  {
74  mValidator = new QDoubleValidator( parent );
75  }
76  }
77  break;
78 
79  case QVariant::LongLong :
80  mValidator = new QgsLongLongValidator( parent );
81  break;
82 
83  default:
84  mValidator = nullptr;
85  }
86 
87  QSettings settings;
89 }
90 
92 {
93  delete mValidator;
94 }
95 
96 QValidator::State QgsFieldValidator::validate( QString &s, int &i ) const
97 {
98  // empty values are considered NULL for numbers and dates and are acceptable
99  if ( s.isEmpty() &&
100  ( mField.type() == QVariant::Double
101  || mField.type() == QVariant::Int
102  || mField.type() == QVariant::LongLong
103  || mField.type() == QVariant::Date
104  )
105  )
106  {
107  return Acceptable;
108  }
109 
110  if ( s == mDefaultValue )
111  return Acceptable;
112 
113  // delegate to the child validator if any
114  if ( mValidator )
115  {
116  QValidator::State result = mValidator->validate( s, i );
117  return result;
118  }
119  else if ( mField.type() == QVariant::String )
120  {
121  // allow entering the NULL representation, which might be
122  // longer than the actual field
123  if ( !mNullValue.isEmpty() && !s.isEmpty() && s.size() < mNullValue.size() && s == mNullValue.left( s.size() ) )
124  return Intermediate;
125 
126  if ( !mDefaultValue.isEmpty() && !s.isEmpty() && s.size() < mDefaultValue.size() && s == mDefaultValue.left( s.size() ) )
127  return Intermediate;
128 
129  if ( s == mNullValue )
130  return Acceptable;
131 
132  if ( mField.length() > 0 && s.size() > mField.length() )
133  return Invalid;
134  }
135  else if ( mField.type() == QVariant::Date )
136  {
137  return QDate::fromString( s, mDateFormat ).isValid() ? Acceptable : Intermediate;
138  }
139  else
140  {
141  QgsDebugMsg( QString( "unsupported type %1 for validation" ).arg( mField.type() ) );
142  return Invalid;
143  }
144 
145  return Acceptable;
146 }
147 
148 void QgsFieldValidator::fixup( QString &s ) const
149 {
150  if ( mValidator )
151  {
152  mValidator->fixup( s );
153  }
154  else if ( mField.type() == QVariant::String && mField.length() > 0 && s.size() > mField.length() && s != mDefaultValue )
155  {
156  // if the value is longer, this must be a partial NULL representation
157  s = mNullValue;
158  }
159  else if ( mField.type() == QVariant::Date )
160  {
161  // invalid dates will also translate to NULL
162  s = QLatin1String( "" );
163  }
164 }
int precision
Definition: qgsfield.h:51
virtual State validate(QString &, int &) const override
#define QgsDebugMsg(str)
Definition: qgslogger.h:36
QgsFieldValidator(QObject *parent, const QgsField &field, const QString &defaultValue, const QString &dateFormat="yyyy-MM-dd")
int length
Definition: qgsfield.h:50
static QString nullRepresentation()
This string is used to represent the value NULL throughout QGIS.
virtual void fixup(QString &) const override
Encapsulate a field in an attribute table or data source.
Definition: qgsfield.h:45
QVariant::Type type() const
Gets variant type of the field as it will be retrieved from data source.
Definition: qgsfield.cpp:94