QGIS API Documentation  2.99.0-Master (009e47e)
qgsattributetablemodel.h
Go to the documentation of this file.
1 /***************************************************************************
2  QgsAttributeTableModel.h - Models for attribute table
3  -------------------
4  date : Feb 2009
5  copyright : 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 "qgsvectorlayer.h" // QgsAttributeList
29 #include "qgsconditionalstyle.h"
31 #include "qgsvectorlayercache.h"
32 #include "qgis_gui.h"
33 
34 class QgsMapCanvas;
35 class QgsMapLayerAction;
37 class QgsFieldFormatter;
38 
49 class GUI_EXPORT QgsAttributeTableModel: public QAbstractTableModel
50 {
51  Q_OBJECT
52 
53  public:
54  enum Role
55  {
56  SortRole = Qt::UserRole + 1,
59  UserRole
60  };
61 
62  public:
63 
69  QgsAttributeTableModel( QgsVectorLayerCache *layerCache, QObject *parent = nullptr );
70 
75  virtual int rowCount( const QModelIndex &parent = QModelIndex() ) const override;
76 
81  int columnCount( const QModelIndex &parent = QModelIndex() ) const override;
82 
89  QVariant headerData( int section, Qt::Orientation orientation, int role = Qt::DisplayRole ) const override;
90 
96  virtual QVariant data( const QModelIndex &index, int role ) const override;
97 
104  virtual bool setData( const QModelIndex &index, const QVariant &value, int role = Qt::EditRole ) override;
105 
110  Qt::ItemFlags flags( const QModelIndex &index ) const override;
111 
117  void reload( const QModelIndex &index1, const QModelIndex &index2 );
118 
122  bool removeRows( int row, int count, const QModelIndex &parent = QModelIndex() ) override;
123 
129  inline void resetModel() { loadLayer(); }
130 
135  int idToRow( QgsFeatureId id ) const;
136 
137  QModelIndex idToIndex( QgsFeatureId id ) const;
138 
139  QModelIndexList idToIndexList( QgsFeatureId id ) const;
140 
144  int fieldIdx( int col ) const;
145 
149  int fieldCol( int idx ) const;
150 
155  QgsFeatureId rowToId( int row ) const;
156 
162  void swapRows( QgsFeatureId a, QgsFeatureId b );
163 
167  inline QgsVectorLayer *layer() const { return mLayerCache ? mLayerCache->layer() : nullptr; }
168 
172  inline QgsVectorLayerCache *layerCache() const { return mLayerCache; }
173 
177  void executeAction( const QUuid &action, const QModelIndex &idx ) const;
178 
182  void executeMapLayerAction( QgsMapLayerAction *action, const QModelIndex &idx ) const;
183 
188  QgsFeature feature( const QModelIndex &idx ) const;
189 
197  void prefetchColumnData( int column );
198 
205  void prefetchSortData( const QString &expression );
206 
210  QString sortCacheExpression() const;
211 
219  void setRequest( const QgsFeatureRequest &request );
220 
224  // TODO QGIS 3: return copy instead of reference
225  const QgsFeatureRequest &request() const;
226 
233  void setEditorContext( const QgsAttributeEditorContext &context ) { mEditorContext = context; }
234 
241  const QgsAttributeEditorContext &editorContext() const { return mEditorContext; }
242 
247  int extraColumns() const;
248 
253  void setExtraColumns( int extraColumns );
254 
255  public slots:
256 
261  virtual void loadLayer();
262 
267  void fieldConditionalStyleChanged( const QString &fieldName );
268 
269  signals:
270 
274  void modelChanged();
275 
277  void progress( int i, bool &cancel ) SIP_SKIP;
278  void finished();
279 
280  private slots:
281 
285  virtual void updatedFields();
286 
292  virtual void editCommandEnded();
293 
297  virtual void attributeDeleted( int idx );
298 
305  virtual void attributeValueChanged( QgsFeatureId fid, int idx, const QVariant &value );
306 
311  virtual void featuresDeleted( const QgsFeatureIds &fids );
312 
319  virtual void featureAdded( QgsFeatureId fid, bool resettingModel = false );
320 
324  virtual void layerDeleted();
325 
326  virtual void fieldFormatterRemoved( QgsFieldFormatter *fieldFormatter );
327 
328  private:
329  QgsVectorLayerCache *mLayerCache = nullptr;
330  int mFieldCount;
331 
332  mutable QgsFeature mFeat;
333 
334  QgsAttributeList mAttributes;
335  QVector<QgsEditorWidgetFactory *> mWidgetFactories;
336  QVector<QgsFieldFormatter *> mFieldFormatters;
337  QVector<QVariant> mAttributeWidgetCaches;
338  QVector<QVariantMap> mWidgetConfigs;
339 
340  QHash<QgsFeatureId, int> mIdRowMap;
341  QHash<int, QgsFeatureId> mRowIdMap;
342  mutable QHash<int, QList<QgsConditionalStyle> > mRowStylesMap;
343 
344  mutable QgsExpressionContext mExpressionContext;
345 
349  virtual void loadAttributes();
350 
358  virtual bool loadFeatureAtId( QgsFeatureId fid ) const;
359 
360  QgsFeatureRequest mFeatureRequest;
361 
363  QgsExpression mSortCacheExpression;
364  QgsAttributeList mSortCacheAttributes;
366  int mSortFieldIndex;
368  QHash<QgsFeatureId, QVariant> mSortCache;
369 
377  QRect mChangedCellBounds;
378 
379  QgsAttributeEditorContext mEditorContext;
380 
381  int mExtraColumns;
382 
383  friend class TestQgsAttributeTable;
384 
385 };
386 
387 
388 #endif
QgsVectorLayer * layer() const
Returns the layer this model uses as backend.
Get the field index of this column.
QSet< QgsFeatureId > QgsFeatureIds
Definition: qgsfeature.h:519
This class contains context information for attribute editor widgets.
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
Definition: qgsfeature.h:61
A model backed by a QgsVectorLayerCache which is able to provide feature/attribute information to a Q...
Map canvas is a class for displaying all GIS data types on a canvas.
Definition: qgsmapcanvas.h:73
void resetModel()
Resets the model.
Get the feature id of the feature in this row.
#define SIP_SKIP
Definition: qgis_sip.h:107
void setEditorContext(const QgsAttributeEditorContext &context)
Sets the context in which this table is shown.
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
Every attribute editor widget needs a factory, which inherits this class.
This class wraps a request for features to a vector layer (or directly its vector data provider)...
QgsVectorLayerCache * layerCache() const
Returns the layer cache this model uses as backend.
A field formatter helps to handle and display values for a field.
This class caches features of a given QgsVectorLayer.
qint64 QgsFeatureId
Definition: qgsfeature.h:37
const QgsAttributeEditorContext & editorContext() const
Returns the context in which this table is shown.
QList< int > QgsAttributeList
Definition: qgsfield.h:27
Represents a vector layer which manages a vector based data sets.
An action which can run on map layers.