-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- 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
- Loading branch information
jef
committed
Mar 22, 2011
1 parent
c6ee44c
commit 71c6454
Showing
7 changed files
with
239 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
/* \brief create attribute widget for editing */ | ||
class QgsAttributeEditor : public QObject | ||
{ | ||
%TypeHeaderCode | ||
#incude <qgsattributeeditor.h> | ||
%End | ||
public: | ||
QgsAttributeEditor( QObject *parent ); | ||
static QWidget *createAttributeEditor( QWidget *parent, QWidget *editor, QgsVectorLayer *vl, int idx, const QVariant &value ); | ||
static bool retrieveValue( QWidget *widget, QgsVectorLayer *vl, int idx, QVariant &value ); | ||
static bool setValue( QWidget *widget, QgsVectorLayer *vl, int idx, const QVariant &value ); | ||
|
||
public slots: | ||
void selectFileName( void ); | ||
void selectDate( void ); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
class QgsFieldValidator : public QValidator | ||
{ | ||
%TypeHeaderCode | ||
#include <qgsfieldvalidator.h> | ||
%End | ||
QgsFieldValidator( QObject *parent, const QgsField &field ); | ||
~QgsFieldValidator(); | ||
|
||
virtual State validate( QString &, int & ) const; | ||
virtual void fixup( QString & ) const; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,154 @@ | ||
/*************************************************************************** | ||
qgsfieldvalidator.cpp - description | ||
------------------- | ||
begin : March 2011 | ||
copyright : (C) 2011 by SunilRajKiran-kCube | ||
email : sunilraj.kiran@kcubeconsulting.com | ||
adapted version of QValidator for QgsField | ||
***************************************************************************/ | ||
|
||
/*************************************************************************** | ||
* * | ||
* This program is free software; you can redistribute it and/or modify * | ||
* it under the terms of the GNU General Public License as published by * | ||
* the Free Software Foundation; either version 2 of the License, or * | ||
* (at your option) any later version. * | ||
* * | ||
***************************************************************************/ | ||
/* $Id$ */ | ||
|
||
#include "qgsfieldvalidator.h" | ||
|
||
#include <QValidator> | ||
#include <QRegExpValidator> | ||
#include <QDate> | ||
#include <QVariant> | ||
#include <QSettings> | ||
|
||
#include "qgslogger.h" | ||
#include "qgslonglongvalidator.h" | ||
#include "qgsfield.h" | ||
|
||
QgsFieldValidator::QgsFieldValidator( QObject *parent, const QgsField &field ) | ||
: QValidator( parent ) | ||
, mField( field ) | ||
{ | ||
switch ( mField.type() ) | ||
{ | ||
case QVariant::Int: | ||
{ | ||
if ( mField.length() > 0 ) | ||
{ | ||
QString re = QString( "-?\\d{0,%1}" ).arg( mField.length() ); | ||
mValidator = new QRegExpValidator( QRegExp( re ), parent ); | ||
} | ||
else | ||
{ | ||
mValidator = new QIntValidator( parent ); | ||
} | ||
} | ||
break; | ||
|
||
case QVariant::Double: | ||
{ | ||
if ( mField.length() > 0 && mField.precision() > 0 ) | ||
{ | ||
QString re = QString( "-?\\d{0,%1}(\\.\\d{0,%2})?" ).arg( mField.length() - mField.precision() ).arg( mField.precision() ); | ||
mValidator = new QRegExpValidator( QRegExp( re ), parent ); | ||
} | ||
else if ( mField.precision() > 0 ) | ||
{ | ||
QString re = QString( "-?\\d*(\\.\\d{0,%1))?" ).arg( mField.precision() ); | ||
mValidator = new QRegExpValidator( QRegExp( re ), parent ); | ||
} | ||
else | ||
{ | ||
mValidator = new QDoubleValidator( parent ); | ||
} | ||
} | ||
break; | ||
|
||
case QVariant::LongLong : | ||
mValidator = new QgsLongLongValidator( parent ); | ||
break; | ||
|
||
default: | ||
mValidator = 0; | ||
} | ||
|
||
QSettings settings; | ||
mNullValue = settings.value( "qgis/nullValue", "NULL" ).toString(); | ||
} | ||
|
||
QgsFieldValidator::~QgsFieldValidator() | ||
{ | ||
delete mValidator; | ||
} | ||
|
||
QValidator::State QgsFieldValidator::validate( QString &s, int &i ) const | ||
{ | ||
// empty values are considered NULL for numbers and dates and are acceptable | ||
if ( s.isEmpty() && | ||
( mField.type() == QVariant::Double | ||
|| mField.type() == QVariant::Int | ||
|| mField.type() == QVariant::LongLong | ||
|| mField.type() == QVariant::Date | ||
) | ||
) | ||
{ | ||
return Acceptable; | ||
} | ||
|
||
// delegate to the child validator if any | ||
if ( mValidator ) | ||
{ | ||
QValidator::State result = mValidator->validate( s, i ); | ||
return result; | ||
} | ||
else if ( mField.type() == QVariant::String ) | ||
{ | ||
// allow to enter the NULL representation, which might be | ||
// longer than the actual field | ||
if ( mNullValue.size() > 0 && | ||
s.size() > 0 && | ||
s.size() < mNullValue.size() && | ||
s == mNullValue.left( s.size() ) ) | ||
return Intermediate; | ||
|
||
if ( s == mNullValue ) | ||
return Acceptable; | ||
|
||
if ( mField.length() > 0 && s.size() > mField.length() ) | ||
return Invalid; | ||
} | ||
else if ( mField.type() == QVariant::Date ) | ||
{ | ||
return QDate::fromString( s ).isValid() ? Acceptable : Intermediate; | ||
} | ||
else | ||
{ | ||
QgsDebugMsg( "unsupported type for validation" ); | ||
return Invalid; | ||
} | ||
|
||
return Acceptable; | ||
} | ||
|
||
void QgsFieldValidator::fixup( QString &s ) const | ||
{ | ||
if ( mValidator ) | ||
{ | ||
mValidator->fixup( s ); | ||
} | ||
else if ( mField.type() == QVariant::String && mField.length() > 0 && s.size() > mField.length() ) | ||
{ | ||
// if the value is longer, this must be a partial NULL representation | ||
s = mNullValue; | ||
} | ||
else if ( mField.type() == QVariant::Date ) | ||
{ | ||
// invalid dates will also translate to NULL | ||
s = ""; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/*************************************************************************** | ||
qgsfieldvalidator.h - description | ||
------------------- | ||
begin : March 2011 | ||
copyright : (C) 2011 by SunilRajKiran-kCube | ||
email : sunilraj.kiran@kcubeconsulting.com | ||
adapted version of QValidator for QgsField | ||
***************************************************************************/ | ||
|
||
/*************************************************************************** | ||
* * | ||
* This program is free software; you can redistribute it and/or modify * | ||
* it under the terms of the GNU General Public License as published by * | ||
* the Free Software Foundation; either version 2 of the License, or * | ||
* (at your option) any later version. * | ||
* * | ||
***************************************************************************/ | ||
/* $Id$ */ | ||
|
||
#ifndef QGSFIELDVALIDATOR_H | ||
#define QGSFIELDVALIDATOR_H | ||
|
||
#include <QValidator> | ||
#include <QVariant> | ||
#include <QSettings> | ||
#include "qgsfield.h" | ||
|
||
|
||
class GUI_EXPORT QgsFieldValidator : public QValidator | ||
{ | ||
Q_OBJECT | ||
|
||
public: | ||
QgsFieldValidator( QObject *parent, const QgsField &field ); | ||
~QgsFieldValidator(); | ||
|
||
virtual State validate( QString &, int & ) const; | ||
virtual void fixup( QString & ) const; | ||
|
||
private: | ||
// Disables copy constructing | ||
Q_DISABLE_COPY( QgsFieldValidator ) | ||
|
||
QValidator *mValidator; | ||
QgsField mField; | ||
QString mNullValue; | ||
}; | ||
|
||
#endif // QGSFIELDVALIDATOR_H |