QGIS API Documentation  3.26.3-Buenos Aires (65e4edfdad)
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 : [email protected]
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 
23 QString QgsDateTimeFieldFormatter::DATE_FORMAT = QStringLiteral( "yyyy-MM-dd" );
24 const QString QgsDateTimeFieldFormatter::TIME_FORMAT = QStringLiteral( "HH:mm:ss" );
25 QString QgsDateTimeFieldFormatter::DATETIME_FORMAT = QStringLiteral( "yyyy-MM-dd HH:mm:ss" );
26 // we need to use Qt::ISODate rather than a string format definition in QDate::fromString
27 const QString QgsDateTimeFieldFormatter::QT_ISO_FORMAT = QStringLiteral( "Qt ISO Date" );
28 // but QDateTimeEdit::setDisplayFormat only accepts string formats, so use with time zone by default
29 const QString QgsDateTimeFieldFormatter::DISPLAY_FOR_ISO_FORMAT = QStringLiteral( "yyyy-MM-dd HH:mm:ss+t" );
30 
31 
33 {
34  return QStringLiteral( "DateTime" );
35 }
36 
37 QString QgsDateTimeFieldFormatter::representValue( QgsVectorLayer *layer, int fieldIndex, const QVariantMap &config, const QVariant &cache, const QVariant &value ) const
38 {
39  Q_UNUSED( cache )
40 
41  QString result;
42 
43  if ( value.isNull() )
44  {
46  }
47 
48  if ( fieldIndex < 0 || fieldIndex >= layer->fields().size() )
49  {
50  return value.toString();
51  }
52 
53  const QgsField field = layer->fields().at( fieldIndex );
54  const bool fieldIsoFormat = config.value( QStringLiteral( "field_iso_format" ), false ).toBool();
55  const QString fieldFormat = config.value( QStringLiteral( "field_format" ), defaultFormat( field.type() ) ).toString();
56  const QString displayFormat = config.value( QStringLiteral( "display_format" ), defaultFormat( field.type() ) ).toString();
57 
58  QDateTime date;
59  bool showTimeZone = false;
60  if ( static_cast<QMetaType::Type>( value.type() ) == QMetaType::QDate )
61  {
62  date = value.toDateTime();
63  }
64  else if ( static_cast<QMetaType::Type>( value.type() ) == QMetaType::QDateTime )
65  {
66  date = value.toDateTime();
67  // we always show time zones for datetime values
68  showTimeZone = true;
69  }
70  else if ( static_cast<QMetaType::Type>( value.type() ) == QMetaType::QTime )
71  {
72  return value.toTime().toString( displayFormat );
73  }
74  else
75  {
76  if ( fieldIsoFormat )
77  {
78  date = QDateTime::fromString( value.toString(), Qt::ISODate );
79  }
80  else
81  {
82  date = QDateTime::fromString( value.toString(), fieldFormat );
83  }
84  }
85 
86  if ( date.isValid() )
87  {
88  if ( showTimeZone && displayFormat == QgsDateTimeFieldFormatter::DATETIME_FORMAT )
89  {
90  // using default display format for datetimes, so ensure we include the timezone
91  result = QStringLiteral( "%1 (%2)" ).arg( date.toString( displayFormat ), date.timeZoneAbbreviation() );
92  }
93  else
94  {
95  result = date.toString( displayFormat );
96  }
97  }
98  else
99  {
100  result = value.toString();
101  }
102 
103  return result;
104 }
105 
106 QString QgsDateTimeFieldFormatter::defaultFormat( QVariant::Type type )
107 {
108  switch ( type )
109  {
110  case QVariant::DateTime:
112  case QVariant::Time:
114  default:
116  }
117 }
118 
120 {
121  QString dateFormat = QLocale().dateFormat( QLocale::FormatType::ShortFormat );
124 }
QgsDateTimeFieldFormatter::DATETIME_FORMAT
static QString DATETIME_FORMAT
Definition: qgsdatetimefieldformatter.h:50
field
const QgsField & field
Definition: qgsfield.h:463
qgsapplication.h
QgsVectorLayer::fields
QgsFields fields() const FINAL
Returns the list of fields of this layer.
Definition: qgsvectorlayer.cpp:3436
QgsApplication::nullRepresentation
static QString nullRepresentation()
This string is used to represent the value NULL throughout QGIS.
Definition: qgsapplication.cpp:2018
QgsDateTimeFieldFormatter::applyLocaleChange
static void applyLocaleChange()
Adjusts the date time formats according to locale.
Definition: qgsdatetimefieldformatter.cpp:119
QgsFields::size
int size() const
Returns number of items.
Definition: qgsfields.cpp:138
qgsdatetimefieldformatter.h
qgsvectorlayer.h
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:37
QgsDateTimeFieldFormatter::defaultFormat
static QString defaultFormat(QVariant::Type type)
Gets the default format in function of the type.
Definition: qgsdatetimefieldformatter.cpp:106
QgsDateTimeFieldFormatter::DISPLAY_FOR_ISO_FORMAT
static const QString DISPLAY_FOR_ISO_FORMAT
Definition: qgsdatetimefieldformatter.h:52
QgsDateTimeFieldFormatter::DATE_FORMAT
static QString DATE_FORMAT
Definition: qgsdatetimefieldformatter.h:48
QgsVectorLayer
Represents a vector layer which manages a vector based data sets.
Definition: qgsvectorlayer.h:391
qgsfield.h
QgsDateTimeFieldFormatter::QT_ISO_FORMAT
static const QString QT_ISO_FORMAT
Definition: qgsdatetimefieldformatter.h:51
qgssettings.h
QgsDateTimeFieldFormatter::TIME_FORMAT
static const QString TIME_FORMAT
Definition: qgsdatetimefieldformatter.h:49
QgsFields::at
QgsField at(int i) const
Returns the field at particular index (must be in range 0..N-1).
Definition: qgsfields.cpp:163
QgsDateTimeFieldFormatter::id
QString id() const override
Returns a unique id for this field formatter.
Definition: qgsdatetimefieldformatter.cpp:32
QgsField::type
QVariant::Type type
Definition: qgsfield.h:58
QgsField
Encapsulate a field in an attribute table or data source.
Definition: qgsfield.h:50