Skip to content

Commit 71c6454

Browse files
author
jef
committed
fix #2554:
- apply heavy modified patch from sunil. - introduces QgsFieldValidator including sip bindings - add missing sip bindings to QgsAttributeEditor git-svn-id: http://svn.osgeo.org/qgis/trunk@15566 c8812cc2-4d05-0410-92ff-de0c093fc19c
1 parent c6ee44c commit 71c6454

File tree

7 files changed

+239
-13
lines changed

7 files changed

+239
-13
lines changed

python/gui/gui.sip

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,5 +33,7 @@
3333
%Include qgstextannotationitem.sip
3434
%Include qgsvertexmarker.sip
3535
%Include qgssearchquerybuilder.sip
36+
%Include qgsattributeeditor.sip
37+
%Include qgsfieldvalidator.sip
3638

3739
%Include symbology-ng-gui.sip

python/gui/qgsattributeeditor.sip

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/* \brief create attribute widget for editing */
2+
class QgsAttributeEditor : public QObject
3+
{
4+
%TypeHeaderCode
5+
#incude <qgsattributeeditor.h>
6+
%End
7+
public:
8+
QgsAttributeEditor( QObject *parent );
9+
static QWidget *createAttributeEditor( QWidget *parent, QWidget *editor, QgsVectorLayer *vl, int idx, const QVariant &value );
10+
static bool retrieveValue( QWidget *widget, QgsVectorLayer *vl, int idx, QVariant &value );
11+
static bool setValue( QWidget *widget, QgsVectorLayer *vl, int idx, const QVariant &value );
12+
13+
public slots:
14+
void selectFileName( void );
15+
void selectDate( void );
16+
};

python/gui/qgsfieldvalidator.sip

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class QgsFieldValidator : public QValidator
2+
{
3+
%TypeHeaderCode
4+
#include <qgsfieldvalidator.h>
5+
%End
6+
QgsFieldValidator( QObject *parent, const QgsField &field );
7+
~QgsFieldValidator();
8+
9+
virtual State validate( QString &, int & ) const;
10+
virtual void fixup( QString & ) const;
11+
};

src/gui/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ qgsdetaileditemwidget.cpp
3535
qgsdetaileditemdata.cpp
3636
qgsencodingfiledialog.cpp
3737
qgsfiledropedit.cpp
38+
qgsfieldvalidator.cpp
3839
qgsformannotationitem.cpp
3940
qgsgenericprojectionselector.cpp
4041
qgsmapcanvas.cpp
@@ -89,6 +90,7 @@ qgsdetaileditemwidget.h
8990
qgslegendinterface.h
9091
qgisinterface.h
9192
qgsencodingfiledialog.h
93+
qgsfieldvalidator.h
9294
qgsformannotationitem.h
9395
qgsgenericprojectionselector.h
9496
qgsmapcanvas.h
@@ -206,6 +208,8 @@ qgsrubberband.h
206208
qgsvertexmarker.h
207209
qgsmaptip.h
208210
qgssearchquerybuilder.h
211+
qgsattributeeditor.h
212+
qgsfieldvalidator.h
209213

210214
${CMAKE_CURRENT_BINARY_DIR}/../ui/ui_qgsdetaileditemwidgetbase.h
211215
${CMAKE_CURRENT_BINARY_DIR}/../ui/ui_qgsgenericprojectionselectorbase.h

src/gui/qgsattributeeditor.cpp

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include <qgscategorizedsymbolrendererv2.h>
2424
#include <qgssymbol.h>
2525
#include <qgslonglongvalidator.h>
26+
#include <qgsfieldvalidator.h>
2627

2728
#include <QPushButton>
2829
#include <QLineEdit>
@@ -357,19 +358,7 @@ QWidget *QgsAttributeEditor::createAttributeEditor( QWidget *parent, QWidget *ed
357358
le->setCompleter( c );
358359
}
359360

360-
if ( myFieldType == QVariant::Int )
361-
{
362-
le->setValidator( new QIntValidator( le ) );
363-
}
364-
else if ( myFieldType == QVariant::LongLong )
365-
{
366-
le->setValidator( new QgsLongLongValidator( le ) );
367-
}
368-
else if ( myFieldType == QVariant::Double )
369-
{
370-
le->setValidator( new QDoubleValidator( le ) );
371-
}
372-
361+
le->setValidator( new QgsFieldValidator( le, field ) );
373362
myWidget = le;
374363
}
375364

src/gui/qgsfieldvalidator.cpp

Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
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+
84+
QgsFieldValidator::~QgsFieldValidator()
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+
}

src/gui/qgsfieldvalidator.h

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/***************************************************************************
2+
qgsfieldvalidator.h - 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+
#ifndef QGSFIELDVALIDATOR_H
22+
#define QGSFIELDVALIDATOR_H
23+
24+
#include <QValidator>
25+
#include <QVariant>
26+
#include <QSettings>
27+
#include "qgsfield.h"
28+
29+
30+
class GUI_EXPORT QgsFieldValidator : public QValidator
31+
{
32+
Q_OBJECT
33+
34+
public:
35+
QgsFieldValidator( QObject *parent, const QgsField &field );
36+
~QgsFieldValidator();
37+
38+
virtual State validate( QString &, int & ) const;
39+
virtual void fixup( QString & ) const;
40+
41+
private:
42+
// Disables copy constructing
43+
Q_DISABLE_COPY( QgsFieldValidator )
44+
45+
QValidator *mValidator;
46+
QgsField mField;
47+
QString mNullValue;
48+
};
49+
50+
#endif // QGSFIELDVALIDATOR_H

0 commit comments

Comments
 (0)