25#include "moc_qgsfeaturepickerwidget.cpp"
30 , mCompleter( new QCompleter( mModel ) )
32 QHBoxLayout *layout =
new QHBoxLayout();
33 mComboBox =
new QComboBox(
this );
34 mComboBox->setEditable(
true );
35 layout->addWidget( mComboBox );
37 mPreviousButton =
new QToolButton(
this );
39 mPreviousButton->setEnabled(
false );
40 mPreviousButton->setVisible( mShowBrowserButtons );
41 layout->addWidget( mPreviousButton );
43 mNextButton =
new QToolButton(
this );
45 mNextButton->setEnabled(
false );
46 mNextButton->setVisible( mShowBrowserButtons );
47 layout->addWidget( mNextButton );
48 layout->setContentsMargins( 0, 0, 0, 0 );
52 mCompleter->setCaseSensitivity( Qt::CaseInsensitive );
53 mCompleter->setFilterMode( Qt::MatchContains );
54 mComboBox->setCompleter( mCompleter );
55 mCompleter->setWidget( mComboBox );
66 connect( mCompleter,
static_cast<void ( QCompleter::* )(
const QModelIndex & )
>( &QCompleter::highlighted ),
this, &QgsFeaturePickerWidget::onItemSelected );
67 connect( mCompleter,
static_cast<void ( QCompleter::* )(
const QModelIndex & )
>( &QCompleter::activated ),
this, &QgsFeaturePickerWidget::onActivated );
71 connect( mModel, &QgsFeaturePickerModel::dataChanged,
this, &QgsFeaturePickerWidget::onDataChanged );
73 connect( mComboBox,
static_cast<void ( QComboBox::* )(
int )
>( &QComboBox::currentIndexChanged ),
this, &QgsFeaturePickerWidget::onCurrentIndexChanged );
75 connect( mPreviousButton, &QToolButton::clicked,
this, [
this]() { browseFeatures( -1 ); } );
76 connect( mNextButton, &QToolButton::clicked,
this, [
this]() { browseFeatures( 1 ); } );
79 mLineEdit->setSelectOnFocus(
true );
80 mLineEdit->setShowClearButton(
allowNull() );
82 mComboBox->setEditable(
true );
83 mComboBox->setLineEdit( mLineEdit );
84 mComboBox->setModel( mModel );
86 connect( mLineEdit, &QgsFilterLineEdit::textEdited,
this, &QgsFeaturePickerWidget::onCurrentTextChanged );
91 return mModel->sourceLayer();
96 mModel->setSourceLayer( sourceLayer );
101 mModel->setFeature( featureId );
106 return mModel->feature();
111 return mModel->displayExpression();
116 mModel->setDisplayExpression( expression );
119void QgsFeaturePickerWidget::onCurrentTextChanged(
const QString &text )
121 mIsCurrentlyEdited =
true;
122 mPopupRequested =
true;
126void QgsFeaturePickerWidget::onFilterUpdateCompleted()
128 if ( mPopupRequested )
129 mCompleter->complete();
131 mPopupRequested =
false;
134void QgsFeaturePickerWidget::onLoadingChanged()
136 mLineEdit->setShowSpinner( mModel->isLoading() );
139void QgsFeaturePickerWidget::onItemSelected(
const QModelIndex &index )
141 mComboBox->setCurrentIndex( index.row() );
144void QgsFeaturePickerWidget::onCurrentIndexChanged(
int i )
146 if ( !mLineEdit->hasStateStored() )
147 mIsCurrentlyEdited =
false;
149 mPreviousButton->setEnabled( i > 0 );
150 mNextButton->setEnabled( i < mComboBox->model()->rowCount() - 1 );
155 const QModelIndex modelIndex = mModel->index( i, 0, QModelIndex() );
158 mLineEdit->setFont( mModel->data( modelIndex, Qt::FontRole ).value<QFont>() );
159 QPalette palette = mLineEdit->palette();
160 palette.setBrush( mLineEdit->foregroundRole(), mModel->data( modelIndex, Qt::ForegroundRole ).value<QBrush>() );
161 mLineEdit->setPalette( palette );
164void QgsFeaturePickerWidget::onActivated( QModelIndex modelIndex )
170void QgsFeaturePickerWidget::storeLineEditState()
172 if ( mIsCurrentlyEdited )
174 mLineEdit->storeState();
178void QgsFeaturePickerWidget::restoreLineEditState()
180 if ( mIsCurrentlyEdited )
182 mLineEdit->restoreState();
198void QgsFeaturePickerWidget::onDataChanged(
const QModelIndex &topLeft,
const QModelIndex &bottomRight,
const QVector<int> &roles )
201 if ( !mIsCurrentlyEdited )
204 if ( currentIndex >= topLeft.row() && currentIndex <= bottomRight.row() )
206 const QModelIndex modelIndex = mModel->
index( currentIndex, 0, QModelIndex() );
212void QgsFeaturePickerWidget::browseFeatures(
int direction )
214 const int newIndex = std::min( std::max( 0, mComboBox->currentIndex() + direction ), mComboBox->model()->rowCount() - 1 );
215 mComboBox->setCurrentIndex( newIndex );
220 return mModel->index( mModel->extraIdentifierValueIndex(), 0, QModelIndex() );
226 QWidget::focusOutEvent( event );
232 if ( event->key() == Qt::Key_Escape )
236 QWidget::keyReleaseEvent( event );
241 return mModel->allowNull();
252 return mModel->filterExpression();
262 return mModel->fetchGeometry();
272 return mModel->fetchLimit();
282 return mShowBrowserButtons;
291 mPreviousButton->setVisible( mShowBrowserButtons );
292 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