QGIS API Documentation  3.2.0-Bonn (bc43194)
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 
22 const QString QgsDateTimeFieldFormatter::DATE_FORMAT = QStringLiteral( "yyyy-MM-dd" );
23 const QString QgsDateTimeFieldFormatter::TIME_FORMAT = QStringLiteral( "HH:mm:ss" );
24 const QString QgsDateTimeFieldFormatter::DATETIME_FORMAT = QStringLiteral( "yyyy-MM-dd HH:mm:ss" );
25 // we need to use Qt::ISODate rather than a string format definition in QDate::fromString
26 const QString QgsDateTimeFieldFormatter::QT_ISO_FORMAT = QStringLiteral( "Qt ISO Date" );
27 // but QDateTimeEdit::setDisplayFormat only accepts string formats, so use with time zone by default
28 const QString QgsDateTimeFieldFormatter::DISPLAY_FOR_ISO_FORMAT = QStringLiteral( "yyyy-MM-dd HH:mm:ss+t" );
29 
30 
32 {
33  return QStringLiteral( "DateTime" );
34 }
35 
36 QString QgsDateTimeFieldFormatter::representValue( QgsVectorLayer *layer, int fieldIndex, const QVariantMap &config, const QVariant &cache, const QVariant &value ) const
37 {
38  Q_UNUSED( cache )
39 
40  QString result;
41 
42  if ( value.isNull() )
43  {
45  }
46 
47  const QgsField field = layer->fields().at( fieldIndex );
48  const bool fieldIsoFormat = config.value( QStringLiteral( "field_iso_format" ), false ).toBool();
49  const QString fieldFormat = config.value( QStringLiteral( "field_format" ), defaultFormat( field.type() ) ).toString();
50  const QString displayFormat = config.value( QStringLiteral( "display_format" ), defaultFormat( field.type() ) ).toString();
51 
52  QDateTime date;
53  if ( fieldIsoFormat )
54  {
55  date = QDateTime::fromString( value.toString(), Qt::ISODate );
56  }
57  else
58  {
59  date = QDateTime::fromString( value.toString(), fieldFormat );
60  }
61 
62  if ( date.isValid() )
63  {
64  result = date.toString( displayFormat );
65  }
66  else
67  {
68  result = value.toString();
69  }
70 
71  return result;
72 }
73 
74 QString QgsDateTimeFieldFormatter::defaultFormat( QVariant::Type type )
75 {
76  switch ( type )
77  {
78  case QVariant::DateTime:
80  break;
81  case QVariant::Time:
83  break;
84  default:
86  }
87 }
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 DISPLAY_FOR_ISO_FORMAT
QgsField at(int i) const
Gets field at particular index (must be in range 0..N-1)
Definition: qgsfields.cpp:145
static QString defaultFormat(QVariant::Type type)
Gets the default format in function of the type.
QgsFields fields() const override
Returns the list of fields of this layer.
static const QString DATETIME_FORMAT
QString id() const override
Returns a unique id for this field formatter.
static QString nullRepresentation()
This string is used to represent the value NULL throughout QGIS.
Encapsulate a field in an attribute table or data source.
Definition: qgsfield.h:48
Represents a vector layer which manages a vector based data sets.
QVariant::Type type
Definition: qgsfield.h:55