|
1 |
/***************************************************************************
|
|
2 |
qgsfieldvalidator.h - 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 |
/* $Id$ */
|
|
20 |
|
|
21 |
#ifndef QGSFIELDVALIDATOR_H
|
|
22 |
#define QGSFIELDVALIDATOR_H
|
|
23 |
|
|
24 |
#include <QMessageBox>
|
|
25 |
#include <QValidator>
|
|
26 |
#include <QLocale>
|
|
27 |
#include <QVariant>
|
|
28 |
|
|
29 |
#include "qgslogger.h"
|
|
30 |
#include "qgslonglongvalidator.h"
|
|
31 |
#include "qgsfield.h"
|
|
32 |
|
|
33 |
class GUI_EXPORT QgsFieldValidator : public QValidator
|
|
34 |
{
|
|
35 |
Q_OBJECT
|
|
36 |
|
|
37 |
public:
|
|
38 |
QgsFieldValidator(QObject *parent, const QgsField &field, int t, int b):QValidator(parent)
|
|
39 |
{
|
|
40 |
fieldLength = field.length();
|
|
41 |
fieldPrecision = field.precision();
|
|
42 |
fieldType = field.type();
|
|
43 |
|
|
44 |
switch(fieldType)
|
|
45 |
{
|
|
46 |
case QVariant::Int :
|
|
47 |
validator = new QIntValidator(b, t, parent);
|
|
48 |
break;
|
|
49 |
case QVariant::Double :
|
|
50 |
validator = new QDoubleValidator(parent);
|
|
51 |
break;
|
|
52 |
case QVariant::LongLong :
|
|
53 |
validator = new QgsLongLongValidator(b, t, parent);
|
|
54 |
break;
|
|
55 |
default:
|
|
56 |
validator = 0;
|
|
57 |
}
|
|
58 |
|
|
59 |
}
|
|
60 |
|
|
61 |
QgsFieldValidator(QObject *parent, const QgsField &field):QValidator(parent)
|
|
62 |
{
|
|
63 |
fieldLength = field.length();
|
|
64 |
fieldPrecision = field.precision();
|
|
65 |
fieldType = field.type();
|
|
66 |
|
|
67 |
switch(fieldType)
|
|
68 |
{
|
|
69 |
case QVariant::Int :
|
|
70 |
validator = new QIntValidator(parent);
|
|
71 |
break;
|
|
72 |
case QVariant::Double :
|
|
73 |
validator = new QDoubleValidator(parent);
|
|
74 |
break;
|
|
75 |
case QVariant::LongLong :
|
|
76 |
validator = new QgsLongLongValidator(parent);
|
|
77 |
break;
|
|
78 |
default:
|
|
79 |
validator = 0;
|
|
80 |
}
|
|
81 |
}
|
|
82 |
|
|
83 |
~QgsFieldValidator()
|
|
84 |
{
|
|
85 |
delete validator;
|
|
86 |
}
|
|
87 |
|
|
88 |
QValidator::State validate(QString &s, int &i) const
|
|
89 |
{
|
|
90 |
QgsDebugMsg("**********###########entering validate\n");
|
|
91 |
|
|
92 |
if(validator && validator->validate(s, i) == Invalid)
|
|
93 |
{
|
|
94 |
QgsDebugMsg("**********###########entering validate2\n");
|
|
95 |
return Invalid;
|
|
96 |
}
|
|
97 |
|
|
98 |
if(s.length() > fieldLength)
|
|
99 |
return Invalid;
|
|
100 |
|
|
101 |
if(fieldType == QVariant::Double)
|
|
102 |
{
|
|
103 |
int len = s.length();
|
|
104 |
int prec = s.indexOf(QChar('.'));
|
|
105 |
QgsDebugMsg(tr("length %1 %2") . arg(len). arg(prec));
|
|
106 |
if(s.indexOf(QChar('.')) != -1 && s.length() - (s.indexOf(QChar('.')) + 1) > fieldPrecision)
|
|
107 |
return Invalid;
|
|
108 |
}
|
|
109 |
|
|
110 |
return Acceptable;
|
|
111 |
}
|
|
112 |
|
|
113 |
private:
|
|
114 |
// Disables copy constructing
|
|
115 |
Q_DISABLE_COPY( QgsFieldValidator )
|
|
116 |
|
|
117 |
QValidator *validator;
|
|
118 |
int fieldLength;
|
|
119 |
QVariant::Type fieldType;
|
|
120 |
int fieldPrecision;
|
|
121 |
};
|
|
122 |
|
|
123 |
#endif // QGSFIELDVALIDATOR_H
|