QGIS API Documentation 3.99.0-Master (26c88405ac0)
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
17#include "qgsanimatedicon.h"
18#include "qgsapplication.h"
20#include "qgsfilterlineedit.h"
21#include "qgslogger.h"
22
23#include <QCompleter>
24#include <QKeyEvent>
25#include <QLineEdit>
26
27#include "moc_qgsfeaturelistcombobox.cpp"
28
30 : QComboBox( parent )
31 , mModel( new QgsFeatureFilterModel( this ) )
32 , mCompleter( new QCompleter( mModel ) )
33{
34 setMinimumContentsLength( 1 );
35 setSizeAdjustPolicy( QComboBox::SizeAdjustPolicy::AdjustToMinimumContentsLengthWithIcon );
36 mCompleter->setCaseSensitivity( Qt::CaseInsensitive );
37 mCompleter->setFilterMode( Qt::MatchContains );
38 setEditable( true );
39 setCompleter( mCompleter );
40 mCompleter->setWidget( this );
46 connect( mModel, &QgsFeatureFilterModel::isLoadingChanged, this, &QgsFeatureListComboBox::onLoadingChanged );
47 connect( mModel, &QgsFeatureFilterModel::filterJobCompleted, this, &QgsFeatureListComboBox::onFilterUpdateCompleted );
50 connect( mModel, &QgsFeatureFilterModel::extraIdentifierValueIndexChanged, this, &QgsFeatureListComboBox::setCurrentIndex );
52 connect( mCompleter, static_cast<void ( QCompleter::* )( const QModelIndex & )>( &QCompleter::highlighted ), this, &QgsFeatureListComboBox::onItemSelected );
53 connect( mCompleter, static_cast<void ( QCompleter::* )( const QModelIndex & )>( &QCompleter::activated ), this, &QgsFeatureListComboBox::onActivated );
54 connect( mModel, &QgsFeatureFilterModel::beginUpdate, this, &QgsFeatureListComboBox::storeLineEditState );
55 connect( mModel, &QgsFeatureFilterModel::endUpdate, this, &QgsFeatureListComboBox::restoreLineEditState );
57 connect( mModel, &QgsFeatureFilterModel::dataChanged, this, &QgsFeatureListComboBox::onDataChanged );
58
59 connect( this, static_cast<void ( QgsFeatureListComboBox::* )( int )>( &QgsFeatureListComboBox::currentIndexChanged ), this, &QgsFeatureListComboBox::onCurrentIndexChanged );
60
61 mLineEdit = new QgsFilterLineEdit( nullptr, QgsApplication::nullRepresentation() );
62 mLineEdit->setSelectOnFocus( true );
63 mLineEdit->setShowClearButton( true );
64
65 setLineEdit( mLineEdit );
66 setModel( mModel );
67
68 connect( mLineEdit, &QgsFilterLineEdit::textEdited, this, &QgsFeatureListComboBox::onCurrentTextChanged );
69 connect( mLineEdit, &QgsFilterLineEdit::cleared, this, &QgsFeatureListComboBox::onFilterLineEditCleared );
70
72 // To avoid wrongly signaling a foreign key change, handle model feature found state following feature gathering separately
74
75 setToolTip( tr( "Just start typing what you are looking for." ) );
76}
77
79{
80 return mModel->sourceLayer();
81}
82
87
89{
90 QVariantList values;
91 const QStringList fields = mModel->identifierFields();
92 for ( const QString &field : fields )
93 {
94 values << feature.attribute( field );
95 }
96 setIdentifierValues( values );
97}
98
100{
101 return mModel->displayExpression();
102}
103
104void QgsFeatureListComboBox::setDisplayExpression( const QString &expression )
105{
106 mModel->setDisplayExpression( expression );
107}
108
109void QgsFeatureListComboBox::onCurrentTextChanged( const QString &text )
110{
111 mIsCurrentlyEdited = true;
112 mPopupRequested = true;
113 mModel->setFilterValue( text );
114}
115
116void QgsFeatureListComboBox::onFilterLineEditCleared()
117{
118 // Reset the combobox when the search is cleared
119 const QString clearedValue = allowNull() ? mLineEdit->nullValue() : mLineEdit->defaultValue();
120 mModel->setFilterValue( clearedValue );
121}
122
123void QgsFeatureListComboBox::onFilterUpdateCompleted()
124{
125 if ( mPopupRequested )
126 mCompleter->complete();
127
128 mPopupRequested = false;
129}
130
131void QgsFeatureListComboBox::onLoadingChanged()
132{
133 mLineEdit->setShowSpinner( mModel->isLoading() );
134}
135
136void QgsFeatureListComboBox::onItemSelected( const QModelIndex &index )
137{
138 setCurrentIndex( index.row() );
139}
140
141void QgsFeatureListComboBox::onCurrentIndexChanged( int i )
142{
143 if ( !mLineEdit->hasStateStored() )
144 mIsCurrentlyEdited = false;
145 const QModelIndex modelIndex = mModel->index( i, 0, QModelIndex() );
146 mModel->setExtraIdentifierValues( mModel->data( modelIndex, static_cast<int>( QgsFeatureFilterModel::CustomRole::IdentifierValues ) ).toList() );
147 mLineEdit->setText( mModel->data( modelIndex, static_cast<int>( QgsFeatureFilterModel::CustomRole::Value ) ).toString() );
148 mLineEdit->setFont( mModel->data( modelIndex, Qt::FontRole ).value<QFont>() );
149 QPalette palette = mLineEdit->palette();
150 palette.setBrush( mLineEdit->foregroundRole(), mModel->data( modelIndex, Qt::ForegroundRole ).value<QBrush>() );
151 mLineEdit->setPalette( palette );
152}
153
154void QgsFeatureListComboBox::onActivated( QModelIndex modelIndex )
155{
156 setIdentifierValues( mModel->data( modelIndex, static_cast<int>( QgsFeatureFilterModel::CustomRole::IdentifierValues ) ).toList() );
157 mLineEdit->setText( mModel->data( modelIndex, static_cast<int>( QgsFeatureFilterModel::CustomRole::Value ) ).toString() );
158}
159
160void QgsFeatureListComboBox::storeLineEditState()
161{
162 if ( mIsCurrentlyEdited )
163 {
164 mLineEdit->storeState();
165 }
166}
167
168void QgsFeatureListComboBox::restoreLineEditState()
169{
170 if ( mIsCurrentlyEdited )
171 {
172 mLineEdit->restoreState();
173 }
174}
175
177{
178 int index = -1;
179
180 if ( allowNull() )
181 {
182 index = findText( QgsApplication::nullRepresentation() );
183 }
184
185 return index;
186}
187
188void QgsFeatureListComboBox::onDataChanged( const QModelIndex &topLeft, const QModelIndex &bottomRight, const QVector<int> &roles )
189{
190 Q_UNUSED( roles )
191 if ( !mIsCurrentlyEdited )
192 {
193 const int currentIndex = mModel->extraIdentifierValueIndex();
194 if ( currentIndex >= topLeft.row() && currentIndex <= bottomRight.row() )
195 {
196 const QModelIndex modelIndex = mModel->index( currentIndex, 0, QModelIndex() );
197 mLineEdit->setText( mModel->data( modelIndex, static_cast<int>( QgsFeatureFilterModel::CustomRole::Value ) ).toString() );
198 }
199 }
200}
201
203{
204 const QStringList list = mModel->identifierFields();
205 if ( list.isEmpty() )
206 return QString();
207 else
208 return list.at( 0 );
209}
210
212{
213 return mModel->identifierFields();
214}
215
217{
218 mModel->setIdentifierFields( QStringList() << identifierField );
219}
220
222{
223 mModel->setIdentifierFields( identifierFields );
224}
225
227{
228 return mModel->index( currentIndex(), 0, QModelIndex() );
229}
230
232{
233 Q_UNUSED( event )
234 QComboBox::focusOutEvent( event );
235 mLineEdit->setText( mModel->data( currentModelIndex(), static_cast<int>( QgsFeatureFilterModel::CustomRole::Value ) ).toString() );
236}
237
239{
240 if ( event->key() == Qt::Key_Escape )
241 {
242 mLineEdit->setText( mModel->data( currentModelIndex(), static_cast<int>( QgsFeatureFilterModel::CustomRole::Value ) ).toString() );
243 }
244 QComboBox::keyReleaseEvent( event );
245}
246
248{
249 return mModel->allowNull();
250}
251
253{
254 mModel->setAllowNull( allowNull );
256}
257
259{
260 return mModel->fetchLimit();
261}
262
264{
265 mModel->setFetchLimit( fetchLimit );
266}
267
269{
271 return mModel->extraIdentifierValues().value( 0 );
273}
274
276{
277 return mModel->extraIdentifierValues();
278}
279
281{
282 setIdentifierValues( QVariantList() << identifierValue );
283}
284
286{
287 mModel->setExtraIdentifierValues( identifierValues );
288}
289
291{
292 mModel->setExtraIdentifierValueToNull();
293}
294
296{
297 QgsFeatureRequest request;
298 request.setRequestMayBeNested( true );
299 if ( mModel->extraIdentifierValues().isEmpty() )
300 {
301 request.setFilterFids( QgsFeatureIds() ); // NULL: Return a request that's guaranteed to not return anything
302 }
303 else
304 {
305 QStringList filtersAttrs;
306 const QStringList identifierFields = mModel->identifierFields();
307 const QVariantList values = mModel->extraIdentifierValues();
308 for ( int i = 0; i < identifierFields.count(); i++ )
309 {
310 if ( i >= values.count() )
311 {
312 filtersAttrs << QgsExpression::createFieldEqualityExpression( identifierFields.at( i ), QVariant() );
313 }
314 else
315 {
316 filtersAttrs << QgsExpression::createFieldEqualityExpression( identifierFields.at( i ), values.at( i ) );
317 }
318 }
319 const QString expression = filtersAttrs.join( QLatin1String( " AND " ) );
320 return request.setFilterExpression( expression );
321 }
322 return request;
323}
324
326{
327 return mModel->filterExpression();
328}
329
331{
332 mModel->setFilterExpression( filterExpression );
333}
334
336{
337 return mModel->formFeature();
338}
339
341{
342 mModel->setFormFeature( feature );
343}
344
346{
347 return mModel->parentFormFeature();
348}
349
351{
352 mModel->setParentFormFeature( feature );
353}
354
356{
357 return mModel->orderExpression();
358}
359
361{
362 mModel->setOrderExpression( orderExpression );
363}
364
366{
367 return mModel->sortOrder();
368}
369
371{
372 mModel->setSortOrder( sortOrder );
373}
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 identifierFieldsChanged()
The identifier field should be a unique field that can be used to identify individual features.
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 setSortOrder(const Qt::SortOrder sortOrder)
The order direction of the model.
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.
QgsFeature parentFormFeature() const
Returns a parent attribute form feature to be used with the filter expression.
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 setParentFormFeature(const QgsFeature &feature)
Sets a parent attribute form feature to be used with the filter expression.
void modelUpdated()
The underlying model has been updated.
QgsFeature formFeature() const
Returns an attribute form feature to be used with the filter expression.
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 formFeatureChanged()
An attribute form feature to be used alongside the filter expression.
void identifierValueChanged()
The identifier value of the currently selected feature.
Qt::SortOrder sortOrder() const
The order direction of the model.
void setOrderExpression(const QString &orderExpression)
The order expression of the model.
QString orderExpression() const
The order expression of the model.
void sourceLayerChanged()
The layer from which features should be listed.
void setFormFeature(const QgsFeature &feature)
Sets an attribute form feature to be used with the filter expression.
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 parentFormFeatureChanged()
A parent attribute form feature to be used alongside the filter expression.
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 parentFormFeatureChanged()
A parent attribute form feature to be used alongside the filter expression.
void filterExpressionChanged()
An additional filter expression to apply, next to the filterValue.
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.
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 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 formFeatureChanged()
An attribute form feature to be used alongside the filter expression.
void endUpdate()
Notification that the model change is finished.
void displayExpressionChanged()
The display expression will be used for.
Wraps a request for features to a vector layer (or directly its vector data provider).
QgsFeatureRequest & setRequestMayBeNested(bool requestMayBeNested)
In case this request may be run nested within another already running iteration on the same connectio...
QgsFeatureRequest & setFilterFids(const QgsFeatureIds &fids)
Sets the feature IDs that should be fetched.
QgsExpression * filterExpression() const
Returns the filter expression (if set).
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...
@ ClearToNull
Reset value to null.
@ ClearToDefault
Reset value to default value (see defaultValue() ).
void cleared()
Emitted when the widget is cleared.
Represents a vector layer which manages a vector based dataset.
#define Q_NOWARN_DEPRECATED_POP
Definition qgis.h:7170
#define Q_NOWARN_DEPRECATED_PUSH
Definition qgis.h:7169
QSet< QgsFeatureId > QgsFeatureIds