QGIS API Documentation 4.1.0-Master (3b8ef1f72a3)
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
19#include "qgssettingstree.h"
20#include "qgsvariantutils.h"
21#include "qgsvectorlayer.h"
22
23#include <QString>
24
25#include "moc_qgsfeaturefiltermodel.cpp"
26
27using namespace Qt::StringLiterals;
28
30 = new QgsSettingsEntryInteger( u"max-entries-relation-widget"_s, QgsSettingsTree::sTreeGui, 100, u"Maximum number of entries in relation widget"_s );
31
32bool qVariantListCompare( const QVariantList &a, const QVariantList &b )
33{
34 if ( a.size() != b.size() )
35 return false;
36
37 for ( int i = 0; i < a.size(); ++i )
38 {
39 if ( !qgsVariantEqual( a.at( i ), b.at( i ) ) )
40 return false;
41 }
42 return true;
43}
44
45
53
55{
56 return mIdentifierFields.value( 0 );
57}
58
59void QgsFeatureFilterModel::requestToReloadCurrentFeature( QgsFeatureRequest &request )
60{
61 QStringList conditions;
62 for ( int i = 0; i < mIdentifierFields.count(); i++ )
63 {
64 if ( i >= mExtraIdentifierValue.toList().count() )
65 {
66 conditions << QgsExpression::createFieldEqualityExpression( mIdentifierFields.at( i ), QVariant() );
67 }
68 else
69 {
70 conditions << QgsExpression::createFieldEqualityExpression( mIdentifierFields.at( i ), mExtraIdentifierValue.toList().at( i ) );
71 }
72 }
73 request.setFilterExpression( conditions.join( " AND "_L1 ) );
74}
75
77{
78 return QSet<QString>( mIdentifierFields.begin(), mIdentifierFields.end() );
79}
80
81QVariant QgsFeatureFilterModel::entryIdentifier( const QgsFeatureExpressionValuesGatherer::Entry &entry ) const
82{
83 return entry.featureId;
84}
85
86QgsFeatureExpressionValuesGatherer::Entry QgsFeatureFilterModel::createEntry( const QVariant &identifier ) const
87{
88 const QVariantList constValues = identifier.toList();
89
90 QStringList values;
91 for ( const QVariant &v : constValues )
92 values << u"(%1)"_s.arg( v.toString() );
93
94 return QgsFeatureExpressionValuesGatherer::Entry( constValues, values.join( ' '_L1 ), QgsFeature( sourceLayer() ? sourceLayer()->fields() : QgsFields() ) );
95}
96
97bool QgsFeatureFilterModel::compareEntries( const QgsFeatureExpressionValuesGatherer::Entry &a, const QgsFeatureExpressionValuesGatherer::Entry &b ) const
98{
99 return qVariantListCompare( a.identifierFields, b.identifierFields );
100}
101
102bool QgsFeatureFilterModel::identifierIsNull( const QVariant &identifier ) const
103{
104 const QVariantList values = identifier.toList();
105 for ( const QVariant &value : values )
106 {
107 if ( !QgsVariantUtils::isNull( value ) )
108 {
109 return false;
110 }
111 }
112 return true;
113}
114
116{
117 QVariantList nullValues;
118 for ( int i = 0; i < mIdentifierFields.count(); i++ )
119 nullValues << QgsVariantUtils::createNullVariant( QMetaType::Type::Int );
120 return nullValues;
121}
122
124{
125 return mIdentifierFields;
126}
127
128
130{
131 if ( mIdentifierFields == identifierFields )
132 return;
133
134 mIdentifierFields = identifierFields;
137}
138
139QgsFeatureExpressionValuesGatherer *QgsFeatureFilterModel::createValuesGatherer( const QgsFeatureRequest &request ) const
140{
141 return new QgsFeatureExpressionValuesGatherer( sourceLayer(), displayExpression(), orderExpression(), request, mIdentifierFields );
142}
143
144
146{
147 QVariantList values = mExtraIdentifierValue.toList();
148 if ( values.count() != mIdentifierFields.count() )
149 {
150 return nullIdentifier().toList();
151 }
152 return values;
153}
154
159
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.
static const QgsSettingsEntryInteger * settingsMaxEntriesRelationWidget
Settings for maximum number of entries in relation widget.
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.
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...
virtual QSet< QString > requestedAttributes() const
Returns the attributes to be fetched in the request.
QModelIndex parent(const QModelIndex &child) const override
QVariant mExtraIdentifierValue
The current identifier value.
QgsFeaturePickerModelBase(QObject *parent=nullptr)
Create a new QgsFeaturePickerModelBase, optionally specifying a parent.
virtual QVariant nullIdentifier() const =0
Returns a null identifier.
Wraps a request for features to a vector layer (or directly its vector data provider).
QgsFeatureRequest & setFilterExpression(const QString &expression)
Set the filter expression.
An integer settings entry.
static QgsSettingsTreeNode * sTreeGui
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:657
bool qVariantListCompare(const QVariantList &a, const QVariantList &b)