QGIS API Documentation  2.99.0-Master (8ec3eaf)
qgseditorwidgetfactory.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgseditorwidgetfactory.cpp
3  --------------------------------------
4  Date : 21.4.2013
5  Copyright : (C) 2013 Matthias Kuhn
6  Email : matthias at opengis dot ch
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 "qgseditorwidgetfactory.h"
18 #include "qgssearchwidgetwrapper.h"
19 #include "qgsfields.h"
20 #include "qgsvectordataprovider.h"
21 
22 #include <QSettings>
23 
25 
27  : mName( name )
28 {
29 }
30 
32 {
33 }
34 
40 {
41  return new QgsDefaultSearchWidgetWrapper( vl, fieldIdx, parent );
42 }
43 
45 {
46  return mName;
47 }
48 
49 QgsEditorWidgetConfig QgsEditorWidgetFactory::readEditorConfig( const QDomElement& configElement, QgsVectorLayer* layer, int fieldIdx )
50 {
51  return readConfig( configElement, layer, fieldIdx );
52 }
53 
54 void QgsEditorWidgetFactory::writeConfig( const QgsEditorWidgetConfig& config, QDomElement& configElement, QDomDocument& doc, const QgsVectorLayer* layer, int fieldIdx )
55 {
56  Q_UNUSED( config );
57  Q_UNUSED( configElement );
58  Q_UNUSED( doc );
59  Q_UNUSED( layer );
60  Q_UNUSED( fieldIdx );
61 }
62 
63 QString QgsEditorWidgetFactory::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( config )
68  Q_UNUSED( cache )
69  Q_UNUSED( value )
70 
71  QString defVal;
72  if ( vl->fields().fieldOrigin( fieldIdx ) == QgsFields::OriginProvider && vl->dataProvider() )
73  defVal = vl->dataProvider()->defaultValueClause( vl->fields().fieldOriginIndex( fieldIdx ) );
74 
75  return value == defVal ? defVal : vl->fields().at( fieldIdx ).displayString( value );
76 }
77 
78 QVariant QgsEditorWidgetFactory::sortValue( QgsVectorLayer* vl, int fieldIdx, const QgsEditorWidgetConfig& config, const QVariant& cache, const QVariant& value ) const
79 {
80  Q_UNUSED( vl )
81  Q_UNUSED( fieldIdx )
82  Q_UNUSED( config )
83  Q_UNUSED( cache )
84  return value;
85 }
86 
87 Qt::AlignmentFlag QgsEditorWidgetFactory::alignmentFlag( QgsVectorLayer* vl, int fieldIdx, const QgsEditorWidgetConfig& config ) const
88 {
89  Q_UNUSED( config );
90 
91  QVariant::Type fldType = vl->fields().at( fieldIdx ).type();
92  bool alignRight = ( fldType == QVariant::Int || fldType == QVariant::Double || fldType == QVariant::LongLong
93  || fldType == QVariant::DateTime || fldType == QVariant::Date || fldType == QVariant::Time );
94 
95  if ( alignRight )
96  return Qt::AlignRight;
97  else
98  return Qt::AlignLeft;
99 }
100 
102 {
103  Q_UNUSED( vl )
104  Q_UNUSED( fieldIdx )
105  Q_UNUSED( config )
106 
107  return QVariant();
108 }
109 
110 QgsEditorWidgetConfig QgsEditorWidgetFactory::readConfig( const QDomElement& configElement, QgsVectorLayer* layer, int fieldIdx )
111 {
112  Q_UNUSED( configElement );
113  Q_UNUSED( layer );
114  Q_UNUSED( fieldIdx );
115 
116  return QgsEditorWidgetConfig();
117 }
118 
119 unsigned int QgsEditorWidgetFactory::fieldScore( const QgsVectorLayer* vl, int fieldIdx ) const
120 {
121  Q_UNUSED( vl )
122  Q_UNUSED( fieldIdx )
123  return 5;
124 }
125 
126 void QgsEditorWidgetFactory::config2xml( const QgsEditorWidgetConfig& config, QDomElement& configElement, const QString& fieldName )
127 {
128  const QVariant value = config.value( fieldName );
129  if ( value.isValid() )
130  {
131  if ( value.type() == QVariant::Bool )
132  {
133  configElement.setAttribute( fieldName, value.toBool() ? "1" : "0" );
134  }
135  else
136  {
137  configElement.setAttribute( fieldName, value.toString() );
138  }
139  }
140 }
141 
142 void QgsEditorWidgetFactory::xml2config( const QDomElement& configElement, QgsEditorWidgetConfig& config, const QString& fieldName )
143 {
144  const QString value = configElement.attribute( fieldName );
145  if ( !value.isNull() )
146  {
147  config.insert( fieldName, value );
148  }
149 }
Manages an editor widget Widget and wrapper share the same parent.
FieldOrigin fieldOrigin(int fieldIdx) const
Get field&#39;s origin (value from an enumeration)
Definition: qgsfields.cpp:163
static void config2xml(const QgsEditorWidgetConfig &config, QDomElement &configElement, const QString &fieldName)
Copy the given config element to a XML attribute.
Field comes from the underlying data provider of the vector layer (originIndex = index in provider&#39;s ...
Definition: qgsfields.h:43
QgsEditorWidgetFactory(const QString &name)
Constructor.
static void xml2config(const QDomElement &configElement, QgsEditorWidgetConfig &config, const QString &fieldName)
Copy the given XML attribute to the configuration element.
virtual QVariant createCache(QgsVectorLayer *vl, int fieldIdx, const QgsEditorWidgetConfig &config)
Create a cache for a given field.
QgsFields fields() const
Returns the list of fields of this layer.
QgsField at(int i) const
Get field at particular index (must be in range 0..N-1)
Definition: qgsfields.cpp:137
int fieldOriginIndex(int fieldIdx) const
Get field&#39;s origin index (its meaning is specific to each type of origin)
Definition: qgsfields.cpp:171
QVariantMap QgsEditorWidgetConfig
Holds a set of configuration parameters for a editor widget wrapper.
virtual QVariant sortValue(QgsVectorLayer *vl, int fieldIdx, const QgsEditorWidgetConfig &config, const QVariant &cache, const QVariant &value) const
If the default sort order should be overwritten for this widget, you can transform the value in here...
virtual QString defaultValueClause(int fieldIndex) const
Returns any default value clauses which are present at the provider for a specified field index...
virtual QString representValue(QgsVectorLayer *vl, int fieldIdx, const QgsEditorWidgetConfig &config, const QVariant &cache, const QVariant &value) const
Create a pretty String representation of the value.
QString displayString(const QVariant &v) const
Formats string for display.
Definition: qgsfield.cpp:209
virtual QgsSearchWidgetWrapper * createSearchWidget(QgsVectorLayer *vl, int fieldIdx, QWidget *parent) const
By default a simple QgsFilterLineEdit is returned as search widget.
virtual Qt::AlignmentFlag alignmentFlag(QgsVectorLayer *vl, int fieldIdx, const QgsEditorWidgetConfig &config) const
Return the alignment for a particular field.
QString name()
Return The human readable identifier name of this widget type.
virtual unsigned int fieldScore(const QgsVectorLayer *vl, int fieldIdx) const
This method allows disabling this editor widget type for a certain field.
virtual QgsEditorWidgetConfig readConfig(const QDomElement &configElement, QgsVectorLayer *layer, int fieldIdx)
Read the config from an XML file and map it to a proper QgsEditorWidgetConfig.
QgsDefaultSearchWidgetWrapper(QgsVectorLayer *vl, int fieldIdx, QWidget *parent=nullptr)
QgsEditorWidgetConfig readEditorConfig(const QDomElement &configElement, QgsVectorLayer *layer, int fieldIdx)
Read the config from an XML file and map it to a proper QgsEditorWidgetConfig.
virtual void writeConfig(const QgsEditorWidgetConfig &config, QDomElement &configElement, QDomDocument &doc, const QgsVectorLayer *layer, int fieldIdx)
Serialize your configuration and save it in a xml doc.
QgsVectorDataProvider * dataProvider()
Returns the data provider.
QgsEditorWidgetConfig config() const
Returns the whole config.
QgsVectorLayer * layer() const
Access the QgsVectorLayer, you are working on.
Represents a vector layer which manages a vector based data sets.
QVariant::Type type() const
Gets variant type of the field as it will be retrieved from data source.
Definition: qgsfield.cpp:98