26 : QAbstractItemModel( parent )
28 mReloadTimer.setInterval( 100 );
29 mReloadTimer.setSingleShot(
true );
30 connect( &mReloadTimer, &QTimer::timeout,
this, &QgsFeaturePickerModelBase::scheduledReload );
42 connect( mGatherer, &QgsFeatureExpressionValuesGatherer::finished, mGatherer, &QgsFeatureExpressionValuesGatherer::deleteLater );
105 return mFilterExpression;
134 return createIndex( row, column,
nullptr );
141 return QModelIndex();
155 if ( !
index.isValid() )
160 case Qt::DisplayRole:
173 const QVariantList values =
mEntries.value(
index.row() ).identifierFields;
174 return values.value( 0 );
180 #if QT_VERSION < QT_VERSION_CHECK(5, 13, 0)
181 case Qt::BackgroundColorRole:
182 case Qt::TextColorRole:
184 case Qt::BackgroundRole:
185 case Qt::ForegroundRole:
187 case Qt::DecorationRole:
194 #if QT_VERSION < QT_VERSION_CHECK(5, 13, 0)
195 if ( role == Qt::TextColorRole )
197 if ( role == Qt::ForegroundRole )
200 return QBrush( QColor( Qt::gray ) );
202 if ( role == Qt::FontRole )
204 QFont font = QFont();
206 font.setBold(
true );
208 font.setItalic(
true );
219 #if QT_VERSION < QT_VERSION_CHECK(5, 13, 0)
225 #if QT_VERSION < QT_VERSION_CHECK(5, 13, 0)
231 if ( role == Qt::DecorationRole )
233 if ( role == Qt::FontRole )
245 void QgsFeaturePickerModelBase::updateCompleter()
249 QgsFeatureExpressionValuesGatherer *gatherer = qobject_cast<QgsFeatureExpressionValuesGatherer *>( sender() );
250 if ( gatherer->wasCanceled() )
256 QVector<QgsFeatureExpressionValuesGatherer::Entry> entries = mGatherer->entries();
264 const bool reloadCurrentFeatureOnly = mGatherer->data().toBool();
265 if ( reloadCurrentFeatureOnly )
267 if ( !entries.isEmpty() )
271 mShouldReloadCurrentFeature =
false;
272 setExtraValueDoesNotExist(
false );
276 setExtraValueDoesNotExist(
true );
279 mKeepCurrentEntry =
true;
280 mShouldReloadCurrentFeature =
false;
282 if ( mFilterValue.isEmpty() )
288 std::sort( entries.begin(), entries.end(), [](
const QgsFeatureExpressionValuesGatherer::Entry & a,
const QgsFeatureExpressionValuesGatherer::Entry & b ) { return a.value.localeAwareCompare( b.value ) < 0; } );
290 if ( mAllowNull && mSourceLayer )
292 entries.prepend( QgsFeatureExpressionValuesGatherer::nullEntry( mSourceLayer ) );
295 const int newEntriesSize = entries.size();
298 const int nbFixedEntry = ( mKeepCurrentEntry ? 1 : 0 ) + ( mAllowNull ? 1 : 0 );
301 int currentEntryInNewList = -1;
304 for (
int i = 0; i < newEntriesSize; ++i )
309 currentEntryInNewList = i;
310 setExtraValueDoesNotExist(
false );
332 beginRemoveRows( QModelIndex(), firstRow,
mEntries.size() - firstRow );
338 mIsSettingExtraIdentifierValue =
true;
340 mIsSettingExtraIdentifierValue =
false;
342 if ( currentEntryInNewList == -1 )
344 beginInsertRows( QModelIndex(), firstRow, entries.size() + 1 );
351 setExtraIdentifierValueIndex( firstRow == 0 && mAllowNull && !entries.isEmpty() ? 1 : 0, firstRow == 0 );
355 if ( currentEntryInNewList != 0 )
357 beginInsertRows( QModelIndex(), 0, currentEntryInNewList - 1 );
363 mEntries.replace( 0, entries.at( 0 ) );
367 if ( currentEntryInNewList >= nbFixedEntry )
369 emit dataChanged(
index( currentEntryInNewList, 0, QModelIndex() ),
index( currentEntryInNewList, 0, QModelIndex() ) );
372 beginInsertRows( QModelIndex(), currentEntryInNewList + 1, newEntriesSize - currentEntryInNewList - 1 );
373 mEntries += entries.mid( currentEntryInNewList + 1 );
375 setExtraIdentifierValueIndex( currentEntryInNewList );
380 mKeepCurrentEntry =
false;
386 Q_ASSERT( gatherer == mGatherer );
393 void QgsFeaturePickerModelBase::scheduledReload()
398 bool wasLoading =
false;
408 if ( mShouldReloadCurrentFeature )
414 QString filterClause;
416 if ( mFilterValue.isEmpty() && !mFilterExpression.isEmpty() )
417 filterClause = mFilterExpression;
418 else if ( mFilterExpression.isEmpty() && !mFilterValue.isEmpty() )
419 filterClause = QStringLiteral(
"(%1) ILIKE '%%2%'" ).arg( mDisplayExpression, mFilterValue );
420 else if ( !mFilterExpression.isEmpty() && !mFilterValue.isEmpty() )
421 filterClause = QStringLiteral(
"(%1) AND ((%2) ILIKE '%%3%')" ).arg( mFilterExpression, mDisplayExpression, mFilterValue );
423 if ( !filterClause.isEmpty() )
430 if ( !attributes.isEmpty() )
433 attributes += lFilterExpression->referencedColumns();
434 attributes += requestedAttributesForStyle();
439 if ( !mFetchGeometry )
441 if ( mFetchLimit > 0 )
445 mGatherer->setData( mShouldReloadCurrentFeature );
446 connect( mGatherer, &QgsFeatureExpressionValuesGatherer::finished,
this, &QgsFeaturePickerModelBase::updateCompleter );
454 QSet<QString> QgsFeaturePickerModelBase::requestedAttributesForStyle()
const
456 QSet<QString> requestedAttrs;
463 requestedAttrs += exp.referencedColumns();
466 if ( mDisplayExpression.
isField() )
473 requestedAttrs += exp.referencedColumns();
477 return requestedAttrs;
481 void QgsFeaturePickerModelBase::setExtraIdentifierValueIndex(
int index,
bool force )
491 void QgsFeaturePickerModelBase::reloadCurrentFeature()
493 mShouldReloadCurrentFeature =
true;
494 mReloadTimer.start();
500 const QVector<QgsFeatureExpressionValuesGatherer::Entry> entries =
mEntries;
503 for (
const QgsFeatureExpressionValuesGatherer::Entry &entry : entries )
507 setExtraIdentifierValueIndex(
index );
518 if ( !isNull || mAllowNull )
520 beginInsertRows( QModelIndex(), 0, 0 );
524 setExtraValueDoesNotExist(
true );
525 reloadCurrentFeature();
529 mEntries.prepend( QgsFeatureExpressionValuesGatherer::nullEntry( mSourceLayer ) );
530 setExtraValueDoesNotExist(
false );
534 setExtraIdentifierValueIndex( 0,
true );
558 styles += matchingFieldStyles;
563 mEntryStylesMap.insert( fid, style );
588 return mFetchGeometry;
617 return mExtraValueDoesNotExist;
621 void QgsFeaturePickerModelBase::setExtraValueDoesNotExist(
bool extraValueDoesNotExist )
637 void QgsFeaturePickerModelBase::reload()
639 mReloadTimer.start();
648 if ( mIsSettingExtraIdentifierValue )
651 mIsSettingExtraIdentifierValue =
true;
657 mIsSettingExtraIdentifierValue =
false;