QGIS API Documentation  3.19.0-Master (898a03ac22)
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 
92  void setSourceModel( QgsAttributeTableModel *sourceModel );
93 
100  void setSelectedOnTop( bool selectedOnTop );
101 
107  bool selectedOnTop();
108 
115  virtual void setFilteredFeatures( const QgsFeatureIds &ids );
116 
122  QgsFeatureIds filteredFeatures();
123 
129  void setFilterMode( FilterMode filterMode );
130 
134  void disconnectFilterModeConnections();
135 
139  void connectFilterModeConnections( FilterMode filterMode );
140 
144  FilterMode filterMode() { return mFilterMode; }
145 
151  inline QgsVectorLayer *layer() const { return masterModel()->layer(); }
152 
158  inline QgsVectorLayerCache *layerCache() const { return masterModel()->layerCache(); }
159 
165  inline QgsAttributeTableModel *masterModel() const { return mTableModel; }
166 
174  QgsFeatureId rowToId( const QModelIndex &row );
175 
176  QModelIndex fidToIndex( QgsFeatureId fid ) override;
177 
178  QModelIndexList fidToIndexList( QgsFeatureId fid );
179 
180  inline QModelIndex mapToMaster( const QModelIndex &proxyIndex ) const { return mapToSource( proxyIndex ); }
181 
182  inline QModelIndex mapFromMaster( const QModelIndex &sourceIndex ) const { return mapFromSource( sourceIndex ); }
183 
184  QModelIndex mapToSource( const QModelIndex &proxyIndex ) const override;
185 
186  QModelIndex mapFromSource( const QModelIndex &sourceIndex ) const override;
187 
188  Qt::ItemFlags flags( const QModelIndex &index ) const override;
189 
197  void sort( int column, Qt::SortOrder order = Qt::AscendingOrder ) override;
198 
206  void sort( const QString &expression, Qt::SortOrder order = Qt::AscendingOrder );
207 
211  QString sortExpression() const;
212 
214  QgsMapCanvas *mapCanvas() const { return mCanvas; }
215 
216  QVariant data( const QModelIndex &index, int role ) const override;
217 
218  QVariant headerData( int section, Qt::Orientation orientation, int role ) const override;
219 
224  int actionColumnIndex() const;
225 
226  int columnCount( const QModelIndex &parent ) const override;
227 
233  void setAttributeTableConfig( const QgsAttributeTableConfig &config );
234 
241  void setFilterExpression( const QgsExpression &expression, const QgsExpressionContext &context );
242 
243  signals:
244 
250  void sortColumnChanged( int column, Qt::SortOrder order );
251 
256 
261 
266  void filterError( const QString &errorMessage );
267 
268  protected:
269 
276  bool filterAcceptsRow( int sourceRow, const QModelIndex &sourceParent ) const override;
277 
282  void generateListOfVisibleFeatures();
283 
288  bool lessThan( const QModelIndex &left, const QModelIndex &right ) const override;
289 
290  public slots:
291 
298  Q_DECL_DEPRECATED void extentsChanged();
299 
306  void filterFeatures();
307 
308  private slots:
309  void selectionChanged();
310  void onColumnsChanged();
311  void reloadVisible();
312  void onAttributeValueChanged( QgsFeatureId fid, int idx, const QVariant &value );
313  void onGeometryChanged();
314 
315  private:
316  QgsFeatureIds mFilteredFeatures;
317  QgsMapCanvas *mCanvas = nullptr;
318  FilterMode mFilterMode = FilterMode::ShowAll;
319  bool mSelectedOnTop = false;
320  QgsAttributeTableModel *mTableModel = nullptr;
321 
322  QgsAttributeTableConfig mConfig;
323  QVector<int> mColumnMapping;
324  QgsExpression mFilterExpression;
325  QgsExpressionContext mFilterExpressionContext;
326 
327  int mapColumnToSource( int column ) const;
328  int mapColumnFromSource( int column ) const;
329 
330  QTimer mReloadVisibleTimer;
331  QTimer mFilterFeaturesTimer;
332  void startTimedReloadVisible();
333  void startTimedFilterFeatures();
334 };
335 
336 #endif
This is a container for configuration of the attribute table.
FilterMode filterMode()
The current filterModel.
Role
The additional roles defined by this filter model.
QgsVectorLayer * layer() const
Returns the layer this filter acts on.
QgsMapCanvas * mapCanvas() const
Returns the map canvas.
FilterMode
The filter mode defines how the rows should be filtered.
@ ShowFilteredList
Show only features whose ids are on the filter list. {.
@ ShowVisible
Show only visible features (depends on the map canvas)
@ ShowSelected
Show only selected features.
void filterError(const QString &errorMessage)
Emitted when an error occurred while filtering features.
QModelIndex mapFromMaster(const QModelIndex &sourceIndex) const
QModelIndex mapToMaster(const QModelIndex &proxyIndex) const
void featuresFiltered()
Emitted when the filtering of the features has been done.
void visibleReloaded()
Emitted when the the visible features on extend are reloaded (the list is created)
@ ColumnTypeField
This column shows a field.
QgsVectorLayerCache * layerCache() const
Returns the layerCache this filter acts on.
QgsAttributeTableModel * masterModel() const
Returns the table model this filter is using.
void sortColumnChanged(int column, Qt::SortOrder order)
Emitted whenever the sort column is changed.
A model backed by a QgsVectorLayerCache which is able to provide feature/attribute information to a Q...
@ UserRole
Start further roles starting from this role.
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
Class for parsing and evaluation of expressions (formerly called "search strings").
virtual QModelIndex fidToIndex(QgsFeatureId fid)=0
Map canvas is a class for displaying all GIS data types on a canvas.
Definition: qgsmapcanvas.h:86
This class caches features of a given QgsVectorLayer.
Represents a vector layer which manages a vector based data sets.
#define SIP_TRANSFERTHIS
Definition: qgis_sip.h:53
QSet< QgsFeatureId > QgsFeatureIds
Definition: qgsfeatureid.h:37
qint64 QgsFeatureId
64 bit feature ids negative numbers are used for uncommitted/newly added features
Definition: qgsfeatureid.h:28