Skip to content

Commit 3a19182

Browse files
authoredJan 28, 2019
add QgsField::isDateTime (#9007)
* add QgsField::isDateTime * use QgsField::isDateTime * rename to isDateOrTime
1 parent e97649a commit 3a19182

File tree

6 files changed

+52
-7
lines changed

6 files changed

+52
-7
lines changed
 

‎python/core/auto_generated/qgsfield.sip.in

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,13 @@ Returns if this field is numeric. Any integer or floating point type
136136
will return true for this.
137137

138138
.. versionadded:: 2.18
139+
%End
140+
141+
bool isDateOrTime() const;
142+
%Docstring
143+
Returns if this field is a date and/or time type.
144+
145+
.. versionadded:: 3.6
139146
%End
140147

141148
void setName( const QString &name );

‎src/core/qgsfield.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,11 @@ bool QgsField::isNumeric() const
127127
return d->type == QVariant::Double || d->type == QVariant::Int || d->type == QVariant::UInt || d->type == QVariant::LongLong || d->type == QVariant::ULongLong;
128128
}
129129

130+
bool QgsField::isDateOrTime() const
131+
{
132+
return d->type == QVariant::Date || d->type == QVariant::Time || d->type == QVariant::DateTime;
133+
}
134+
130135
/***************************************************************************
131136
* This class is considered CRITICAL and any change MUST be accompanied with
132137
* full unit tests in testqgsfield.cpp.

‎src/core/qgsfield.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ class CORE_EXPORT QgsField
5050
Q_GADGET
5151

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

163+
/**
164+
* Returns if this field is a date and/or time type.
165+
*
166+
* \since QGIS 3.6
167+
*/
168+
bool isDateOrTime() const;
169+
162170
/**
163171
* Set the field name.
164172
* \param name Name of the field

‎src/core/qgsfieldformatter.cpp

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
***************************************************************************/
1616
#include "qgsfieldformatter.h"
1717

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

52-
QVariant::Type fldType = layer->fields().at( fieldIndex ).type();
53-
bool alignRight = ( fldType == QVariant::Int || fldType == QVariant::Double || fldType == QVariant::LongLong
54-
|| fldType == QVariant::DateTime || fldType == QVariant::Date || fldType == QVariant::Time );
55-
56-
if ( alignRight )
53+
QgsField field = layer->fields().at( fieldIndex );
54+
if ( field.isNumeric() || field.isDateOrTime() )
5755
return Qt::AlignRight;
5856
else
5957
return Qt::AlignLeft;

‎src/gui/editorwidgets/qgsdatetimeeditfactory.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,8 @@ QHash<const char *, int> QgsDateTimeEditFactory::supportedWidgetTypes()
5252
unsigned int QgsDateTimeEditFactory::fieldScore( const QgsVectorLayer *vl, int fieldIdx ) const
5353
{
5454
const QgsField field = vl->fields().field( fieldIdx );
55-
const QVariant::Type type = field.type();
5655
const QVariantMap config = field.editorWidgetSetup().config();
57-
if ( type == QVariant::DateTime || type == QVariant::Date || type == QVariant::Time || config.contains( QStringLiteral( "field_format" ) ) )
56+
if ( field.isDateOrTime() || config.contains( QStringLiteral( "field_format" ) ) )
5857
{
5958
return 20;
6059
}

‎tests/src/core/testqgsfield.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ class TestQgsField: public QObject
4040
void assignment();
4141
void gettersSetters(); //test getters and setters
4242
void isNumeric(); //test isNumeric
43+
void isDateTime(); //test isNumeric
4344
void equality(); //test equality operators
4445
void asVariant(); //test conversion to and from a QVariant
4546
void displayString();
@@ -199,6 +200,33 @@ void TestQgsField::isNumeric()
199200
QVERIFY( !field.isNumeric() );
200201
}
201202

203+
void TestQgsField::isDateTime()
204+
{
205+
QgsField field;
206+
field.setType( QVariant::Int );
207+
QVERIFY( !field.isDateOrTime() );
208+
field.setType( QVariant::UInt );
209+
QVERIFY( !field.isDateOrTime() );
210+
field.setType( QVariant::Double );
211+
QVERIFY( !field.isDateOrTime() );
212+
field.setType( QVariant::LongLong );
213+
QVERIFY( !field.isDateOrTime() );
214+
field.setType( QVariant::ULongLong );
215+
QVERIFY( !field.isDateOrTime() );
216+
field.setType( QVariant::String );
217+
QVERIFY( !field.isDateOrTime() );
218+
field.setType( QVariant::DateTime );
219+
QVERIFY( field.isDateOrTime() );
220+
field.setType( QVariant::Time );
221+
QVERIFY( field.isDateOrTime() );
222+
field.setType( QVariant::Date );
223+
QVERIFY( field.isDateOrTime() );
224+
field.setType( QVariant::Bool );
225+
QVERIFY( !field.isDateOrTime() );
226+
field.setType( QVariant::Invalid );
227+
QVERIFY( !field.isDateOrTime() );
228+
}
229+
202230
void TestQgsField::equality()
203231
{
204232
QgsField field1;

0 commit comments

Comments
 (0)
Please sign in to comment.