16 #include <QItemSelectionModel> 33 : QSortFilterProxyModel( parent )
35 , mFilterMode( ShowAll )
36 , mSelectedOnTop( false )
39 setDynamicSortFilter(
true );
51 if ( leftSelected && !rightSelected )
53 return sortOrder() == Qt::AscendingOrder;
55 else if ( rightSelected && !leftSelected )
57 return sortOrder() == Qt::DescendingOrder;
73 if ( order != Qt::AscendingOrder && order != Qt::DescendingOrder )
74 order = Qt::AscendingOrder;
76 int myColumn = mColumnMapping.at( column );
78 QSortFilterProxyModel::sort( myColumn, order );
84 if ( mapColumnToSource( index.column() ) == -1 )
90 QModelIndex fieldIndex = QSortFilterProxyModel::mapToSource( QSortFilterProxyModel::index( index.row(), 0, index.parent() ) );
97 return QSortFilterProxyModel::data( index, role );
102 if ( orientation == Qt::Horizontal )
104 if ( mColumnMapping.at( section ) == -1 && role == Qt::DisplayRole )
105 return tr(
"Actions" );
107 return QSortFilterProxyModel::headerData( section, orientation, role );
111 if ( role == Qt::DisplayRole )
115 int sourceSection =
mapToSource( index( section, ( !mColumnMapping.isEmpty() && mColumnMapping.at( 0 ) == -1 ) ? 1 : 0 ) ).row();
116 return sourceModel()->headerData( sourceSection, orientation, role );
123 return mColumnMapping.indexOf( -1 );
129 return mColumnMapping.count();
143 QVector<int> newColumnMapping;
147 if ( columnConfig.
hidden )
152 newColumnMapping << newValue;
155 if ( newColumnMapping != mColumnMapping )
157 bool requiresReset =
false;
158 int firstRemovedColumn = -1;
159 int removedColumnCount = 0;
162 for (
int i = 0; i < std::min( newColumnMapping.size(), mColumnMapping.size() - removedColumnCount ); ++i )
164 if ( newColumnMapping.at( i ) == mColumnMapping.at( i + removedColumnCount ) )
167 if ( firstRemovedColumn == -1 )
169 firstRemovedColumn = i;
171 while ( i < mColumnMapping.size() - removedColumnCount && mColumnMapping.at( i + removedColumnCount ) != newColumnMapping.at( i ) )
173 ++removedColumnCount;
178 requiresReset =
true;
184 if ( firstRemovedColumn == -1 )
186 if ( newColumnMapping.size() > mColumnMapping.size() )
189 beginInsertColumns( QModelIndex(), mColumnMapping.size(), newColumnMapping.size() - 1 );
190 mColumnMapping = newColumnMapping;
196 beginRemoveColumns( QModelIndex(), newColumnMapping.size(), mColumnMapping.size() - 1 );
197 mColumnMapping = newColumnMapping;
203 if ( newColumnMapping.size() == mColumnMapping.size() - removedColumnCount )
206 beginRemoveColumns( QModelIndex(), firstRemovedColumn, firstRemovedColumn + removedColumnCount - 1 );
207 mColumnMapping = newColumnMapping;
212 requiresReset =
true;
219 mColumnMapping = newColumnMapping;
230 if ( order != Qt::AscendingOrder && order != Qt::DescendingOrder )
231 order = Qt::AscendingOrder;
233 QSortFilterProxyModel::sort( -1 );
235 QSortFilterProxyModel::sort( 0, order );
245 if ( mSelectedOnTop != selectedOnTop )
248 int column = sortColumn();
249 Qt::SortOrder order = sortOrder();
255 if ( order != Qt::AscendingOrder && order != Qt::DescendingOrder )
256 order = Qt::AscendingOrder;
258 sort( column, order );
265 mTableModel = sourceModel;
269 mColumnMapping.append( i );
272 QSortFilterProxyModel::setSourceModel( sourceModel );
275 disconnect( mTableModel, SIGNAL( columnsAboutToBeInserted( QModelIndex,
int,
int ) ),
this, SLOT( _q_sourceColumnsAboutToBeInserted( QModelIndex,
int,
int ) ) );
276 disconnect( mTableModel, SIGNAL( columnsInserted( QModelIndex,
int,
int ) ),
this, SLOT( _q_sourceColumnsInserted( QModelIndex,
int,
int ) ) );
277 disconnect( mTableModel, SIGNAL( columnsAboutToBeRemoved( QModelIndex,
int,
int ) ),
this, SLOT( _q_sourceColumnsAboutToBeRemoved( QModelIndex,
int,
int ) ) );
278 disconnect( mTableModel, SIGNAL( columnsRemoved( QModelIndex,
int,
int ) ),
this, SLOT( _q_sourceColumnsRemoved( QModelIndex,
int,
int ) ) );
280 connect( mTableModel, &QAbstractItemModel::columnsAboutToBeInserted,
this, &QgsAttributeTableFilterModel::onColumnsChanged );
281 connect( mTableModel, &QAbstractItemModel::columnsAboutToBeRemoved,
this, &QgsAttributeTableFilterModel::onColumnsChanged );
287 return mSelectedOnTop;
292 mFilteredFeatures = ids;
300 ids.reserve( rowCount() );
301 for (
int i = 0; i < rowCount(); ++i )
303 QModelIndex row = index( i, 0 );
311 if ( filterMode != mFilterMode )
330 Q_UNUSED( sourceParent );
331 switch ( mFilterMode )
379 void QgsAttributeTableFilterModel::selectionChanged()
385 else if ( mSelectedOnTop )
387 sort( sortColumn(), sortOrder() );
392 void QgsAttributeTableFilterModel::onColumnsChanged()
397 int QgsAttributeTableFilterModel::mapColumnToSource(
int column )
const 399 if ( mColumnMapping.isEmpty() )
401 if ( column < 0 || column >= mColumnMapping.size() )
404 return mColumnMapping.at( column );
417 mFilteredFeatures.clear();
418 if ( !
layer()->renderer() )
424 std::unique_ptr< QgsFeatureRenderer > renderer(
layer()->renderer()->clone() );
447 renderer->startRender( renderContext,
layer()->fields() );
465 if ( !filter || renderer->willRenderFeature( f, renderContext ) )
467 mFilteredFeatures << f.
id();
470 if ( t.elapsed() > 5000 )
473 emit progress( i, cancel );
486 renderer->stopRender( renderContext );
502 QModelIndexList indexes;
503 Q_FOREACH (
const QModelIndex &idx,
masterModel()->idToIndexList( fid ) )
513 if ( !proxyIndex.isValid() )
514 return QModelIndex();
516 int sourceColumn = mapColumnToSource( proxyIndex.column() );
520 if ( sourceColumn == -1 )
523 return QSortFilterProxyModel::mapToSource( index( proxyIndex.row(), sourceColumn, proxyIndex.parent() ) );
528 QModelIndex proxyIndex = QSortFilterProxyModel::mapFromSource( sourceIndex );
530 if ( proxyIndex.column() < 0 )
531 return QModelIndex();
533 int col = mapColumnToSource( proxyIndex.column() );
537 return index( proxyIndex.row(), col, proxyIndex.parent() );
543 if ( mapColumnToSource( index.column() ) == -1 )
544 return Qt::ItemIsEnabled | Qt::ItemIsSelectable;
int lookupField(const QString &fieldName) const
Look up field's index from the field name.
void generateListOfVisibleFeatures()
Updates the list of currently visible features on the map canvas.
QgsFeatureId rowToId(const QModelIndex &row)
Returns the feature id for a given model index.
QgsVectorLayer * layer() const
Returns the layer this filter acts on.
Wrapper for iterator of features from vector data provider or vector layer.
QVariant data(const QModelIndex &index, int role) const override
QgsAttributeTableConfig::Type type
The type of this column.
int extraColumns() const
Empty extra columns to announce from this model.
A rectangle specified with double values.
Depends on scale if feature will be rendered (rule based )
void update(const QgsFields &fields)
Update the configuration with the given fields.
bool filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const override
Returns true if the source row will be accepted.
bool selectedOnTop()
Returns if selected features are currently shown on top.
void setAttributeTableConfig(const QgsAttributeTableConfig &config)
Set the attribute table configuration to control which fields are shown, in which order they are show...
QgsAttributeTableModel * masterModel() const
Returns the table model this filter is using.
void setSelectedOnTop(bool selectedOnTop)
Changes the sort order of the features.
void setFilterMode(FilterMode filterMode)
Set the filter mode the filter will use.
QModelIndex mapFromMaster(const QModelIndex &sourceIndex) const
QVariant headerData(int section, Qt::Orientation orientation, int role) const override
void setFeature(const QgsFeature &feature)
Convenience function for setting a feature for the context.
QSet< QgsFeatureId > QgsFeatureIds
Features may be filtered, i.e. some features may not be rendered (categorized, rule based ...
void setRendererScale(double scale)
Sets the renderer map scale.
bool lessThan(const QModelIndex &left, const QModelIndex &right) const override
Used by the sorting algorithm.
QModelIndex mapToSource(const QModelIndex &proxyIndex) const override
QgsRectangle intersect(const QgsRectangle *rect) const
Return the intersection with the given rectangle.
This column represents an action widget.
bool isFeatureAdded(QgsFeatureId id) const
Returns true if the specified feature ID has been added but not committed.
QModelIndex mapFromSource(const QModelIndex &sourceIndex) const override
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
A model backed by a QgsVectorLayerCache which is able to provide feature/attribute information to a Q...
Show only visible features (depends on the map canvas)
const QgsRectangle & filterRect() const
Returns the rectangle from which features will be taken.
void setExtent(const QgsRectangle &extent)
QgsRectangle visibleExtent() const
Return the actual extent derived from requested extent that takes takes output image size into accoun...
Map canvas is a class for displaying all GIS data types on a canvas.
bool qgsVariantLessThan(const QVariant &lhs, const QVariant &rhs)
Compares two QVariant values and returns whether the first is less than the second.
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.
The QgsMapSettings class contains configuration for rendering of the map.
Get the feature id of the feature in this row.
int actionColumnIndex() const
Get the index of the first column that contains an action widget.
QgsVectorLayerEditBuffer * editBuffer()
Buffer with uncommitted editing operations. Only valid after editing has been turned on...
Show only selected features.
FilterMode filterMode()
The current filterModel.
const QgsFeatureIds & selectedFeatureIds() const
Return reference to identifiers of selected features.
QgsFields fields() const override
Returns the list of fields of this layer.
int columnCount(const QModelIndex &parent=QModelIndex()) const override
Returns the number of columns.
FilterMode
The filter mode defines how the rows should be filtered.
double scale() const
Returns the calculated map scale.
void selectionChanged(const QgsFeatureIds &selected, const QgsFeatureIds &deselected, const bool clearAndSelect)
This signal is emitted when selection was changed.
QgsRectangle extent() const
Returns the current zoom extent of the map canvas.
bool hidden
Flag that controls if the column is hidden.
This class wraps a request for features to a vector layer (or directly its vector data provider)...
QgsVectorLayerCache * layerCache() const
Returns the layer cache this model uses as backend.
QgsFeatureRequest & setFilterRect(const QgsRectangle &rectangle)
Sets the rectangle from which features will be taken.
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.
bool isFeatureAttributesChanged(QgsFeatureId id) const
Returns true if the specified feature ID has had an attribute changed but not committed.
This column shows action buttons.
const QgsMapToPixel & mapToPixel() const
QString name
The name of the attribute if this column represents a field.
void setSourceModel(QgsAttributeTableModel *sourceModel)
Set the attribute table model that backs this model.
void extentsChanged()
Is called upon every change of the visible extents on the map canvas.
QString sortExpression() const
The expression which is used to sort the attribute table.
This column shows a field.
QgsExpressionContext & expressionContext()
Gets the expression context.
Show only features whose ids are on the filter list. {.
QVector< QgsAttributeTableConfig::ColumnConfig > columns() const
Get the list with all columns and their configuration.
const QgsMapSettings & mapSettings() const
Get access to properties used for map rendering.
Contains information about the context of a rendering operation.
void sort(int column, Qt::SortOrder order=Qt::AscendingOrder) override
Sort by the given column using the given order.
int columnCount(const QModelIndex &parent) const override
void setMapToPixel(const QgsMapToPixel &mtp)
bool isNull() const
Test if the rectangle is null (all coordinates zero or after call to setMinimal()).
Defines the configuration of a column in the attribute table.
QgsFeatureId rowToId(int row) const
Maps row to feature id.
QString sortCacheExpression() const
The expression which was used to fill the sorting cache.
QgsFeatureIds filteredFeatures()
Get a list of currently filtered feature ids.
void appendScopes(const QList< QgsExpressionContextScope *> &scopes)
Appends a list of scopes to the end of the context.
bool nextFeature(QgsFeature &f)
This is a container for configuration of the attribute table.
Qt::SortOrder sortOrder() const
Get the sort order.
The type of a given column.
QgsPointXY mapToLayerCoordinates(const QgsMapLayer *layer, QgsPointXY point) const
transform point coordinates from output CRS to layer's CRS
QString sortExpression() const
Get the expression used for sorting.
void sortColumnChanged(int column, Qt::SortOrder order)
Is emitted whenever the sort column is changed.
QgsFeatureIterator getFeatures(const QgsFeatureRequest &featureRequest=QgsFeatureRequest())
Query this VectorLayerCache for features.
QModelIndexList fidToIndexList(QgsFeatureId fid)
void extentsChanged()
Emitted when the extents of the map change.
bool hasSameColumns(const QgsAttributeTableConfig &other) const
Compare this configuration's columns name, type, and order to other.
void prefetchColumnData(int column)
Caches the entire data for one column.
void prefetchSortData(const QString &expression)
Prefetches the entire data for one expression.
Qt::ItemFlags flags(const QModelIndex &index) const override
Returns item flags for the index.
QModelIndex fidToIndex(QgsFeatureId fid) override
Qt::ItemFlags flags(const QModelIndex &index) const override
bool isFeatureGeometryChanged(QgsFeatureId id) const
Returns true if the specified feature ID has had its geometry changed but not committed.