QGIS API Documentation  2.18.21-Las Palmas (9fba24a)
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"
20 
21 #include <QSettings>
22 
24  : QgsEditorWidgetFactory( name )
25 {
26 }
27 
29 {
30  return new QgsDateTimeEditWrapper( vl, fieldIdx, editor, parent );
31 }
32 
34 {
35  return new QgsDateTimeSearchWidgetWrapper( vl, fieldIdx, parent );
36 }
37 
39 {
40  return new QgsDateTimeEditConfig( vl, fieldIdx, parent );
41 }
42 
44 {
45  Q_UNUSED( layer );
46  Q_UNUSED( fieldIdx );
48 
49  cfg.insert( "field_format", configElement.attribute( "field_format" ) );
50  cfg.insert( "display_format", configElement.attribute( "display_format" ) );
51  cfg.insert( "calendar_popup", configElement.attribute( "calendar_popup" ) == "1" );
52  cfg.insert( "allow_null", configElement.attribute( "allow_null" ) == "1" );
53 
54  return cfg;
55 }
56 
57 void QgsDateTimeEditFactory::writeConfig( const QgsEditorWidgetConfig& config, QDomElement& configElement, QDomDocument& doc, const QgsVectorLayer* layer, int fieldIdx )
58 {
59  Q_UNUSED( doc );
60  Q_UNUSED( layer );
61  Q_UNUSED( fieldIdx );
62 
63  configElement.setAttribute( "field_format", config["field_format"].toString() );
64  configElement.setAttribute( "display_format", config["display_format"].toString() );
65  configElement.setAttribute( "calendar_popup", config["calendar_popup"].toBool() );
66  configElement.setAttribute( "allow_null", config["allow_null"].toBool() );
67 }
68 
69 QString QgsDateTimeEditFactory::representValue( QgsVectorLayer* vl, int fieldIdx, const QgsEditorWidgetConfig& config, const QVariant& cache, const QVariant& value ) const
70 {
71  Q_UNUSED( vl )
72  Q_UNUSED( fieldIdx )
73  Q_UNUSED( cache )
74 
75  QString result;
76 
77  if ( value.isNull() )
78  {
79  QSettings settings;
80  return settings.value( "qgis/nullValue", "NULL" ).toString();
81  }
82 
83  const QString displayFormat = config.value( "display_format", QGSDATETIMEEDIT_DATEFORMAT ).toString();
84  const QString fieldFormat = config.value( "field_format", QGSDATETIMEEDIT_DATEFORMAT ).toString();
85 
86  QDateTime date = QDateTime::fromString( value.toString(), fieldFormat );
87 
88  if ( date.isValid() )
89  {
90  result = date.toString( displayFormat );
91  }
92  else
93  {
94  result = value.toString();
95  }
96 
97  return result;
98 }
99 
100 Qt::AlignmentFlag QgsDateTimeEditFactory::alignmentFlag( QgsVectorLayer* vl, int fieldIdx, const QgsEditorWidgetConfig& config ) const
101 {
102  Q_UNUSED( vl );
103  Q_UNUSED( fieldIdx );
104  Q_UNUSED( config );
105 
106  return Qt::AlignLeft;
107 }
108 
110 {
112  map.insert( QDateTimeEdit::staticMetaObject.className(), 10 );
113  map.insert( QgsDateTimeEdit::staticMetaObject.className(), 10 );
114  return map;
115 }
QString toString(Qt::DateFormat format) const
Manages an editor widget Widget and wrapper share the same parent.
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.
Wraps a date/time edit widget for searching.
Qt::AlignmentFlag alignmentFlag(QgsVectorLayer *vl, int fieldIdx, const QgsEditorWidgetConfig &config) const override
Return the alignment for a particular field.
QVariantMap QgsEditorWidgetConfig
Holds a set of configuration parameters for a editor widget wrapper.
bool isNull() const
void setAttribute(const QString &name, const QString &value)
Every attribute editor widget needs a factory, which inherits this class.
QgsSearchWidgetWrapper * createSearchWidget(QgsVectorLayer *vl, int fieldIdx, QWidget *parent) const override
By default a simple QgsFilterLineEdit is returned as search widget.
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