21 : QAbstractListModel( parent ),
22 mCurrentLayer( nullptr )
32 if ( parent.isValid() )
35 return mFeatures.count();
42 if ( !mFeatureTitleField.isEmpty() )
45 if ( !title.isEmpty() )
50 context.setFeature( featurePair.
feature() );
52 title = expr.evaluate( &context ).toString();
54 if ( title.isEmpty() )
62 int row = index.row();
63 if ( row < 0 || row >= mFeatures.count() )
66 if ( !index.isValid() )
75 case Feature:
return QVariant::fromValue<QgsFeature>( pair.
feature() );
79 case Qt::DisplayRole:
return featureTitle( pair );
87 if ( mSearchExpression.isEmpty() )
91 QStringList words = mSearchExpression.split(
' ', QString::SplitBehavior::SkipEmptyParts );
92 QStringList foundPairs;
94 for (
const QString &word : words )
100 if ( attrValue.toLower().indexOf( word.toLower() ) != -1 )
102 foundPairs <<
field.
name() +
": " + attrValue;
105 fields.remove( fields.lookupField(
field.
name() ) );
110 return foundPairs.join(
", " );
113 QString QgsQuickFeaturesListModel::buildSearchExpression()
115 if ( mSearchExpression.isEmpty() || !mCurrentLayer )
119 QStringList expressionParts;
120 QStringList wordExpressions;
122 QStringList words = mSearchExpression.split(
' ', QString::SplitBehavior::SkipEmptyParts );
124 for (
const QString &word : words )
126 bool searchExpressionIsNumeric;
127 int filterInt = word.toInt( &searchExpressionIsNumeric );
128 Q_UNUSED( filterInt );
135 else if (
field.
type() == QVariant::String )
138 wordExpressions << QStringLiteral(
"(%1)" ).arg( expressionParts.join( QLatin1String(
" ) OR ( " ) ) );
139 expressionParts.clear();
142 QString expression = QStringLiteral(
"(%1)" ).arg( wordExpressions.join( QLatin1String(
" ) AND ( " ) ) );
147 void QgsQuickFeaturesListModel::setupFeatureRequest(
QgsFeatureRequest &request )
149 if ( !mFilterExpression.isEmpty() && !mSearchExpression.isEmpty() )
154 else if ( !mSearchExpression.isEmpty() )
158 else if ( !mFilterExpression.isEmpty() )
178 void QgsQuickFeaturesListModel::loadFeaturesFromLayer(
QgsVectorLayer *layer )
180 if ( layer && layer->
isValid() )
181 mCurrentLayer = layer;
189 setupFeatureRequest( req );
216 setKeyField( fields.
field( config.value( QStringLiteral(
"Key" ) ).toString() ).name() );
222 loadFeaturesFromLayer( layer );
233 loadFeaturesFromLayer( layer );
239 loadFeaturesFromLayer();
242 void QgsQuickFeaturesListModel::emptyData()
245 mCurrentLayer =
nullptr;
247 mFeatureTitleField.clear();
248 mFilterExpression.clear();
249 mSearchExpression.clear();
255 QHash<int, QByteArray>
roleNames = QAbstractListModel::roleNames();
267 if ( mCurrentLayer && mCurrentLayer->
isValid() )
274 return mSearchExpression;
282 loadFeaturesFromLayer();
287 mFeatureTitleField = attribute;
292 mKeyField = attribute;
297 mFilterExpression = filterExpression;
302 if ( mCurrentFeature == feature )
305 mCurrentFeature = feature;
312 return mCurrentFeature;
317 return FEATURES_LIMIT;
322 for (
int i = 0; i < mFeatures.count(); ++i )
324 QVariant d =
data( index( i, 0 ), role );
335 for (
int i = 0; i < mFeatures.count(); ++i )
337 QVariant d =
data( index( i, 0 ), role );
340 QVariant key =
data( index( i, 0 ), requestedRole );
350 QList<QVariant> retList;
352 for (
const QString &i : list )
356 retList.append( var );
366 if ( i.feature().id() == featureId )
static QgsExpressionContextScope * formScope(const QgsFeature &formFeature=QgsFeature(), const QString &formMode=QString())
Creates a new scope which contains functions and variables from the current attribute form/table form...
static QList< QgsExpressionContextScope * > globalProjectLayerScopes(const QgsMapLayer *layer)
Creates a list of three scopes: global, layer's project and layer.
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
void appendScope(QgsExpressionContextScope *scope)
Appends a scope to the end of the context.
Class for parsing and evaluation of expressions (formerly called "search strings").
static QString quotedColumnRef(QString name)
Returns a quoted column reference (in double quotes)
Wrapper for iterator of features from vector data provider or vector layer.
bool nextFeature(QgsFeature &f)
This class wraps a request for features to a vector layer (or directly its vector data provider).
QgsFeatureRequest & combineFilterExpression(const QString &expression)
Modifies the existing filter expression to add an additional expression filter.
QgsFeatureRequest & setLimit(long limit)
Set the maximum number of features to request.
QgsFeatureRequest & setFilterExpression(const QString &expression)
Set the filter expression.
QgsFeatureRequest & setExpressionContext(const QgsExpressionContext &context)
Sets the expression context used to evaluate filter expressions.
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
bool isValid() const
Returns the validity of this feature.
QVariant attribute(const QString &name) const
Lookup attribute value from attribute name.
Encapsulate a field in an attribute table or data source.
Container of fields for a vector layer.
QgsField field(int fieldIdx) const
Gets field at particular index (must be in range 0..N-1)
static QgsProject * instance()
Returns the QgsProject singleton instance.
Pair of QgsFeature and QgsVectorLayer.
QgsFeature feature
Feature that belongs to layer.
Q_GADGET QgsVectorLayer * layer
Vector layer to which the feature belongs.
int rowCount(const QModelIndex &parent=QModelIndex()) const override
QString searchExpression
Search expression represents a filter used when querying for data in current layer.
QgsFeature currentFeature
Feature that has opened feature form.
~QgsQuickFeaturesListModel() override
Q_INVOKABLE void setupValueRelation(const QVariantMap &config)
setupValueRelation populates model with value relation data from config
QVariant data(const QModelIndex &index, int role=Qt::DisplayRole) const override
QHash< int, QByteArray > roleNames() const override
QgsQuickFeaturesListModel(QObject *parent=nullptr)
Create features list model.
void currentFeatureChanged(QgsFeature feature)
Signal emitted when current feature has changed.
int featuresCount
Read only property holding true number of features in layer - not only requested features Changing se...
Q_INVOKABLE int rowFromAttribute(const int role, const QVariant &value) const
rowFromAttribute finds feature with requested role and value, returns its row
void featuresCountChanged(int featuresCount)
featuresCountChanged Signal emitted when features are reloaded or layer is changed
void setKeyField(const QString &attribute)
Sets name of attribute used as "key" in value relation.
Q_INVOKABLE void populateFromLayer(QgsVectorLayer *layer)
populateFromLayer populates model with features from layer
Q_INVOKABLE QVariant attributeFromValue(const int role, const QVariant &value, const int requestedRole) const
attributeFromValue finds feature with role and value, returns value for requested role
@ KeyColumn
secondary text in list view
@ FoundPair
key in value relation
Q_INVOKABLE QgsQuickFeatureLayerPair featureLayerPair(const int &featureId)
Function to get QgsQuickFeatureLayerPair by feature id.
Q_INVOKABLE void reloadFeatures()
reloadFeatures reloads features from current layer
void setFeatureTitleField(const QString &attribute)
setFeatureTitleField Sets name of attribute that will be used for FeatureTitle and Qt::DisplayRole
void setSearchExpression(const QString &searchExpression)
setSearchExpression Sets search expression, upon setting also reloads features from current layer wit...
void setFilterExpression(const QString &filterExpression)
setFilterExpression Sets filter expression for current layer that will be used when querying for data
void searchExpressionChanged(QString searchExpression)
Signal emitted when search expression has changed.
int featuresLimit
Property limiting maximum number of features queried from layer Read only property.
void setCurrentFeature(QgsFeature feature)
Sets current feature property.
Q_INVOKABLE QVariant convertMultivalueFormat(const QVariant &multivalue, const int requestedRole=Qt::DisplayRole)
convertMultivalueFormat converts postgres string like string to an array of variants with requested r...
Represents a vector layer which manages a vector based data sets.
QgsFeatureIterator getFeatures(const QgsFeatureRequest &request=QgsFeatureRequest()) const FINAL
Queries the layer for features specified in request.
QgsFields fields() const FINAL
Returns the list of fields of this layer.
QString displayExpression
long featureCount(const QString &legendKey) const
Number of features rendered with specified legend key.