QGIS API Documentation  3.4.15-Madeira (e83d02e274)
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  Q_FOREACH ( std::shared_ptr<QgsEditorWidgetAutoConfPlugin> cur, plugins )
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  plugins.append( std::shared_ptr<QgsEditorWidgetAutoConfPlugin>( plugin ) );
128 }
Widget auto conf plugin that guesses what widget type to use in function of what the widgets support...
Widget auto conf plugin that reads the widget setup to use from what the data provider says...
Base class for plugins allowing to pick automatically a widget type for editing fields.
Field comes from the underlying data provider of the vector layer (originIndex = index in provider&#39;s ...
Definition: qgsfields.h:49
QMap< QString, QgsEditorWidgetFactory * > factories()
Gets access to all registered factories.
QgsFields fields() const FINAL
Returns the list of fields of this layer.
static QgsEditorWidgetRegistry * editorWidgetRegistry()
Returns the global editor widget registry, used for managing all known edit widget factories...
Definition: qgsgui.cpp:58
int fieldOriginIndex(int fieldIdx) const
Gets field&#39;s origin index (its meaning is specific to each type of origin)
Definition: qgsfields.cpp:197
Encapsulate a field in an attribute table or data source.
Definition: qgsfield.h:48
int lookupField(const QString &fieldName) const
Looks up field&#39;s index from the field name.
Definition: qgsfields.cpp:320
QgsField field(int fieldIdx) const
Gets field at particular index (must be in range 0..N-1)
Definition: qgsfields.cpp:168
int indexFromName(const QString &fieldName) const
Gets the field index from the field name.
Definition: qgsfields.cpp:202
Holder for the widget type and its configuration for a field.
FieldOrigin fieldOrigin(int fieldIdx) const
Gets field&#39;s origin (value from an enumeration)
Definition: qgsfields.cpp:189
QgsEditorWidgetSetup editorWidgetSetup(const QgsVectorLayer *vl, const QString &fieldName, int &score) const override
Typical scores are:
QgsEditorWidgetSetup editorWidgetSetup(const QgsVectorLayer *vl, const QString &fieldName, int &score) const override
Typical scores are:
QgsVectorDataProvider * dataProvider() FINAL
Returns the layer&#39;s data provider.
virtual QString defaultValueClause(int fieldIndex) const
Returns any default value clauses which are present at the provider for a specified field index...
Represents a vector layer which manages a vector based data sets.
QgsEditorWidgetSetup editorWidgetSetup() const
Gets the editor widget setup for the field.
Definition: qgsfield.cpp:429