QGIS API Documentation  3.17.0-Master (8af46bc54f)
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 
23 #include "qgsattributetablemodel.h"
24 #include "qgsfeaturemodel.h"
25 #include "qgis_gui.h"
26 
28 class QgsMapCanvas;
29 class QItemSelectionModel;
30 
35 class GUI_EXPORT QgsAttributeTableFilterModel: public QSortFilterProxyModel, public QgsFeatureModel
36 {
37  Q_OBJECT
38 
39  public:
40 
45  {
50  ShowEdited
51  };
52  Q_ENUM( FilterMode )
53 
54 
58  {
60  ColumnTypeActionButton
61  };
62  Q_ENUM( ColumnType )
63 
64 
69  enum Role
70  {
72  };
73 
74 
82  QgsAttributeTableFilterModel( QgsMapCanvas *canvas, QgsAttributeTableModel *sourceModel, QObject *parent SIP_TRANSFERTHIS = nullptr );
83 
91  void setSourceModel( QgsAttributeTableModel *sourceModel );
92 
99  void setSelectedOnTop( bool selectedOnTop );
100 
106  bool selectedOnTop();
107 
114  virtual void setFilteredFeatures( const QgsFeatureIds &ids );
115 
121  QgsFeatureIds filteredFeatures();
122 
128  void setFilterMode( FilterMode filterMode );
129 
133  void disconnectFilterModeConnections();
134 
138  void connectFilterModeConnections( FilterMode filterMode );
139 
143  FilterMode filterMode() { return mFilterMode; }
144 
150  inline QgsVectorLayer *layer() const { return masterModel()->layer(); }
151 
157  inline QgsVectorLayerCache *layerCache() const { return masterModel()->layerCache(); }
158 
164  inline QgsAttributeTableModel *masterModel() const { return mTableModel; }
165 
173  QgsFeatureId rowToId( const QModelIndex &row );
174 
175  QModelIndex fidToIndex( QgsFeatureId fid ) override;
176 
177  QModelIndexList fidToIndexList( QgsFeatureId fid );
178 
179  inline QModelIndex mapToMaster( const QModelIndex &proxyIndex ) const { return mapToSource( proxyIndex ); }
180 
181  inline QModelIndex mapFromMaster( const QModelIndex &sourceIndex ) const { return mapFromSource( sourceIndex ); }
182 
183  QModelIndex mapToSource( const QModelIndex &proxyIndex ) const override;
184 
185  QModelIndex mapFromSource( const QModelIndex &sourceIndex ) const override;
186 
187  Qt::ItemFlags flags( const QModelIndex &index ) const override;
188 
196  void sort( int column, Qt::SortOrder order = Qt::AscendingOrder ) override;
197 
205  void sort( const QString &expression, Qt::SortOrder order = Qt::AscendingOrder );
206 
210  QString sortExpression() const;
211 
213  QgsMapCanvas *mapCanvas() const { return mCanvas; }
214 
215  QVariant data( const QModelIndex &index, int role ) const override;
216 
217  QVariant headerData( int section, Qt::Orientation orientation, int role ) const override;
218 
223  int actionColumnIndex() const;
224 
225  int columnCount( const QModelIndex &parent ) const override;
226 
232  void setAttributeTableConfig( const QgsAttributeTableConfig &config );
233 
240  void setFilterExpression( const QgsExpression &expression, const QgsExpressionContext &context );
241 
242  signals:
243 
249  void sortColumnChanged( int column, Qt::SortOrder order );
250 
254  void featuresFiltered();
255 
259  void visibleReloaded();
260 
261  protected:
262 
269  bool filterAcceptsRow( int sourceRow, const QModelIndex &sourceParent ) const override;
270 
275  void generateListOfVisibleFeatures();
276 
281  bool lessThan( const QModelIndex &left, const QModelIndex &right ) const override;
282 
283  public slots:
284 
291  Q_DECL_DEPRECATED void extentsChanged();
292 
299  void filterFeatures();
300 
301  private slots:
302  void selectionChanged();
303  void onColumnsChanged();
304  void reloadVisible();
305  void onAttributeValueChanged( QgsFeatureId fid, int idx, const QVariant &value );
306  void onGeometryChanged();
307 
308  private:
309  QgsFeatureIds mFilteredFeatures;
310  QgsMapCanvas *mCanvas = nullptr;
311  FilterMode mFilterMode = FilterMode::ShowAll;
312  bool mSelectedOnTop = false;
313  QgsAttributeTableModel *mTableModel = nullptr;
314 
315  QgsAttributeTableConfig mConfig;
316  QVector<int> mColumnMapping;
317  QgsExpression mFilterExpression;
318  QgsExpressionContext mFilterExpressionContext;
319 
320  int mapColumnToSource( int column ) const;
321  int mapColumnFromSource( int column ) const;
322 
323  QTimer mReloadVisibleTimer;
324  QTimer mFilterFeaturesTimer;
325  void startTimedReloadVisible();
326  void startTimedFilterFeatures();
327 };
328 
329 #endif
Class for parsing and evaluation of expressions (formerly called "search strings").
QgsVectorLayer * layer() const
Returns the layer this filter acts on.
QSet< QgsFeatureId > QgsFeatureIds
Definition: qgsfeatureid.h:37
QgsAttributeTableModel * masterModel() const
Returns the table model this filter is using.
#define SIP_TRANSFERTHIS
Definition: qgis_sip.h:53
QModelIndex mapFromMaster(const QModelIndex &sourceIndex) const
qint64 QgsFeatureId
64 bit feature ids negative numbers are used for uncommitted/newly added features ...
Definition: qgsfeatureid.h:28
Role
The additional roles defined by this filter model.
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)
Map canvas is a class for displaying all GIS data types on a canvas.
Definition: qgsmapcanvas.h:84
virtual QModelIndex fidToIndex(QgsFeatureId fid)=0
FilterMode filterMode()
The current filterModel.
FilterMode
The filter mode defines how the rows should be filtered.
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
This class caches features of a given QgsVectorLayer.
Show only features whose ids are on the filter list. {.
Start further roles starting from this role.
QgsMapCanvas * mapCanvas() const
Returns the map canvas.
This is a container for configuration of the attribute table.
Represents a vector layer which manages a vector based data sets.
QgsVectorLayerCache * layerCache() const
Returns the layerCache this filter acts on.
QModelIndex mapToMaster(const QModelIndex &proxyIndex) const