26#include "moc_qgsfeaturepickerwidget.cpp"
28using namespace Qt::StringLiterals;
33 , mCompleter( new QCompleter( mModel ) )
35 QHBoxLayout *layout =
new QHBoxLayout();
36 mComboBox =
new QComboBox(
this );
37 mComboBox->setEditable(
true );
38 layout->addWidget( mComboBox );
40 mPreviousButton =
new QToolButton(
this );
42 mPreviousButton->setEnabled(
false );
43 mPreviousButton->setVisible( mShowBrowserButtons );
44 layout->addWidget( mPreviousButton );
46 mNextButton =
new QToolButton(
this );
48 mNextButton->setEnabled(
false );
49 mNextButton->setVisible( mShowBrowserButtons );
50 layout->addWidget( mNextButton );
51 layout->setContentsMargins( 0, 0, 0, 0 );
55 mCompleter->setCaseSensitivity( Qt::CaseInsensitive );
56 mCompleter->setFilterMode( Qt::MatchContains );
57 mComboBox->setCompleter( mCompleter );
58 mCompleter->setWidget( mComboBox );
69 connect( mCompleter,
static_cast<void ( QCompleter::* )(
const QModelIndex & )
>( &QCompleter::highlighted ),
this, &QgsFeaturePickerWidget::onItemSelected );
70 connect( mCompleter,
static_cast<void ( QCompleter::* )(
const QModelIndex & )
>( &QCompleter::activated ),
this, &QgsFeaturePickerWidget::onActivated );
74 connect( mModel, &QgsFeaturePickerModel::dataChanged,
this, &QgsFeaturePickerWidget::onDataChanged );
76 connect( mComboBox,
static_cast<void ( QComboBox::* )(
int )
>( &QComboBox::currentIndexChanged ),
this, &QgsFeaturePickerWidget::onCurrentIndexChanged );
78 connect( mPreviousButton, &QToolButton::clicked,
this, [
this]() { browseFeatures( -1 ); } );
79 connect( mNextButton, &QToolButton::clicked,
this, [
this]() { browseFeatures( 1 ); } );
82 mLineEdit->setSelectOnFocus(
true );
83 mLineEdit->setShowClearButton(
allowNull() );
85 mComboBox->setEditable(
true );
86 mComboBox->setLineEdit( mLineEdit );
87 mComboBox->setModel( mModel );
89 connect( mLineEdit, &QgsFilterLineEdit::textEdited,
this, &QgsFeaturePickerWidget::onCurrentTextChanged );
94 return mModel->sourceLayer();
99 mModel->setSourceLayer( sourceLayer );
104 mModel->setFeature( featureId );
109 return mModel->feature();
114 return mModel->displayExpression();
119 mModel->setDisplayExpression( expression );
122void QgsFeaturePickerWidget::onCurrentTextChanged(
const QString &text )
124 mIsCurrentlyEdited =
true;
125 mPopupRequested =
true;
129void QgsFeaturePickerWidget::onFilterUpdateCompleted()
131 if ( mPopupRequested )
132 mCompleter->complete();
134 mPopupRequested =
false;
137void QgsFeaturePickerWidget::onLoadingChanged()
139 mLineEdit->setShowSpinner( mModel->isLoading() );
142void QgsFeaturePickerWidget::onItemSelected(
const QModelIndex &index )
144 mComboBox->setCurrentIndex( index.row() );
147void QgsFeaturePickerWidget::onCurrentIndexChanged(
int i )
149 if ( !mLineEdit->hasStateStored() )
150 mIsCurrentlyEdited =
false;
152 mPreviousButton->setEnabled( i > 0 );
153 mNextButton->setEnabled( i < mComboBox->model()->rowCount() - 1 );
158 const QModelIndex modelIndex = mModel->index( i, 0, QModelIndex() );
161 mLineEdit->setFont( mModel->data( modelIndex, Qt::FontRole ).value<QFont>() );
162 QPalette palette = mLineEdit->palette();
163 palette.setBrush( mLineEdit->foregroundRole(), mModel->data( modelIndex, Qt::ForegroundRole ).value<QBrush>() );
164 mLineEdit->setPalette( palette );
167void QgsFeaturePickerWidget::onActivated( QModelIndex modelIndex )
173void QgsFeaturePickerWidget::storeLineEditState()
175 if ( mIsCurrentlyEdited )
177 mLineEdit->storeState();
181void QgsFeaturePickerWidget::restoreLineEditState()
183 if ( mIsCurrentlyEdited )
185 mLineEdit->restoreState();
201void QgsFeaturePickerWidget::onDataChanged(
const QModelIndex &topLeft,
const QModelIndex &bottomRight,
const QVector<int> &roles )
204 if ( !mIsCurrentlyEdited )
207 if ( currentIndex >= topLeft.row() && currentIndex <= bottomRight.row() )
209 const QModelIndex modelIndex = mModel->
index( currentIndex, 0, QModelIndex() );
215void QgsFeaturePickerWidget::browseFeatures(
int direction )
217 const int newIndex = std::min( std::max( 0, mComboBox->currentIndex() + direction ), mComboBox->model()->rowCount() - 1 );
218 mComboBox->setCurrentIndex( newIndex );
223 return mModel->index( mModel->extraIdentifierValueIndex(), 0, QModelIndex() );
229 QWidget::focusOutEvent( event );
235 if ( event->key() == Qt::Key_Escape )
239 QWidget::keyReleaseEvent( event );
244 return mModel->allowNull();
255 return mModel->filterExpression();
265 return mModel->fetchGeometry();
275 return mModel->fetchLimit();
285 return mShowBrowserButtons;
294 mPreviousButton->setVisible( mShowBrowserButtons );
295 mNextButton->setVisible( mShowBrowserButtons );
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.
@ 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.
QVariant data(const QModelIndex &index, int role) const override
QModelIndex index(int row, int column, const QModelIndex &parent) const override
int extraIdentifierValueIndex
void fetchLimitChanged()
Emitted when the fetching limit for the feature request changes.
void sourceLayerChanged()
The source layer from which features will be fetched.
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.
Provides a list of features based on filter conditions.
void featureChanged(const QgsFeature &feature)
Emitted when the current feature changes.
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
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() ).
Represents a vector layer which manages a vector based dataset.
qint64 QgsFeatureId
64 bit feature ids negative numbers are used for uncommitted/newly added features