Skip to content
Permalink
Browse files

add QgsField::isDateTime (#9007)

* add QgsField::isDateTime

* use QgsField::isDateTime

* rename to isDateOrTime
  • Loading branch information
3nids committed Jan 28, 2019
1 parent e97649a commit 3a19182fe8ca1c0faf91eaeb4b0a846a624872fe
@@ -136,6 +136,13 @@ Returns if this field is numeric. Any integer or floating point type
will return true for this.

.. versionadded:: 2.18
%End

bool isDateOrTime() const;
%Docstring
Returns if this field is a date and/or time type.

.. versionadded:: 3.6
%End

void setName( const QString &name );
@@ -127,6 +127,11 @@ 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;
}

bool QgsField::isDateOrTime() const
{
return d->type == QVariant::Date || d->type == QVariant::Time || d->type == QVariant::DateTime;
}

/***************************************************************************
* This class is considered CRITICAL and any change MUST be accompanied with
* full unit tests in testqgsfield.cpp.
@@ -50,6 +50,7 @@ class CORE_EXPORT QgsField
Q_GADGET

Q_PROPERTY( bool isNumeric READ isNumeric )
Q_PROPERTY( bool isDateOrTime READ isDateOrTime )
Q_PROPERTY( int length READ length WRITE setLength )
Q_PROPERTY( int precision READ precision WRITE setPrecision )
Q_PROPERTY( QVariant::Type type READ type WRITE setType )
@@ -159,6 +160,13 @@ class CORE_EXPORT QgsField
*/
bool isNumeric() const;

/**
* Returns if this field is a date and/or time type.
*
* \since QGIS 3.6
*/
bool isDateOrTime() const;

/**
* Set the field name.
* \param name Name of the field
@@ -15,6 +15,7 @@
***************************************************************************/
#include "qgsfieldformatter.h"

#include "qgsfield.h"
#include "qgsfields.h"
#include "qgsvectorlayer.h"
#include "qgsvectordataprovider.h"
@@ -49,11 +50,8 @@ Qt::AlignmentFlag QgsFieldFormatter::alignmentFlag( QgsVectorLayer *layer, int f
{
Q_UNUSED( config );

QVariant::Type fldType = layer->fields().at( fieldIndex ).type();
bool alignRight = ( fldType == QVariant::Int || fldType == QVariant::Double || fldType == QVariant::LongLong
|| fldType == QVariant::DateTime || fldType == QVariant::Date || fldType == QVariant::Time );

if ( alignRight )
QgsField field = layer->fields().at( fieldIndex );
if ( field.isNumeric() || field.isDateOrTime() )
return Qt::AlignRight;
else
return Qt::AlignLeft;
@@ -52,9 +52,8 @@ QHash<const char *, int> QgsDateTimeEditFactory::supportedWidgetTypes()
unsigned int QgsDateTimeEditFactory::fieldScore( const QgsVectorLayer *vl, int fieldIdx ) const
{
const QgsField field = vl->fields().field( fieldIdx );
const QVariant::Type type = field.type();
const QVariantMap config = field.editorWidgetSetup().config();
if ( type == QVariant::DateTime || type == QVariant::Date || type == QVariant::Time || config.contains( QStringLiteral( "field_format" ) ) )
if ( field.isDateOrTime() || config.contains( QStringLiteral( "field_format" ) ) )
{
return 20;
}
@@ -40,6 +40,7 @@ class TestQgsField: public QObject
void assignment();
void gettersSetters(); //test getters and setters
void isNumeric(); //test isNumeric
void isDateTime(); //test isNumeric
void equality(); //test equality operators
void asVariant(); //test conversion to and from a QVariant
void displayString();
@@ -199,6 +200,33 @@ void TestQgsField::isNumeric()
QVERIFY( !field.isNumeric() );
}

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

void TestQgsField::equality()
{
QgsField field1;

0 comments on commit 3a19182

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