QGIS API Documentation 3.41.0-Master (45a0abf3bec)
Loading...
Searching...
No Matches
qgsfeaturepickerwidget.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsfeaturepickerwidget.cpp - QgsFeaturePickerWidget
3 ---------------------
4 begin : 03.04.2020
5 copyright : (C) 2020 by Denis Rouzaud
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 ***************************************************************************/
15
16#include <QHBoxLayout>
17#include <QToolButton>
18#include <QKeyEvent>
19
21#include "qgsfilterlineedit.h"
23#include "moc_qgsfeaturepickerwidget.cpp"
24
26 : QWidget( parent )
27 , mModel( new QgsFeaturePickerModel( this ) )
28 , mCompleter( new QCompleter( mModel ) )
29{
30 QHBoxLayout *layout = new QHBoxLayout();
31 mComboBox = new QComboBox( this );
32 mComboBox->setEditable( true );
33 layout->addWidget( mComboBox );
34
35 mPreviousButton = new QToolButton( this );
36 mPreviousButton->setIcon( QgsApplication::getThemeIcon( QStringLiteral( "/mActionArrowLeft.svg" ) ) );
37 mPreviousButton->setEnabled( false );
38 mPreviousButton->setVisible( mShowBrowserButtons );
39 layout->addWidget( mPreviousButton );
40
41 mNextButton = new QToolButton( this );
42 mNextButton->setIcon( QgsApplication::getThemeIcon( QStringLiteral( "/mActionArrowRight.svg" ) ) );
43 mNextButton->setEnabled( false );
44 mNextButton->setVisible( mShowBrowserButtons );
45 layout->addWidget( mNextButton );
46 layout->setContentsMargins( 0, 0, 0, 0 );
47
48 setLayout( layout );
49
50 mCompleter->setCaseSensitivity( Qt::CaseInsensitive );
51 mCompleter->setFilterMode( Qt::MatchContains );
52 mComboBox->setCompleter( mCompleter );
53 mCompleter->setWidget( mComboBox );
57 connect( mModel, &QgsFeaturePickerModel::isLoadingChanged, this, &QgsFeaturePickerWidget::onLoadingChanged );
58 connect( mModel, &QgsFeaturePickerModel::filterJobCompleted, this, &QgsFeaturePickerWidget::onFilterUpdateCompleted );
62 connect( mModel, &QgsFeaturePickerModel::extraIdentifierValueIndexChanged, mComboBox, &QComboBox::setCurrentIndex );
64 connect( mCompleter, static_cast<void( QCompleter::* )( const QModelIndex & )>( &QCompleter::highlighted ), this, &QgsFeaturePickerWidget::onItemSelected );
65 connect( mCompleter, static_cast<void( QCompleter::* )( const QModelIndex & )>( &QCompleter::activated ), this, &QgsFeaturePickerWidget::onActivated );
66 connect( mModel, &QgsFeaturePickerModel::beginUpdate, this, &QgsFeaturePickerWidget::storeLineEditState );
67 connect( mModel, &QgsFeaturePickerModel::endUpdate, this, &QgsFeaturePickerWidget::restoreLineEditState );
69 connect( mModel, &QgsFeaturePickerModel::dataChanged, this, &QgsFeaturePickerWidget::onDataChanged );
70
71 connect( mComboBox, static_cast<void( QComboBox::* )( int )>( &QComboBox::currentIndexChanged ), this, &QgsFeaturePickerWidget::onCurrentIndexChanged );
72
73 connect( mPreviousButton, &QToolButton::clicked, this, [ = ]() {browseFeatures( -1 );} );
74 connect( mNextButton, &QToolButton::clicked, this, [ = ]() {browseFeatures( 1 );} );
75
76 mLineEdit = new QgsFilterLineEdit( nullptr, QgsApplication::nullRepresentation() );
77 mLineEdit->setSelectOnFocus( true );
78 mLineEdit->setShowClearButton( allowNull() );
79
80 mComboBox->setEditable( true );
81 mComboBox->setLineEdit( mLineEdit );
82 mComboBox->setModel( mModel );
83
84 connect( mLineEdit, &QgsFilterLineEdit::textEdited, this, &QgsFeaturePickerWidget::onCurrentTextChanged );
85}
86
88{
89 return mModel->sourceLayer();
90}
91
93{
94 mModel->setSourceLayer( sourceLayer );
95}
96
98{
99 mModel->setFeature( featureId );
100}
101
103{
104 return mModel->feature();
105}
106
108{
109 return mModel->displayExpression();
110}
111
112void QgsFeaturePickerWidget::setDisplayExpression( const QString &expression )
113{
114 mModel->setDisplayExpression( expression );
115}
116
117void QgsFeaturePickerWidget::onCurrentTextChanged( const QString &text )
118{
119 mIsCurrentlyEdited = true;
120 mPopupRequested = true;
121 mModel->setFilterValue( text );
122}
123
124void QgsFeaturePickerWidget::onFilterUpdateCompleted()
125{
126 if ( mPopupRequested )
127 mCompleter->complete();
128
129 mPopupRequested = false;
130}
131
132void QgsFeaturePickerWidget::onLoadingChanged()
133{
134 mLineEdit->setShowSpinner( mModel->isLoading() );
135}
136
137void QgsFeaturePickerWidget::onItemSelected( const QModelIndex &index )
138{
139 mComboBox->setCurrentIndex( index.row() );
140}
141
142void QgsFeaturePickerWidget::onCurrentIndexChanged( int i )
143{
144 if ( !mLineEdit->hasStateStored() )
145 mIsCurrentlyEdited = false;
146
147 mPreviousButton->setEnabled( i > 0 );
148 mNextButton->setEnabled( i < mComboBox->model()->rowCount() - 1 );
149
150 if ( i < 0 )
151 return;
152
153 const QModelIndex modelIndex = mModel->index( i, 0, QModelIndex() );
154 mModel->setFeature( mModel->data( modelIndex, static_cast< int >( QgsFeaturePickerModel::CustomRole::FeatureId ) ).value<QgsFeatureId>() );
155 mLineEdit->setText( mModel->data( modelIndex, static_cast< int >( QgsFeaturePickerModel::CustomRole::Value ) ).toString() );
156 mLineEdit->setFont( mModel->data( modelIndex, Qt::FontRole ).value<QFont>() );
157 QPalette palette = mLineEdit->palette();
158 palette.setBrush( mLineEdit->foregroundRole(), mModel->data( modelIndex, Qt::ForegroundRole ).value<QBrush>() );
159 mLineEdit->setPalette( palette );
160}
161
162void QgsFeaturePickerWidget::onActivated( QModelIndex modelIndex )
163{
164 setFeature( mModel->data( modelIndex, static_cast< int >( QgsFeaturePickerModel::CustomRole::FeatureId ) ).value<QgsFeatureId>() );
165 mLineEdit->setText( mModel->data( modelIndex, static_cast< int >( QgsFeaturePickerModel::CustomRole::Value ) ).toString() );
166}
167
168void QgsFeaturePickerWidget::storeLineEditState()
169{
170 if ( mIsCurrentlyEdited )
171 {
172 mLineEdit->storeState( );
173 }
174}
175
176void QgsFeaturePickerWidget::restoreLineEditState()
177{
178 if ( mIsCurrentlyEdited )
179 {
180 mLineEdit->restoreState( );
181 }
182}
183
185{
186 int index = -1;
187
188 if ( allowNull() )
189 {
190 index = mComboBox->findText( QgsApplication::nullRepresentation( ) );
191 }
192
193 return index;
194}
195
196void QgsFeaturePickerWidget::onDataChanged( const QModelIndex &topLeft, const QModelIndex &bottomRight, const QVector<int> &roles )
197{
198 Q_UNUSED( roles )
199 if ( !mIsCurrentlyEdited )
200 {
201 const int currentIndex = mModel->extraIdentifierValueIndex();
202 if ( currentIndex >= topLeft.row() && currentIndex <= bottomRight.row() )
203 {
204 const QModelIndex modelIndex = mModel->index( currentIndex, 0, QModelIndex() );
205 mLineEdit->setText( mModel->data( modelIndex, static_cast< int >( QgsFeaturePickerModel::CustomRole::Value ) ).toString() );
206 }
207 }
208}
209
210void QgsFeaturePickerWidget::browseFeatures( int direction )
211{
212 const int newIndex = std::min( std::max( 0, mComboBox->currentIndex() + direction ), mComboBox->model()->rowCount() - 1 );
213 mComboBox->setCurrentIndex( newIndex );
214}
215
217{
218 return mModel->index( mModel->extraIdentifierValueIndex(), 0, QModelIndex() );
219}
220
222{
223 Q_UNUSED( event )
224 QWidget::focusOutEvent( event );
225 mLineEdit->setText( mModel->data( currentModelIndex(), static_cast< int >( QgsFeaturePickerModel::CustomRole::Value ) ).toString() );
226}
227
229{
230 if ( event->key() == Qt::Key_Escape )
231 {
232 mLineEdit->setText( mModel->data( currentModelIndex(), static_cast< int >( QgsFeaturePickerModel::CustomRole::Value ) ).toString() );
233 }
234 QWidget::keyReleaseEvent( event );
235}
236
238{
239 return mModel->allowNull();
240}
241
247
249{
250 return mModel->filterExpression();
251}
252
253void QgsFeaturePickerWidget::setFilterExpression( const QString &filterExpression )
254{
256}
257
259{
260 return mModel->fetchGeometry();
261}
262
264{
266}
267
269{
270 return mModel->fetchLimit();
271}
272
274{
275 mModel->setFetchLimit( fetchLimit );
276}
277
279{
280 return mShowBrowserButtons;
281}
282
284{
285 if ( showBrowserButtons == mShowBrowserButtons )
286 return;
287
288 mShowBrowserButtons = showBrowserButtons;
289 mPreviousButton->setVisible( mShowBrowserButtons );
290 mNextButton->setVisible( mShowBrowserButtons );
292}
static QString nullRepresentation()
Returns the string used to represent the value NULL throughout QGIS.
static QIcon getThemeIcon(const QString &name, const QColor &fillColor=QColor(), const QColor &strokeColor=QColor())
Helper to get a theme icon.
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 setFetchGeometry(bool fetchGeometry)
Defines if the geometry will be fetched.
@ FeatureId
Used to retrieve the id 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 fetchLimitChanged()
Emitted when the fetching limit for the feature request changes.
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 fetchGeometryChanged()
Emitted when the fetching of the geometry changes.
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.
Provides a list of features based on filter conditions.
void setFeature(const QgsFeatureId &fid)
Set the feature to the given feature id.
void featureChanged(const QgsFeature &feature)
Emitted when the current feature changes.
QgsFeature feature() const
Returns the current feature.
void keyPressEvent(QKeyEvent *event) override
void setAllowNull(bool allowNull)
Determines if a NULL value should be available in the list.
QgsFeaturePickerWidget(QWidget *parent=nullptr)
Create a new QgsFeaturePickerWidget, optionally specifying a parent.
void setFilterExpression(const QString &filterExpression)
An additional expression to further restrict the available features.
void allowNullChanged()
Determines if a NULL value should be available in the list.
QgsFeature feature() const
Returns the current feature.
void filterExpressionChanged()
An additional expression to further restrict the available features.
void displayExpressionChanged()
The display expression will be used to display features as well as the the value to match the typed t...
void layerChanged()
The layer from which features should be listed.
void modelUpdated()
The underlying model has been updated.
void setFetchLimit(int fetchLimit)
Defines the feature request fetch limit If set to 0, no limit is applied when fetching.
void setLayer(QgsVectorLayer *layer)
The layer from which features should be listed.
void showBrowserButtonsChanged()
Emitted when showing the browser buttons changes.
void setShowBrowserButtons(bool showBrowserButtons)
Defines if the browsing buttons are shown.
void fetchGeometryChanged()
Emitted when the fetching of the geometry changes.
void setFeature(QgsFeatureId featureId)
Sets the current index by using the given feature.
void featureChanged(const QgsFeature &feature)
Sends the feature as soon as it is chosen.
QModelIndex currentModelIndex() const
The index of the currently selected item.
void setDisplayExpression(const QString &displayExpression)
The display expression will be used to display features as well as the value to match the typed text ...
int nullIndex() const
Returns the current index of the NULL value, or -1 if NULL values are not allowed.
void fetchLimitChanged()
Emitted when the fetching limit for the feature request changes.
void focusOutEvent(QFocusEvent *event) override
void setFetchGeometry(bool fetchGeometry)
Defines if the geometry will be fetched.
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
Definition qgsfeature.h:58
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() )
bool hasStateStored() const
Returns if a state is already saved.
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.
qint64 QgsFeatureId
64 bit feature ids negative numbers are used for uncommitted/newly added features