QGIS API Documentation  3.6.0-Noosa (5873452)
qgsfeaturelistcombobox.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsfieldlistcombobox.cpp - QgsFieldListComboBox
3 
4  ---------------------
5  begin : 10.3.2017
6  copyright : (C) 2017 by Matthias Kuhn
7  email : [email protected]
8  ***************************************************************************
9  * *
10  * This program is free software; you can redistribute it and/or modify *
11  * it under the terms of the GNU General Public License as published by *
12  * the Free Software Foundation; either version 2 of the License, or *
13  * (at your option) any later version. *
14  * *
15  ***************************************************************************/
16 #include "qgsfeaturelistcombobox.h"
17 
18 #include "qgsfeaturefiltermodel.h"
19 #include "qgsanimatedicon.h"
20 #include "qgsfilterlineedit.h"
21 #include "qgslogger.h"
22 #include "qgsapplication.h"
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  mCompleter->setCaseSensitivity( Qt::CaseInsensitive );
34  mCompleter->setFilterMode( Qt::MatchContains );
35  setCompleter( mCompleter );
36  mCompleter->setWidget( this );
40  connect( mModel, &QgsFeatureFilterModel::isLoadingChanged, this, &QgsFeatureListComboBox::onLoadingChanged );
41  connect( mModel, &QgsFeatureFilterModel::filterJobCompleted, this, &QgsFeatureListComboBox::onFilterUpdateCompleted );
44  connect( mModel, &QgsFeatureFilterModel::extraIdentifierValueIndexChanged, this, &QgsFeatureListComboBox::setCurrentIndex );
46  connect( mCompleter, static_cast<void( QCompleter::* )( const QModelIndex & )>( &QCompleter::highlighted ), this, &QgsFeatureListComboBox::onItemSelected );
47  connect( mCompleter, static_cast<void( QCompleter::* )( const QModelIndex & )>( &QCompleter::activated ), this, &QgsFeatureListComboBox::onActivated );
48  connect( mModel, &QgsFeatureFilterModel::beginUpdate, this, &QgsFeatureListComboBox::storeLineEditState );
49  connect( mModel, &QgsFeatureFilterModel::endUpdate, this, &QgsFeatureListComboBox::restoreLineEditState );
51  connect( mModel, &QgsFeatureFilterModel::dataChanged, this, &QgsFeatureListComboBox::onDataChanged );
52 
53  connect( this, static_cast<void( QgsFeatureListComboBox::* )( int )>( &QgsFeatureListComboBox::currentIndexChanged ), this, &QgsFeatureListComboBox::onCurrentIndexChanged );
54 
55  mLineEdit = new QgsFilterLineEdit( nullptr, QgsApplication::nullRepresentation() );
56  mLineEdit->setSelectOnFocus( true );
57  mLineEdit->setShowClearButton( true );
58 
59  setEditable( true );
60  setLineEdit( mLineEdit );
61  setModel( mModel );
62 
63  connect( mLineEdit, &QgsFilterLineEdit::textEdited, this, &QgsFeatureListComboBox::onCurrentTextChanged );
64 
65  setToolTip( tr( "Just start typing what you are looking for." ) );
66 }
67 
69 {
70  return mModel->sourceLayer();
71 }
72 
74 {
75  mModel->setSourceLayer( sourceLayer );
76 }
77 
79 {
80  return mModel->displayExpression();
81 }
82 
83 void QgsFeatureListComboBox::setDisplayExpression( const QString &expression )
84 {
85  mModel->setDisplayExpression( expression );
86 }
87 
88 void QgsFeatureListComboBox::onCurrentTextChanged( const QString &text )
89 {
90  mIsCurrentlyEdited = true;
91  mPopupRequested = true;
92  mModel->setFilterValue( text );
93 }
94 
95 void QgsFeatureListComboBox::onFilterUpdateCompleted()
96 {
97  if ( mPopupRequested )
98  mCompleter->complete();
99 
100  mPopupRequested = false;
101 }
102 
103 void QgsFeatureListComboBox::onLoadingChanged()
104 {
105  mLineEdit->setShowSpinner( mModel->isLoading() );
106 }
107 
108 void QgsFeatureListComboBox::onItemSelected( const QModelIndex &index )
109 {
110  setCurrentIndex( index.row() );
111 }
112 
113 void QgsFeatureListComboBox::onCurrentIndexChanged( int i )
114 {
115  if ( !mHasStoredEditState )
116  mIsCurrentlyEdited = false;
117  QModelIndex modelIndex = mModel->index( i, 0, QModelIndex() );
119  mLineEdit->setText( mModel->data( modelIndex, QgsFeatureFilterModel::ValueRole ).toString() );
120  mLineEdit->setFont( mModel->data( modelIndex, Qt::FontRole ).value<QFont>() );
121  QPalette palette = mLineEdit->palette();
122  palette.setBrush( mLineEdit->foregroundRole(), mModel->data( modelIndex, Qt::ForegroundRole ).value<QBrush>() );
123  mLineEdit->setPalette( palette );
124 }
125 
126 void QgsFeatureListComboBox::onActivated( QModelIndex modelIndex )
127 {
129  mLineEdit->setText( mModel->data( modelIndex, QgsFeatureFilterModel::ValueRole ).toString() );
130 }
131 
132 void QgsFeatureListComboBox::storeLineEditState()
133 {
134  if ( mIsCurrentlyEdited )
135  {
136  mHasStoredEditState = true;
137  mLineEditState.store( mLineEdit );
138  }
139 }
140 
141 void QgsFeatureListComboBox::restoreLineEditState()
142 {
143  if ( mIsCurrentlyEdited )
144  {
145  mHasStoredEditState = false;
146  mLineEditState.restore( mLineEdit );
147  }
148 }
149 
151 {
152  int index = -1;
153 
154  if ( allowNull() )
155  {
156  index = findText( QgsApplication::nullRepresentation( ) );
157  }
158 
159  return index;
160 }
161 
162 void QgsFeatureListComboBox::onDataChanged( const QModelIndex &topLeft, const QModelIndex &bottomRight, const QVector<int> &roles )
163 {
164  Q_UNUSED( roles )
165  if ( !mIsCurrentlyEdited )
166  {
167  const int currentIndex = mModel->extraIdentifierValueIndex();
168  if ( currentIndex >= topLeft.row() && currentIndex <= bottomRight.row() )
169  {
170  QModelIndex modelIndex = mModel->index( currentIndex, 0, QModelIndex() );
171  mLineEdit->setText( mModel->data( modelIndex, QgsFeatureFilterModel::ValueRole ).toString() );
172  }
173  }
174 }
175 
177 {
178  return mModel->identifierField();
179 }
180 
182 {
183  mModel->setIdentifierField( identifierField );
184 }
185 
187 {
188  return mModel->index( mModel->extraIdentifierValueIndex(), 0, QModelIndex() );
189 }
190 
191 void QgsFeatureListComboBox::focusOutEvent( QFocusEvent *event )
192 {
193  Q_UNUSED( event )
194  QComboBox::focusOutEvent( event );
195  mLineEdit->setText( mModel->data( currentModelIndex(), QgsFeatureFilterModel::ValueRole ).toString() );
196 }
197 
199 {
200  if ( event->key() == Qt::Key_Escape )
201  {
202  mLineEdit->setText( mModel->data( currentModelIndex(), QgsFeatureFilterModel::ValueRole ).toString() );
203  }
204  QComboBox::keyReleaseEvent( event );
205 }
206 
208 {
209  return mModel->allowNull();
210 }
211 
213 {
214  mModel->setAllowNull( allowNull );
215 }
216 
218 {
219  return mModel->extraIdentifierValue();
220 }
221 
223 {
224  mModel->setExtraIdentifierValue( identifierValue );
225 }
226 
228 {
229  if ( mModel->extraIdentifierValue().isNull() )
230  return QgsFeatureRequest().setFilterFids( QgsFeatureIds() ); // NULL: Return a request that's guaranteed to not return anything
231  else
232  return QgsFeatureRequest().setFilterExpression( QStringLiteral( "%1 = %2" ).arg( QgsExpression::quotedColumnRef( mModel->identifierField() ), QgsExpression::quotedValue( mModel->extraIdentifierValue() ) ) );
233 }
234 
236 {
237  return mModel->filterExpression();
238 }
239 
241 {
242  mModel->setFilterExpression( filterExpression );
243 }
244 
245 void QgsFeatureListComboBox::LineEditState::store( QLineEdit *lineEdit )
246 {
247  text = lineEdit->text();
248  selectionStart = lineEdit->selectionStart();
249  selectionLength = lineEdit->selectedText().length();
250  cursorPosition = lineEdit->cursorPosition();
251 
252 }
253 
254 void QgsFeatureListComboBox::LineEditState::restore( QLineEdit *lineEdit ) const
255 {
256  lineEdit->setText( text );
257  lineEdit->setCursorPosition( cursorPosition );
258  if ( selectionStart > -1 )
259  lineEdit->setSelection( selectionStart, selectionLength );
260 }
void setIdentifierValue(const QVariant &identifierValue)
The identifier value of the currently selected feature.
QString identifierField() const
Field name that will be used to uniquely identify the current feature.
Provides a list of features based on filter conditions.
This offers a combobox with autocompleter that allows selecting features from a layer.
QgsFeatureRequest currentFeatureRequest() const
Shorthand for getting a feature request to query the currently selected feature.
QModelIndex currentModelIndex() const
The index of the currently selected item.
QSet< QgsFeatureId > QgsFeatureIds
Definition: qgsfeatureid.h:34
int nullIndex() const
Returns the current index of the NULL value, or -1 if NULL values are not allowed.
void setSelectOnFocus(bool selectOnFocus)
Will select all text when this widget receives the focus.
static QString quotedColumnRef(QString name)
Returns a quoted column reference (in double quotes)
void beginUpdate()
Notification that the model is about to be changed because a job was completed.
Used to retrieve the displayExpression of a feature.
void identifierFieldChanged()
Field name that will be used to uniquely identify the current feature.
void setFilterExpression(const QString &filterExpression)
An additional filter expression to apply, next to the filterValue.
QString filterExpression() const
An additional expression to further restrict the available features.
void setShowSpinner(bool showSpinner)
Show a spinner icon.
void displayExpressionChanged()
The display expression will be used to display features as well as the the value to match the typed t...
void filterJobCompleted()
Indicates that a filter job has been completed and new data may be available.
void extraIdentifierValueIndexChanged(int index)
The index at which the extra identifier value is available within the model.
void setSourceLayer(QgsVectorLayer *sourceLayer)
The layer from which features should be listed.
void filterExpressionChanged()
An additional expression to further restrict the available features.
QgsVectorLayer * sourceLayer() const
The layer from which features should be listed.
QgsFeatureRequest & setFilterExpression(const QString &expression)
Set the filter expression.
void identifierFieldChanged()
The identifier field should be a unique field that can be used to identify individual features...
void setAllowNull(bool allowNull)
Determines if a NULL value should be available in the list.
QString displayExpression() const
The display expression will be used to display features as well as the value to match the typed text ...
QgsFeatureListComboBox(QWidget *parent=nullptr)
Create a new QgsFeatureListComboBox, optionally specifying a parent.
void setFilterValue(const QString &filterValue)
This value will be used to filter the features available from this model.
QModelIndex index(int row, int column, const QModelIndex &parent) const override
QString identifierField
A field of sourceLayer that is unique and should be used to identify features.
This class wraps a request for features to a vector layer (or directly its vector data provider)...
QLineEdit subclass with built in support for clearing the widget&#39;s value and handling custom null val...
static QString nullRepresentation()
This string is used to represent the value NULL throughout QGIS.
QVariant extraIdentifierValue
The value that identifies the current feature.
void identifierValueChanged()
The identifier value of the currently selected feature.
void isLoadingChanged()
Indicator if the model is currently performing any feature iteration in the background.
void allowNullChanged()
Add a NULL entry to the list.
void filterExpressionChanged()
An additional filter expression to apply, next to the filterValue.
void endUpdate()
Notification that the model change is finished.
void extraIdentifierValueChanged()
Allows specifying one value that does not need to match the filter criteria but will still be availab...
void setIdentifierField(const QString &identifierField)
The identifier field should be a unique field that can be used to identify individual features...
void setSourceLayer(QgsVectorLayer *sourceLayer)
The source layer from which features will be fetched.
void setFilterExpression(const QString &filterExpression)
An additional expression to further restrict the available features.
void sourceLayerChanged()
The source layer from which features will be fetched.
QgsFeatureRequest & setFilterFids(const QgsFeatureIds &fids)
Sets feature IDs that should be fetched.
void allowNullChanged()
Determines if a NULL value should be available in the list.
void keyPressEvent(QKeyEvent *event) override
void setExtraIdentifierValue(const QVariant &extraIdentifierValue)
Allows specifying one value that does not need to match the filter criteria but will still be availab...
void sourceLayerChanged()
The layer from which features should be listed.
void focusOutEvent(QFocusEvent *event) override
QVariant data(const QModelIndex &index, int role) const override
void setAllowNull(bool allowNull)
Add a NULL entry to the list.
QVariant identifierValue() const
The identifier value of the currently selected feature.
static QString quotedValue(const QVariant &value)
Returns a string representation of a literal value, including appropriate quotations where required...
Used to retrieve the identifierValue (primary key) of a feature.
void setDisplayExpression(const QString &displayExpression)
The display expression will be used for.
void setShowClearButton(bool visible)
Sets whether the widget&#39;s clear button is visible.
Represents a vector layer which manages a vector based data sets.
void setDisplayExpression(const QString &displayExpression)
The display expression will be used to display features as well as the value to match the typed text ...
void modelUpdated()
The underlying model has been updated.
void setIdentifierField(const QString &identifierField)
Field name that will be used to uniquely identify the current feature.
bool allowNull() const
Determines if a NULL value should be available in the list.
void displayExpressionChanged()
The display expression will be used for.