QGIS API Documentation 3.99.0-Master (2fe06baccd8)
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
17
18#include "qgsdummyconfigdlg.h"
19#include "qgsfields.h"
21#include "qgsvectorlayer.h"
22
23#include <QSettings>
24#include <QVariant>
25
30
31QgsEditorWidgetWrapper *QgsKeyValueWidgetFactory::create( QgsVectorLayer *vl, int fieldIdx, QWidget *editor, QWidget *parent ) const
32{
33 return new QgsKeyValueWidgetWrapper( vl, fieldIdx, editor, parent );
34}
35
37{
38 Q_UNUSED( vl )
39 Q_UNUSED( fieldIdx )
40 Q_UNUSED( parent )
41 return new QgsDummyConfigDlg( vl, fieldIdx, parent, QObject::tr( "Key/Value field" ) );
42}
43
44unsigned int QgsKeyValueWidgetFactory::fieldScore( const QgsVectorLayer *vl, int fieldIdx ) const
45{
46 const QgsField field = vl->fields().field( fieldIdx );
47 if ( field.type() == QMetaType::Type::QVariantMap && ( field.typeName().compare( QStringLiteral( "JSON" ), Qt::CaseSensitivity::CaseInsensitive ) == 0 || field.subType() == QMetaType::Type::QString ) )
48 {
49 // Look for the first not-null value (limiting to the first 20 features) and check if it is really a map
50 const int MAX_FEATURE_LIMIT { 20 };
53 req.setSubsetOfAttributes( { fieldIdx } );
54 req.setLimit( MAX_FEATURE_LIMIT );
55 QgsFeature f;
56 QgsFeatureIterator featureIt { vl->getFeatures( req ) };
57 // The counter is an extra safety measure in case the provider does not respect the limit
58 int featureCount = 0;
59 while ( featureIt.nextFeature( f ) )
60 {
61 ++featureCount;
62 if ( featureCount > MAX_FEATURE_LIMIT )
63 {
64 break;
65 }
66 // Get attribute value and check if it is a valid JSON object
67 const QVariant value( f.attribute( fieldIdx ) );
68 if ( !value.isNull() )
69 {
70 switch ( value.type() )
71 {
72 case QVariant::Type::Map:
73 {
74 return 20;
75 }
76 default:
77 case QVariant::Type::String:
78 {
79 const QJsonDocument doc = QJsonDocument::fromJson( value.toString().toUtf8() );
80 if ( doc.isObject() )
81 {
82 return 20;
83 }
84 else
85 {
86 return 0;
87 }
88 }
89 }
90 }
91 }
92 }
93 return field.type() == QMetaType::Type::QVariantMap && field.subType() != QMetaType::Type::UnknownType ? 20 : 0;
94}
@ NoGeometry
Geometry is not required. It may still be returned if e.g. required for a filter condition.
Definition qgis.h:2196
Configuration widget for dummy widgets.
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).
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.
unsigned int fieldScore(const QgsVectorLayer *vl, int fieldIdx) const override
This method allows disabling this editor widget type for a certain field.
QgsKeyValueWidgetFactory(const QString &name, const QIcon &icon=QIcon())
Constructor for QgsKeyValueWidgetFactory, where name is a human-readable name for the factory and ico...
Wraps a key/value 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.