QGIS API Documentation
3.14.0-Pi (9f7028fd23)
|
Go to the documentation of this file.
16 #include <QItemSelectionModel>
36 : QSortFilterProxyModel( parent )
40 setDynamicSortFilter(
true );
44 mReloadVisibleTimer.setSingleShot(
true );
45 connect( &mReloadVisibleTimer, &QTimer::timeout,
this, &QgsAttributeTableFilterModel::reloadVisible );
46 mFilterFeaturesTimer.setSingleShot(
true );
57 if ( leftSelected && !rightSelected )
59 return sortOrder() == Qt::AscendingOrder;
61 else if ( rightSelected && !leftSelected )
63 return sortOrder() == Qt::DescendingOrder;
79 if ( order != Qt::AscendingOrder && order != Qt::DescendingOrder )
80 order = Qt::AscendingOrder;
81 if ( column < 0 || column >= mColumnMapping.size() )
87 int myColumn = mColumnMapping.at( column );
89 QSortFilterProxyModel::sort( myColumn, order );
96 if ( mapColumnToSource( index.column() ) == -1 )
102 QModelIndex fieldIndex = QSortFilterProxyModel::mapToSource( QSortFilterProxyModel::index( index.row(), 0, index.parent() ) );
109 return QSortFilterProxyModel::data( index, role );
114 if ( orientation == Qt::Horizontal )
116 if ( mColumnMapping.at( section ) == -1 && role == Qt::DisplayRole )
117 return tr(
"Actions" );
119 return QSortFilterProxyModel::headerData( section, orientation, role );
123 if ( role == Qt::DisplayRole )
127 int sourceSection =
mapToSource( index( section, ( !mColumnMapping.isEmpty() && mColumnMapping.at( 0 ) == -1 ) ? 1 : 0 ) ).row();
128 return sourceModel()->headerData( sourceSection, orientation, role );
135 return mColumnMapping.indexOf( -1 );
141 return mColumnMapping.count();
155 QVector<int> newColumnMapping;
156 const auto constColumns = mConfig.
columns();
160 if ( columnConfig.hidden )
165 newColumnMapping << newValue;
168 if ( newColumnMapping != mColumnMapping )
170 bool requiresReset =
false;
171 int firstRemovedColumn = -1;
172 int removedColumnCount = 0;
175 for (
int i = 0; i < std::min( newColumnMapping.size(), mColumnMapping.size() - removedColumnCount ); ++i )
177 if ( newColumnMapping.at( i ) == mColumnMapping.at( i + removedColumnCount ) )
180 if ( firstRemovedColumn == -1 )
182 firstRemovedColumn = i;
184 while ( i < mColumnMapping.size() - removedColumnCount && mColumnMapping.at( i + removedColumnCount ) != newColumnMapping.at( i ) )
186 ++removedColumnCount;
191 requiresReset =
true;
197 if ( firstRemovedColumn == -1 )
199 if ( newColumnMapping.size() > mColumnMapping.size() )
202 beginInsertColumns( QModelIndex(), mColumnMapping.size(), newColumnMapping.size() - 1 );
203 mColumnMapping = newColumnMapping;
209 beginRemoveColumns( QModelIndex(), newColumnMapping.size(), mColumnMapping.size() - 1 );
210 mColumnMapping = newColumnMapping;
216 if ( newColumnMapping.size() == mColumnMapping.size() - removedColumnCount )
219 beginRemoveColumns( QModelIndex(), firstRemovedColumn, firstRemovedColumn + removedColumnCount - 1 );
220 mColumnMapping = newColumnMapping;
225 requiresReset =
true;
232 mColumnMapping = newColumnMapping;
243 mFilterExpression = expression;
244 mFilterExpressionContext = context;
249 if ( order != Qt::AscendingOrder && order != Qt::DescendingOrder )
250 order = Qt::AscendingOrder;
252 QSortFilterProxyModel::sort( -1 );
254 QSortFilterProxyModel::sort( 0, order );
267 int column = sortColumn();
268 Qt::SortOrder order = sortOrder();
274 if ( order != Qt::AscendingOrder && order != Qt::DescendingOrder )
275 order = Qt::AscendingOrder;
277 sort( 0, Qt::AscendingOrder );
284 mTableModel = sourceModel;
288 mColumnMapping.append( i );
291 QSortFilterProxyModel::setSourceModel( sourceModel );
294 disconnect( mTableModel, SIGNAL( columnsAboutToBeInserted( QModelIndex,
int,
int ) ),
this, SLOT( _q_sourceColumnsAboutToBeInserted( QModelIndex,
int,
int ) ) );
295 disconnect( mTableModel, SIGNAL( columnsInserted( QModelIndex,
int,
int ) ),
this, SLOT( _q_sourceColumnsInserted( QModelIndex,
int,
int ) ) );
296 disconnect( mTableModel, SIGNAL( columnsAboutToBeRemoved( QModelIndex,
int,
int ) ),
this, SLOT( _q_sourceColumnsAboutToBeRemoved( QModelIndex,
int,
int ) ) );
297 disconnect( mTableModel, SIGNAL( columnsRemoved( QModelIndex,
int,
int ) ),
this, SLOT( _q_sourceColumnsRemoved( QModelIndex,
int,
int ) ) );
299 connect( mTableModel, &QAbstractItemModel::columnsAboutToBeInserted,
this, &QgsAttributeTableFilterModel::onColumnsChanged );
300 connect( mTableModel, &QAbstractItemModel::columnsAboutToBeRemoved,
this, &QgsAttributeTableFilterModel::onColumnsChanged );
306 return mSelectedOnTop;
311 mFilteredFeatures = ids;
319 ids.reserve( rowCount() );
320 for (
int i = 0; i < rowCount(); ++i )
322 QModelIndex row = index( i, 0 );
342 switch ( mFilterMode )
386 Q_UNUSED( sourceParent )
387 switch ( mFilterMode )
434 void QgsAttributeTableFilterModel::reloadVisible()
441 void QgsAttributeTableFilterModel::onAttributeValueChanged(
QgsFeatureId fid,
int idx,
const QVariant &value )
448 startTimedFilterFeatures();
452 void QgsAttributeTableFilterModel::onGeometryChanged()
456 startTimedFilterFeatures();
460 void QgsAttributeTableFilterModel::startTimedReloadVisible()
462 mReloadVisibleTimer.start( 10 );
465 void QgsAttributeTableFilterModel::startTimedFilterFeatures()
467 mFilterFeaturesTimer.start( 10 );
472 if ( !mFilterExpression.
isValid() )
483 QApplication::setOverrideCursor( Qt::WaitCursor );
506 if ( mFilterExpression.
evaluate( &mFilterExpressionContext ).toInt() != 0 )
518 QApplication::restoreOverrideCursor();
524 void QgsAttributeTableFilterModel::selectionChanged()
530 else if ( mSelectedOnTop )
536 void QgsAttributeTableFilterModel::onColumnsChanged()
541 int QgsAttributeTableFilterModel::mapColumnToSource(
int column )
const
543 if ( mColumnMapping.isEmpty() )
545 if ( column < 0 || column >= mColumnMapping.size() )
548 return mColumnMapping.at( column );
551 int QgsAttributeTableFilterModel::mapColumnFromSource(
int column )
const
553 if ( mColumnMapping.isEmpty() )
556 return mColumnMapping.indexOf( column );
569 mFilteredFeatures.clear();
570 if ( !
layer()->renderer() )
572 QgsDebugMsg( QStringLiteral(
"Cannot get renderer" ) );
576 std::unique_ptr< QgsFeatureRenderer > renderer(
layer()->renderer()->clone() );
581 QgsDebugMsg( QStringLiteral(
"Out of scale limits" ) );
599 renderer->startRender( renderContext,
layer()->fields() );
617 if ( !filter || renderer->willRenderFeature( f, renderContext ) )
619 mFilteredFeatures << f.
id();
622 if ( t.elapsed() > 5000 )
625 emit progress( i, cancel );
638 renderer->stopRender( renderContext );
654 QModelIndexList indexes;
656 for (
const QModelIndex &idx : constIdToIndexList )
666 if ( !proxyIndex.isValid() )
667 return QModelIndex();
669 int sourceColumn = mapColumnToSource( proxyIndex.column() );
673 if ( sourceColumn == -1 )
676 return QSortFilterProxyModel::mapToSource( index( proxyIndex.row(), sourceColumn, proxyIndex.parent() ) );
681 QModelIndex proxyIndex = QSortFilterProxyModel::mapFromSource( sourceIndex );
683 if ( proxyIndex.column() < 0 )
684 return QModelIndex();
686 int col = mapColumnFromSource( proxyIndex.column() );
691 return index( proxyIndex.row(), col, proxyIndex.parent() );
697 if ( mapColumnToSource( index.column() ) == -1 )
698 return Qt::ItemIsEnabled | Qt::ItemIsSelectable;
@ NoGeometry
Geometry is not required. It may still be returned if e.g. required for a filter condition.
Qt::SortOrder sortOrder() const
Gets the sort order.
QgsFeatureIterator getFeatures(const QgsFeatureRequest &request=QgsFeatureRequest()) const FINAL
Queries the layer for features specified in request.
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
QgsCoordinateReferenceSystem crs
Q_INVOKABLE QgsVectorLayerEditBuffer * editBuffer()
Buffer with uncommitted editing operations. Only valid after editing has been turned on.
@ ShowAll
Show all features.
@ Filter
Features may be filtered, i.e. some features may not be rendered (categorized, rule based ....
bool isFeatureAttributesChanged(QgsFeatureId id) const
Returns true if the specified feature ID has had an attribute changed but not committed.
void appendScopes(const QList< QgsExpressionContextScope * > &scopes)
Appends a list of scopes to the end of the context.
QgsRectangle extent() const
Returns the current zoom extent of the map canvas.
QString sortExpression() const
Gets the expression used for sorting.
QgsPointXY mapToLayerCoordinates(const QgsMapLayer *layer, QgsPointXY point) const
transform point coordinates from output CRS to layer's CRS
QModelIndexList idToIndexList(QgsFeatureId id) const
void setDistanceUnits(QgsUnitTypes::DistanceUnit unit)
Sets the desired distance units for calculations involving geomCalculator(), e.g.,...
QString sortExpression() const
The expression which is used to sort the attribute table.
QgsExpressionContext & expressionContext()
Gets the expression context.
void visibleReloaded()
Emitted when the the visible features on extend are reloaded (the list is created)
QSet< QString > referencedColumns() const
Gets list of columns referenced by the expression.
void connectFilterModeConnections(FilterMode filterMode)
Disconnect the connections set for the new filterMode.
const QgsMapSettings & mapSettings() const
Gets access to properties used for map rendering.
bool qgsVariantLessThan(const QVariant &lhs, const QVariant &rhs)
Compares two QVariant values and returns whether the first is less than the second.
const Flags & flags() const
const QgsRectangle & filterRect() const
Returns the rectangle from which features will be taken.
void setSourceModel(QgsAttributeTableModel *sourceModel)
Set the attribute table model that backs this model.
@ ShowFilteredList
Show only features whose ids are on the filter list. {.
Q_DECL_DEPRECATED void extentsChanged()
Is called upon every change of the visible extents on the map canvas.
QSet< int > referencedAttributeIndexes(const QgsFields &fields) const
Returns a list of field name indexes obtained from the provided fields.
bool isValid() const
Checks if this expression is valid.
Defines the configuration of a column in the attribute table.
void sort(int column, Qt::SortOrder order=Qt::AscendingOrder) override
Sort by the given column using the given order.
QgsCoordinateTransformContext transformContext
static QgsProject * instance()
Returns the QgsProject singleton instance.
@ ShowEdited
Show only features which have unsaved changes.
static QList< QgsExpressionContextScope * > globalProjectLayerScopes(const QgsMapLayer *layer)
Creates a list of three scopes: global, layer's project and layer.
QgsFeatureRequest & setSubsetOfAttributes(const QgsAttributeList &attrs)
Set a subset of attributes that will be fetched.
QgsFeatureIterator getFeatures(const QgsFeatureRequest &featureRequest=QgsFeatureRequest())
Query this VectorLayerCache for features.
QgsRectangle intersect(const QgsRectangle &rect) const
Returns the intersection with the given rectangle.
QgsFeatureIds filteredFeatures()
Gets a list of currently filtered feature ids.
int columnCount(const QModelIndex &parent) const override
@ ColumnTypeField
This column shows a field.
bool isFeatureGeometryChanged(QgsFeatureId id) const
Returns true if the specified feature ID has had its geometry changed but not committed.
void setAreaUnits(QgsUnitTypes::AreaUnit unit)
Sets the desired areal units for calculations involving geomCalculator(), e.g., "$area".
QgsFeatureRequest & setFilterRect(const QgsRectangle &rectangle)
Sets the rectangle from which features will be taken.
void setGeomCalculator(const QgsDistanceArea *calc)
Sets the geometry calculator used for distance and area calculations in expressions.
QgsFields fields() const FINAL
Returns the list of fields of this layer.
bool setEllipsoid(const QString &ellipsoid)
Sets the ellipsoid by its acronym.
bool hasEvalError() const
Returns true if an error occurred when evaluating last input.
Qt::ItemFlags flags(const QModelIndex &index) const override
Returns item flags for the index.
void featuresFiltered()
Emitted when the filtering of the features has been done.
@ Action
This column represents an action widget.
void selectionChanged(const QgsFeatureIds &selected, const QgsFeatureIds &deselected, bool clearAndSelect)
Emitted when selection was changed.
QgsFeatureId rowToId(int row) const
Maps row to feature id.
void generateListOfVisibleFeatures()
Updates the list of currently visible features on the map canvas.
void sortColumnChanged(int column, Qt::SortOrder order)
Emitted whenever the sort column is changed.
void setExtent(const QgsRectangle &extent)
When rendering a map layer, calling this method sets the "clipping" extent for the layer (in the laye...
QgsVectorLayer * layer() const
Returns the layer this filter acts on.
QModelIndexList fidToIndexList(QgsFeatureId fid)
Is able to generate editor widgets for its QModelIndexes as well. Is mostly referred to as "master mo...
void attributeValueChanged(QgsFeatureId fid, int idx, const QVariant &value)
Emitted whenever an attribute value change is done in the edit buffer.
QVector< QgsAttributeTableConfig::ColumnConfig > columns() const
Gets the list with all columns and their configuration.
const Q_INVOKABLE QgsFeatureIds & selectedFeatureIds() const
Returns a list of the selected features IDs in this layer.
void extentsChanged()
Emitted when the extents of the map change.
void setAttributeTableConfig(const QgsAttributeTableConfig &config)
Set the attribute table configuration to control which fields are shown, in which order they are show...
QModelIndex fidToIndex(QgsFeatureId fid) override
QgsAttributeTableModel * masterModel() const
Returns the table model this filter is using.
void setMapToPixel(const QgsMapToPixel &mtp)
Sets the context's map to pixel transform, which transforms between map coordinates and device coordi...
void prefetchSortData(const QString &expression, unsigned long cacheIndex=0)
Prefetches the entire data for an expression.
Qt::ItemFlags flags(const QModelIndex &index) const override
void setSelectedOnTop(bool selectedOnTop)
Changes the sort order of the features.
QgsFeatureId rowToId(const QModelIndex &row)
Returns the feature id for a given model index.
QVariant data(const QModelIndex &index, int role) const override
double scale() const
Returns the calculated map scale.
int extraColumns() const
Empty extra columns to announce from this model.
QgsVectorLayer * layer() const
Returns the layer this model uses as backend.
void setSourceCrs(const QgsCoordinateReferenceSystem &crs, const QgsCoordinateTransformContext &context)
Sets source spatial reference system crs.
QVariant headerData(int section, Qt::Orientation orientation, int role) const override
void prefetchColumnData(int column)
Caches the entire data for one column.
@ TypeRole
The type of a given column.
bool filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const override
Returns true if the source row will be accepted.
QSet< QgsFeatureId > QgsFeatureIds
bool selectedOnTop()
Returns if selected features are currently shown on top.
QVariant evaluate()
Evaluate the feature and return the result.
QString sortCacheExpression(unsigned long cacheIndex=0) const
The expression which was used to fill the sorting cache at index cacheIndex.
void setRendererScale(double scale)
Sets the renderer map scale.
@ FeatureIdRole
Get the feature id of the feature in this row.
void setFilterMode(FilterMode filterMode)
Set the filter mode the filter will use.
void disconnectFilterModeConnections()
Disconnect the connections set for the current filterMode.
bool isFeatureAdded(QgsFeatureId id) const
Returns true if the specified feature ID has been added but not committed.
QModelIndex mapToSource(const QModelIndex &proxyIndex) const override
QModelIndex mapFromMaster(const QModelIndex &sourceIndex) const
@ ColumnTypeActionButton
This column shows action buttons.
@ ShowSelected
Show only selected features.
QModelIndex mapFromSource(const QModelIndex &sourceIndex) const override
bool nextFeature(QgsFeature &f)
void setFilterExpression(const QgsExpression &expression, const QgsExpressionContext &context)
Set the expression and the context to be stored in case of the features need to be filtered again (li...
void filterFeatures()
Updates the filtered features in the filter model.
@ ScaleDependent
Depends on scale if feature will be rendered (rule based )
void geometryChanged(QgsFeatureId fid, const QgsGeometry &geometry)
Emitted whenever a geometry change is done in the edit buffer.
bool lessThan(const QModelIndex &left, const QModelIndex &right) const override
Used by the sorting algorithm.
QgsAttributeTableFilterModel(QgsMapCanvas *canvas, QgsAttributeTableModel *sourceModel, QObject *parent=nullptr)
Make sure, the master model is already loaded, so the selection will get synchronized.
virtual void setFilteredFeatures(const QgsFeatureIds &ids)
Specify a list of features, which the filter will accept.
int actionColumnIndex() const
Gets the index of the first column that contains an action widget.
bool needsGeometry() const
Returns true if the expression uses feature geometry for some computation.
FilterMode filterMode()
The current filterModel.
@ SortRole
Role used for sorting start here.
int columnCount(const QModelIndex &parent=QModelIndex()) const override
Returns the number of columns.
int lookupField(const QString &fieldName) const
Looks up field's index from the field name.
const QgsFeatureRequest & request() const
Gets the the feature request.
QgsRectangle visibleExtent() const
Returns the actual extent derived from requested extent that takes takes output image size into accou...
bool hasSameColumns(const QgsAttributeTableConfig &other) const
Compare this configuration's columns name, type, and order to other.
FilterMode
The filter mode defines how the rows should be filtered.
QgsFeatureRequest & setFlags(QgsFeatureRequest::Flags flags)
Sets flags that affect how features will be fetched.
const QgsMapToPixel & mapToPixel() const
bool isNull() const
Test if the rectangle is null (all coordinates zero or after call to setMinimal()).
QgsVectorLayerCache * layerCache() const
Returns the layer cache this model uses as backend.
@ ShowVisible
Show only visible features (depends on the map canvas)
void setFeature(const QgsFeature &feature)
Convenience function for setting a feature for the context.
void update(const QgsFields &fields)
Update the configuration with the given fields.
void featureAdded(QgsFeatureId fid)
Emitted when a new feature has been added to the layer.