QGIS API Documentation 4.3.0-Master (91d4a333d5d)
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
21#include <QString>
22
23using namespace Qt::StringLiterals;
24
33{
34 public:
35 QgsEditorWidgetSetup editorWidgetSetup( const QgsVectorLayer *vl, const QString &fieldName, int &score ) const override
36 {
37 int bestScore = 0;
38 QString bestType;
39 const QMap<QString, QgsEditorWidgetFactory *> factories = QgsGui::editorWidgetRegistry()->factories();
40 for ( QMap<QString, QgsEditorWidgetFactory *>::const_iterator i = factories.begin(); i != factories.end(); ++i )
41 {
42 const int index = vl->fields().lookupField( fieldName );
43 if ( index >= 0 )
44 {
45 const int score = i.value()->fieldScore( vl, index );
46 if ( score > bestScore )
47 {
48 bestType = i.key();
49 bestScore = score;
50 }
51 }
52 }
53 if ( bestScore > 0 )
54 {
55 score = 10;
56 return QgsEditorWidgetSetup( bestType, QVariantMap() );
57 }
58 return QgsEditorWidgetSetup();
59 }
60};
61
62
71{
72 public:
73 QgsEditorWidgetSetup editorWidgetSetup( const QgsVectorLayer *vl, const QString &fieldName, int &score ) const override
74 {
75 const QgsField field = vl->fields().field( fieldName );
76 if ( !field.editorWidgetSetup().isNull() )
77 {
78 score = 20;
79 return field.editorWidgetSetup();
80 }
81 else
82 {
83 return QgsEditorWidgetSetup();
84 }
85 }
86};
87
89QgsEditorWidgetAutoConf::QgsEditorWidgetAutoConf()
90{
91 registerPlugin( new FromFactoriesPlugin() );
92 registerPlugin( new FromDbTablePlugin() );
93}
94
95QgsEditorWidgetSetup QgsEditorWidgetAutoConf::editorWidgetSetup( const QgsVectorLayer *vl, const QString &fieldName ) const
96{
97 QgsEditorWidgetSetup result( u"TextEdit"_s, QVariantMap() );
98
99 const int fieldIndex = vl->fields().indexFromName( fieldName );
100 if ( fieldIndex >= 0 )
101 {
102 if ( vl->fields().fieldOrigin( fieldIndex ) == Qgis::FieldOrigin::Provider )
103 {
104 // important check - for provider fields, we CANNOT use auto configured widgets if the field
105 // uses a default value clause - otherwise the widget will obliterate the default value clause
106 // (e.g., by trying to convert it to a number/date/etc). Instead we have to use a text edit
107 // widget so that the clause remains intact
108 const int providerOrigin = vl->fields().fieldOriginIndex( fieldIndex );
109 if ( !vl->dataProvider()->defaultValueClause( providerOrigin ).isEmpty() )
110 return result;
111 }
112
113 int bestScore = 0;
114 for ( const std::shared_ptr<QgsEditorWidgetAutoConfPlugin> &cur : mPlugins )
115 {
116 int score = 0;
117 const QgsEditorWidgetSetup curResult = cur->editorWidgetSetup( vl, fieldName, score );
118 if ( score > bestScore )
119 {
120 result = curResult;
121 bestScore = score;
122 }
123 }
124 }
125
126 return result;
127}
128
129void QgsEditorWidgetAutoConf::registerPlugin( QgsEditorWidgetAutoConfPlugin *plugin )
130{
131 mPlugins.append( std::shared_ptr<QgsEditorWidgetAutoConfPlugin>( plugin ) );
132}
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:1876
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:56
QgsEditorWidgetSetup editorWidgetSetup() const
Gets the editor widget setup for the field.
Definition qgsfield.cpp:754
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) or -1 if no field e...
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:110
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.