QGIS API Documentation 3.41.0-Master (45a0abf3bec)
Loading...
Searching...
No Matches
qgsfeaturelistcombobox.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsfeaturelistcombobox.cpp - QgsFeatureListComboBox
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
18#include "qgsanimatedicon.h"
19#include "qgsfilterlineedit.h"
20#include "qgslogger.h"
21#include "qgsapplication.h"
22#include "moc_qgsfeaturelistcombobox.cpp"
23
24#include <QCompleter>
25#include <QLineEdit>
26#include <QKeyEvent>
27
29 : QComboBox( parent )
30 , mModel( new QgsFeatureFilterModel( this ) )
31 , mCompleter( new QCompleter( mModel ) )
32{
33 setMinimumContentsLength( 1 );
34 setSizeAdjustPolicy( QComboBox::SizeAdjustPolicy::AdjustToMinimumContentsLengthWithIcon );
35 mCompleter->setCaseSensitivity( Qt::CaseInsensitive );
36 mCompleter->setFilterMode( Qt::MatchContains );
37 setEditable( true );
38 setCompleter( mCompleter );
39 mCompleter->setWidget( this );
43 connect( mModel, &QgsFeatureFilterModel::isLoadingChanged, this, &QgsFeatureListComboBox::onLoadingChanged );
44 connect( mModel, &QgsFeatureFilterModel::filterJobCompleted, this, &QgsFeatureListComboBox::onFilterUpdateCompleted );
47 connect( mModel, &QgsFeatureFilterModel::extraIdentifierValueIndexChanged, this, &QgsFeatureListComboBox::setCurrentIndex );
49 connect( mCompleter, static_cast<void( QCompleter::* )( const QModelIndex & )>( &QCompleter::highlighted ), this, &QgsFeatureListComboBox::onItemSelected );
50 connect( mCompleter, static_cast<void( QCompleter::* )( const QModelIndex & )>( &QCompleter::activated ), this, &QgsFeatureListComboBox::onActivated );
51 connect( mModel, &QgsFeatureFilterModel::beginUpdate, this, &QgsFeatureListComboBox::storeLineEditState );
52 connect( mModel, &QgsFeatureFilterModel::endUpdate, this, &QgsFeatureListComboBox::restoreLineEditState );
54 connect( mModel, &QgsFeatureFilterModel::dataChanged, this, &QgsFeatureListComboBox::onDataChanged );
55
56 connect( this, static_cast<void( QgsFeatureListComboBox::* )( int )>( &QgsFeatureListComboBox::currentIndexChanged ), this, &QgsFeatureListComboBox::onCurrentIndexChanged );
57
58 mLineEdit = new QgsFilterLineEdit( nullptr, QgsApplication::nullRepresentation() );
59 mLineEdit->setSelectOnFocus( true );
60 mLineEdit->setShowClearButton( true );
61
62 setLineEdit( mLineEdit );
63 setModel( mModel );
64
65 connect( mLineEdit, &QgsFilterLineEdit::textEdited, this, &QgsFeatureListComboBox::onCurrentTextChanged );
66 connect( mLineEdit, &QgsFilterLineEdit::cleared, this, &QgsFeatureListComboBox::onFilterLineEditCleared );
67
69 // To avoid wrongly signaling a foreign key change, handle model feature found state following feature gathering separately
71
72 setToolTip( tr( "Just start typing what you are looking for." ) );
73}
74
79
84
86{
87 QVariantList values;
88 const QStringList fields = mModel->identifierFields();
89 for ( const QString &field : fields )
90 {
91 values << feature.attribute( field );
92 }
93 setIdentifierValues( values );
94}
95
97{
98 return mModel->displayExpression();
99}
100
101void QgsFeatureListComboBox::setDisplayExpression( const QString &expression )
102{
103 mModel->setDisplayExpression( expression );
104}
105
106void QgsFeatureListComboBox::onCurrentTextChanged( const QString &text )
107{
108 mIsCurrentlyEdited = true;
109 mPopupRequested = true;
110 mModel->setFilterValue( text );
111}
112
113void QgsFeatureListComboBox::onFilterLineEditCleared()
114{
115 // Reset the combobox when the search is cleared
116 const QString clearedValue = allowNull() ? mLineEdit->nullValue() : mLineEdit->defaultValue();
117 mModel->setFilterValue( clearedValue );
118}
119
120void QgsFeatureListComboBox::onFilterUpdateCompleted()
121{
122 if ( mPopupRequested )
123 mCompleter->complete();
124
125 mPopupRequested = false;
126}
127
128void QgsFeatureListComboBox::onLoadingChanged()
129{
130 mLineEdit->setShowSpinner( mModel->isLoading() );
131}
132
133void QgsFeatureListComboBox::onItemSelected( const QModelIndex &index )
134{
135 setCurrentIndex( index.row() );
136}
137
138void QgsFeatureListComboBox::onCurrentIndexChanged( int i )
139{
140 if ( !mLineEdit->hasStateStored() )
141 mIsCurrentlyEdited = false;
142 const QModelIndex modelIndex = mModel->index( i, 0, QModelIndex() );
143 mModel->setExtraIdentifierValues( mModel->data( modelIndex, static_cast< int >( QgsFeatureFilterModel::CustomRole::IdentifierValues ) ).toList() );
144 mLineEdit->setText( mModel->data( modelIndex, static_cast< int >( QgsFeatureFilterModel::CustomRole::Value ) ).toString() );
145 mLineEdit->setFont( mModel->data( modelIndex, Qt::FontRole ).value<QFont>() );
146 QPalette palette = mLineEdit->palette();
147 palette.setBrush( mLineEdit->foregroundRole(), mModel->data( modelIndex, Qt::ForegroundRole ).value<QBrush>() );
148 mLineEdit->setPalette( palette );
149}
150
151void QgsFeatureListComboBox::onActivated( QModelIndex modelIndex )
152{
153 setIdentifierValues( mModel->data( modelIndex, static_cast< int >( QgsFeatureFilterModel::CustomRole::IdentifierValues ) ).toList() );
154 mLineEdit->setText( mModel->data( modelIndex, static_cast< int >( QgsFeatureFilterModel::CustomRole::Value ) ).toString() );
155}
156
157void QgsFeatureListComboBox::storeLineEditState()
158{
159 if ( mIsCurrentlyEdited )
160 {
161 mLineEdit->storeState( );
162 }
163}
164
165void QgsFeatureListComboBox::restoreLineEditState()
166{
167 if ( mIsCurrentlyEdited )
168 {
169 mLineEdit->restoreState( );
170 }
171}
172
174{
175 int index = -1;
176
177 if ( allowNull() )
178 {
179 index = findText( QgsApplication::nullRepresentation( ) );
180 }
181
182 return index;
183}
184
185void QgsFeatureListComboBox::onDataChanged( const QModelIndex &topLeft, const QModelIndex &bottomRight, const QVector<int> &roles )
186{
187 Q_UNUSED( roles )
188 if ( !mIsCurrentlyEdited )
189 {
190 const int currentIndex = mModel->extraIdentifierValueIndex();
191 if ( currentIndex >= topLeft.row() && currentIndex <= bottomRight.row() )
192 {
193 const QModelIndex modelIndex = mModel->index( currentIndex, 0, QModelIndex() );
194 mLineEdit->setText( mModel->data( modelIndex, static_cast< int >( QgsFeatureFilterModel::CustomRole::Value ) ).toString() );
195 }
196 }
197}
198
200{
201 const QStringList list = mModel->identifierFields();
202 if ( list.isEmpty() )
203 return QString();
204 else
205 return list.at( 0 );
206}
207
209{
210 return mModel->identifierFields();
211}
212
213void QgsFeatureListComboBox::setIdentifierField( const QString &identifierField )
214{
215 mModel->setIdentifierFields( QStringList() << identifierField );
216}
217
218void QgsFeatureListComboBox::setIdentifierFields( const QStringList &identifierFields )
219{
221}
222
224{
225 return mModel->index( currentIndex(), 0, QModelIndex() );
226}
227
229{
230 Q_UNUSED( event )
231 QComboBox::focusOutEvent( event );
232 mLineEdit->setText( mModel->data( currentModelIndex(), static_cast< int >( QgsFeatureFilterModel::CustomRole::Value ) ).toString() );
233}
234
236{
237 if ( event->key() == Qt::Key_Escape )
238 {
239 mLineEdit->setText( mModel->data( currentModelIndex(), static_cast< int >( QgsFeatureFilterModel::CustomRole::Value ) ).toString() );
240 }
241 QComboBox::keyReleaseEvent( event );
242}
243
245{
246 return mModel->allowNull();
247}
248
254
256{
257 return mModel->fetchLimit();
258}
259
261{
262 mModel->setFetchLimit( fetchLimit );
263}
264
271
273{
274 return mModel->extraIdentifierValues();
275}
276
277void QgsFeatureListComboBox::setIdentifierValue( const QVariant &identifierValue )
278{
279 setIdentifierValues( QVariantList() << identifierValue );
280}
281
282void QgsFeatureListComboBox::setIdentifierValues( const QVariantList &identifierValues )
283{
285}
286
291
293{
294 if ( mModel->extraIdentifierValues().isEmpty() )
295 {
296 return QgsFeatureRequest().setFilterFids( QgsFeatureIds() ); // NULL: Return a request that's guaranteed to not return anything
297 }
298 else
299 {
300 QStringList filtersAttrs;
301 const QStringList identifierFields = mModel->identifierFields();
302 const QVariantList values = mModel->extraIdentifierValues();
303 for ( int i = 0; i < identifierFields.count(); i++ )
304 {
305 if ( i >= values.count() )
306 {
307 filtersAttrs << QgsExpression::createFieldEqualityExpression( identifierFields.at( i ), QVariant() );
308 }
309 else
310 {
311 filtersAttrs << QgsExpression::createFieldEqualityExpression( identifierFields.at( i ), values.at( i ) );
312 }
313 }
314 const QString expression = filtersAttrs.join( QLatin1String( " AND " ) );
315 return QgsFeatureRequest().setFilterExpression( expression );
316 }
317}
318
320{
321 return mModel->filterExpression();
322}
323
324void QgsFeatureListComboBox::setFilterExpression( const QString &filterExpression )
325{
327}
static QString nullRepresentation()
Returns the string used to represent the value NULL throughout QGIS.
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.
Provides a list of features based on filter conditions.
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...
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.
This offers a combobox with autocompleter that allows selecting features from a layer.
void setIdentifierValues(const QVariantList &identifierValues)
The identifier values of the currently selected feature.
void keyPressEvent(QKeyEvent *event) override
Q_DECL_DEPRECATED void setIdentifierField(const QString &identifierField)
Field name that will be used to uniquely identify the current feature.
void setDisplayExpression(const QString &displayExpression)
The display expression will be used to display features as well as the value to match the typed text ...
QModelIndex currentModelIndex() const
The index of the currently selected item.
void setFilterExpression(const QString &filterExpression)
An additional expression to further restrict the available features.
void setIdentifierFields(const QStringList &identifierFields)
Field name that will be used to uniquely identify the current feature.
void allowNullChanged()
Determines if a NULL value should be available in the list.
QgsFeatureListComboBox(QWidget *parent=nullptr)
Create a new QgsFeatureListComboBox, optionally specifying a parent.
void focusOutEvent(QFocusEvent *event) override
void setSourceLayer(QgsVectorLayer *sourceLayer)
The layer from which features should be listed.
void modelUpdated()
The underlying model has been updated.
QgsFeatureRequest currentFeatureRequest() const
Shorthand for getting a feature request to query the currently selected feature.
void setIdentifierValuesToNull()
Sets the identifier values of the currently selected feature to NULL value(s).
void identifierValueChanged()
The identifier value of the currently selected feature.
void sourceLayerChanged()
The layer from which features should be listed.
void setCurrentFeature(const QgsFeature &feature)
Sets the current index by using the given feature.
int nullIndex() const
Returns the current index of the NULL value, or -1 if NULL values are not allowed.
void currentFeatureFoundChanged(bool found)
Emitted when the feature picker model changes its feature found state.
int fetchLimit() const
Returns the feature request fetch limit.
Q_DECL_DEPRECATED void setIdentifierValue(const QVariant &identifierValue)
The identifier value of the currently selected feature.
void currentFeatureChanged()
Emitted when the current feature changes.
QStringList identifierFields() const
Field name that will be used to uniquely identify the current feature.
void setAllowNull(bool allowNull)
Determines if a NULL value should be available in the list.
void displayExpressionChanged()
The display expression will be used to display features as well as the the value to match the typed t...
void identifierFieldChanged()
Field name that will be used to uniquely identify the current feature.
void setFetchLimit(int fetchLimit)
Defines the feature request fetch limit If set to 0, no limit is applied when fetching.
void filterExpressionChanged()
An additional expression to further restrict the available features.
void extraIdentifierValueIndexChanged(int index)
The index at which the extra identifier value is available within the model.
void beginUpdate()
Notification that the model is about to be changed because a job was completed.
void setFilterValue(const QString &filterValue)
This value will be used to filter the features available from this model.
void filterExpressionChanged()
An additional filter expression to apply, next to the filterValue.
void setFetchLimit(int fetchLimit)
Defines the feature request fetch limit If set to 0, no limit is applied when fetching.
void extraValueDoesNotExistChanged(bool found)
Notification whether the model has found a feature tied to the extraIdentifierValue or not.
void extraIdentifierValueChanged()
Allows specifying one value that does not need to match the filter criteria but will still be availab...
@ IdentifierValues
Used to retrieve the identifierValues (primary keys) of a feature.
@ Value
Used to retrieve the displayExpression of a feature.
void filterJobCompleted()
Indicates that a filter job has been completed and new data may be available.
void setAllowNull(bool allowNull)
Add a NULL entry to the list.
void setDisplayExpression(const QString &displayExpression)
The display expression will be used for.
bool isLoading() const
Indicator if the model is currently performing any feature iteration in the background.
QVariant data(const QModelIndex &index, int role) const override
QModelIndex index(int row, int column, const QModelIndex &parent) const override
void sourceLayerChanged()
The source layer from which features will be fetched.
void setFilterExpression(const QString &filterExpression)
An additional filter expression to apply, next to the filterValue.
void allowNullChanged()
Add a NULL entry to the list.
void currentFeatureChanged()
Emitted when the current feature in the model has changed This emitted both when the extra value chan...
void isLoadingChanged()
Indicator if the model is currently performing any feature iteration in the background.
void endUpdate()
Notification that the model change is finished.
void displayExpressionChanged()
The display expression will be used for.
void setSourceLayer(QgsVectorLayer *sourceLayer)
The source layer from which features will be fetched.
This class wraps a request for features to a vector layer (or directly its vector data provider).
QgsFeatureRequest & setFilterFids(const QgsFeatureIds &fids)
Sets the feature IDs that should be fetched.
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
Q_INVOKABLE QVariant attribute(const QString &name) const
Lookup attribute value by attribute name.
QLineEdit subclass with built in support for clearing the widget's value and handling custom null val...
void restoreState()
Restores the current state of the line edit (selection and cursor position)
void storeState()
Stores the current state of the line edit (selection and cursor position)
@ ClearToNull
Reset value to null.
@ ClearToDefault
Reset value to default value (see defaultValue() )
void setShowClearButton(bool visible)
Sets whether the widget's clear button is visible.
bool hasStateStored() const
Returns if a state is already saved.
void setSelectOnFocus(bool selectOnFocus)
Will select all text when this widget receives the focus.
void cleared()
Emitted when the widget is cleared.
void setShowSpinner(bool showSpinner)
Show a spinner icon.
void setClearMode(ClearMode mode)
Sets the clear mode for the widget.
Represents a vector layer which manages a vector based data sets.
#define Q_NOWARN_DEPRECATED_POP
Definition qgis.h:6535
#define Q_NOWARN_DEPRECATED_PUSH
Definition qgis.h:6534
QSet< QgsFeatureId > QgsFeatureIds