QGIS API Documentation  2.18.21-Las Palmas (9fba24a)
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 "qgsfield.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 
50 {
51  return readConfig( configElement, layer, fieldIdx );
52 }
53 
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()->defaultValue( vl->fields().fieldOriginIndex( fieldIdx ), true ).toString();
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 
111 {
112  Q_UNUSED( configElement );
113  Q_UNUSED( layer );
114  Q_UNUSED( fieldIdx );
115 
116  return QgsEditorWidgetConfig();
117 }
118 
119 bool QgsEditorWidgetFactory::isFieldSupported( QgsVectorLayer* vl, int fieldIdx )
120 {
121  Q_UNUSED( vl )
122  Q_UNUSED( fieldIdx )
123  return true;
124 }
125 
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: qgsfield.cpp:448
static QString toString(FilterFlag flag)
Returns a translated string representing a filter flag.
field comes from the underlying data provider of the vector layer (originIndex = index in provider&#39;s ...
Definition: qgsfield.h:259
QgsEditorWidgetFactory(const QString &name)
Constructor.
virtual QVariant createCache(QgsVectorLayer *vl, int fieldIdx, const QgsEditorWidgetConfig &config)
Create a cache for a given field.
const QgsField & at(int i) const
Get field at particular index (must be in range 0..N-1)
Definition: qgsfield.cpp:422
QgsFields fields() const
Returns the list of fields of this layer.
int fieldOriginIndex(int fieldIdx) const
Get field&#39;s origin index (its meaning is specific to each type of origin)
Definition: qgsfield.cpp:456
const char * name() const
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 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:188
virtual QVariant defaultValue(int fieldId, bool forceLazyEval=false)
Returns the default value for field specified by fieldId.
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 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.
QObject * parent() const
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:97