QGIS API Documentation  3.2.0-Bonn (bc43194)
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 "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 
50 class GUI_EXPORT QgsAttributeTableModel: public QAbstractTableModel
51 {
52  Q_OBJECT
53 
54  public:
55  enum Role
56  {
57  FeatureIdRole = Qt::UserRole,
60  // Insert new values here, SortRole needs to be the last one
62  };
63 
64  public:
65 
71  QgsAttributeTableModel( QgsVectorLayerCache *layerCache, QObject *parent = nullptr );
72 
77  int rowCount( const QModelIndex &parent = QModelIndex() ) const override;
78 
83  int columnCount( const QModelIndex &parent = QModelIndex() ) const override;
84 
91  QVariant headerData( int section, Qt::Orientation orientation, int role = Qt::DisplayRole ) const override;
92 
98  QVariant data( const QModelIndex &index, int role ) const override;
99 
106  bool setData( const QModelIndex &index, const QVariant &value, int role = Qt::EditRole ) override;
107 
112  Qt::ItemFlags flags( const QModelIndex &index ) const override;
113 
119  void reload( const QModelIndex &index1, const QModelIndex &index2 );
120 
124  bool removeRows( int row, int count, const QModelIndex &parent = QModelIndex() ) override;
125 
131  inline void resetModel() { loadLayer(); }
132 
137  int idToRow( QgsFeatureId id ) const;
138 
139  QModelIndex idToIndex( QgsFeatureId id ) const;
140 
141  QModelIndexList idToIndexList( QgsFeatureId id ) const;
142 
146  int fieldIdx( int col ) const;
147 
151  int fieldCol( int idx ) const;
152 
157  QgsFeatureId rowToId( int row ) const;
158 
164  void swapRows( QgsFeatureId a, QgsFeatureId b );
165 
169  inline QgsVectorLayer *layer() const { return mLayerCache ? mLayerCache->layer() : nullptr; }
170 
174  inline QgsVectorLayerCache *layerCache() const { return mLayerCache; }
175 
179  void executeAction( QUuid action, const QModelIndex &idx ) const;
180 
184  void executeMapLayerAction( QgsMapLayerAction *action, const QModelIndex &idx ) const;
185 
190  QgsFeature feature( const QModelIndex &idx ) const;
191 
199  void prefetchColumnData( int column );
200 
207  void prefetchSortData( const QString &expression, unsigned long cacheIndex = 0 );
208 
214  QString sortCacheExpression( unsigned long cacheIndex = 0 ) const;
215 
223  void setRequest( const QgsFeatureRequest &request );
224 
228  // TODO QGIS 3: return copy instead of reference
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 
324  virtual void featureAdded( QgsFeatureId fid, bool resettingModel = false );
325 
329  virtual void layerDeleted();
330 
331  virtual void fieldFormatterRemoved( QgsFieldFormatter *fieldFormatter );
332 
333  private:
334  QgsVectorLayerCache *mLayerCache = nullptr;
335  int mFieldCount = 0;
336 
337  mutable QgsFeature mFeat;
338 
339  QgsAttributeList mAttributes;
340  QVector<QgsEditorWidgetFactory *> mWidgetFactories;
341  QVector<QgsFieldFormatter *> mFieldFormatters;
342  QVector<QVariant> mAttributeWidgetCaches;
343  QVector<QVariantMap> mWidgetConfigs;
344 
345  QHash<QgsFeatureId, int> mIdRowMap;
346  QHash<int, QgsFeatureId> mRowIdMap;
347  mutable QHash<int, QList<QgsConditionalStyle> > mRowStylesMap;
348 
349  mutable QgsExpressionContext mExpressionContext;
350 
354  virtual void loadAttributes();
355 
363  virtual bool loadFeatureAtId( QgsFeatureId fid ) const;
364 
365  bool fieldIsEditable( const QgsVectorLayer &layer, int fieldIndex, QgsFeatureId fid ) const;
366 
367  QgsFeatureRequest mFeatureRequest;
368 
369  struct SortCache
370  {
372  int sortFieldIndex;
374  QgsExpression sortCacheExpression;
375  QgsAttributeList sortCacheAttributes;
377  QHash<QgsFeatureId, QVariant> sortCache;
378  };
379 
380  std::vector<SortCache> mSortCaches;
381 
389  QRect mChangedCellBounds;
390 
391  QgsAttributeEditorContext mEditorContext;
392 
393  int mExtraColumns = 0;
394 
395  friend class TestQgsAttributeTable;
396 
397 };
398 
399 
400 #endif
Class for parsing and evaluation of expressions (formerly called "search strings").
QgsVectorLayer * layer() const
Returns the layer this model uses as backend.
Get the field index of this column.
QSet< QgsFeatureId > QgsFeatureIds
Definition: qgsfeature.h:544
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:62
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:74
void resetModel()
Resets the model.
Roles used for sorting start here.
#define SIP_SKIP
Definition: qgis_sip.h:119
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.
Start further roles starting from this role.
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.