16 #include <QHBoxLayout>
17 #include <QToolButton>
27 , mCompleter( new QCompleter( mModel ) )
29 QHBoxLayout *layout =
new QHBoxLayout();
30 mComboBox =
new QComboBox(
this );
31 mComboBox->setEditable(
true );
32 layout->addWidget( mComboBox );
34 mPreviousButton =
new QToolButton(
this );
36 mPreviousButton->setEnabled(
false );
37 mPreviousButton->setVisible( mShowBrowserButtons );
38 layout->addWidget( mPreviousButton );
40 mNextButton =
new QToolButton(
this );
42 mNextButton->setEnabled(
false );
43 mNextButton->setVisible( mShowBrowserButtons );
44 layout->addWidget( mNextButton );
45 layout->setContentsMargins( 0, 0, 0, 0 );
49 mCompleter->setCaseSensitivity( Qt::CaseInsensitive );
50 mCompleter->setFilterMode( Qt::MatchContains );
51 mComboBox->setCompleter( mCompleter );
52 mCompleter->setWidget( mComboBox );
63 connect( mCompleter,
static_cast<void( QCompleter::* )(
const QModelIndex & )
>( &QCompleter::highlighted ),
this, &QgsFeaturePickerWidget::onItemSelected );
64 connect( mCompleter,
static_cast<void( QCompleter::* )(
const QModelIndex & )
>( &QCompleter::activated ),
this, &QgsFeaturePickerWidget::onActivated );
68 connect( mModel, &QgsFeaturePickerModel::dataChanged,
this, &QgsFeaturePickerWidget::onDataChanged );
70 connect( mComboBox,
static_cast<void( QComboBox::* )(
int )
>( &QComboBox::currentIndexChanged ),
this, &QgsFeaturePickerWidget::onCurrentIndexChanged );
72 connect( mPreviousButton, &QToolButton::clicked,
this, [ = ]() {browseFeatures( -1 );} );
73 connect( mNextButton, &QToolButton::clicked,
this, [ = ]() {browseFeatures( 1 );} );
76 mLineEdit->setSelectOnFocus(
true );
77 mLineEdit->setShowClearButton(
allowNull() );
79 mComboBox->setEditable(
true );
80 mComboBox->setLineEdit( mLineEdit );
81 mComboBox->setModel( mModel );
83 connect( mLineEdit, &QgsFilterLineEdit::textEdited,
this, &QgsFeaturePickerWidget::onCurrentTextChanged );
117 void QgsFeaturePickerWidget::onCurrentTextChanged(
const QString &text )
119 mIsCurrentlyEdited =
true;
120 mPopupRequested =
true;
124 void QgsFeaturePickerWidget::onFilterUpdateCompleted()
126 if ( mPopupRequested )
127 mCompleter->complete();
129 mPopupRequested =
false;
132 void QgsFeaturePickerWidget::onLoadingChanged()
137 void QgsFeaturePickerWidget::onItemSelected(
const QModelIndex &index )
139 mComboBox->setCurrentIndex( index.row() );
142 void QgsFeaturePickerWidget::onCurrentIndexChanged(
int i )
145 mIsCurrentlyEdited =
false;
147 mPreviousButton->setEnabled( i > 0 );
148 mNextButton->setEnabled( i < mComboBox->model()->rowCount() - 1 );
153 QModelIndex modelIndex = mModel->
index( i, 0, QModelIndex() );
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 );
162 void QgsFeaturePickerWidget::onActivated( QModelIndex modelIndex )
168 void QgsFeaturePickerWidget::storeLineEditState()
170 if ( mIsCurrentlyEdited )
176 void QgsFeaturePickerWidget::restoreLineEditState()
178 if ( mIsCurrentlyEdited )
196 void QgsFeaturePickerWidget::onDataChanged(
const QModelIndex &topLeft,
const QModelIndex &bottomRight,
const QVector<int> &roles )
199 if ( !mIsCurrentlyEdited )
202 if ( currentIndex >= topLeft.row() && currentIndex <= bottomRight.row() )
204 QModelIndex modelIndex = mModel->
index( currentIndex, 0, QModelIndex() );
210 void QgsFeaturePickerWidget::browseFeatures(
int direction )
212 int newIndex = std::min( std::max( 0, mComboBox->currentIndex() + direction ), mComboBox->model()->rowCount() - 1 );
213 mComboBox->setCurrentIndex( newIndex );
224 QWidget::focusOutEvent( event );
230 if ( event->key() == Qt::Key_Escape )
234 QWidget::keyReleaseEvent( event );
280 return mShowBrowserButtons;
289 mPreviousButton->setVisible( mShowBrowserButtons );
290 mNextButton->setVisible( mShowBrowserButtons );