26 : QAbstractItemModel( parent )
28 mReloadTimer.setInterval( 100 );
29 mReloadTimer.setSingleShot(
true );
30 connect( &mReloadTimer, &QTimer::timeout,
this, &QgsFeaturePickerModelBase::scheduledReload );
37 connect( mGatherer, &QgsFeatureExpressionValuesGatherer::finished, mGatherer, &QgsFeatureExpressionValuesGatherer::deleteLater );
97 return mFilterExpression;
126 return createIndex( row, column,
nullptr );
133 return QModelIndex();
147 if ( !
index.isValid() )
152 case Qt::DisplayRole:
165 const QVariantList values =
mEntries.value(
index.row() ).identifierFields;
166 return values.value( 0 );
172 case Qt::BackgroundColorRole:
173 case Qt::TextColorRole:
174 case Qt::DecorationRole:
181 if ( role == Qt::TextColorRole )
183 return QBrush( QColor( Qt::gray ) );
185 if ( role == Qt::FontRole )
187 QFont font = QFont();
189 font.setBold(
true );
191 font.setItalic(
true );
206 if ( role == Qt::DecorationRole )
208 if ( role == Qt::FontRole )
220 void QgsFeaturePickerModelBase::updateCompleter()
224 QgsFeatureExpressionValuesGatherer *gatherer = qobject_cast<QgsFeatureExpressionValuesGatherer *>( sender() );
225 if ( gatherer->wasCanceled() )
231 QVector<QgsFeatureExpressionValuesGatherer::Entry> entries = mGatherer->entries();
239 bool reloadCurrentFeatureOnly = mGatherer->data().toBool();
240 if ( reloadCurrentFeatureOnly )
242 if ( !entries.isEmpty() )
246 mShouldReloadCurrentFeature =
false;
247 setExtraValueDoesNotExist(
false );
251 setExtraValueDoesNotExist(
true );
254 mKeepCurrentEntry =
true;
255 mShouldReloadCurrentFeature =
false;
257 if ( mFilterValue.isEmpty() )
263 std::sort( entries.begin(), entries.end(), [](
const QgsFeatureExpressionValuesGatherer::Entry & a,
const QgsFeatureExpressionValuesGatherer::Entry & b ) { return a.value.localeAwareCompare( b.value ) < 0; } );
267 entries.prepend( QgsFeatureExpressionValuesGatherer::nullEntry( mSourceLayer ) );
270 const int newEntriesSize = entries.size();
273 const int nbFixedEntry = ( mKeepCurrentEntry ? 1 : 0 ) + ( mAllowNull ? 1 : 0 );
276 int currentEntryInNewList = -1;
279 for (
int i = 0; i < newEntriesSize; ++i )
284 currentEntryInNewList = i;
285 setExtraValueDoesNotExist(
false );
307 beginRemoveRows( QModelIndex(), firstRow,
mEntries.size() - firstRow );
313 mIsSettingExtraIdentifierValue =
true;
315 mIsSettingExtraIdentifierValue =
false;
317 if ( currentEntryInNewList == -1 )
319 beginInsertRows( QModelIndex(), firstRow, entries.size() + 1 );
326 setExtraIdentifierValueIndex( firstRow == 0 && mAllowNull && !entries.isEmpty() ? 1 : 0, firstRow == 0 );
330 if ( currentEntryInNewList != 0 )
332 beginInsertRows( QModelIndex(), 0, currentEntryInNewList - 1 );
338 mEntries.replace( 0, entries.at( 0 ) );
342 if ( currentEntryInNewList >= nbFixedEntry )
344 emit dataChanged(
index( currentEntryInNewList, 0, QModelIndex() ),
index( currentEntryInNewList, 0, QModelIndex() ) );
347 beginInsertRows( QModelIndex(), currentEntryInNewList + 1, newEntriesSize - currentEntryInNewList - 1 );
348 mEntries += entries.mid( currentEntryInNewList + 1 );
350 setExtraIdentifierValueIndex( currentEntryInNewList );
355 mKeepCurrentEntry =
false;
361 Q_ASSERT( gatherer == mGatherer );
368 void QgsFeaturePickerModelBase::scheduledReload()
373 bool wasLoading =
false;
383 if ( mShouldReloadCurrentFeature )
389 QString filterClause;
391 if ( mFilterValue.isEmpty() && !mFilterExpression.isEmpty() )
392 filterClause = mFilterExpression;
393 else if ( mFilterExpression.isEmpty() && !mFilterValue.isEmpty() )
394 filterClause = QStringLiteral(
"(%1) ILIKE '%%2%'" ).arg( mDisplayExpression, mFilterValue );
395 else if ( !mFilterExpression.isEmpty() && !mFilterValue.isEmpty() )
396 filterClause = QStringLiteral(
"(%1) AND ((%2) ILIKE '%%3%')" ).arg( mFilterExpression, mDisplayExpression, mFilterValue );
398 if ( !filterClause.isEmpty() )
402 if ( !attributes.isEmpty() )
406 attributes += requestedAttributesForStyle();
411 if ( !mFetchGeometry )
413 if ( mFetchLimit > 0 )
417 mGatherer->setData( mShouldReloadCurrentFeature );
418 connect( mGatherer, &QgsFeatureExpressionValuesGatherer::finished,
this, &QgsFeaturePickerModelBase::updateCompleter );
426 QSet<QString> QgsFeaturePickerModelBase::requestedAttributesForStyle()
const
428 QSet<QString> requestedAttrs;
435 requestedAttrs += exp.referencedColumns();
438 if ( mDisplayExpression.
isField() )
445 requestedAttrs += exp.referencedColumns();
449 return requestedAttrs;
453 void QgsFeaturePickerModelBase::setExtraIdentifierValueIndex(
int index,
bool force )
463 void QgsFeaturePickerModelBase::reloadCurrentFeature()
465 mShouldReloadCurrentFeature =
true;
466 mReloadTimer.start();
472 const QVector<QgsFeatureExpressionValuesGatherer::Entry> entries =
mEntries;
475 for (
const QgsFeatureExpressionValuesGatherer::Entry &entry : entries )
479 setExtraIdentifierValueIndex(
index );
490 if ( !isNull || mAllowNull )
492 beginInsertRows( QModelIndex(), 0, 0 );
496 reloadCurrentFeature();
500 mEntries.prepend( QgsFeatureExpressionValuesGatherer::nullEntry( mSourceLayer ) );
504 setExtraIdentifierValueIndex( 0,
true );
528 styles += matchingFieldStyles;
533 mEntryStylesMap.insert( fid, style );
558 return mFetchGeometry;
587 return mExtraValueDoesNotExist;
591 void QgsFeaturePickerModelBase::setExtraValueDoesNotExist(
bool extraValueDoesNotExist )
607 void QgsFeaturePickerModelBase::reload()
609 mReloadTimer.start();
618 if ( mIsSettingExtraIdentifierValue )
621 mIsSettingExtraIdentifierValue =
true;
627 mIsSettingExtraIdentifierValue =
false;