QGIS API Documentation 3.41.0-Master (25ec5511245)
Loading...
Searching...
No Matches
qgskeyvaluewidgetfactory.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgskeyvaluewidgetfactory.cpp
3 --------------------------------------
4 Date : 08.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
18#include "qgsdummyconfigdlg.h"
19#include "qgsfields.h"
20#include "qgsvectorlayer.h"
21
22#include <QVariant>
23#include <QSettings>
24
29
30QgsEditorWidgetWrapper *QgsKeyValueWidgetFactory::create( QgsVectorLayer *vl, int fieldIdx, QWidget *editor, QWidget *parent ) const
31{
32 return new QgsKeyValueWidgetWrapper( vl, fieldIdx, editor, parent );
33}
34
36{
37 Q_UNUSED( vl )
38 Q_UNUSED( fieldIdx )
39 Q_UNUSED( parent )
40 return new QgsDummyConfigDlg( vl, fieldIdx, parent, QObject::tr( "Key/Value field" ) );
41}
42
43unsigned int QgsKeyValueWidgetFactory::fieldScore( const QgsVectorLayer *vl, int fieldIdx ) const
44{
45 const QgsField field = vl->fields().field( fieldIdx );
46 if ( field.type() == QMetaType::Type::QVariantMap && ( field.typeName().compare( QStringLiteral( "JSON" ), Qt::CaseSensitivity::CaseInsensitive ) == 0 || field.subType() == QMetaType::Type::QString ) )
47 {
48 // Look for the first not-null value (limiting to the first 20 features) and check if it is really a map
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 object
66 const QVariant value( f.attribute( fieldIdx ) );
67 if ( ! value.isNull() )
68 {
69 switch ( value.type() )
70 {
71 case QVariant::Type::Map:
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.isObject() )
80 {
81 return 20;
82 }
83 else
84 {
85 return 0;
86 }
87 }
88 }
89 }
90 }
91 }
92 return 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.
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).
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.
QgsKeyValueWidgetFactory(const QString &name)
Constructor for QgsKeyValueWidgetFactory, where name is a human-readable name for the factory.
unsigned int fieldScore(const QgsVectorLayer *vl, int fieldIdx) const override
This method allows disabling this editor widget type for a certain field.
Wraps a key/value 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.