QGIS API Documentation  3.16.0-Hannover (43b64b13f3)
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 
255 
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:370
QgsAttributeTableFilterModel::ShowAll
@ ShowAll
Show all features.
Definition: qgsattributetablefiltermodel.h:46
QgsVectorLayerCache
This class caches features of a given QgsVectorLayer.
Definition: qgsvectorlayercache.h:46
QgsAttributeTableFilterModel::visibleReloaded
void visibleReloaded()
Emitted when the the visible features on extend are reloaded (the list is created)
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
Map canvas is a class for displaying all GIS data types on a canvas.
Definition: qgsmapcanvas.h:85
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
This is a container for configuration of the attribute table.
Definition: qgsattributetableconfig.h:37
QgsAttributeTableFilterModel::ColumnType
ColumnType
The type of a column.
Definition: qgsattributetablefiltermodel.h:58
QgsAttributeTableFilterModel::featuresFiltered
void featuresFiltered()
Emitted when the filtering of the features has been done.
QgsAttributeTableFilterModel::sortColumnChanged
void sortColumnChanged(int column, Qt::SortOrder order)
Emitted whenever the sort column is changed.
QgsAttributeTableFilterModel::layer
QgsVectorLayer * layer() const
Returns the layer this filter acts on.
Definition: qgsattributetablefiltermodel.h:150
QgsAttributeTableModel
A model backed by a QgsVectorLayerCache which is able to provide feature/attribute information to a Q...
Definition: qgsattributetablemodel.h:50
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:27
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:181
QgsAttributeTableFilterModel::ShowSelected
@ ShowSelected
Show only selected features.
Definition: qgsattributetablefiltermodel.h:47
QgsVectorLayer
Represents a vector layer which manages a vector based data sets.
Definition: qgsvectorlayer.h:387
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:143
QgsExpression
Class for parsing and evaluation of expressions (formerly called "search strings").
Definition: qgsexpression.h:105
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:48
QgsFeatureId
qint64 QgsFeatureId
64 bit feature ids negative numbers are used for uncommitted/newly added features
Definition: qgsfeatureid.h:28