QGIS API Documentation  3.14.0-Pi (9f7028fd23)
qgseditorwidgetautoconf.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgseditorwidgetautoconf.cpp
3  ---------------------
4  begin : July 2016
5  copyright : (C) 2016 by Patrick Valsecchi
6  email : patrick.valsecchi at camptocamp.com
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  ***************************************************************************/
17 #include "qgsvectordataprovider.h"
18 #include "qgsgui.h"
19 
28 {
29  public:
30  QgsEditorWidgetSetup editorWidgetSetup( const QgsVectorLayer *vl, const QString &fieldName, int &score ) const override
31  {
32  int bestScore = 0;
33  QString bestType;
34  const QMap<QString, QgsEditorWidgetFactory *> factories = QgsGui::editorWidgetRegistry()->factories();
35  for ( QMap<QString, QgsEditorWidgetFactory *>::const_iterator i = factories.begin(); i != factories.end(); ++i )
36  {
37  const int index = vl->fields().lookupField( fieldName );
38  if ( index >= 0 )
39  {
40  const int score = i.value()->fieldScore( vl, index );
41  if ( score > bestScore )
42  {
43  bestType = i.key();
44  bestScore = score;
45  }
46  }
47  }
48  if ( bestScore > 0 )
49  {
50  score = 10;
51  return QgsEditorWidgetSetup( bestType, QVariantMap() );
52  }
53  return QgsEditorWidgetSetup();
54  }
55 };
56 
57 
66 {
67  public:
68  QgsEditorWidgetSetup editorWidgetSetup( const QgsVectorLayer *vl, const QString &fieldName, int &score ) const override
69  {
70  QgsField field = vl->fields().field( fieldName );
71  if ( !field.editorWidgetSetup().isNull() )
72  {
73  score = 20;
74  return field.editorWidgetSetup();
75  }
76  else
77  {
78  return QgsEditorWidgetSetup();
79  }
80  }
81 };
82 
84 QgsEditorWidgetAutoConf::QgsEditorWidgetAutoConf()
85 {
86  registerPlugin( new FromFactoriesPlugin() );
87  registerPlugin( new FromDbTablePlugin() );
88 }
89 
90 QgsEditorWidgetSetup QgsEditorWidgetAutoConf::editorWidgetSetup( const QgsVectorLayer *vl, const QString &fieldName ) const
91 {
92  QgsEditorWidgetSetup result( QStringLiteral( "TextEdit" ), QVariantMap() );
93 
94  int fieldIndex = vl->fields().indexFromName( fieldName );
95  if ( fieldIndex >= 0 )
96  {
97 
98  if ( vl->fields().fieldOrigin( fieldIndex ) == QgsFields::OriginProvider )
99  {
100  // important check - for provider fields, we CANNOT use auto configured widgets if the field
101  // uses a default value clause - otherwise the widget will obliterate the default value clause
102  // (e.g., by trying to convert it to a number/date/etc). Instead we have to use a text edit
103  // widget so that the clause remains intact
104  int providerOrigin = vl->fields().fieldOriginIndex( fieldIndex );
105  if ( !vl->dataProvider()->defaultValueClause( providerOrigin ).isEmpty() )
106  return result;
107  }
108 
109  int bestScore = 0;
110  for ( const std::shared_ptr<QgsEditorWidgetAutoConfPlugin> &cur : mPlugins )
111  {
112  int score = 0;
113  const QgsEditorWidgetSetup curResult = cur->editorWidgetSetup( vl, fieldName, score );
114  if ( score > bestScore )
115  {
116  result = curResult;
117  bestScore = score;
118  }
119  }
120  }
121 
122  return result;
123 }
124 
125 void QgsEditorWidgetAutoConf::registerPlugin( QgsEditorWidgetAutoConfPlugin *plugin )
126 {
127  mPlugins.append( std::shared_ptr<QgsEditorWidgetAutoConfPlugin>( plugin ) );
128 }
QgsFields::OriginProvider
@ OriginProvider
Field comes from the underlying data provider of the vector layer (originIndex = index in provider's ...
Definition: qgsfields.h:51
QgsGui::editorWidgetRegistry
static QgsEditorWidgetRegistry * editorWidgetRegistry()
Returns the global editor widget registry, used for managing all known edit widget factories.
Definition: qgsgui.cpp:73
QgsVectorLayer::dataProvider
QgsVectorDataProvider * dataProvider() FINAL
Returns the layer's data provider, it may be nullptr.
Definition: qgsvectorlayer.cpp:627
QgsEditorWidgetSetup
Definition: qgseditorwidgetsetup.h:28
qgsgui.h
QgsEditorWidgetRegistry::factories
QMap< QString, QgsEditorWidgetFactory * > factories()
Gets access to all registered factories.
Definition: qgseditorwidgetregistry.cpp:167
QgsVectorLayer::fields
QgsFields fields() const FINAL
Returns the list of fields of this layer.
Definition: qgsvectorlayer.cpp:3280
QgsEditorWidgetAutoConfPlugin
Definition: qgseditorwidgetautoconf.h:32
QgsFields::fieldOrigin
FieldOrigin fieldOrigin(int fieldIdx) const
Gets field's origin (value from an enumeration)
Definition: qgsfields.cpp:189
qgsvectordataprovider.h
QgsFields::field
QgsField field(int fieldIdx) const
Gets field at particular index (must be in range 0..N-1)
Definition: qgsfields.cpp:168
QgsEditorWidgetSetup::isNull
bool isNull() const
Definition: qgseditorwidgetsetup.h:69
FromFactoriesPlugin::editorWidgetSetup
QgsEditorWidgetSetup editorWidgetSetup(const QgsVectorLayer *vl, const QString &fieldName, int &score) const override
Typical scores are:
Definition: qgseditorwidgetautoconf.cpp:43
qgseditorwidgetregistry.h
FromDbTablePlugin::editorWidgetSetup
QgsEditorWidgetSetup editorWidgetSetup(const QgsVectorLayer *vl, const QString &fieldName, int &score) const override
Typical scores are:
Definition: qgseditorwidgetautoconf.cpp:68
QgsVectorLayer
Definition: qgsvectorlayer.h:385
QgsFields::fieldOriginIndex
int fieldOriginIndex(int fieldIdx) const
Gets field's origin index (its meaning is specific to each type of origin)
Definition: qgsfields.cpp:197
QgsVectorDataProvider::defaultValueClause
virtual QString defaultValueClause(int fieldIndex) const
Returns any default value clauses which are present at the provider for a specified field index.
Definition: qgsvectordataprovider.cpp:142
FromDbTablePlugin
Definition: qgseditorwidgetautoconf.cpp:65
qgseditorwidgetautoconf.h
QgsFields::lookupField
int lookupField(const QString &fieldName) const
Looks up field's index from the field name.
Definition: qgsfields.cpp:324
QgsField::editorWidgetSetup
QgsEditorWidgetSetup editorWidgetSetup() const
Gets the editor widget setup for the field.
Definition: qgsfield.cpp:496
QgsFields::indexFromName
int indexFromName(const QString &fieldName) const
Gets the field index from the field name.
Definition: qgsfields.cpp:202
FromFactoriesPlugin
Definition: qgseditorwidgetautoconf.cpp:27
QgsField
Definition: qgsfield.h:49