Skip to content
Permalink
Browse files

Add isNumeric and QML bindings to QgsField

  • Loading branch information
m-kuhn committed Aug 17, 2016
1 parent 1391aad commit de41c747161a8a7181cb4535163b21922872816a
Showing with 54 additions and 1 deletion.
  1. +8 −0 python/core/qgsfield.sip
  2. +1 −1 src/core/CMakeLists.txt
  3. +5 −0 src/core/qgsfield.cpp
  4. +16 −0 src/core/qgsfield.h
  5. +24 −0 tests/src/core/testqgsfield.cpp
@@ -73,6 +73,14 @@ class QgsField
*/
QString comment() const;

/**
* Returns if this field is numeric. Any integer or floating point type
* will return true for this.
*
* @note added in QGIS 2.18
*/
bool isNumeric() const;

/**
* Set the field name.
* @param name Name of the field
@@ -460,6 +460,7 @@ SET(QGIS_CORE_MOC_HDRS
qgsdataprovider.h
qgsdbfilterproxymodel.h
qgseditformconfig.h
qgsfield.h
qgsgeometryvalidator.h
qgsgml.h
qgsgmlschema.h
@@ -648,7 +649,6 @@ SET(QGIS_CORE_HDRS
qgsfeatureiterator.h
qgsfeaturerequest.h
qgsfeaturestore.h
qgsfield.h
qgsfield_p.h
qgsfontutils.h
qgsgeometrycache.h
@@ -111,6 +111,11 @@ QString QgsField::comment() const
return d->comment;
}

bool QgsField::isNumeric() const
{
return d->type == QVariant::Double || d->type == QVariant::Int || d->type == QVariant::UInt || d->type == QVariant::LongLong || d->type == QVariant::ULongLong;
}

/***************************************************************************
* This class is considered CRITICAL and any change MUST be accompanied with
* full unit tests in testqgsfield.cpp.
@@ -43,6 +43,14 @@ class QgsFieldsPrivate;

class CORE_EXPORT QgsField
{
Q_GADGET

Q_PROPERTY( bool isNumeric READ isNumeric )
Q_PROPERTY( int length READ length )
Q_PROPERTY( int precision READ precision )
Q_PROPERTY( QString comment READ comment )
Q_PROPERTY( QString name READ name )

public:
/** Constructor. Constructs a new QgsField object.
* @param name Field name
@@ -107,6 +115,14 @@ class CORE_EXPORT QgsField
*/
QString comment() const;

/**
* Returns if this field is numeric. Any integer or floating point type
* will return true for this.
*
* @note added in QGIS 2.18
*/
bool isNumeric() const;

/**
* Set the field name.
* @param name Name of the field
@@ -34,6 +34,7 @@ class TestQgsField: public QObject
void copy();// test cpy destruction (double delete)
void assignment();
void gettersSetters(); //test getters and setters
void isNumeric(); //test isNumeric
void equality(); //test equality operators
void asVariant(); //test conversion to and from a QVariant
void displayString();
@@ -117,6 +118,29 @@ void TestQgsField::gettersSetters()
QCOMPARE( field.comment(), QString( "comment" ) );
}

void TestQgsField::isNumeric()
{
QgsField field;
field.setType( QVariant::Int );
QVERIFY( field.isNumeric() );
field.setType( QVariant::UInt );
QVERIFY( field.isNumeric() );
field.setType( QVariant::Double );
QVERIFY( field.isNumeric() );
field.setType( QVariant::LongLong );
QVERIFY( field.isNumeric() );
field.setType( QVariant::ULongLong );
QVERIFY( field.isNumeric() );
field.setType( QVariant::String );
QVERIFY( !field.isNumeric() );
field.setType( QVariant::DateTime );
QVERIFY( !field.isNumeric() );
field.setType( QVariant::Bool );
QVERIFY( !field.isNumeric() );
field.setType( QVariant::Invalid );
QVERIFY( !field.isNumeric() );
}

void TestQgsField::equality()
{
QgsField field1;

0 comments on commit de41c74

Please sign in to comment.
You can’t perform that action at this time.