QGIS API Documentation 3.41.0-Master (af5edcb665c)
Loading...
Searching...
No Matches
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 if ( vl->fields().fieldOrigin( fieldIndex ) == Qgis::FieldOrigin::Provider )
98 {
99 // important check - for provider fields, we CANNOT use auto configured widgets if the field
100 // uses a default value clause - otherwise the widget will obliterate the default value clause
101 // (e.g., by trying to convert it to a number/date/etc). Instead we have to use a text edit
102 // widget so that the clause remains intact
103 const int providerOrigin = vl->fields().fieldOriginIndex( fieldIndex );
104 if ( !vl->dataProvider()->defaultValueClause( providerOrigin ).isEmpty() )
105 return result;
106 }
107
108 int bestScore = 0;
109 for ( const std::shared_ptr<QgsEditorWidgetAutoConfPlugin> &cur : mPlugins )
110 {
111 int score = 0;
112 const QgsEditorWidgetSetup curResult = cur->editorWidgetSetup( vl, fieldName, score );
113 if ( score > bestScore )
114 {
115 result = curResult;
116 bestScore = score;
117 }
118 }
119 }
120
121 return result;
122}
123
124void QgsEditorWidgetAutoConf::registerPlugin( QgsEditorWidgetAutoConfPlugin *plugin )
125{
126 mPlugins.append( std::shared_ptr<QgsEditorWidgetAutoConfPlugin>( plugin ) );
127}
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:
@ Provider
Field originates from the underlying data provider of the vector layer.
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:740
Q_INVOKABLE int indexFromName(const QString &fieldName) const
Gets the field index from the field name.
QgsField field(int fieldIdx) const
Returns the field at particular index (must be in range 0..N-1).
Qgis::FieldOrigin fieldOrigin(int fieldIdx) const
Returns the field's origin (value from an enumeration).
int fieldOriginIndex(int fieldIdx) const
Returns the field's origin index (its meaning is specific to each type of origin).
Q_INVOKABLE int lookupField(const QString &fieldName) const
Looks up field's index from the field name.
static QgsEditorWidgetRegistry * editorWidgetRegistry()
Returns the global editor widget registry, used for managing all known edit widget factories.
Definition qgsgui.cpp:95
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.
QgsVectorDataProvider * dataProvider() FINAL
Returns the layer's data provider, it may be nullptr.