QGIS API Documentation  3.16.0-Hannover (43b64b13f3)
qgsattributetablemodel.h
Go to the documentation of this file.
1 /***************************************************************************
2  QgsAttributeTableModel.h - Models 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 QGSATTRIBUTETABLEMODEL_H
18 #define QGSATTRIBUTETABLEMODEL_H
19 
20 #include <QAbstractTableModel>
21 #include "qgis_sip.h"
22 #include <QModelIndex>
23 #include <QObject>
24 #include <QHash>
25 #include <QQueue>
26 #include <QMap>
27 
28 #include "qgsconditionalstyle.h"
30 #include "qgsvectorlayercache.h"
31 #include "qgis_gui.h"
32 
33 class QgsMapCanvas;
34 class QgsMapLayerAction;
36 class QgsFieldFormatter;
37 
49 class GUI_EXPORT QgsAttributeTableModel: public QAbstractTableModel
50 {
51  Q_OBJECT
52 
53  public:
54  enum Role
55  {
56  FeatureIdRole = Qt::UserRole,
59  // Insert new values here, SortRole needs to be the last one
61  };
62 
63  public:
64 
70  QgsAttributeTableModel( QgsVectorLayerCache *layerCache, QObject *parent = nullptr );
71 
76  int rowCount( const QModelIndex &parent = QModelIndex() ) const override;
77 
82  int columnCount( const QModelIndex &parent = QModelIndex() ) const override;
83 
90  QVariant headerData( int section, Qt::Orientation orientation, int role = Qt::DisplayRole ) const override;
91 
97  QVariant data( const QModelIndex &index, int role ) const override;
98 
105  bool setData( const QModelIndex &index, const QVariant &value, int role = Qt::EditRole ) override;
106 
111  Qt::ItemFlags flags( const QModelIndex &index ) const override;
112 
118  void reload( const QModelIndex &index1, const QModelIndex &index2 );
119 
123  bool removeRows( int row, int count, const QModelIndex &parent = QModelIndex() ) override;
124 
130  inline void resetModel() { loadLayer(); }
131 
136  int idToRow( QgsFeatureId id ) const;
137 
138  QModelIndex idToIndex( QgsFeatureId id ) const;
139 
140  QModelIndexList idToIndexList( QgsFeatureId id ) const;
141 
145  int fieldIdx( int col ) const;
146 
150  int fieldCol( int idx ) const;
151 
156  QgsFeatureId rowToId( int row ) const;
157 
163  void swapRows( QgsFeatureId a, QgsFeatureId b );
164 
168  inline QgsVectorLayer *layer() const { return mLayerCache ? mLayerCache->layer() : nullptr; }
169 
173  inline QgsVectorLayerCache *layerCache() const { return mLayerCache; }
174 
178  void executeAction( QUuid action, const QModelIndex &idx ) const;
179 
183  void executeMapLayerAction( QgsMapLayerAction *action, const QModelIndex &idx ) const;
184 
189  QgsFeature feature( const QModelIndex &idx ) const;
190 
198  void prefetchColumnData( int column );
199 
206  void prefetchSortData( const QString &expression, unsigned long cacheIndex = 0 );
207 
213  QString sortCacheExpression( unsigned long cacheIndex = 0 ) const;
214 
222  void setRequest( const QgsFeatureRequest &request );
223 
224  // TODO QGIS 4: return copy instead of reference
225 
229  const QgsFeatureRequest &request() const;
230 
237  void setEditorContext( const QgsAttributeEditorContext &context ) { mEditorContext = context; }
238 
245  const QgsAttributeEditorContext &editorContext() const { return mEditorContext; }
246 
251  int extraColumns() const;
252 
257  void setExtraColumns( int extraColumns );
258 
259  public slots:
260 
265  virtual void loadLayer();
266 
272  void fieldConditionalStyleChanged( const QString &fieldName );
273 
274  signals:
275 
279  void modelChanged();
280 
282  void progress( int i, bool &cancel ) SIP_SKIP;
283  void finished();
284 
285  private slots:
286 
290  virtual void updatedFields();
291 
297  virtual void editCommandEnded();
298 
302  virtual void attributeDeleted( int idx );
303 
310  virtual void attributeValueChanged( QgsFeatureId fid, int idx, const QVariant &value );
311 
316  virtual void featuresDeleted( const QgsFeatureIds &fids );
317 
322  virtual void featureAdded( QgsFeatureId fid );
323 
327  virtual void layerDeleted();
328 
329  virtual void fieldFormatterRemoved( QgsFieldFormatter *fieldFormatter );
330 
331  private:
332  QgsVectorLayerCache *mLayerCache = nullptr;
333  int mFieldCount = 0;
334 
335  mutable QgsFeature mFeat;
336 
337  QgsAttributeList mAttributes;
338  QVector<QgsEditorWidgetFactory *> mWidgetFactories;
339  QVector<QgsFieldFormatter *> mFieldFormatters;
340  QVector<QVariant> mAttributeWidgetCaches;
341  QVector<QVariantMap> mWidgetConfigs;
342 
343  QHash<QgsFeatureId, int> mIdRowMap;
344  QHash<int, QgsFeatureId> mRowIdMap;
345  mutable QHash<int, QList<QgsConditionalStyle> > mRowStylesMap;
346 
347  mutable QgsExpressionContext mExpressionContext;
348 
352  virtual void loadAttributes();
353 
361  virtual bool loadFeatureAtId( QgsFeatureId fid ) const;
362 
363  QgsFeatureRequest mFeatureRequest;
364 
365  struct SortCache
366  {
368  int sortFieldIndex;
370  QgsExpression sortCacheExpression;
371  QgsAttributeList sortCacheAttributes;
373  QHash<QgsFeatureId, QVariant> sortCache;
374  };
375 
376  std::vector<SortCache> mSortCaches;
377 
378  QgsAttributeEditorContext mEditorContext;
379 
380  int mExtraColumns = 0;
381 
383  bool mBulkEditCommandRunning = false;
384 
386  bool mResettingModel = false;
387 
389  void bulkEditCommandStarted();
390 
392  void bulkEditCommandEnded();
393 
395  QMap<QPair<QgsFeatureId, int>, QVariant> mAttributeValueChanges;
396 
397  friend class TestQgsAttributeTable;
398 
399 };
400 
401 
402 #endif
QgsExpressionContext
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
Definition: qgsexpressioncontext.h:370
qgsconditionalstyle.h
QgsEditorWidgetFactory
Every attribute editor widget needs a factory, which inherits this class.
Definition: qgseditorwidgetfactory.h:43
QgsMapLayerAction
An action which can run on map layers.
Definition: qgsmaplayeractionregistry.h:35
qgsvectorlayercache.h
QgsVectorLayerCache
This class caches features of a given QgsVectorLayer.
Definition: qgsvectorlayercache.h:46
QgsAttributeTableModel::progress
void progress(int i, bool &cancel)
QgsMapCanvas
Map canvas is a class for displaying all GIS data types on a canvas.
Definition: qgsmapcanvas.h:85
QgsAttributeTableModel::modelChanged
void modelChanged()
Model has been changed.
QgsAttributeList
QList< int > QgsAttributeList
Definition: qgsfield.h:26
QgsAttributeTableModel::editorContext
const QgsAttributeEditorContext & editorContext() const
Returns the context in which this table is shown.
Definition: qgsattributetablemodel.h:245
QgsAttributeTableModel::UserRole
@ UserRole
Start further roles starting from this role.
Definition: qgsattributetablemodel.h:58
QgsFeatureRequest
This class wraps a request for features to a vector layer (or directly its vector data provider).
Definition: qgsfeaturerequest.h:76
QgsAttributeTableModel::FieldIndexRole
@ FieldIndexRole
Get the field index of this column.
Definition: qgsattributetablemodel.h:57
QgsAttributeTableModel::Role
Role
Definition: qgsattributetablemodel.h:55
SIP_SKIP
#define SIP_SKIP
Definition: qgis_sip.h:126
QgsAttributeTableModel
A model backed by a QgsVectorLayerCache which is able to provide feature/attribute information to a Q...
Definition: qgsattributetablemodel.h:50
qgis_sip.h
QgsAttributeTableModel::finished
void finished()
qgsattributeeditorcontext.h
QgsAttributeTableModel::layer
QgsVectorLayer * layer() const
Returns the layer this model uses as backend.
Definition: qgsattributetablemodel.h:168
QgsFeatureIds
QSet< QgsFeatureId > QgsFeatureIds
Definition: qgsfeatureid.h:37
QgsVectorLayer
Represents a vector layer which manages a vector based data sets.
Definition: qgsvectorlayer.h:387
QgsFeature
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
Definition: qgsfeature.h:56
QgsAttributeTableModel::SortRole
@ SortRole
Role used for sorting start here.
Definition: qgsattributetablemodel.h:60
QgsExpression
Class for parsing and evaluation of expressions (formerly called "search strings").
Definition: qgsexpression.h:105
QgsAttributeEditorContext
This class contains context information for attribute editor widgets.
Definition: qgsattributeeditorcontext.h:41
QgsAttributeTableModel::setEditorContext
void setEditorContext(const QgsAttributeEditorContext &context)
Sets the context in which this table is shown.
Definition: qgsattributetablemodel.h:237
QgsFieldFormatter
A field formatter helps to handle and display values for a field.
Definition: qgsfieldformatter.h:73
QgsAttributeTableModel::resetModel
void resetModel()
Resets the model.
Definition: qgsattributetablemodel.h:130
QgsAttributeTableModel::layerCache
QgsVectorLayerCache * layerCache() const
Returns the layer cache this model uses as backend.
Definition: qgsattributetablemodel.h:173
QgsFeatureId
qint64 QgsFeatureId
64 bit feature ids negative numbers are used for uncommitted/newly added features
Definition: qgsfeatureid.h:28