QGIS API Documentation 3.37.0-Master (fdefdf9c27f)
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 ***************************************************************************/
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 const 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
84QgsEditorWidgetAutoConf::QgsEditorWidgetAutoConf()
85{
86 registerPlugin( new FromFactoriesPlugin() );
87 registerPlugin( new FromDbTablePlugin() );
88}
89
90QgsEditorWidgetSetup QgsEditorWidgetAutoConf::editorWidgetSetup( const QgsVectorLayer *vl, const QString &fieldName ) const
91{
92 QgsEditorWidgetSetup result( QStringLiteral( "TextEdit" ), QVariantMap() );
93
94 const 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 const 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
125void QgsEditorWidgetAutoConf::registerPlugin( QgsEditorWidgetAutoConfPlugin *plugin )
126{
127 mPlugins.append( std::shared_ptr<QgsEditorWidgetAutoConfPlugin>( plugin ) );
128}
Widget auto conf plugin that reads the widget setup to use from what the data provider says.
QgsEditorWidgetSetup editorWidgetSetup(const QgsVectorLayer *vl, const QString &fieldName, int &score) const override
Typical scores are:
Widget auto conf plugin that guesses what widget type to use in function of what the widgets support.
QgsEditorWidgetSetup editorWidgetSetup(const QgsVectorLayer *vl, const QString &fieldName, int &score) const override
Typical scores are:
Base class for plugins allowing to pick automatically a widget type for editing fields.
QMap< QString, QgsEditorWidgetFactory * > factories()
Gets access to all registered factories.
Holder for the widget type and its configuration for a field.
Encapsulate a field in an attribute table or data source.
Definition: qgsfield.h:53
QgsEditorWidgetSetup editorWidgetSetup() const
Gets the editor widget setup for the field.
Definition: qgsfield.cpp:714
int indexFromName(const QString &fieldName) const
Gets the field index from the field name.
Definition: qgsfields.cpp:202
@ OriginProvider
Field comes from the underlying data provider of the vector layer (originIndex = index in provider's ...
Definition: qgsfields.h:51
FieldOrigin fieldOrigin(int fieldIdx) const
Returns the field's origin (value from an enumeration).
Definition: qgsfields.cpp:189
QgsField field(int fieldIdx) const
Returns the field at particular index (must be in range 0..N-1).
Definition: qgsfields.cpp:168
int fieldOriginIndex(int fieldIdx) const
Returns the field's origin index (its meaning is specific to each type of origin).
Definition: qgsfields.cpp:197
int lookupField(const QString &fieldName) const
Looks up field's index from the field name.
Definition: qgsfields.cpp:359
static QgsEditorWidgetRegistry * editorWidgetRegistry()
Returns the global editor widget registry, used for managing all known edit widget factories.
Definition: qgsgui.cpp:89
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.
QgsFields fields() const FINAL
Returns the list of fields of this layer.
QgsVectorDataProvider * dataProvider() FINAL
Returns the layer's data provider, it may be nullptr.