QGIS API Documentation
4.3.0-Master (16eddb2670e)
Toggle main menu visibility
Loading...
Searching...
No Matches
src
core
fieldformatter
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
7
email : matthias@opengis.ch
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
***************************************************************************/
16
#include "
qgsdatetimefieldformatter.h
"
17
18
#include "
qgsapplication.h
"
19
#include "
qgsfield.h
"
20
#include "
qgssettings.h
"
21
#include "
qgsvariantutils.h
"
22
#include "
qgsvectorlayer.h
"
23
24
#include <QString>
25
26
using namespace
Qt::StringLiterals;
27
28
const
QString
QgsDateTimeFieldFormatter::DATE_FORMAT
= u
"yyyy-MM-dd"
_s;
29
const
QString
QgsDateTimeFieldFormatter::TIME_FORMAT
= u
"HH:mm:ss"
_s;
30
const
QString
QgsDateTimeFieldFormatter::DATETIME_FORMAT
= u
"yyyy-MM-dd HH:mm:ss"
_s;
31
// we need to use Qt::ISODate rather than a string format definition in QDate::fromString
32
const
QString
QgsDateTimeFieldFormatter::QT_ISO_FORMAT
= u
"Qt ISO Date"
_s;
33
// but QDateTimeEdit::setDisplayFormat only accepts string formats, so use with time zone by default
34
const
QString
QgsDateTimeFieldFormatter::DISPLAY_FOR_ISO_FORMAT
= u
"yyyy-MM-dd HH:mm:ss+t"
_s;
35
QString
QgsDateTimeFieldFormatter::DATE_DISPLAY_FORMAT
= u
"yyyy-MM-dd"
_s;
36
QString
QgsDateTimeFieldFormatter::DATETIME_DISPLAY_FORMAT
= u
"yyyy-MM-dd HH:mm:ss"
_s;
37
38
39
QString
QgsDateTimeFieldFormatter::id
()
const
40
{
41
return
u
"DateTime"
_s;
42
}
43
44
QString
QgsDateTimeFieldFormatter::representValue
(
QgsVectorLayer
*layer,
int
fieldIndex,
const
QVariantMap &config,
const
QVariant &cache,
const
QVariant &value )
const
45
{
46
Q_UNUSED( cache )
47
48
QString result;
49
50
if
(
QgsVariantUtils::isNull
( value ) )
51
{
52
return
QgsApplication::nullRepresentation
();
53
}
54
55
if
( fieldIndex < 0 || fieldIndex >= layer->
fields
().
size
() )
56
{
57
return
value.toString();
58
}
59
60
const
QgsField
field = layer->
fields
().
at
( fieldIndex );
61
const
bool
fieldIsoFormat = config.value( u
"field_iso_format"
_s,
false
).toBool();
62
const
QString fieldFormat = config.value( u
"field_format"
_s,
defaultFormat
( field.
type
() ) ).toString();
63
const
QString displayFormat = config.value( u
"display_format"
_s,
defaultDisplayFormat
( field.
type
() ) ).toString();
64
65
QDateTime date;
66
bool
showTimeZone =
false
;
67
if
(
static_cast<
QMetaType::Type
>
( value.userType() ) == QMetaType::QDate )
68
{
69
date = value.toDateTime();
70
}
71
else
if
(
static_cast<
QMetaType::Type
>
( value.userType() ) == QMetaType::QDateTime )
72
{
73
date = value.toDateTime();
74
// we always show time zones for datetime values
75
showTimeZone =
true
;
76
}
77
else
if
(
static_cast<
QMetaType::Type
>
( value.userType() ) == QMetaType::QTime )
78
{
79
return
value.toTime().toString( displayFormat );
80
}
81
else
82
{
83
if
( fieldIsoFormat )
84
{
85
date = QDateTime::fromString( value.toString(), Qt::ISODate );
86
}
87
else
88
{
89
date = QDateTime::fromString( value.toString(), fieldFormat );
90
}
91
}
92
93
if
( date.isValid() )
94
{
95
if
( showTimeZone && displayFormat ==
QgsDateTimeFieldFormatter::DATETIME_DISPLAY_FORMAT
)
96
{
97
// using default display format for datetimes, so ensure we include the timezone
98
result = u
"%1 (%2)"
_s.arg( date.toString( displayFormat ), date.timeZoneAbbreviation() );
99
}
100
else
101
{
102
// Convert to UTC if the format string includes a Z, as QLocale::toString() doesn't do it
103
if
( displayFormat.indexOf(
"Z"
) > 0 )
104
date = date.toUTC();
105
106
result = date.toString( displayFormat );
107
}
108
}
109
else
110
{
111
result = value.toString();
112
}
113
114
return
result;
115
}
116
117
QString
QgsDateTimeFieldFormatter::defaultFormat
( QMetaType::Type type )
118
{
119
switch
( type )
120
{
121
case
QMetaType::Type::QDateTime:
122
return
QgsDateTimeFieldFormatter::DATETIME_FORMAT
;
123
case
QMetaType::Type::QTime:
124
return
QgsDateTimeFieldFormatter::TIME_FORMAT
;
125
default
:
126
return
QgsDateTimeFieldFormatter::DATE_FORMAT
;
127
}
128
}
129
130
QString
QgsDateTimeFieldFormatter::defaultFormat
( QVariant::Type type )
131
{
132
return
defaultFormat
(
QgsVariantUtils::variantTypeToMetaType
( type ) );
133
}
134
135
136
QString
QgsDateTimeFieldFormatter::defaultDisplayFormat
( QMetaType::Type type )
137
{
138
switch
( type )
139
{
140
case
QMetaType::Type::QDateTime:
141
return
QgsDateTimeFieldFormatter::DATETIME_DISPLAY_FORMAT
;
142
case
QMetaType::Type::QTime:
143
return
QgsDateTimeFieldFormatter::TIME_FORMAT
;
144
default
:
145
return
QgsDateTimeFieldFormatter::DATE_DISPLAY_FORMAT
;
146
}
147
}
148
149
QString
QgsDateTimeFieldFormatter::defaultDisplayFormat
( QVariant::Type type )
150
{
151
return
defaultDisplayFormat
(
QgsVariantUtils::variantTypeToMetaType
( type ) );
152
}
153
154
void
QgsDateTimeFieldFormatter::applyLocaleChange
()
155
{
156
QString dateFormat = QLocale().dateFormat( QLocale::FormatType::ShortFormat );
157
QgsDateTimeFieldFormatter::DATETIME_DISPLAY_FORMAT
= QString(
"%1 %2"
).arg( dateFormat,
QgsDateTimeFieldFormatter::TIME_FORMAT
);
158
QgsDateTimeFieldFormatter::DATE_DISPLAY_FORMAT
= dateFormat;
159
}
QgsApplication::nullRepresentation
static QString nullRepresentation()
Returns the string used to represent the value NULL throughout QGIS.
Definition
qgsapplication.cpp:2227
QgsDateTimeFieldFormatter::DATE_FORMAT
static const QString DATE_FORMAT
Definition
qgsdatetimefieldformatter.h:33
QgsDateTimeFieldFormatter::applyLocaleChange
static void applyLocaleChange()
Adjusts the date time display formats according to locale.
Definition
qgsdatetimefieldformatter.cpp:154
QgsDateTimeFieldFormatter::representValue
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.
Definition
qgsdatetimefieldformatter.cpp:44
QgsDateTimeFieldFormatter::QT_ISO_FORMAT
static const QString QT_ISO_FORMAT
Date time format was localized by applyLocaleChange before QGIS 3.30.
Definition
qgsdatetimefieldformatter.h:36
QgsDateTimeFieldFormatter::DATE_DISPLAY_FORMAT
static QString DATE_DISPLAY_FORMAT
Definition
qgsdatetimefieldformatter.h:38
QgsDateTimeFieldFormatter::id
QString id() const override
Returns a unique id for this field formatter.
Definition
qgsdatetimefieldformatter.cpp:39
QgsDateTimeFieldFormatter::DISPLAY_FOR_ISO_FORMAT
static const QString DISPLAY_FOR_ISO_FORMAT
Definition
qgsdatetimefieldformatter.h:37
QgsDateTimeFieldFormatter::defaultDisplayFormat
static QString defaultDisplayFormat(QMetaType::Type type)
Gets the default display format in function of the type.
Definition
qgsdatetimefieldformatter.cpp:136
QgsDateTimeFieldFormatter::defaultFormat
static QString defaultFormat(QMetaType::Type type)
Gets the default format in function of the type.
Definition
qgsdatetimefieldformatter.cpp:117
QgsDateTimeFieldFormatter::DATETIME_FORMAT
static const QString DATETIME_FORMAT
Definition
qgsdatetimefieldformatter.h:35
QgsDateTimeFieldFormatter::DATETIME_DISPLAY_FORMAT
static QString DATETIME_DISPLAY_FORMAT
Date display format is localized by applyLocaleChange.
Definition
qgsdatetimefieldformatter.h:39
QgsDateTimeFieldFormatter::TIME_FORMAT
static const QString TIME_FORMAT
Date format was localized by applyLocaleChange before QGIS 3.30.
Definition
qgsdatetimefieldformatter.h:34
QgsField
Encapsulate a field in an attribute table or data source.
Definition
qgsfield.h:56
QgsField::type
QMetaType::Type type
Definition
qgsfield.h:63
QgsFields::size
int size() const
Returns number of items.
Definition
qgsfields.cpp:184
QgsFields::at
QgsField at(int i) const
Returns the field at particular index (must be in range 0..N-1).
Definition
qgsfields.cpp:209
QgsVariantUtils::variantTypeToMetaType
static QMetaType::Type variantTypeToMetaType(QVariant::Type variantType)
Converts a QVariant::Type to a QMetaType::Type.
Definition
qgsvariantutils.cpp:501
QgsVariantUtils::isNull
static bool isNull(const QVariant &variant, bool silenceNullWarnings=false)
Returns true if the specified variant should be considered a NULL value.
Definition
qgsvariantutils.cpp:176
QgsVectorLayer
Represents a vector layer which manages a vector based dataset.
Definition
qgsvectorlayer.h:414
QgsVectorLayer::fields
QgsFields fields
Definition
qgsvectorlayer.h:422
qgsapplication.h
qgsdatetimefieldformatter.h
qgsfield.h
qgssettings.h
qgsvariantutils.h
qgsvectorlayer.h
Generated on
for QGIS API Documentation by
1.17.0