QGIS API Documentation  3.14.0-Pi (9f7028fd23)
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
QgsExpressionContext
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
Definition: qgsexpressioncontext.h:369
QgsAttributeTableFilterModel::ShowAll
@ ShowAll
Show all features.
Definition: qgsattributetablefiltermodel.h:46
QgsVectorLayerCache
The cached features can be indexed by QgsAbstractCacheIndex.
Definition: qgsvectorlayercache.h:45
QgsFeatureModel::fidToIndex
virtual QModelIndex fidToIndex(QgsFeatureId fid)=0
QgsAttributeTableFilterModel::ShowFilteredList
@ ShowFilteredList
Show only features whose ids are on the filter list. {.
Definition: qgsattributetablefiltermodel.h:49
QgsMapCanvas
Definition: qgsmapcanvas.h:83
QgsAttributeTableFilterModel::ColumnTypeField
@ ColumnTypeField
This column shows a field.
Definition: qgsattributetablefiltermodel.h:59
QgsAttributeTableFilterModel::mapCanvas
QgsMapCanvas * mapCanvas() const
Returns the map canvas.
Definition: qgsattributetablefiltermodel.h:213
QgsAttributeTableModel::UserRole
@ UserRole
Start further roles starting from this role.
Definition: qgsattributetablemodel.h:58
QgsAttributeTableConfig
Definition: qgsattributetableconfig.h:36
QgsAttributeTableFilterModel::ColumnType
ColumnType
The type of a column.
Definition: qgsattributetablefiltermodel.h:57
QgsAttributeTableFilterModel::layer
QgsVectorLayer * layer() const
Returns the layer this filter acts on.
Definition: qgsattributetablefiltermodel.h:150
QgsAttributeTableModel
Is able to generate editor widgets for its QModelIndexes as well. Is mostly referred to as "master mo...
Definition: qgsattributetablemodel.h:49
QgsAttributeTableFilterModel::layerCache
QgsVectorLayerCache * layerCache() const
Returns the layerCache this filter acts on.
Definition: qgsattributetablefiltermodel.h:157
QgsAttributeTableFilterModel::masterModel
QgsAttributeTableModel * masterModel() const
Returns the table model this filter is using.
Definition: qgsattributetablefiltermodel.h:164
QgsAttributeTableFilterModel::mapToMaster
QModelIndex mapToMaster(const QModelIndex &proxyIndex) const
Definition: qgsattributetablefiltermodel.h:179
qgsattributetablemodel.h
QgsFeatureModel
Definition: qgsfeaturemodel.h:26
QgsFeatureIds
QSet< QgsFeatureId > QgsFeatureIds
Definition: qgsfeatureid.h:34
qgsfeaturemodel.h
QgsAttributeTableFilterModel
Definition: qgsattributetablefiltermodel.h:35
QgsAttributeTableFilterModel::mapFromMaster
QModelIndex mapFromMaster(const QModelIndex &sourceIndex) const
Definition: qgsattributetablefiltermodel.h:181
QgsAttributeTableFilterModel::ShowSelected
@ ShowSelected
Show only selected features.
Definition: qgsattributetablefiltermodel.h:47
QgsVectorLayer
Definition: qgsvectorlayer.h:385
QgsAttributeTableFilterModel::Role
Role
The additional roles defined by this filter model.
Definition: qgsattributetablefiltermodel.h:69
QgsAttributeTableFilterModel::filterMode
FilterMode filterMode()
The current filterModel.
Definition: qgsattributetablefiltermodel.h:143
QgsExpression
Definition: qgsexpression.h:113
QgsAttributeTableFilterModel::FilterMode
FilterMode
The filter mode defines how the rows should be filtered.
Definition: qgsattributetablefiltermodel.h:44
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:48
QgsFeatureId
qint64 QgsFeatureId
Definition: qgsfeatureid.h:25