QGIS API Documentation 3.41.0-Master (af5edcb665c)
Loading...
Searching...
No Matches
qgsfeaturefiltermodel.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsfeaturefiltermodel.cpp - QgsFeatureFilterModel
3 ---------------------
4 begin : 10.3.2017
5 copyright : (C) 2017 by Matthias Kuhn
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#include "moc_qgsfeaturefiltermodel.cpp"
18
19#include "qgsvectorlayer.h"
20#include "qgssettings.h"
21#include "qgsvariantutils.h"
22
23bool qVariantListCompare( const QVariantList &a, const QVariantList &b )
24{
25 if ( a.size() != b.size() )
26 return false;
27
28 for ( int i = 0; i < a.size(); ++i )
29 {
30 if ( !qgsVariantEqual( a.at( i ), b.at( i ) ) )
31 return false;
32 }
33 return true;
34}
35
36
39{
40 setFetchGeometry( false );
41 setFetchLimit( QgsSettings().value( QStringLiteral( "maxEntriesRelationWidget" ), 100, QgsSettings::Gui ).toInt() );
42 setExtraIdentifierValueUnguarded( nullIdentifier() );
43}
44
46{
47 return mIdentifierFields.value( 0 );
48}
49
50void QgsFeatureFilterModel::requestToReloadCurrentFeature( QgsFeatureRequest &request )
51{
52 QStringList conditions;
53 for ( int i = 0; i < mIdentifierFields.count(); i++ )
54 {
55 if ( i >= mExtraIdentifierValue.toList().count() )
56 {
57 conditions << QgsExpression::createFieldEqualityExpression( mIdentifierFields.at( i ), QVariant() );
58 }
59 else
60 {
61 conditions << QgsExpression::createFieldEqualityExpression( mIdentifierFields.at( i ), mExtraIdentifierValue.toList().at( i ) );
62 }
63 }
64 request.setFilterExpression( conditions.join( QLatin1String( " AND " ) ) );
65}
66
67QSet<QString> QgsFeatureFilterModel::requestedAttributes() const
68{
69 return QSet<QString>( mIdentifierFields.begin(), mIdentifierFields.end() );
70}
71
72QVariant QgsFeatureFilterModel::entryIdentifier( const QgsFeatureExpressionValuesGatherer::Entry &entry ) const
73{
74 return entry.featureId;
75}
76
77QgsFeatureExpressionValuesGatherer::Entry QgsFeatureFilterModel::createEntry( const QVariant &identifier ) const
78{
79 const QVariantList constValues = identifier.toList();
80
81 QStringList values;
82 for ( const QVariant &v : constValues )
83 values << QStringLiteral( "(%1)" ).arg( v.toString() );
84
85 return QgsFeatureExpressionValuesGatherer::Entry( constValues, values.join( QLatin1Char( ' ' ) ), QgsFeature( sourceLayer() ? sourceLayer()->fields() : QgsFields() ) );
86}
87
88bool QgsFeatureFilterModel::compareEntries( const QgsFeatureExpressionValuesGatherer::Entry &a, const QgsFeatureExpressionValuesGatherer::Entry &b ) const
89{
90 return qVariantListCompare( a.identifierFields, b.identifierFields );
91}
92
93bool QgsFeatureFilterModel::identifierIsNull( const QVariant &identifier ) const
94{
95 const QVariantList values = identifier.toList();
96 for ( const QVariant &value : values )
97 {
98 if ( !QgsVariantUtils::isNull( value ) )
99 {
100 return false;
101 }
102 }
103 return true;
104}
105
106QVariant QgsFeatureFilterModel::nullIdentifier() const
107{
108 QVariantList nullValues;
109 for ( int i = 0; i < mIdentifierFields.count(); i++ )
110 nullValues << QgsVariantUtils::createNullVariant( QMetaType::Type::Int );
111 return nullValues;
112}
113
115{
116 return mIdentifierFields;
117}
118
119
120void QgsFeatureFilterModel::setIdentifierFields( const QStringList &identifierFields )
121{
122 if ( mIdentifierFields == identifierFields )
123 return;
124
125 mIdentifierFields = identifierFields;
128}
129
130QgsFeatureExpressionValuesGatherer *QgsFeatureFilterModel::createValuesGatherer( const QgsFeatureRequest &request ) const
131{
132 return new QgsFeatureExpressionValuesGatherer( sourceLayer(), displayExpression(), request, mIdentifierFields );
133}
134
135
137{
138 QVariantList values = mExtraIdentifierValue.toList();
139 if ( values.count() != mIdentifierFields.count() )
140 {
141 return nullIdentifier().toList();
142 }
143 return values;
144}
145
146void QgsFeatureFilterModel::setExtraIdentifierValues( const QVariantList &extraIdentifierValues )
147{
149}
150
155
static QString createFieldEqualityExpression(const QString &fieldName, const QVariant &value, QMetaType::Type fieldType=QMetaType::Type::UnknownType)
Create an expression allowing to evaluate if a field is equal to a value.
void setExtraIdentifierValues(const QVariantList &extraIdentifierValues)
Allows specifying one value that does not need to match the filter criteria but will still be availab...
void setExtraIdentifierValueToNull() override
Allows specifying one value that does not need to match the filter criteria but will still be availab...
Q_DECL_DEPRECATED QString identifierField() const
The identifier field should be a unique field that can be used to identify individual features.
QgsFeatureFilterModel(QObject *parent=nullptr)
Create a new QgsFeatureFilterModel, optionally specifying a parent.
QStringList identifierFields
A set of fields of sourceLayer that is unique and should be used to identify features.
QVariantList extraIdentifierValues
The values that identifies the current feature.
void identifierFieldsChanged()
The identifier field should be a unique field that can be used to identify individual features.
void setIdentifierFields(const QStringList &identifierFields)
The identifier field should be a unique field that can be used to identify individual features.
Provides a list of features based on filter conditions.
void setExtraIdentifierValue(const QVariant &extraIdentifierValue)
Allows specifying one value that does not need to match the filter criteria but will still be availab...
void setFetchLimit(int fetchLimit)
Defines the feature request fetch limit If set to 0, no limit is applied when fetching.
void setFetchGeometry(bool fetchGeometry)
Defines if the geometry will be fetched.
void setExtraIdentifierValueUnguarded(const QVariant &identifierValue)
This will set the identifier value to be set in the model even if it doesn't exist currently in the d...
QVariant mExtraIdentifierValue
The current identifier value.
This class wraps a request for features to a vector layer (or directly its vector data provider).
QgsFeatureRequest & setFilterExpression(const QString &expression)
Set the filter expression.
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
Definition qgsfeature.h:58
Container of fields for a vector layer.
Definition qgsfields.h:46
This class is a composition of two QSettings instances:
Definition qgssettings.h:64
static bool isNull(const QVariant &variant, bool silenceNullWarnings=false)
Returns true if the specified variant should be considered a NULL value.
static QVariant createNullVariant(QMetaType::Type metaType)
Helper method to properly create a null QVariant from a metaType Returns the created QVariant.
bool qgsVariantEqual(const QVariant &lhs, const QVariant &rhs)
Compares two QVariant values and returns whether they are equal, two NULL values are always treated a...
Definition qgis.cpp:248
bool qVariantListCompare(const QVariantList &a, const QVariantList &b)