QGIS API Documentation  2.17.0-Master (872e6d2)
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 ) ).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.
QString displayString(const QVariant &v) const
Formats string for display.
Definition: qgsfield.cpp:155
QgsFields fields() const
Returns the list of fields of this layer.
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:200
QgsEditorWidgetFactory(const QString &name)
Constructor.
virtual QVariant createCache(QgsVectorLayer *vl, int fieldIdx, const QgsEditorWidgetConfig &config)
Create a cache for a given field.
const char * name() const
QVariantMap QgsEditorWidgetConfig
Holds a set of configuration parameters for a editor widget wrapper.
int fieldOriginIndex(int fieldIdx) const
Get field&#39;s origin index (its meaning is specific to each type of origin)
Definition: qgsfield.cpp:419
const QgsField & at(int i) const
Get field at particular index (must be in range 0..N-1)
Definition: qgsfield.cpp:385
virtual Qt::AlignmentFlag alignmentFlag(QgsVectorLayer *vl, int fieldIdx, const QgsEditorWidgetConfig &config) const
Return the alignment for a particular field.
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...
QString name()
Return The human readable identifier name of this widget type.
virtual QVariant defaultValue(int fieldId)
Returns the default value for field specified by fieldId.
virtual QgsEditorWidgetConfig readConfig(const QDomElement &configElement, QgsVectorLayer *layer, int fieldIdx)
Read the config from an XML file and map it to a proper QgsEditorWidgetConfig.
QgsVectorLayer * layer() const
Access the QgsVectorLayer, you are working on.
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.
FieldOrigin fieldOrigin(int fieldIdx) const
Get field&#39;s origin (value from an enumeration)
Definition: qgsfield.cpp:411
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.
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.
QgsEditorWidgetConfig config() const
Returns the whole config.
QgsVectorDataProvider * dataProvider()
Returns the data provider.
QObject * parent() const
Represents a vector layer which manages a vector based data sets.
virtual QgsSearchWidgetWrapper * createSearchWidget(QgsVectorLayer *vl, int fieldIdx, QWidget *parent) const
By default a simple QgsFilterLineEdit is returned as search widget.
QVariant::Type type() const
Gets variant type of the field as it will be retrieved from data source.
Definition: qgsfield.cpp:89