16 #include <QItemSelectionModel> 33 : QSortFilterProxyModel( parent )
37 setDynamicSortFilter(
true );
49 if ( leftSelected && !rightSelected )
51 return sortOrder() == Qt::AscendingOrder;
53 else if ( rightSelected && !leftSelected )
55 return sortOrder() == Qt::DescendingOrder;
71 if ( order != Qt::AscendingOrder && order != Qt::DescendingOrder )
72 order = Qt::AscendingOrder;
74 int myColumn = mColumnMapping.at( column );
76 QSortFilterProxyModel::sort( myColumn, order );
82 if ( mapColumnToSource( index.column() ) == -1 )
88 QModelIndex fieldIndex = QSortFilterProxyModel::mapToSource( QSortFilterProxyModel::index( index.row(), 0, index.parent() ) );
95 return QSortFilterProxyModel::data( index, role );
100 if ( orientation == Qt::Horizontal )
102 if ( mColumnMapping.at( section ) == -1 && role == Qt::DisplayRole )
103 return tr(
"Actions" );
105 return QSortFilterProxyModel::headerData( section, orientation, role );
109 if ( role == Qt::DisplayRole )
113 int sourceSection =
mapToSource( index( section, ( !mColumnMapping.isEmpty() && mColumnMapping.at( 0 ) == -1 ) ? 1 : 0 ) ).row();
114 return sourceModel()->headerData( sourceSection, orientation, role );
121 return mColumnMapping.indexOf( -1 );
127 return mColumnMapping.count();
141 QVector<int> newColumnMapping;
145 if ( columnConfig.
hidden )
150 newColumnMapping << newValue;
153 if ( newColumnMapping != mColumnMapping )
155 bool requiresReset =
false;
156 int firstRemovedColumn = -1;
157 int removedColumnCount = 0;
160 for (
int i = 0; i < std::min( newColumnMapping.size(), mColumnMapping.size() - removedColumnCount ); ++i )
162 if ( newColumnMapping.at( i ) == mColumnMapping.at( i + removedColumnCount ) )
165 if ( firstRemovedColumn == -1 )
167 firstRemovedColumn = i;
169 while ( i < mColumnMapping.size() - removedColumnCount && mColumnMapping.at( i + removedColumnCount ) != newColumnMapping.at( i ) )
171 ++removedColumnCount;
176 requiresReset =
true;
182 if ( firstRemovedColumn == -1 )
184 if ( newColumnMapping.size() > mColumnMapping.size() )
187 beginInsertColumns( QModelIndex(), mColumnMapping.size(), newColumnMapping.size() - 1 );
188 mColumnMapping = newColumnMapping;
194 beginRemoveColumns( QModelIndex(), newColumnMapping.size(), mColumnMapping.size() - 1 );
195 mColumnMapping = newColumnMapping;
201 if ( newColumnMapping.size() == mColumnMapping.size() - removedColumnCount )
204 beginRemoveColumns( QModelIndex(), firstRemovedColumn, firstRemovedColumn + removedColumnCount - 1 );
205 mColumnMapping = newColumnMapping;
210 requiresReset =
true;
217 mColumnMapping = newColumnMapping;
228 if ( order != Qt::AscendingOrder && order != Qt::DescendingOrder )
229 order = Qt::AscendingOrder;
231 QSortFilterProxyModel::sort( -1 );
233 QSortFilterProxyModel::sort( 0, order );
243 if ( mSelectedOnTop != selectedOnTop )
246 int column = sortColumn();
247 Qt::SortOrder order = sortOrder();
250 if ( column < 0 || ( order != Qt::AscendingOrder && order != Qt::DescendingOrder ) )
252 sort( 0, Qt::AscendingOrder );
260 mTableModel = sourceModel;
264 mColumnMapping.append( i );
267 QSortFilterProxyModel::setSourceModel( sourceModel );
270 disconnect( mTableModel, SIGNAL( columnsAboutToBeInserted( QModelIndex,
int,
int ) ),
this, SLOT( _q_sourceColumnsAboutToBeInserted( QModelIndex,
int,
int ) ) );
271 disconnect( mTableModel, SIGNAL( columnsInserted( QModelIndex,
int,
int ) ),
this, SLOT( _q_sourceColumnsInserted( QModelIndex,
int,
int ) ) );
272 disconnect( mTableModel, SIGNAL( columnsAboutToBeRemoved( QModelIndex,
int,
int ) ),
this, SLOT( _q_sourceColumnsAboutToBeRemoved( QModelIndex,
int,
int ) ) );
273 disconnect( mTableModel, SIGNAL( columnsRemoved( QModelIndex,
int,
int ) ),
this, SLOT( _q_sourceColumnsRemoved( QModelIndex,
int,
int ) ) );
275 connect( mTableModel, &QAbstractItemModel::columnsAboutToBeInserted,
this, &QgsAttributeTableFilterModel::onColumnsChanged );
276 connect( mTableModel, &QAbstractItemModel::columnsAboutToBeRemoved,
this, &QgsAttributeTableFilterModel::onColumnsChanged );
282 return mSelectedOnTop;
287 mFilteredFeatures = ids;
295 ids.reserve( rowCount() );
296 for (
int i = 0; i < rowCount(); ++i )
298 QModelIndex row = index( i, 0 );
306 if ( filterMode != mFilterMode )
325 Q_UNUSED( sourceParent );
326 switch ( mFilterMode )
374 void QgsAttributeTableFilterModel::selectionChanged()
380 else if ( mSelectedOnTop )
386 void QgsAttributeTableFilterModel::onColumnsChanged()
391 int QgsAttributeTableFilterModel::mapColumnToSource(
int column )
const 393 if ( mColumnMapping.isEmpty() )
395 if ( column < 0 || column >= mColumnMapping.size() )
398 return mColumnMapping.at( column );
401 int QgsAttributeTableFilterModel::mapColumnFromSource(
int column )
const 403 if ( mColumnMapping.isEmpty() )
406 return mColumnMapping.indexOf( column );
419 mFilteredFeatures.clear();
420 if ( !
layer()->renderer() )
422 QgsDebugMsg( QStringLiteral(
"Cannot get renderer" ) );
426 std::unique_ptr< QgsFeatureRenderer > renderer(
layer()->renderer()->clone() );
431 QgsDebugMsg( QStringLiteral(
"Out of scale limits" ) );
449 renderer->startRender( renderContext,
layer()->fields() );
467 if ( !filter || renderer->willRenderFeature( f, renderContext ) )
469 mFilteredFeatures << f.
id();
472 if ( t.elapsed() > 5000 )
475 emit progress( i, cancel );
488 renderer->stopRender( renderContext );
504 QModelIndexList indexes;
505 Q_FOREACH (
const QModelIndex &idx,
masterModel()->idToIndexList( fid ) )
515 if ( !proxyIndex.isValid() )
516 return QModelIndex();
518 int sourceColumn = mapColumnToSource( proxyIndex.column() );
522 if ( sourceColumn == -1 )
525 return QSortFilterProxyModel::mapToSource( index( proxyIndex.row(), sourceColumn, proxyIndex.parent() ) );
530 QModelIndex proxyIndex = QSortFilterProxyModel::mapFromSource( sourceIndex );
532 if ( proxyIndex.column() < 0 )
533 return QModelIndex();
535 int col = mapColumnFromSource( proxyIndex.column() );
540 return index( proxyIndex.row(), col, proxyIndex.parent() );
546 if ( mapColumnToSource( index.column() ) == -1 )
547 return Qt::ItemIsEnabled | Qt::ItemIsSelectable;
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.
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.
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.
QSet< QgsFeatureId > QgsFeatureIds
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...
double scale() const
Returns the calculated map scale.
void setSelectedOnTop(bool selectedOnTop)
Changes the sort order of the features.
int actionColumnIndex() const
Gets the index of the first column that contains an action widget.
void setFilterMode(FilterMode filterMode)
Set the filter mode the filter will use.
const QgsRectangle & filterRect() const
Returns the rectangle from which features will be taken.
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.
Features may be filtered, i.e. some features may not be rendered (categorized, rule based ...
int extraColumns() const
Empty extra columns to announce from this model.
void setRendererScale(double scale)
Sets the renderer map scale.
void prefetchSortData(const QString &expression, unsigned long cacheIndex=0)
Prefetches the entire data for an expression.
bool lessThan(const QModelIndex &left, const QModelIndex &right) const override
Used by the sorting algorithm.
bool isNull() const
Test if the rectangle is null (all coordinates zero or after call to setMinimal()).
QgsRectangle visibleExtent() const
Returns the actual extent derived from requested extent that takes takes output image size into accou...
bool isFeatureGeometryChanged(QgsFeatureId id) const
Returns true if the specified feature ID has had its geometry changed but not committed.
const QgsMapSettings & mapSettings() const
Gets access to properties used for map rendering.
QModelIndex mapToSource(const QModelIndex &proxyIndex) const override
This column represents an action widget.
const QgsMapToPixel & mapToPixel() const
QgsPointXY mapToLayerCoordinates(const QgsMapLayer *layer, QgsPointXY point) const
transform point coordinates from output CRS to layer's CRS
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)
void setExtent(const QgsRectangle &extent)
When rendering a map layer, calling this method sets the "clipping" extent for the layer (in the laye...
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.
QString sortExpression() const
Gets the expression used for sorting.
Get the feature id of the feature in this row.
QgsVectorLayer * layer() const
Returns the layer this filter acts on.
Roles used for sorting start here.
QgsVectorLayerEditBuffer * editBuffer()
Buffer with uncommitted editing operations. Only valid after editing has been turned on...
Qt::SortOrder sortOrder() const
Gets the sort order.
Show only selected features.
FilterMode filterMode()
The current filterModel.
QgsFields fields() const FINAL
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.
bool hasSameColumns(const QgsAttributeTableConfig &other) const
Compare this configuration's columns name, type, and order to other.
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)...
QgsFeatureRequest & setFilterRect(const QgsRectangle &rectangle)
Sets the rectangle from which features will be taken.
void selectionChanged(const QgsFeatureIds &selected, const QgsFeatureIds &deselected, bool clearAndSelect)
This signal is emitted when selection was changed.
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.
const QgsFeatureIds & selectedFeatureIds() const
Returns a list of the selected features IDs in this layer.
int lookupField(const QString &fieldName) const
Looks up field's index from the field name.
QgsVectorLayerCache * layerCache() const
Returns the layer cache this model uses as backend.
This column shows action buttons.
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.
QgsRectangle intersect(const QgsRectangle &rect) const
Returns the intersection with the given rectangle.
void extentsChanged()
Is called upon every change of the visible extents on the map canvas.
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
Gets the list with all columns and their configuration.
Contains information about the context of a rendering operation.
QgsAttributeTableModel * masterModel() const
Returns the table model this filter is using.
QModelIndex mapFromMaster(const QModelIndex &sourceIndex) const
void sort(int column, Qt::SortOrder order=Qt::AscendingOrder) override
Sort by the given column using the given order.
QString sortExpression() const
The expression which is used to sort the attribute table.
int columnCount(const QModelIndex &parent) const override
QString sortCacheExpression(unsigned long cacheIndex=0) const
The expression which was used to fill the sorting cache at index cacheIndex.
void setMapToPixel(const QgsMapToPixel &mtp)
Sets the context's map to pixel transform, which transforms between map coordinates and device coordi...
Defines the configuration of a column in the attribute table.
QgsFeatureIds filteredFeatures()
Gets a list of currently filtered feature ids.
QgsRectangle extent() const
Returns the current zoom extent of the map canvas.
QgsFeatureId rowToId(int row) const
Maps row to feature id.
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.
bool nextFeature(QgsFeature &f)
This is a container for configuration of the attribute table.
bool isFeatureAdded(QgsFeatureId id) const
Returns true if the specified feature ID has been added but not committed.
The type of a given column.
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.
void prefetchColumnData(int column)
Caches the entire data for one column.
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