22 #include "qgssettings.h"
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 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() )
427 if ( !attributes.isEmpty() )
430 attributes += lFilterExpression->referencedColumns();
431 attributes += requestedAttributesForStyle();
436 if ( !mFetchGeometry )
438 if ( mFetchLimit > 0 )
442 mGatherer->setData( mShouldReloadCurrentFeature );
443 connect( mGatherer, &QgsFeatureExpressionValuesGatherer::finished,
this, &QgsFeaturePickerModelBase::updateCompleter );
451 QSet<QString> QgsFeaturePickerModelBase::requestedAttributesForStyle()
const
453 QSet<QString> requestedAttrs;
460 requestedAttrs += exp.referencedColumns();
463 if ( mDisplayExpression.
isField() )
470 requestedAttrs += exp.referencedColumns();
474 return requestedAttrs;
478 void QgsFeaturePickerModelBase::setExtraIdentifierValueIndex(
int index,
bool force )
488 void QgsFeaturePickerModelBase::reloadCurrentFeature()
490 mShouldReloadCurrentFeature =
true;
491 mReloadTimer.start();
497 const QVector<QgsFeatureExpressionValuesGatherer::Entry> entries =
mEntries;
500 for (
const QgsFeatureExpressionValuesGatherer::Entry &entry : entries )
504 setExtraIdentifierValueIndex(
index );
515 if ( !isNull || mAllowNull )
517 beginInsertRows( QModelIndex(), 0, 0 );
521 setExtraValueDoesNotExist(
true );
522 reloadCurrentFeature();
526 mEntries.prepend( QgsFeatureExpressionValuesGatherer::nullEntry( mSourceLayer ) );
527 setExtraValueDoesNotExist(
false );
531 setExtraIdentifierValueIndex( 0,
true );
555 styles += matchingFieldStyles;
560 mEntryStylesMap.insert( fid, style );
585 return mFetchGeometry;
614 return mExtraValueDoesNotExist;
618 void QgsFeaturePickerModelBase::setExtraValueDoesNotExist(
bool extraValueDoesNotExist )
634 void QgsFeaturePickerModelBase::reload()
636 mReloadTimer.start();
645 if ( mIsSettingExtraIdentifierValue )
648 mIsSettingExtraIdentifierValue =
true;
654 mIsSettingExtraIdentifierValue =
false;
QgsConditionalStyles rowStyles() const
Returns a list of row styles associated with the layer.
QList< QgsConditionalStyle > fieldStyles(const QString &fieldName) const
Returns the conditional styles set for the field with matching fieldName.
Conditional styling for a rule.
static QgsConditionalStyle compressStyles(const QList< QgsConditionalStyle > &styles)
Compress a list of styles into a single style.
static QList< QgsConditionalStyle > matchingConditionalStyles(const QList< QgsConditionalStyle > &styles, const QVariant &value, QgsExpressionContext &context)
Find and return the matching styles for the value and feature.
QColor backgroundColor() const
The background color for style.
QColor textColor() const
The text color set for style.
QFont font() const
The font for the style.
bool validTextColor() const
Check if the text color is valid for render.
bool isValid() const
isValid Check if this rule is valid.
QPixmap icon() const
The icon set for style generated from the set symbol.
bool validBackgroundColor() const
Check if the background color is valid for render.
void setFeature(const QgsFeature &feature)
Convenience function for setting a feature for the context.
Class for parsing and evaluation of expressions (formerly called "search strings").
QString expression() const
Returns the original, unmodified expression string.
bool isField() const
Checks whether an expression consists only of a single field reference.
QSet< QString > referencedColumns() const
Gets list of columns referenced by the expression.
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.
virtual QVariant entryIdentifier(const QgsFeatureExpressionValuesGatherer::Entry &entry) const =0
Returns the identifier of the given entry.
void filterValueChanged()
This value will be used to filter the features available from this model.
void setFilterValue(const QString &filterValue)
This value will be used to filter the features available from this model.
void setExtraIdentifierValue(const QVariant &extraIdentifierValue)
Allows specifying one value that does not need to match the filter criteria but will still be availab...
virtual void requestToReloadCurrentFeature(QgsFeatureRequest &request)=0
Update the request to match the current feature to be reloaded.
void filterExpressionChanged()
An additional filter expression to apply, next to the filterValue.
void setFetchLimit(int fetchLimit)
Defines the feature request fetch limit If set to 0, no limit is applied when fetching.
QVariant extraIdentifierValue() const
Allows specifying one value that does not need to match the filter criteria but will still be availab...
void setFetchGeometry(bool fetchGeometry)
Defines if the geometry will be fetched.
void setExtraIdentifierValueUnguarded(const QVariant &identifierValue)
This will set the identifier value to be set in the model even if it doesn't exist currently in the d...
void extraIdentifierValueChanged()
Allows specifying one value that does not need to match the filter criteria but will still be availab...
int mExtraValueIndex
The current index.
QModelIndex parent(const QModelIndex &child) const override
void filterJobCompleted()
Indicates that a filter job has been completed and new data may be available.
void setAllowNull(bool allowNull)
Add a NULL entry to the list.
void setDisplayExpression(const QString &displayExpression)
The display expression will be used for.
QVariant mExtraIdentifierValue
The current identifier value.
QgsFeaturePickerModelBase(QObject *parent=nullptr)
Create a new QgsFeaturePickerModelBase, optionally specifying a parent.
virtual QgsFeatureExpressionValuesGatherer * createValuesGatherer(const QgsFeatureRequest &request) const =0
Creates the value gatherer.
virtual QgsFeatureExpressionValuesGatherer::Entry createEntry(const QVariant &identifier) const =0
Creates an entry with just the identifier so the feature can be retrieved in a next iteration.
bool isLoading() const
Indicator if the model is currently performing any feature iteration in the background.
virtual bool identifierIsNull(const QVariant &identifier) const =0
Returns true if the entry is null The identifier can be either the feature ID or the list of identifi...
QgsVectorLayer * sourceLayer
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.
~QgsFeaturePickerModelBase() override
virtual QSet< QString > requestedAttributes() const
Returns the attributes to be fetched in the request.
@ FeatureIdRole
Used to retrieve the id of a feature.
@ IdentifierValuesRole
Used to retrieve the identifierValues (primary keys) of a feature.
@ FeatureRole
Used to retrieve the feature, it might be incomplete if the request doesn't fetch all attributes or g...
@ ValueRole
Used to retrieve the displayExpression of a feature.
void setFilterExpression(const QString &filterExpression)
An additional filter expression to apply, next to the filterValue.
void allowNullChanged()
Add a NULL entry to the list.
int rowCount(const QModelIndex &parent) const override
virtual bool compareEntries(const QgsFeatureExpressionValuesGatherer::Entry &a, const QgsFeatureExpressionValuesGatherer::Entry &b) const =0
Returns true if the 2 entries refers to the same feature.
void currentFeatureChanged()
Emitted when the current feature in the model has changed This emitted both when the extra value chan...
void isLoadingChanged()
Indicator if the model is currently performing any feature iteration in the background.
QString displayExpression
QVector< QgsFeatureExpressionValuesGatherer::Entry > mEntries
virtual QVariant nullIdentifier() const =0
Returns a null identifier.
void endUpdate()
Notification that the model change is finished.
void displayExpressionChanged()
The display expression will be used for.
void extraValueDoesNotExistChanged()
Flag indicating that the extraIdentifierValue does not exist in the data.
void setSourceLayer(QgsVectorLayer *sourceLayer)
The source layer from which features will be fetched.
bool extraValueDoesNotExist() const
Flag indicating that the extraIdentifierValue does not exist in the data.
This class wraps a request for features to a vector layer (or directly its vector data provider).
QgsFeatureRequest & setLimit(long long limit)
Set the maximum number of features to request.
QgsExpression * filterExpression() const
Returns the filter expression if set.
QgsFeatureRequest & setFlags(QgsFeatureRequest::Flags flags)
Sets flags that affect how features will be fetched.
QgsFeatureRequest & setSubsetOfAttributes(const QgsAttributeList &attrs)
Set a subset of attributes that will be fetched.
QgsFeatureRequest & setFilterExpression(const QString &expression)
Set the filter expression.
@ NoGeometry
Geometry is not required. It may still be returned if e.g. required for a filter condition.
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
QVariant attribute(const QString &name) const
Lookup attribute value by attribute name.
Represents a vector layer which manages a vector based data sets.
QgsFields fields() const FINAL
Returns the list of fields of this layer.
QgsExpressionContext createExpressionContext() const FINAL
This method needs to be reimplemented in all classes which implement this interface and return an exp...
QString displayExpression
QgsConditionalLayerStyles * conditionalStyles() const
Returns the conditional styles that are set for this layer.
qint64 QgsFeatureId
64 bit feature ids negative numbers are used for uncommitted/newly added features