QGIS API Documentation 4.1.0-Master (5bf3c20f3c9)
Loading...
Searching...
No Matches
qgsfeaturelistmodel.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsfeaturelistmodel.cpp
3 ---------------------
4 begin : February 2013
5 copyright : (C) 2013 by Matthias Kuhn
6 email : matthias at opengis dot ch
7 ***************************************************************************
8 * *
9 * This program is free software; you can redistribute it and/or modify *
10 * it under the terms of the GNU General Public License as published by *
11 * the Free Software Foundation; either version 2 of the License, or *
12 * (at your option) any later version. *
13 * *
14 ***************************************************************************/
15#include "qgsfeaturelistmodel.h"
16
17#include "qgsapplication.h"
20#include "qgsexception.h"
22#include "qgsvectorlayercache.h"
24
25#include <QItemSelection>
26#include <QSettings>
27
28#include "moc_qgsfeaturelistmodel.cpp"
29
31 : QSortFilterProxyModel( parent )
32{
33 setSourceModel( sourceModel );
34}
35
37{
38 if ( mSourceLayer )
39 disconnect( mSourceLayer->conditionalStyles(), &QgsConditionalLayerStyles::changed, this, &QgsFeatureListModel::conditionalStylesChanged );
40
41 QSortFilterProxyModel::setSourceModel( sourceModel );
42 mExpressionContext = sourceModel->layer()->createExpressionContext();
43 mFilterModel = sourceModel;
44
45 mSourceLayer = sourceModel->layer();
46 connect( mSourceLayer->conditionalStyles(), &QgsConditionalLayerStyles::changed, this, &QgsFeatureListModel::conditionalStylesChanged );
47}
48
50{
51 return mFilterModel->layerCache();
52}
53
54QgsFeatureId QgsFeatureListModel::idxToFid( const QModelIndex &index ) const
55{
56 return mFilterModel->masterModel()->rowToId( mapToMaster( index ).row() );
57}
58
59QModelIndex QgsFeatureListModel::fidToIdx( const QgsFeatureId fid ) const
60{
61 return mapFromMaster( mFilterModel->masterModel()->idToIndex( fid ) );
62}
63
64QVariant QgsFeatureListModel::data( const QModelIndex &index, int role ) const
65{
66 if ( mInjectNull && index.row() == 0 )
67 {
68 if ( role == Qt::DisplayRole )
69 {
71 }
72 else
73 {
74 return QgsVariantUtils::createNullVariant( QMetaType::Type::UnknownType );
75 }
76 }
77
78 if ( role == Qt::DisplayRole || role == Qt::EditRole )
79 {
80 QgsFeature feat;
81
82 mFilterModel->layerCache()->featureAtIdWithAllAttributes( idxToFid( index ), feat );
83
84 mExpressionContext.setFeature( feat );
85 return mDisplayExpression.evaluate( &mExpressionContext );
86 }
87
88 if ( role == FeatureInfoRole )
89 {
90 FeatureInfo featInfo;
91
92 QgsFeature feat;
93
94 mFilterModel->layerCache()->featureAtIdWithAllAttributes( idxToFid( index ), feat );
95
96 QgsVectorLayerEditBuffer *editBuffer = mFilterModel->layer()->editBuffer();
97
98 if ( editBuffer )
99 {
100 if ( editBuffer->isFeatureAdded( feat.id() ) )
101 {
102 featInfo.isNew = true;
103 }
104 if ( editBuffer->isFeatureAttributesChanged( feat.id() ) )
105 {
106 featInfo.isEdited = true;
107 }
108 }
109
110 return QVariant::fromValue( featInfo );
111 }
112 else if ( role == FeatureRole )
113 {
114 QgsFeature feat;
115
116 mFilterModel->layerCache()->featureAtIdWithAllAttributes( idxToFid( index ), feat );
117
118 return QVariant::fromValue( feat );
119 }
120 else if ( role == FeatureWithGeometryRole )
121 {
122 QgsFeature feat;
123
124 mFilterModel->layerCache()->completeFeatureAtId( idxToFid( index ), feat );
125
126 return QVariant::fromValue( feat );
127 }
128 else if ( role == Qt::TextAlignmentRole )
129 {
130 return static_cast<Qt::Alignment::Int>( Qt::AlignLeft );
131 }
132
133 if ( role == Qt::BackgroundRole || role == Qt::ForegroundRole || role == Qt::DecorationRole || role == Qt::FontRole )
134 {
135 QgsVectorLayer *layer = mFilterModel->layer();
136 QgsFeature feat;
137 const QgsFeatureId fid = idxToFid( index );
138 mFilterModel->layerCache()->featureAtIdWithAllAttributes( fid, feat );
139 mExpressionContext.setFeature( feat );
140 QList<QgsConditionalStyle> styles;
141
142 if ( mRowStylesMap.contains( fid ) )
143 {
144 styles = mRowStylesMap.value( fid );
145 }
146 else
147 {
148 styles = QgsConditionalStyle::matchingConditionalStyles( layer->conditionalStyles()->rowStyles(), QVariant(), mExpressionContext );
149 mRowStylesMap.insert( fid, styles );
150 }
151
153
154 if ( mDisplayExpression.isField() )
155 {
156 const QString fieldName = *mDisplayExpression.referencedColumns().constBegin();
157 styles = layer->conditionalStyles()->fieldStyles( fieldName );
158 styles = QgsConditionalStyle::matchingConditionalStyles( styles, feat.attribute( fieldName ), mExpressionContext );
159 }
160
161 styles.insert( 0, rowstyle );
162
164
165 if ( style.isValid() )
166 {
167 if ( role == Qt::BackgroundRole && style.validBackgroundColor() )
168 return style.backgroundColor().isValid() ? style.backgroundColor() : QVariant();
169 if ( role == Qt::ForegroundRole && style.validTextColor() )
170 return style.textColor().isValid() ? style.textColor() : QVariant();
171 if ( role == Qt::DecorationRole )
172 return style.icon().isNull() ? QVariant() : style.icon();
173 if ( role == Qt::FontRole )
174 return style.font();
175 }
176
177 return QVariant();
178 }
179
180 return sourceModel()->data( mapToSource( index ), role );
181}
182
183Qt::ItemFlags QgsFeatureListModel::flags( const QModelIndex &index ) const
184{
185 if ( mInjectNull && index.row() == 0 )
186 {
187 return Qt::ItemIsSelectable | Qt::ItemIsEnabled;
188 }
189 else
190 {
191 return sourceModel()->flags( mapToSource( index ) ) & ~Qt::ItemIsEditable;
192 }
193}
194
196{
197 if ( mInjectNull == injectNull )
198 return;
199
200 if ( injectNull )
202
203 beginResetModel();
204 mInjectNull = injectNull;
205 endResetModel();
206}
207
209{
210 return mInjectNull;
211}
212
214{
215 return mFilterModel->masterModel();
216}
217
218bool QgsFeatureListModel::setDisplayExpression( const QString &expression )
219{
220 QgsExpression exp = QgsExpression( expression );
221
222 exp.prepare( &mExpressionContext );
223
224 if ( exp.hasParserError() )
225 {
226 mParserErrorString = exp.parserErrorString();
227 return false;
228 }
229
230 mDisplayExpression = exp;
231
232 if ( mSortByDisplayExpression )
233 {
235 }
236
237 emit dataChanged( index( 0, 0 ), index( rowCount() - 1, 0 ) );
238
239 invalidate();
240 return true;
241}
242
244{
245 return mParserErrorString;
246}
247
249{
250 return mDisplayExpression.expression();
251}
252
253bool QgsFeatureListModel::featureByIndex( const QModelIndex &index, QgsFeature &feat )
254{
255 return mFilterModel->layerCache()->featureAtIdWithAllAttributes( idxToFid( index ), feat );
256}
257
258void QgsFeatureListModel::onBeginRemoveRows( const QModelIndex &parent, int first, int last )
259{
260 beginRemoveRows( parent, first, last );
261}
262
263void QgsFeatureListModel::onEndRemoveRows( const QModelIndex &parent, int first, int last )
264{
265 Q_UNUSED( parent )
266 Q_UNUSED( first )
267 Q_UNUSED( last )
268 endRemoveRows();
269}
270
271void QgsFeatureListModel::onBeginInsertRows( const QModelIndex &parent, int first, int last )
272{
273 beginInsertRows( parent, first, last );
274}
275
276void QgsFeatureListModel::onEndInsertRows( const QModelIndex &parent, int first, int last )
277{
278 Q_UNUSED( parent )
279 Q_UNUSED( first )
280 Q_UNUSED( last )
281 endInsertRows();
282}
283
284void QgsFeatureListModel::conditionalStylesChanged()
285{
286 mRowStylesMap.clear();
287 emit dataChanged( index( 0, 0 ), index( rowCount() - 1, columnCount() - 1 ) );
288}
289
291{
292 return mSortByDisplayExpression;
293}
294
296{
297 if ( !mSortByDisplayExpression && sortByDisplayExpression )
298 {
299 masterModel()->prefetchSortData( mDisplayExpression.expression(), QGSFEATURELISTMODEL_CACHE_INDEX );
300 }
301
302 mSortByDisplayExpression = sortByDisplayExpression;
303
304 // If we are sorting by display expression, we do not support injected null
305 if ( mSortByDisplayExpression )
306 {
307 setInjectNull( false );
308 }
309
310 setSortRole( static_cast<int>( QgsAttributeTableModel::CustomRole::Sort ) + 1 );
311 setDynamicSortFilter( mSortByDisplayExpression );
312 sort( 0, order );
313}
314
315QModelIndex QgsFeatureListModel::mapToMaster( const QModelIndex &proxyIndex ) const
316{
317 QModelIndex masterIndex;
318
319 if ( proxyIndex.isValid() )
320 {
321 if ( mSortByDisplayExpression )
322 {
323 masterIndex = mFilterModel->mapToMaster( mapToSource( proxyIndex ) );
324 }
325 else
326 {
327 const int offset = mInjectNull ? 1 : 0;
328
329 masterIndex = mFilterModel->mapToMaster( mFilterModel->index( proxyIndex.row() - offset, proxyIndex.column() ) );
330 }
331 }
332 return masterIndex;
333}
334
335QModelIndex QgsFeatureListModel::mapFromMaster( const QModelIndex &masterIndex ) const
336{
337 QModelIndex proxyIndex;
338
339 if ( masterIndex.isValid() )
340 {
341 if ( mSortByDisplayExpression )
342 {
343 proxyIndex = mapFromSource( mFilterModel->mapFromMaster( masterIndex ) );
344 }
345 else
346 {
347 const int offset = mInjectNull ? 1 : 0;
348
349 return createIndex( mFilterModel->mapFromMaster( masterIndex ).row() + offset, 0 );
350 }
351 }
352
353 return proxyIndex;
354}
355
356QItemSelection QgsFeatureListModel::mapSelectionFromMaster( const QItemSelection &selection ) const
357{
358 return mapSelectionFromSource( mFilterModel->mapSelectionFromSource( selection ) );
359}
360
361QItemSelection QgsFeatureListModel::mapSelectionToMaster( const QItemSelection &selection ) const
362{
363 return mFilterModel->mapSelectionToSource( mapSelectionToSource( selection ) );
364}
365
366// Override some methods from QAbstractProxyModel, not that interesting
367
368QModelIndex QgsFeatureListModel::mapToSource( const QModelIndex &proxyIndex ) const
369{
370 QModelIndex sourceIndex;
371
372 if ( mSortByDisplayExpression )
373 {
374 sourceIndex = QSortFilterProxyModel::mapToSource( proxyIndex );
375 }
376 else
377 {
378 if ( !proxyIndex.isValid() )
379 return QModelIndex();
380
381 const int offset = mInjectNull ? 1 : 0;
382
383 sourceIndex = sourceModel()->index( proxyIndex.row() - offset, proxyIndex.column() );
384 }
385
386 return sourceIndex;
387}
388
389QModelIndex QgsFeatureListModel::mapFromSource( const QModelIndex &sourceIndex ) const
390{
391 QModelIndex proxyIndex;
392
393 if ( mSortByDisplayExpression )
394 {
395 proxyIndex = QSortFilterProxyModel::mapFromSource( sourceIndex );
396 }
397 else
398 {
399 if ( sourceIndex.isValid() )
400 proxyIndex = createIndex( sourceIndex.row(), 0 );
401 }
402
403 return proxyIndex;
404}
405
406QModelIndex QgsFeatureListModel::parent( const QModelIndex &child ) const
407{
408 Q_UNUSED( child )
409 return QModelIndex();
410}
411
412int QgsFeatureListModel::columnCount( const QModelIndex &parent ) const
413{
414 Q_UNUSED( parent )
415 return 1;
416}
417
418int QgsFeatureListModel::rowCount( const QModelIndex &parent ) const
419{
420 Q_UNUSED( parent )
421
422 const int offset = mInjectNull ? 1 : 0;
423
424 return sourceModel()->rowCount() + offset;
425}
426
428{
429 return mapFromMaster( masterModel()->idToIndex( fid ) );
430}
431
433{
434 return QModelIndexList() << fidToIndex( fid );
435}
static QString nullRepresentation()
Returns the string used to represent the value NULL throughout QGIS.
A proxy model for filtering an attribute table model.
QgsVectorLayer * layer() const
Returns the layer this filter acts on.
A model backed by a QgsVectorLayerCache which is able to provide feature/attribute information to a Q...
void prefetchSortData(const QString &expression, unsigned long cacheIndex=0)
Prefetches the entire data for an expression.
@ Sort
Role used for sorting start here.
void changed()
Emitted when the conditional styles are changed.
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.
Handles parsing and evaluation of expressions (formerly called "search strings").
bool prepare(const QgsExpressionContext *context)
Gets the expression ready for evaluation - find out column indexes.
bool hasParserError() const
Returns true if an error occurred when parsing the input expression.
QString parserErrorString() const
Returns parser error.
QgsFeatureId idxToFid(const QModelIndex &index) const
Returns the feature ID corresponding to an index from the model.
virtual void setSourceModel(QgsAttributeTableFilterModel *sourceModel)
void setInjectNull(bool injectNull)
If true is specified, a NULL value will be injected.
Q_DECL_DEPRECATED void onEndRemoveRows(const QModelIndex &parent, int first, int last)
Does nothing except for calling endRemoveRows().
Q_DECL_DEPRECATED void onBeginInsertRows(const QModelIndex &parent, int first, int last)
Does nothing except for calling beginInsertRows().
Qt::ItemFlags flags(const QModelIndex &index) const override
int rowCount(const QModelIndex &parent=QModelIndex()) const override
QModelIndexList fidToIndexList(QgsFeatureId fid)
bool featureByIndex(const QModelIndex &index, QgsFeature &feat)
virtual QModelIndex mapToMaster(const QModelIndex &proxyIndex) const
void setSortByDisplayExpression(bool sortByDisplayExpression, Qt::SortOrder order=Qt::AscendingOrder)
Sort this model by its display expression.
QModelIndex fidToIdx(QgsFeatureId fid) const
Returns the model index corresponding to a feature ID.
QString parserErrorString()
Returns a detailed message about errors while parsing a QgsExpression.
Q_DECL_DEPRECATED void onBeginRemoveRows(const QModelIndex &parent, int first, int last)
Does nothing except for calling beginRemoveRows().
QVariant data(const QModelIndex &index, int role) const override
bool injectNull()
Returns the current state of null value injection.
virtual QItemSelection mapSelectionToMaster(const QItemSelection &selection) const
bool sortByDisplayExpression() const
Sort this model by its display expression.
QModelIndex parent(const QModelIndex &child) const override
bool setDisplayExpression(const QString &expression)
Sets the display expression.
virtual QModelIndex mapFromMaster(const QModelIndex &sourceIndex) const
QString displayExpression() const
QModelIndex mapFromSource(const QModelIndex &sourceIndex) const override
QgsVectorLayerCache * layerCache()
Returns the vector layer cache which is being used to populate the model.
QModelIndex fidToIndex(QgsFeatureId fid) override
int columnCount(const QModelIndex &parent=QModelIndex()) const override
QgsFeatureListModel(QgsAttributeTableFilterModel *sourceModel, QObject *parent=nullptr)
Constructor for QgsFeatureListModel.
QModelIndex mapToSource(const QModelIndex &proxyIndex) const override
Q_DECL_DEPRECATED void onEndInsertRows(const QModelIndex &parent, int first, int last)
Does nothing except for calling endInsertRows().
@ FeatureWithGeometryRole
Feature with all attributes and geometry,.
@ FeatureRole
Feature with all attributes and no geometry.
virtual QItemSelection mapSelectionFromMaster(const QItemSelection &selection) const
QgsAttributeTableModel * masterModel()
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
Definition qgsfeature.h:60
QgsFeatureId id
Definition qgsfeature.h:68
Q_INVOKABLE QVariant attribute(const QString &name) const
Lookup attribute value by attribute name.
static QVariant createNullVariant(QMetaType::Type metaType)
Helper method to properly create a null QVariant from a metaType Returns the created QVariant.
Caches features for a given QgsVectorLayer.
Stores queued vector layer edit operations prior to committing changes to the layer's data provider.
bool isFeatureAdded(QgsFeatureId id) const
Returns true if the specified feature ID has been added but not committed.
bool isFeatureAttributesChanged(QgsFeatureId id) const
Returns true if the specified feature ID has had an attribute changed but not committed.
Represents a vector layer which manages a vector based dataset.
QgsExpressionContext createExpressionContext() const final
This method needs to be reimplemented in all classes which implement this interface and return an exp...
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
#define QGSFEATURELISTMODEL_CACHE_INDEX
bool isEdited
True if feature has been edited.
bool isNew
True if feature is a newly added feature.