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 #if QT_VERSION < QT_VERSION_CHECK(5, 13, 0)
173 case Qt::BackgroundColorRole:
174 case Qt::TextColorRole:
176 case Qt::BackgroundRole:
177 case Qt::ForegroundRole:
179 case Qt::DecorationRole:
186 #if QT_VERSION < QT_VERSION_CHECK(5, 13, 0)
187 if ( role == Qt::TextColorRole )
189 if ( role == Qt::ForegroundRole )
192 return QBrush( QColor( Qt::gray ) );
194 if ( role == Qt::FontRole )
196 QFont font = QFont();
198 font.setBold(
true );
200 font.setItalic(
true );
211 #if QT_VERSION < QT_VERSION_CHECK(5, 13, 0)
217 #if QT_VERSION < QT_VERSION_CHECK(5, 13, 0)
223 if ( role == Qt::DecorationRole )
225 if ( role == Qt::FontRole )
237 void QgsFeaturePickerModelBase::updateCompleter()
241 QgsFeatureExpressionValuesGatherer *gatherer = qobject_cast<QgsFeatureExpressionValuesGatherer *>( sender() );
242 if ( gatherer->wasCanceled() )
248 QVector<QgsFeatureExpressionValuesGatherer::Entry> entries = mGatherer->entries();
256 bool reloadCurrentFeatureOnly = mGatherer->data().toBool();
257 if ( reloadCurrentFeatureOnly )
259 if ( !entries.isEmpty() )
263 mShouldReloadCurrentFeature =
false;
264 setExtraValueDoesNotExist(
false );
268 setExtraValueDoesNotExist(
true );
271 mKeepCurrentEntry =
true;
272 mShouldReloadCurrentFeature =
false;
274 if ( mFilterValue.isEmpty() )
280 std::sort( entries.begin(), entries.end(), [](
const QgsFeatureExpressionValuesGatherer::Entry & a,
const QgsFeatureExpressionValuesGatherer::Entry & b ) { return a.value.localeAwareCompare( b.value ) < 0; } );
284 entries.prepend( QgsFeatureExpressionValuesGatherer::nullEntry( mSourceLayer ) );
287 const int newEntriesSize = entries.size();
290 const int nbFixedEntry = ( mKeepCurrentEntry ? 1 : 0 ) + ( mAllowNull ? 1 : 0 );
293 int currentEntryInNewList = -1;
296 for (
int i = 0; i < newEntriesSize; ++i )
301 currentEntryInNewList = i;
302 setExtraValueDoesNotExist(
false );
324 beginRemoveRows( QModelIndex(), firstRow,
mEntries.size() - firstRow );
330 mIsSettingExtraIdentifierValue =
true;
332 mIsSettingExtraIdentifierValue =
false;
334 if ( currentEntryInNewList == -1 )
336 beginInsertRows( QModelIndex(), firstRow, entries.size() + 1 );
343 setExtraIdentifierValueIndex( firstRow == 0 && mAllowNull && !entries.isEmpty() ? 1 : 0, firstRow == 0 );
347 if ( currentEntryInNewList != 0 )
349 beginInsertRows( QModelIndex(), 0, currentEntryInNewList - 1 );
355 mEntries.replace( 0, entries.at( 0 ) );
359 if ( currentEntryInNewList >= nbFixedEntry )
361 emit dataChanged(
index( currentEntryInNewList, 0, QModelIndex() ),
index( currentEntryInNewList, 0, QModelIndex() ) );
364 beginInsertRows( QModelIndex(), currentEntryInNewList + 1, newEntriesSize - currentEntryInNewList - 1 );
365 mEntries += entries.mid( currentEntryInNewList + 1 );
367 setExtraIdentifierValueIndex( currentEntryInNewList );
372 mKeepCurrentEntry =
false;
378 Q_ASSERT( gatherer == mGatherer );
385 void QgsFeaturePickerModelBase::scheduledReload()
390 bool wasLoading =
false;
400 if ( mShouldReloadCurrentFeature )
406 QString filterClause;
408 if ( mFilterValue.isEmpty() && !mFilterExpression.isEmpty() )
409 filterClause = mFilterExpression;
410 else if ( mFilterExpression.isEmpty() && !mFilterValue.isEmpty() )
411 filterClause = QStringLiteral(
"(%1) ILIKE '%%2%'" ).arg( mDisplayExpression, mFilterValue );
412 else if ( !mFilterExpression.isEmpty() && !mFilterValue.isEmpty() )
413 filterClause = QStringLiteral(
"(%1) AND ((%2) ILIKE '%%3%')" ).arg( mFilterExpression, mDisplayExpression, mFilterValue );
415 if ( !filterClause.isEmpty() )
419 if ( !attributes.isEmpty() )
422 attributes += lFilterExpression->referencedColumns();
423 attributes += requestedAttributesForStyle();
428 if ( !mFetchGeometry )
430 if ( mFetchLimit > 0 )
434 mGatherer->setData( mShouldReloadCurrentFeature );
435 connect( mGatherer, &QgsFeatureExpressionValuesGatherer::finished,
this, &QgsFeaturePickerModelBase::updateCompleter );
443 QSet<QString> QgsFeaturePickerModelBase::requestedAttributesForStyle()
const
445 QSet<QString> requestedAttrs;
452 requestedAttrs += exp.referencedColumns();
455 if ( mDisplayExpression.
isField() )
462 requestedAttrs += exp.referencedColumns();
466 return requestedAttrs;
470 void QgsFeaturePickerModelBase::setExtraIdentifierValueIndex(
int index,
bool force )
480 void QgsFeaturePickerModelBase::reloadCurrentFeature()
482 mShouldReloadCurrentFeature =
true;
483 mReloadTimer.start();
489 const QVector<QgsFeatureExpressionValuesGatherer::Entry> entries =
mEntries;
492 for (
const QgsFeatureExpressionValuesGatherer::Entry &entry : entries )
496 setExtraIdentifierValueIndex(
index );
507 if ( !isNull || mAllowNull )
509 beginInsertRows( QModelIndex(), 0, 0 );
513 reloadCurrentFeature();
517 mEntries.prepend( QgsFeatureExpressionValuesGatherer::nullEntry( mSourceLayer ) );
521 setExtraIdentifierValueIndex( 0,
true );
545 styles += matchingFieldStyles;
550 mEntryStylesMap.insert( fid, style );
575 return mFetchGeometry;
604 return mExtraValueDoesNotExist;
608 void QgsFeaturePickerModelBase::setExtraValueDoesNotExist(
bool extraValueDoesNotExist )
624 void QgsFeaturePickerModelBase::reload()
626 mReloadTimer.start();
635 if ( mIsSettingExtraIdentifierValue )
638 mIsSettingExtraIdentifierValue =
true;
644 mIsSettingExtraIdentifierValue =
false;