QGIS API Documentation 3.27.0-Master (95e00c50d2)
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 ***************************************************************************/
17
18#include "qgsvectorlayer.h"
19#include "qgssettings.h"
20#include "qgsvariantutils.h"
21
22bool qVariantListCompare( const QVariantList &a, const QVariantList &b )
23{
24 if ( a.size() != b.size() )
25 return false;
26
27 for ( int i = 0; i < a.size(); ++i )
28 {
29 if ( !qgsVariantEqual( a.at( i ), b.at( i ) ) )
30 return false;
31 }
32 return true;
33}
34
35
38{
39 setFetchGeometry( false );
40 setFetchLimit( QgsSettings().value( QStringLiteral( "maxEntriesRelationWidget" ), 100, QgsSettings::Gui ).toInt() );
41 setExtraIdentifierValueUnguarded( nullIdentifier() );
42}
43
45{
46 return mIdentifierFields.value( 0 );
47}
48
49void QgsFeatureFilterModel::requestToReloadCurrentFeature( QgsFeatureRequest &request )
50{
51 QStringList conditions;
52 for ( int i = 0; i < mIdentifierFields.count(); i++ )
53 {
54 if ( i >= mExtraIdentifierValue.toList().count() )
55 {
56 conditions << QgsExpression::createFieldEqualityExpression( mIdentifierFields.at( i ), QVariant() );
57 }
58 else
59 {
60 conditions << QgsExpression::createFieldEqualityExpression( mIdentifierFields.at( i ), mExtraIdentifierValue.toList().at( i ) );
61 }
62 }
63 request.setFilterExpression( conditions.join( QLatin1String( " AND " ) ) );
64}
65
66QSet<QString> QgsFeatureFilterModel::requestedAttributes() const
67{
68 return QSet<QString>( mIdentifierFields.begin(), mIdentifierFields.end() );
69}
70
71QVariant QgsFeatureFilterModel::entryIdentifier( const QgsFeatureExpressionValuesGatherer::Entry &entry ) const
72{
73 return entry.featureId;
74}
75
76QgsFeatureExpressionValuesGatherer::Entry QgsFeatureFilterModel::createEntry( const QVariant &identifier ) const
77{
78 const QVariantList constValues = identifier.toList();
79
80 QStringList values;
81 for ( const QVariant &v : constValues )
82 values << QStringLiteral( "(%1)" ).arg( v.toString() );
83
84 return QgsFeatureExpressionValuesGatherer::Entry( constValues, values.join( QLatin1Char( ' ' ) ), QgsFeature( sourceLayer() ? sourceLayer()->fields() : QgsFields() ) );
85}
86
87bool QgsFeatureFilterModel::compareEntries( const QgsFeatureExpressionValuesGatherer::Entry &a, const QgsFeatureExpressionValuesGatherer::Entry &b ) const
88{
89 return qVariantListCompare( a.identifierFields, b.identifierFields );
90}
91
92bool QgsFeatureFilterModel::identifierIsNull( const QVariant &identifier ) const
93{
94 const QVariantList values = identifier.toList();
95 for ( const QVariant &value : values )
96 {
97 if ( !QgsVariantUtils::isNull( value ) )
98 {
99 return false;
100 }
101 }
102 return true;
103}
104
105QVariant QgsFeatureFilterModel::nullIdentifier() const
106{
107 QVariantList nullValues;
108 for ( int i = 0; i < mIdentifierFields.count(); i++ )
109 nullValues << QVariant( QVariant::Int );
110 return nullValues;
111}
112
114{
115 return mIdentifierFields;
116}
117
118
119void QgsFeatureFilterModel::setIdentifierFields( const QStringList &identifierFields )
120{
121 if ( mIdentifierFields == identifierFields )
122 return;
123
124 mIdentifierFields = identifierFields;
127}
128
129QgsFeatureExpressionValuesGatherer *QgsFeatureFilterModel::createValuesGatherer( const QgsFeatureRequest &request ) const
130{
131 return new QgsFeatureExpressionValuesGatherer( sourceLayer(), displayExpression(), request, mIdentifierFields );
132}
133
134
136{
137 QVariantList values = mExtraIdentifierValue.toList();
138 if ( values.count() != mIdentifierFields.count() )
139 {
140 return nullIdentifier().toList();
141 }
142 return values;
143}
144
145void QgsFeatureFilterModel::setExtraIdentifierValues( const QVariantList &extraIdentifierValues )
146{
148}
149
151{
152 setExtraIdentifierValue( nullIdentifier() );
153}
154
static QString createFieldEqualityExpression(const QString &fieldName, const QVariant &value, QVariant::Type fieldType=QVariant::Type::Invalid)
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:56
Container of fields for a vector layer.
Definition: qgsfields.h:45
This class is a composition of two QSettings instances:
Definition: qgssettings.h:62
static bool isNull(const QVariant &variant)
Returns true if the specified variant should be considered a NULL value.
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:266
bool qVariantListCompare(const QVariantList &a, const QVariantList &b)