Navigation Menu

Skip to content

Commit

Permalink
add QgsField::isDateTime (#9007)
Browse files Browse the repository at this point in the history
* add QgsField::isDateTime

* use QgsField::isDateTime

* rename to isDateOrTime
  • Loading branch information
3nids committed Jan 28, 2019
1 parent e97649a commit 3a19182
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 7 deletions.
7 changes: 7 additions & 0 deletions python/core/auto_generated/qgsfield.sip.in
Expand Up @@ -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 );
Expand Down
5 changes: 5 additions & 0 deletions src/core/qgsfield.cpp
Expand Up @@ -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.
Expand Down
8 changes: 8 additions & 0 deletions src/core/qgsfield.h
Expand Up @@ -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 )
Expand Down Expand Up @@ -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
Expand Down
8 changes: 3 additions & 5 deletions src/core/qgsfieldformatter.cpp
Expand Up @@ -15,6 +15,7 @@
***************************************************************************/
#include "qgsfieldformatter.h"

#include "qgsfield.h"
#include "qgsfields.h"
#include "qgsvectorlayer.h"
#include "qgsvectordataprovider.h"
Expand Down Expand Up @@ -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;
Expand Down
3 changes: 1 addition & 2 deletions src/gui/editorwidgets/qgsdatetimeeditfactory.cpp
Expand Up @@ -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;
}
Expand Down
28 changes: 28 additions & 0 deletions tests/src/core/testqgsfield.cpp
Expand Up @@ -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();
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 3a19182

Please sign in to comment.