QGIS API Documentation 3.41.0-Master (45a0abf3bec)
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
6 Email : patrick.valsecchi@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 ***************************************************************************/
15
18#include "qgslistconfigdlg.h"
19#include "qgsfields.h"
20#include "qgsvectorlayer.h"
22
23#include <QVariant>
24#include <QSettings>
25
28{
29}
30
31QgsEditorWidgetWrapper *QgsListWidgetFactory::create( QgsVectorLayer *vl, int fieldIdx, QWidget *editor, QWidget *parent ) const
32{
33 return new QgsListWidgetWrapper( vl, fieldIdx, editor, parent );
34}
35
37{
38 return new QgsListConfigDlg( vl, fieldIdx, parent );
39}
40
41unsigned int QgsListWidgetFactory::fieldScore( const QgsVectorLayer *vl, int fieldIdx ) const
42{
43 const QgsField field = vl->fields().field( fieldIdx );
44 // Check if this is a JSON field misinterpreted as a map
45 if ( field.type() == QMetaType::Type::QVariantMap && ( field.typeName().compare( QStringLiteral( "JSON" ), Qt::CaseSensitivity::CaseInsensitive ) == 0 || field.subType() == QMetaType::Type::QString ) )
46 {
47 // Look the first not-null value (limiting to the first 20 features) and check if it is really an array
48 const int MAX_FEATURE_LIMIT { 20 };
51 req.setSubsetOfAttributes( { fieldIdx } );
52 req.setLimit( MAX_FEATURE_LIMIT );
53 QgsFeature f;
54 QgsFeatureIterator featureIt { vl->getFeatures( req ) };
55 // The counter is an extra safety measure in case the provider does not respect the limit
56 int featureCount = 0;
57 while ( featureIt.nextFeature( f ) )
58 {
59 ++featureCount;
60 if ( featureCount > MAX_FEATURE_LIMIT )
61 {
62 break;
63 }
64 // Get attribute value and check if it is a valid JSON array
65 const QVariant value( f.attribute( fieldIdx ) );
66 if ( ! value.isNull() )
67 {
68 switch ( value.type() )
69 {
70 case QVariant::Type::List:
71 {
72 return 20;
73 }
74 default:
75 case QVariant::Type::String:
76 {
77 const QJsonDocument doc = QJsonDocument::fromJson( value.toString().toUtf8() );
78 if ( doc.isArray() )
79 {
80 return 20;
81 }
82 else
83 {
84 return 0;
85 }
86 }
87 }
88 }
89 }
90 }
91 return ( field.type() == QMetaType::Type::QVariantList || field.type() == QMetaType::Type::QStringList || field.type() == QMetaType::Type::QVariantMap ) && field.subType() != QMetaType::Type::UnknownType ? 20 : 0;
92}
@ NoGeometry
Geometry is not required. It may still be returned if e.g. required for a filter condition.
This class should be subclassed for every configurable editor widget type.
Every attribute editor widget needs a factory, which inherits this class.
Manages an editor widget Widget and wrapper share the same parent.
Wrapper for iterator of features from vector data provider or vector layer.
This class 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:53
QMetaType::Type type
Definition qgsfield.h:60
QString typeName() const
Gets the field type.
Definition qgsfield.cpp:161
QMetaType::Type subType() const
If the field is a collection, gets its element's type.
Definition qgsfield.cpp:156
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)
Constructor for QgsListWidgetFactory, where name is a human-readable name for the factory.
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 data sets.
QgsFeatureIterator getFeatures(const QgsFeatureRequest &request=QgsFeatureRequest()) const FINAL
Queries the layer for features specified in request.