QGIS API Documentation 3.99.0-Master (26c88405ac0)
Loading...
Searching...
No Matches
qgslistwidgetfactory.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgslistwidgetfactory.cpp
3 --------------------------------------
4 Date : 09.2016
5 Copyright : (C) 2016 Patrick Valsecchi
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 ***************************************************************************/
15
17
19#include "qgsfields.h"
20#include "qgslistconfigdlg.h"
22#include "qgsvectorlayer.h"
23
24#include <QSettings>
25#include <QVariant>
26
31
32QgsEditorWidgetWrapper *QgsListWidgetFactory::create( QgsVectorLayer *vl, int fieldIdx, QWidget *editor, QWidget *parent ) const
33{
34 return new QgsListWidgetWrapper( vl, fieldIdx, editor, parent );
35}
36
38{
39 return new QgsListConfigDlg( vl, fieldIdx, parent );
40}
41
42unsigned int QgsListWidgetFactory::fieldScore( const QgsVectorLayer *vl, int fieldIdx ) const
43{
44 const QgsField field = vl->fields().field( fieldIdx );
45 // Check if this is a JSON field misinterpreted as a map
46 if ( field.type() == QMetaType::Type::QVariantMap && ( field.typeName().compare( QStringLiteral( "JSON" ), Qt::CaseSensitivity::CaseInsensitive ) == 0 || field.subType() == QMetaType::Type::QString ) )
47 {
48 // Look the first not-null value (limiting to the first 20 features) and check if it is really an array
49 const int MAX_FEATURE_LIMIT { 20 };
52 req.setSubsetOfAttributes( { fieldIdx } );
53 req.setLimit( MAX_FEATURE_LIMIT );
54 QgsFeature f;
55 QgsFeatureIterator featureIt { vl->getFeatures( req ) };
56 // The counter is an extra safety measure in case the provider does not respect the limit
57 int featureCount = 0;
58 while ( featureIt.nextFeature( f ) )
59 {
60 ++featureCount;
61 if ( featureCount > MAX_FEATURE_LIMIT )
62 {
63 break;
64 }
65 // Get attribute value and check if it is a valid JSON array
66 const QVariant value( f.attribute( fieldIdx ) );
67 if ( !value.isNull() )
68 {
69 switch ( value.type() )
70 {
71 case QVariant::Type::List:
72 {
73 return 20;
74 }
75 default:
76 case QVariant::Type::String:
77 {
78 const QJsonDocument doc = QJsonDocument::fromJson( value.toString().toUtf8() );
79 if ( doc.isArray() )
80 {
81 return 20;
82 }
83 else
84 {
85 return 0;
86 }
87 }
88 }
89 }
90 }
91 }
92 return ( field.type() == QMetaType::Type::QVariantList || field.type() == QMetaType::Type::QStringList || field.type() == QMetaType::Type::QVariantMap ) && field.subType() != QMetaType::Type::UnknownType ? 20 : 0;
93}
@ NoGeometry
Geometry is not required. It may still be returned if e.g. required for a filter condition.
Definition qgis.h:2196
Base class for widgets which configure editor widget types.
QgsEditorWidgetFactory(const QString &name, const QIcon &icon=QIcon())
Constructor.
QIcon icon() const
Returns the icon of this widget type.
QString name() const
Returns the human readable identifier name of this widget type.
Manages an editor widget.
Wrapper for iterator of features from vector data provider or vector layer.
bool nextFeature(QgsFeature &f)
Fetch next feature and stores in f, returns true on success.
Wraps a request for features to a vector layer (or directly its vector data provider).
QgsFeatureRequest & setFlags(Qgis::FeatureRequestFlags flags)
Sets flags that affect how features will be fetched.
QgsFeatureRequest & setLimit(long long limit)
Set the maximum number of features to request.
QgsFeatureRequest & setSubsetOfAttributes(const QgsAttributeList &attrs)
Set a subset of attributes that will be fetched.
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
Definition qgsfeature.h:58
Q_INVOKABLE QVariant attribute(const QString &name) const
Lookup attribute value by attribute name.
Encapsulate a field in an attribute table or data source.
Definition qgsfield.h:54
QMetaType::Type type
Definition qgsfield.h:61
QString typeName() const
Gets the field type.
Definition qgsfield.cpp:163
QMetaType::Type subType() const
If the field is a collection, gets its element's type.
Definition qgsfield.cpp:158
QgsField field(int fieldIdx) const
Returns the field at particular index (must be in range 0..N-1).
A configuration dialog for the List Widget class.
unsigned int fieldScore(const QgsVectorLayer *vl, int fieldIdx) const override
This method allows disabling this editor widget type for a certain field.
QgsListWidgetFactory(const QString &name, const QIcon &icon=QIcon()=QIcon())
Constructor for QgsListWidgetFactory, where name is a human-readable name for the factory and icon pr...
QgsEditorConfigWidget * configWidget(QgsVectorLayer *vl, int fieldIdx, QWidget *parent) const override
Override this in your implementation.
QgsEditorWidgetWrapper * create(QgsVectorLayer *vl, int fieldIdx, QWidget *editor, QWidget *parent) const override
Override this in your implementation.
Wraps a list widget.
Represents a vector layer which manages a vector based dataset.
QgsFeatureIterator getFeatures(const QgsFeatureRequest &request=QgsFeatureRequest()) const final
Queries the layer for features specified in request.