QGIS API Documentation  2.8.2-Wien
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
qgsdatetimeeditfactory.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsdatetimeeditfactory.cpp
3  --------------------------------------
4  Date : 03.2014
5  Copyright : (C) 2014 Denis Rouzaud
6  Email : [email protected]
7  ***************************************************************************
8  * *
9  * This program is free software; you can redistribute it and/or modify *
10  * it under the terms of the GNU General Public License as published by *
11  * the Free Software Foundation; either version 2 of the License, or *
12  * (at your option) any later version. *
13  * *
14  ***************************************************************************/
15 
16 #include "qgsdatetimeeditfactory.h"
17 #include "qgsdatetimeeditconfig.h"
18 #include "qgsdatetimeeditwrapper.h"
19 
20 #include <QSettings>
21 
24 {
25 }
26 
27 QgsEditorWidgetWrapper *QgsDateTimeEditFactory::create( QgsVectorLayer *vl, int fieldIdx, QWidget *editor, QWidget *parent ) const
28 {
29  return new QgsDateTimeEditWrapper( vl, fieldIdx, editor, parent );
30 }
31 
33 {
34  return new QgsDateTimeEditConfig( vl, fieldIdx, parent );
35 }
36 
37 QgsEditorWidgetConfig QgsDateTimeEditFactory::readConfig( const QDomElement& configElement, QgsVectorLayer* layer, int fieldIdx )
38 {
39  Q_UNUSED( layer );
40  Q_UNUSED( fieldIdx );
42 
43  cfg.insert( "field_format", configElement.attribute( "field_format" ) );
44  cfg.insert( "display_format", configElement.attribute( "display_format" ) );
45  cfg.insert( "calendar_popup", configElement.attribute( "calendar_popup" ) == "1" );
46  cfg.insert( "allow_null", configElement.attribute( "allow_null" ) == "1" );
47 
48  return cfg;
49 }
50 
51 void QgsDateTimeEditFactory::writeConfig( const QgsEditorWidgetConfig& config, QDomElement& configElement, QDomDocument& doc, const QgsVectorLayer* layer, int fieldIdx )
52 {
53  Q_UNUSED( doc );
54  Q_UNUSED( layer );
55  Q_UNUSED( fieldIdx );
56 
57  configElement.setAttribute( "field_format", config["field_format"].toString() );
58  configElement.setAttribute( "display_format", config["display_format"].toString() );
59  configElement.setAttribute( "calendar_popup", config["calendar_popup"].toBool() );
60  configElement.setAttribute( "allow_null", config["allow_null"].toBool() );
61 }
62 
63 QString QgsDateTimeEditFactory::representValue( QgsVectorLayer* vl, int fieldIdx, const QgsEditorWidgetConfig& config, const QVariant& cache, const QVariant& value ) const
64 {
65  Q_UNUSED( vl )
66  Q_UNUSED( fieldIdx )
67  Q_UNUSED( cache )
68 
69  QString result;
70 
71  if ( value.isNull() )
72  {
73  QSettings settings;
74  return settings.value( "qgis/nullValue", "NULL" ).toString();
75  }
76 
77  const QString displayFormat = config.value( "display_format", QGSDATETIMEEDIT_DATEFORMAT ).toString();
78  const QString fieldFormat = config.value( "field_format", QGSDATETIMEEDIT_DATEFORMAT ).toString();
79 
80  QDateTime date = QDateTime::fromString( value.toString(), fieldFormat );
81 
82  if ( date.isValid() )
83  {
84  result = date.toString( displayFormat );
85  }
86  else
87  {
88  result = value.toString();
89  }
90 
91  return result;
92 }