25#include <QItemSelection>
28#include "moc_qgsfeaturelistmodel.cpp"
31 : QSortFilterProxyModel(
parent )
41 QSortFilterProxyModel::setSourceModel( sourceModel );
43 mFilterModel = sourceModel;
45 mSourceLayer = sourceModel->
layer();
51 return mFilterModel->layerCache();
56 return mFilterModel->masterModel()->rowToId(
mapToMaster( index ).row() );
61 return mapFromMaster( mFilterModel->masterModel()->idToIndex( fid ) );
66 if ( mInjectNull && index.row() == 0 )
68 if ( role == Qt::DisplayRole )
78 if ( role == Qt::DisplayRole || role == Qt::EditRole )
82 mFilterModel->layerCache()->featureAtIdWithAllAttributes(
idxToFid( index ), feat );
84 mExpressionContext.setFeature( feat );
85 return mDisplayExpression.evaluate( &mExpressionContext );
94 mFilterModel->layerCache()->featureAtIdWithAllAttributes(
idxToFid( index ), feat );
102 featInfo.
isNew =
true;
110 return QVariant::fromValue( featInfo );
116 mFilterModel->layerCache()->featureAtIdWithAllAttributes(
idxToFid( index ), feat );
118 return QVariant::fromValue( feat );
124 mFilterModel->layerCache()->completeFeatureAtId(
idxToFid( index ), feat );
126 return QVariant::fromValue( feat );
128 else if ( role == Qt::TextAlignmentRole )
130 return static_cast<Qt::Alignment::Int
>( Qt::AlignLeft );
133 if ( role == Qt::BackgroundRole
134 || role == Qt::ForegroundRole
135 || role == Qt::DecorationRole
136 || role == Qt::FontRole )
141 mFilterModel->layerCache()->featureAtIdWithAllAttributes( fid, feat );
142 mExpressionContext.setFeature( feat );
143 QList<QgsConditionalStyle> styles;
145 if ( mRowStylesMap.contains( fid ) )
147 styles = mRowStylesMap.value( fid );
152 mRowStylesMap.insert( fid, styles );
157 if ( mDisplayExpression.isField() )
159 const QString fieldName = *mDisplayExpression.referencedColumns().constBegin();
164 styles.insert( 0, rowstyle );
174 if ( role == Qt::DecorationRole )
175 return style.
icon().isNull() ? QVariant() : style.
icon();
176 if ( role == Qt::FontRole )
183 return sourceModel()->data(
mapToSource( index ), role );
188 if ( mInjectNull && index.row() == 0 )
190 return Qt::ItemIsSelectable | Qt::ItemIsEnabled;
194 return sourceModel()->flags(
mapToSource( index ) ) & ~Qt::ItemIsEditable;
218 return mFilterModel->masterModel();
225 exp.
prepare( &mExpressionContext );
233 mDisplayExpression = exp;
235 if ( mSortByDisplayExpression )
240 emit dataChanged( index( 0, 0 ), index(
rowCount() - 1, 0 ) );
248 return mParserErrorString;
253 return mDisplayExpression.expression();
258 return mFilterModel->layerCache()->featureAtIdWithAllAttributes(
idxToFid( index ), feat );
263 beginRemoveRows(
parent, first, last );
276 beginInsertRows(
parent, first, last );
287void QgsFeatureListModel::conditionalStylesChanged()
289 mRowStylesMap.clear();
295 return mSortByDisplayExpression;
308 if ( mSortByDisplayExpression )
314 setDynamicSortFilter( mSortByDisplayExpression );
320 QModelIndex masterIndex;
322 if ( proxyIndex.isValid() )
324 if ( mSortByDisplayExpression )
326 masterIndex = mFilterModel->mapToMaster(
mapToSource( proxyIndex ) );
330 const int offset = mInjectNull ? 1 : 0;
332 masterIndex = mFilterModel->mapToMaster( mFilterModel->index( proxyIndex.row() - offset, proxyIndex.column() ) );
340 QModelIndex proxyIndex;
342 if ( masterIndex.isValid() )
344 if ( mSortByDisplayExpression )
346 proxyIndex =
mapFromSource( mFilterModel->mapFromMaster( masterIndex ) );
350 const int offset = mInjectNull ? 1 : 0;
352 return createIndex( mFilterModel->mapFromMaster( masterIndex ).row() + offset, 0 );
361 return mapSelectionFromSource( mFilterModel->mapSelectionFromSource( selection ) );
366 return mFilterModel->mapSelectionToSource( mapSelectionToSource( selection ) );
373 QModelIndex sourceIndex;
375 if ( mSortByDisplayExpression )
377 sourceIndex = QSortFilterProxyModel::mapToSource( proxyIndex );
381 if ( !proxyIndex.isValid() )
382 return QModelIndex();
384 const int offset = mInjectNull ? 1 : 0;
386 sourceIndex = sourceModel()->index( proxyIndex.row() - offset, proxyIndex.column() );
394 QModelIndex proxyIndex;
396 if ( mSortByDisplayExpression )
398 proxyIndex = QSortFilterProxyModel::mapFromSource( sourceIndex );
402 if ( sourceIndex.isValid() )
403 proxyIndex = createIndex( sourceIndex.row(), 0 );
412 return QModelIndex();
425 const int offset = mInjectNull ? 1 : 0;
427 return sourceModel()->rowCount() + offset;
437 return QModelIndexList() <<
fidToIndex( fid );
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...
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.