QGIS API Documentation 3.29.0-Master (da8bb1db43)
qgsdatetimefieldformatter.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsdatetimefieldformatter.cpp - QgsDateTimeFieldFormatter
3
4 ---------------------
5 begin : 2.12.2016
6 copyright : (C) 2016 by Matthias Kuhn
8 ***************************************************************************
9 * *
10 * This program is free software; you can redistribute it and/or modify *
11 * it under the terms of the GNU General Public License as published by *
12 * the Free Software Foundation; either version 2 of the License, or *
13 * (at your option) any later version. *
14 * *
15 ***************************************************************************/
17
18#include "qgssettings.h"
19#include "qgsfield.h"
20#include "qgsvectorlayer.h"
21#include "qgsapplication.h"
22#include "qgsvariantutils.h"
23
24QString QgsDateTimeFieldFormatter::DATE_FORMAT = QStringLiteral( "yyyy-MM-dd" );
25const QString QgsDateTimeFieldFormatter::TIME_FORMAT = QStringLiteral( "HH:mm:ss" );
26QString QgsDateTimeFieldFormatter::DATETIME_FORMAT = QStringLiteral( "yyyy-MM-dd HH:mm:ss" );
27// we need to use Qt::ISODate rather than a string format definition in QDate::fromString
28const QString QgsDateTimeFieldFormatter::QT_ISO_FORMAT = QStringLiteral( "Qt ISO Date" );
29// but QDateTimeEdit::setDisplayFormat only accepts string formats, so use with time zone by default
30const QString QgsDateTimeFieldFormatter::DISPLAY_FOR_ISO_FORMAT = QStringLiteral( "yyyy-MM-dd HH:mm:ss+t" );
31
32
34{
35 return QStringLiteral( "DateTime" );
36}
37
38QString QgsDateTimeFieldFormatter::representValue( QgsVectorLayer *layer, int fieldIndex, const QVariantMap &config, const QVariant &cache, const QVariant &value ) const
39{
40 Q_UNUSED( cache )
41
42 QString result;
43
44 if ( QgsVariantUtils::isNull( value ) )
45 {
47 }
48
49 if ( fieldIndex < 0 || fieldIndex >= layer->fields().size() )
50 {
51 return value.toString();
52 }
53
54 const QgsField field = layer->fields().at( fieldIndex );
55 const bool fieldIsoFormat = config.value( QStringLiteral( "field_iso_format" ), false ).toBool();
56 const QString fieldFormat = config.value( QStringLiteral( "field_format" ), defaultFormat( field.type() ) ).toString();
57 const QString displayFormat = config.value( QStringLiteral( "display_format" ), defaultFormat( field.type() ) ).toString();
58
59 QDateTime date;
60 bool showTimeZone = false;
61 if ( static_cast<QMetaType::Type>( value.type() ) == QMetaType::QDate )
62 {
63 date = value.toDateTime();
64 }
65 else if ( static_cast<QMetaType::Type>( value.type() ) == QMetaType::QDateTime )
66 {
67 date = value.toDateTime();
68 // we always show time zones for datetime values
69 showTimeZone = true;
70 }
71 else if ( static_cast<QMetaType::Type>( value.type() ) == QMetaType::QTime )
72 {
73 return value.toTime().toString( displayFormat );
74 }
75 else
76 {
77 if ( fieldIsoFormat )
78 {
79 date = QDateTime::fromString( value.toString(), Qt::ISODate );
80 }
81 else
82 {
83 date = QDateTime::fromString( value.toString(), fieldFormat );
84 }
85 }
86
87 if ( date.isValid() )
88 {
89 if ( showTimeZone && displayFormat == QgsDateTimeFieldFormatter::DATETIME_FORMAT )
90 {
91 // using default display format for datetimes, so ensure we include the timezone
92 result = QStringLiteral( "%1 (%2)" ).arg( date.toString( displayFormat ), date.timeZoneAbbreviation() );
93 }
94 else
95 {
96 result = date.toString( displayFormat );
97 }
98 }
99 else
100 {
101 result = value.toString();
102 }
103
104 return result;
105}
106
107QString QgsDateTimeFieldFormatter::defaultFormat( QVariant::Type type )
108{
109 switch ( type )
110 {
111 case QVariant::DateTime:
113 case QVariant::Time:
115 default:
117 }
118}
119
121{
122 QString dateFormat = QLocale().dateFormat( QLocale::FormatType::ShortFormat );
125}
static QString nullRepresentation()
This string is used to represent the value NULL throughout QGIS.
static void applyLocaleChange()
Adjusts the date time formats according to locale.
QString representValue(QgsVectorLayer *layer, int fieldIndex, const QVariantMap &config, const QVariant &cache, const QVariant &value) const override
Create a pretty String representation of the value.
QString id() const override
Returns a unique id for this field formatter.
static const QString DISPLAY_FOR_ISO_FORMAT
static QString defaultFormat(QVariant::Type type)
Gets the default format in function of the type.
Encapsulate a field in an attribute table or data source.
Definition: qgsfield.h:51
QVariant::Type type
Definition: qgsfield.h:58
int size() const
Returns number of items.
Definition: qgsfields.cpp:138
QgsField at(int i) const
Returns the field at particular index (must be in range 0..N-1).
Definition: qgsfields.cpp:163
static bool isNull(const QVariant &variant)
Returns true if the specified variant should be considered a NULL value.
Represents a vector layer which manages a vector based data sets.
QgsFields fields() const FINAL
Returns the list of fields of this layer.
const QgsField & field
Definition: qgsfield.h:463