QGIS API Documentation  3.26.3-Buenos Aires (65e4edfdad)
qgsattributetablefiltermodel.h
Go to the documentation of this file.
1 /***************************************************************************
2  QgsAttributeTableFilterModel.h - Filter Model for attribute table
3  -------------------
4  date : Feb 2009
5  copyright : (C) 2009 by Vita Cizek
6  email : weetya (at) gmail.com
7 
8  ***************************************************************************
9  * *
10  * This program is free software; you can redistribute it and/or modify *
11  * it under the terms of the GNU General Public License as published by *
12  * the Free Software Foundation; either version 2 of the License, or *
13  * (at your option) any later version. *
14  * *
15  ***************************************************************************/
16 
17 #ifndef QGSATTRIBUTETABLEFILTERMODEL_H
18 #define QGSATTRIBUTETABLEFILTERMODEL_H
19 
20 #include <QSortFilterProxyModel>
21 #include <QModelIndex>
22 #include <QTimer>
23 
24 #include "qgsattributetablemodel.h"
25 #include "qgsfeaturemodel.h"
26 #include "qgis_gui.h"
27 
29 class QgsMapCanvas;
30 class QItemSelectionModel;
31 
36 class GUI_EXPORT QgsAttributeTableFilterModel: public QSortFilterProxyModel, public QgsFeatureModel
37 {
38  Q_OBJECT
39 
40  public:
41 
46  {
51  ShowEdited
52  };
53  Q_ENUM( FilterMode )
54 
55 
59  {
61  ColumnTypeActionButton
62  };
63  Q_ENUM( ColumnType )
64 
65 
70  enum Role
71  {
73  };
74 
75 
83  QgsAttributeTableFilterModel( QgsMapCanvas *canvas, QgsAttributeTableModel *sourceModel, QObject *parent SIP_TRANSFERTHIS = nullptr );
84 
85 #ifdef __clang__
86 #pragma clang diagnostic push
87 #pragma clang diagnostic ignored "-Woverloaded-virtual"
88 #endif
89 
97  void setSourceModel( QgsAttributeTableModel *sourceModel );
98 
99 #ifdef __clang__
100 #pragma clang diagnostic pop
101 #endif
102 
109  void setSelectedOnTop( bool selectedOnTop );
110 
116  bool selectedOnTop();
117 
124  virtual void setFilteredFeatures( const QgsFeatureIds &ids );
125 
131  QgsFeatureIds filteredFeatures();
132 
138  void setFilterMode( FilterMode filterMode );
139 
143  void disconnectFilterModeConnections();
144 
148  void connectFilterModeConnections( FilterMode filterMode );
149 
153  FilterMode filterMode() { return mFilterMode; }
154 
160  inline QgsVectorLayer *layer() const { return masterModel()->layer(); }
161 
167  inline QgsVectorLayerCache *layerCache() const { return masterModel()->layerCache(); }
168 
174  inline QgsAttributeTableModel *masterModel() const { return mTableModel; }
175 
183  QgsFeatureId rowToId( const QModelIndex &row );
184 
185  QModelIndex fidToIndex( QgsFeatureId fid ) override;
186 
187  QModelIndexList fidToIndexList( QgsFeatureId fid );
188 
189  inline QModelIndex mapToMaster( const QModelIndex &proxyIndex ) const { return mapToSource( proxyIndex ); }
190 
191  inline QModelIndex mapFromMaster( const QModelIndex &sourceIndex ) const { return mapFromSource( sourceIndex ); }
192 
193  QModelIndex mapToSource( const QModelIndex &proxyIndex ) const override;
194 
195  QModelIndex mapFromSource( const QModelIndex &sourceIndex ) const override;
196 
197  Qt::ItemFlags flags( const QModelIndex &index ) const override;
198 
206  void sort( int column, Qt::SortOrder order = Qt::AscendingOrder ) override;
207 
215  void sort( const QString &expression, Qt::SortOrder order = Qt::AscendingOrder );
216 
220  QString sortExpression() const;
221 
223  QgsMapCanvas *mapCanvas() const { return mCanvas; }
224 
225  QVariant data( const QModelIndex &index, int role ) const override;
226 
227  QVariant headerData( int section, Qt::Orientation orientation, int role ) const override;
228 
233  int actionColumnIndex() const;
234 
235  int columnCount( const QModelIndex &parent ) const override;
236 
244  void setAttributeTableConfig( const QgsAttributeTableConfig &config, bool force SIP_PYARGREMOVE = false );
245 
252  void setFilterExpression( const QgsExpression &expression, const QgsExpressionContext &context );
253 
254  signals:
255 
261  void sortColumnChanged( int column, Qt::SortOrder order );
262 
266  void featuresFiltered();
267 
271  void visibleReloaded();
272 
277  void filterError( const QString &errorMessage );
278 
279  protected:
280 
287  bool filterAcceptsRow( int sourceRow, const QModelIndex &sourceParent ) const override;
288 
293  void generateListOfVisibleFeatures();
294 
299  bool lessThan( const QModelIndex &left, const QModelIndex &right ) const override;
300 
301  public slots:
302 
309  Q_DECL_DEPRECATED void extentsChanged();
310 
317  void filterFeatures();
318 
319  private slots:
320  void selectionChanged();
321  void onColumnsChanged();
322  void reloadVisible();
323  void onAttributeValueChanged( QgsFeatureId fid, int idx, const QVariant &value );
324  void onGeometryChanged();
325 
326  private:
327  QgsFeatureIds mFilteredFeatures;
328  QgsMapCanvas *mCanvas = nullptr;
329  FilterMode mFilterMode = FilterMode::ShowAll;
330  bool mSelectedOnTop = false;
331  QgsAttributeTableModel *mTableModel = nullptr;
332 
333  QgsAttributeTableConfig mConfig;
334  QVector<int> mColumnMapping;
335  QgsExpression mFilterExpression;
336  QgsExpressionContext mFilterExpressionContext;
337 
338  int mapColumnToSource( int column ) const;
339  int mapColumnFromSource( int column ) const;
340 
341  QTimer mReloadVisibleTimer;
342  QTimer mFilterFeaturesTimer;
343  void startTimedReloadVisible();
344  void startTimedFilterFeatures();
345 };
346 
347 #endif
QgsExpressionContext
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
Definition: qgsexpressioncontext.h:406
QgsAttributeTableFilterModel::ShowAll
@ ShowAll
Show all features.
Definition: qgsattributetablefiltermodel.h:47
QgsVectorLayerCache
This class caches features of a given QgsVectorLayer.
Definition: qgsvectorlayercache.h:46
QgsFeatureModel::fidToIndex
virtual QModelIndex fidToIndex(QgsFeatureId fid)=0
QgsAttributeTableFilterModel::ShowFilteredList
@ ShowFilteredList
Show only features whose ids are on the filter list. {.
Definition: qgsattributetablefiltermodel.h:50
QgsMapCanvas
Map canvas is a class for displaying all GIS data types on a canvas.
Definition: qgsmapcanvas.h:89
SIP_PYARGREMOVE
#define SIP_PYARGREMOVE
Definition: qgis_sip.h:151
QgsAttributeTableFilterModel::ColumnTypeField
@ ColumnTypeField
This column shows a field.
Definition: qgsattributetablefiltermodel.h:60
QgsAttributeTableFilterModel::mapCanvas
QgsMapCanvas * mapCanvas() const
Returns the map canvas.
Definition: qgsattributetablefiltermodel.h:223
QgsAttributeTableModel::UserRole
@ UserRole
Start further roles starting from this role.
Definition: qgsattributetablemodel.h:58
QgsAttributeTableConfig
This is a container for configuration of the attribute table. The configuration is specific for one v...
Definition: qgsattributetableconfig.h:36
QgsAttributeTableFilterModel::ColumnType
ColumnType
The type of a column.
Definition: qgsattributetablefiltermodel.h:58
QgsAttributeTableFilterModel::layer
QgsVectorLayer * layer() const
Returns the layer this filter acts on.
Definition: qgsattributetablefiltermodel.h:160
QgsAttributeTableModel
A model backed by a QgsVectorLayerCache which is able to provide feature/attribute information to a Q...
Definition: qgsattributetablemodel.h:49
QgsAttributeTableFilterModel::layerCache
QgsVectorLayerCache * layerCache() const
Returns the layerCache this filter acts on.
Definition: qgsattributetablefiltermodel.h:167
QgsAttributeTableFilterModel::masterModel
QgsAttributeTableModel * masterModel() const
Returns the table model this filter is using.
Definition: qgsattributetablefiltermodel.h:174
QgsAttributeTableFilterModel::mapToMaster
QModelIndex mapToMaster(const QModelIndex &proxyIndex) const
Definition: qgsattributetablefiltermodel.h:189
qgsattributetablemodel.h
QgsFeatureModel
Definition: qgsfeaturemodel.h:26
QgsFeatureIds
QSet< QgsFeatureId > QgsFeatureIds
Definition: qgsfeatureid.h:37
qgsfeaturemodel.h
QgsAttributeTableFilterModel
Definition: qgsattributetablefiltermodel.h:36
QgsAttributeTableFilterModel::mapFromMaster
QModelIndex mapFromMaster(const QModelIndex &sourceIndex) const
Definition: qgsattributetablefiltermodel.h:191
QgsAttributeTableFilterModel::ShowSelected
@ ShowSelected
Show only selected features.
Definition: qgsattributetablefiltermodel.h:48
QgsVectorLayer
Represents a vector layer which manages a vector based data sets.
Definition: qgsvectorlayer.h:391
QgsAttributeTableFilterModel::Role
Role
The additional roles defined by this filter model.
Definition: qgsattributetablefiltermodel.h:70
QgsAttributeTableFilterModel::filterMode
FilterMode filterMode()
The current filterModel.
Definition: qgsattributetablefiltermodel.h:153
QgsExpression
Class for parsing and evaluation of expressions (formerly called "search strings")....
Definition: qgsexpression.h:102
QgsAttributeTableFilterModel::FilterMode
FilterMode
The filter mode defines how the rows should be filtered.
Definition: qgsattributetablefiltermodel.h:45
SIP_TRANSFERTHIS
#define SIP_TRANSFERTHIS
Definition: qgis_sip.h:53
QgsAttributeTableFilterModel::ShowVisible
@ ShowVisible
Show only visible features (depends on the map canvas)
Definition: qgsattributetablefiltermodel.h:49
QgsFeatureId
qint64 QgsFeatureId
64 bit feature ids negative numbers are used for uncommitted/newly added features
Definition: qgsfeatureid.h:28