QGIS API Documentation  2.12.0-Lyon
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 
28 {
29  return new QgsDateTimeEditWrapper( vl, fieldIdx, editor, parent );
30 }
31 
33 {
34  return new QgsDateTimeEditConfig( vl, fieldIdx, parent );
35 }
36 
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 }
93 
95 {
97  map.insert( QDateTimeEdit::staticMetaObject.className(), 10 );
98  map.insert( QgsDateTimeEdit::staticMetaObject.className(), 10 );
99  return map;
100 }
QString toString(Qt::DateFormat format) const
virtual QMap< const char *, int > supportedWidgetTypes() override
Returns a list of widget types which this editor widget supports.
QgsEditorWidgetWrapper * create(QgsVectorLayer *vl, int fieldIdx, QWidget *editor, QWidget *parent) const override
Override this in your implementation.
This class should be subclassed for every configurable editor widget type.
#define QGSDATETIMEEDIT_DATEFORMAT
QgsDateTimeEditFactory(const QString &name)
QString attribute(const QString &name, const QString &defValue) const
void writeConfig(const QgsEditorWidgetConfig &config, QDomElement &configElement, QDomDocument &doc, const QgsVectorLayer *layer, int fieldIdx) override
Serialize your configuration and save it in a xml doc.
Manages an editor widget Widget and wrapper share the same parent.
Wraps a date time widget.
bool isNull() const
void setAttribute(const QString &name, const QString &value)
Every attribute editor widget needs a factory, which inherits this class.
QString representValue(QgsVectorLayer *vl, int fieldIdx, const QgsEditorWidgetConfig &config, const QVariant &cache, const QVariant &value) const override
Create a pretty String representation of the value.
QDateTime fromString(const QString &string, Qt::DateFormat format)
bool isValid() const
QVariant value(const QString &key, const QVariant &defaultValue) const
QgsEditorWidgetConfig readConfig(const QDomElement &configElement, QgsVectorLayer *layer, int fieldIdx) override
Read the config from an XML file and map it to a proper QgsEditorWidgetConfig.
iterator insert(const Key &key, const T &value)
QgsEditorConfigWidget * configWidget(QgsVectorLayer *vl, int fieldIdx, QWidget *parent) const override
Override this in your implementation.
Represents a vector layer which manages a vector based data sets.
QString toString() const
const T value(const Key &key) const