QGIS API Documentation 3.34.0-Prizren (ffbdd678812)
Loading...
Searching...
No Matches
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
24const QString QgsDateTimeFieldFormatter::DATE_FORMAT = QStringLiteral( "yyyy-MM-dd" );
25const QString QgsDateTimeFieldFormatter::TIME_FORMAT = QStringLiteral( "HH:mm:ss" );
26const QString 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" );
31QString QgsDateTimeFieldFormatter::DATE_DISPLAY_FORMAT = QStringLiteral( "yyyy-MM-dd" );
32QString QgsDateTimeFieldFormatter::DATETIME_DISPLAY_FORMAT = QStringLiteral( "yyyy-MM-dd HH:mm:ss" );
33
34
36{
37 return QStringLiteral( "DateTime" );
38}
39
40QString QgsDateTimeFieldFormatter::representValue( QgsVectorLayer *layer, int fieldIndex, const QVariantMap &config, const QVariant &cache, const QVariant &value ) const
41{
42 Q_UNUSED( cache )
43
44 QString result;
45
46 if ( QgsVariantUtils::isNull( value ) )
47 {
49 }
50
51 if ( fieldIndex < 0 || fieldIndex >= layer->fields().size() )
52 {
53 return value.toString();
54 }
55
56 const QgsField field = layer->fields().at( fieldIndex );
57 const bool fieldIsoFormat = config.value( QStringLiteral( "field_iso_format" ), false ).toBool();
58 const QString fieldFormat = config.value( QStringLiteral( "field_format" ), defaultFormat( field.type() ) ).toString();
59 const QString displayFormat = config.value( QStringLiteral( "display_format" ), defaultDisplayFormat( field.type() ) ).toString();
60
61 QDateTime date;
62 bool showTimeZone = false;
63 if ( static_cast<QMetaType::Type>( value.type() ) == QMetaType::QDate )
64 {
65 date = value.toDateTime();
66 }
67 else if ( static_cast<QMetaType::Type>( value.type() ) == QMetaType::QDateTime )
68 {
69 date = value.toDateTime();
70 // we always show time zones for datetime values
71 showTimeZone = true;
72 }
73 else if ( static_cast<QMetaType::Type>( value.type() ) == QMetaType::QTime )
74 {
75 return value.toTime().toString( displayFormat );
76 }
77 else
78 {
79 if ( fieldIsoFormat )
80 {
81 date = QDateTime::fromString( value.toString(), Qt::ISODate );
82 }
83 else
84 {
85 date = QDateTime::fromString( value.toString(), fieldFormat );
86 }
87 }
88
89 if ( date.isValid() )
90 {
91 if ( showTimeZone && displayFormat == QgsDateTimeFieldFormatter::DATETIME_DISPLAY_FORMAT )
92 {
93 // using default display format for datetimes, so ensure we include the timezone
94 result = QStringLiteral( "%1 (%2)" ).arg( date.toString( displayFormat ), date.timeZoneAbbreviation() );
95 }
96 else
97 {
98 result = date.toString( displayFormat );
99 }
100 }
101 else
102 {
103 result = value.toString();
104 }
105
106 return result;
107}
108
109QString QgsDateTimeFieldFormatter::defaultFormat( QVariant::Type type )
110{
111 switch ( type )
112 {
113 case QVariant::DateTime:
115 case QVariant::Time:
117 default:
119 }
120}
121
123{
124 switch ( type )
125 {
126 case QVariant::DateTime:
128 case QVariant::Time:
130 default:
132 }
133}
134
136{
137 QString dateFormat = QLocale().dateFormat( QLocale::FormatType::ShortFormat );
140}
static QString nullRepresentation()
This string is used to represent the value NULL throughout QGIS.
static void applyLocaleChange()
Adjusts the date time display 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.
static const QString QT_ISO_FORMAT
Date time format was localized by applyLocaleChange before QGIS 3.30.
QString id() const override
Returns a unique id for this field formatter.
static const QString DISPLAY_FOR_ISO_FORMAT
static QString defaultDisplayFormat(QVariant::Type type)
Gets the default display format in function of the type.
static QString DATETIME_DISPLAY_FORMAT
Date display format is localized by applyLocaleChange.
static QString defaultFormat(QVariant::Type type)
Gets the default format in function of the type.
static const QString TIME_FORMAT
Date format was localized by applyLocaleChange before QGIS 3.30.
Encapsulate a field in an attribute table or data source.
Definition qgsfield.h:53
QVariant::Type type
Definition qgsfield.h:60
int size() const
Returns number of items.
QgsField at(int i) const
Returns the field at particular index (must be in range 0..N-1).
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.