QGIS API Documentation 3.99.0-Master (2fe06baccd8)
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 ***************************************************************************/
16
18#include "qgsgui.h"
20
29{
30 public:
31 QgsEditorWidgetSetup editorWidgetSetup( const QgsVectorLayer *vl, const QString &fieldName, int &score ) const override
32 {
33 int bestScore = 0;
34 QString bestType;
35 const QMap<QString, QgsEditorWidgetFactory *> factories = QgsGui::editorWidgetRegistry()->factories();
36 for ( QMap<QString, QgsEditorWidgetFactory *>::const_iterator i = factories.begin(); i != factories.end(); ++i )
37 {
38 const int index = vl->fields().lookupField( fieldName );
39 if ( index >= 0 )
40 {
41 const int score = i.value()->fieldScore( vl, index );
42 if ( score > bestScore )
43 {
44 bestType = i.key();
45 bestScore = score;
46 }
47 }
48 }
49 if ( bestScore > 0 )
50 {
51 score = 10;
52 return QgsEditorWidgetSetup( bestType, QVariantMap() );
53 }
54 return QgsEditorWidgetSetup();
55 }
56};
57
58
67{
68 public:
69 QgsEditorWidgetSetup editorWidgetSetup( const QgsVectorLayer *vl, const QString &fieldName, int &score ) const override
70 {
71 const QgsField field = vl->fields().field( fieldName );
72 if ( !field.editorWidgetSetup().isNull() )
73 {
74 score = 20;
75 return field.editorWidgetSetup();
76 }
77 else
78 {
79 return QgsEditorWidgetSetup();
80 }
81 }
82};
83
85QgsEditorWidgetAutoConf::QgsEditorWidgetAutoConf()
86{
87 registerPlugin( new FromFactoriesPlugin() );
88 registerPlugin( new FromDbTablePlugin() );
89}
90
91QgsEditorWidgetSetup QgsEditorWidgetAutoConf::editorWidgetSetup( const QgsVectorLayer *vl, const QString &fieldName ) const
92{
93 QgsEditorWidgetSetup result( QStringLiteral( "TextEdit" ), QVariantMap() );
94
95 const int fieldIndex = vl->fields().indexFromName( fieldName );
96 if ( fieldIndex >= 0 )
97 {
98 if ( vl->fields().fieldOrigin( fieldIndex ) == Qgis::FieldOrigin::Provider )
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:
@ Provider
Field originates from the underlying data provider of the vector layer.
Definition qgis.h:1706
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.
bool isNull() const
Returns true if there is no widget configured.
Encapsulate a field in an attribute table or data source.
Definition qgsfield.h:54
QgsEditorWidgetSetup editorWidgetSetup() const
Gets the editor widget setup for the field.
Definition qgsfield.cpp:747
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:106
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 dataset.
QgsVectorDataProvider * dataProvider() final
Returns the layer's data provider, it may be nullptr.