Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Add isNumeric and QML bindings to QgsField
  • Loading branch information
m-kuhn committed Aug 17, 2016
1 parent 34e2bea commit af09478
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 1 deletion.
8 changes: 8 additions & 0 deletions python/core/qgsfield.sip
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/core/CMakeLists.txt
Expand Up @@ -455,6 +455,7 @@ SET(QGIS_CORE_MOC_HDRS
qgsdbfilterproxymodel.h
qgseditformconfig.h
qgsfeedback.h
qgsfield.h
qgsgeometryvalidator.h
qgsgml.h
qgsgmlschema.h
Expand Down Expand Up @@ -644,7 +645,6 @@ SET(QGIS_CORE_HDRS
qgsfeatureiterator.h
qgsfeaturerequest.h
qgsfeaturestore.h
qgsfield.h
qgsfield_p.h
qgsfontutils.h
qgsgeometrycache.h
Expand Down
5 changes: 5 additions & 0 deletions src/core/qgsfield.cpp
Expand Up @@ -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.
Expand Down
16 changes: 16 additions & 0 deletions src/core/qgsfield.h
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
24 changes: 24 additions & 0 deletions tests/src/core/testqgsfield.cpp
Expand Up @@ -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();
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit af09478

Please sign in to comment.