Skip to content
Permalink
Browse files
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
  • Loading branch information
jef committed Mar 22, 2011
1 parent c6ee44c commit 71c6454e954256d2a5deb465e513a8eecec05487
@@ -33,5 +33,7 @@
%Include qgstextannotationitem.sip
%Include qgsvertexmarker.sip
%Include qgssearchquerybuilder.sip
%Include qgsattributeeditor.sip
%Include qgsfieldvalidator.sip

%Include symbology-ng-gui.sip
@@ -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 );
};
@@ -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;
};
@@ -35,6 +35,7 @@ qgsdetaileditemwidget.cpp
qgsdetaileditemdata.cpp
qgsencodingfiledialog.cpp
qgsfiledropedit.cpp
qgsfieldvalidator.cpp
qgsformannotationitem.cpp
qgsgenericprojectionselector.cpp
qgsmapcanvas.cpp
@@ -89,6 +90,7 @@ qgsdetaileditemwidget.h
qgslegendinterface.h
qgisinterface.h
qgsencodingfiledialog.h
qgsfieldvalidator.h
qgsformannotationitem.h
qgsgenericprojectionselector.h
qgsmapcanvas.h
@@ -206,6 +208,8 @@ qgsrubberband.h
qgsvertexmarker.h
qgsmaptip.h
qgssearchquerybuilder.h
qgsattributeeditor.h
qgsfieldvalidator.h

${CMAKE_CURRENT_BINARY_DIR}/../ui/ui_qgsdetaileditemwidgetbase.h
${CMAKE_CURRENT_BINARY_DIR}/../ui/ui_qgsgenericprojectionselectorbase.h
@@ -23,6 +23,7 @@
#include <qgscategorizedsymbolrendererv2.h>
#include <qgssymbol.h>
#include <qgslonglongvalidator.h>
#include <qgsfieldvalidator.h>

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

if ( myFieldType == QVariant::Int )
{
le->setValidator( new QIntValidator( le ) );
}
else if ( myFieldType == QVariant::LongLong )
{
le->setValidator( new QgsLongLongValidator( le ) );
}
else if ( myFieldType == QVariant::Double )
{
le->setValidator( new QDoubleValidator( le ) );
}

le->setValidator( new QgsFieldValidator( le, field ) );
myWidget = le;
}

@@ -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 = "";
}
}
@@ -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

0 comments on commit 71c6454

Please sign in to comment.