QGIS API Documentation  2.18.21-Las Palmas (9fba24a)
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 <QModelIndex>
22 #include <QObject>
23 #include <QHash>
24 #include <QQueue>
25 #include <QMap>
26 
27 #include "qgsvectorlayer.h" // QgsAttributeList
28 #include "qgsvectorlayercache.h"
29 #include "qgsconditionalstyle.h"
31 
32 class QgsMapCanvas;
33 class QgsMapLayerAction;
35 
36 
48 {
49  Q_OBJECT
50 
51  public:
52  enum Role
53  {
54  SortRole = Qt::UserRole + 1,
57  UserRole
58  };
59 
60  public:
66  QgsAttributeTableModel( QgsVectorLayerCache *layerCache, QObject *parent = nullptr );
67 
72  virtual int rowCount( const QModelIndex &parent = QModelIndex() ) const override;
73 
78  int columnCount( const QModelIndex &parent = QModelIndex() ) const override;
79 
86  QVariant headerData( int section, Qt::Orientation orientation, int role = Qt::DisplayRole ) const override;
87 
93  virtual QVariant data( const QModelIndex &index, int role ) const override;
94 
101  virtual bool setData( const QModelIndex &index, const QVariant &value, int role = Qt::EditRole ) override;
102 
107  Qt::ItemFlags flags( const QModelIndex &index ) const override;
108 
114  void reload( const QModelIndex &index1, const QModelIndex &index2 );
115 
119  bool removeRows( int row, int count, const QModelIndex &parent = QModelIndex() ) override;
120 
126  inline void resetModel() { loadLayer(); }
127 
132  int idToRow( QgsFeatureId id ) const;
133 
134  QModelIndex idToIndex( QgsFeatureId id ) const;
135 
136  QModelIndexList idToIndexList( QgsFeatureId id ) const;
137 
141  int fieldIdx( int col ) const;
142 
146  int fieldCol( int idx ) const;
147 
152  QgsFeatureId rowToId( int row ) const;
153 
159  void swapRows( QgsFeatureId a, QgsFeatureId b );
160 
164  inline QgsVectorLayer* layer() const { return mLayerCache ? mLayerCache->layer() : nullptr; }
165 
169  inline QgsVectorLayerCache* layerCache() const { return mLayerCache; }
170 
174  void executeAction( int action, const QModelIndex &idx ) const;
175 
179  void executeMapLayerAction( QgsMapLayerAction* action, const QModelIndex &idx ) const;
180 
185  QgsFeature feature( const QModelIndex &idx ) const;
186 
194  void prefetchColumnData( int column );
195 
202  void prefetchSortData( const QString& expression );
203 
207  QString sortCacheExpression() const;
208 
216  void setRequest( const QgsFeatureRequest& request );
217 
221  // TODO QGIS 3: return copy instead of reference
222  const QgsFeatureRequest& request() const;
223 
230  void setEditorContext( const QgsAttributeEditorContext& context ) { mEditorContext = context; }
231 
238  const QgsAttributeEditorContext& editorContext() const { return mEditorContext; }
239 
244  int extraColumns() const;
245 
250  void setExtraColumns( int extraColumns );
251 
252  public slots:
257  virtual void loadLayer();
258 
263  void fieldConditionalStyleChanged( const QString& fieldName );
264 
265  signals:
269  void modelChanged();
270 
272  void progress( int i, bool &cancel );
273  void finished();
274 
275  private slots:
279  virtual void updatedFields();
280 
286  virtual void editCommandEnded();
287 
291  virtual void attributeDeleted( int idx );
292 
293  protected slots:
300  virtual void attributeValueChanged( QgsFeatureId fid, int idx, const QVariant &value );
305  virtual void featuresDeleted( const QgsFeatureIds& fids );
312  virtual void featureAdded( QgsFeatureId fid, bool resettingModel = false );
313 
317  virtual void layerDeleted();
318 
319  protected:
322 
323  mutable QgsFeature mFeat;
324 
329 
333 
335 
339  virtual void loadAttributes();
340 
341  private:
349  virtual bool loadFeatureAtId( QgsFeatureId fid ) const;
350 
351  QgsFeatureRequest mFeatureRequest;
352 
354  QgsExpression mSortCacheExpression;
355  QgsAttributeList mSortCacheAttributes;
357  int mSortFieldIndex;
360 
368  QRect mChangedCellBounds;
369 
370  QgsAttributeEditorContext mEditorContext;
371 
372  int mExtraColumns;
373 };
374 
375 
376 #endif
Class for parsing and evaluation of expressions (formerly called "search strings").
QgsVectorLayer * layer() const
Returns the layer this model uses as backend.
static unsigned index
virtual int rowCount(const QModelIndex &parent) const=0
QHash< int, QgsFeatureId > mRowIdMap
Get the field index of this column.
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:187
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:109
void resetModel()
Resets the model.
QgsVectorLayerCache * mLayerCache
Get the feature id of the feature in this row.
QVector< QgsEditorWidgetFactory * > mWidgetFactories
QgsExpressionContext mExpressionContext
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.
QVector< QgsEditorWidgetConfig > mWidgetConfigs
QHash< QgsFeatureId, int > mIdRowMap
virtual QVariant data(const QModelIndex &index, int role) const=0
virtual QVariant headerData(int section, Qt::Orientation orientation, int role) const
This class caches features of a given QgsVectorLayer.
virtual bool removeRows(int row, int count, const QModelIndex &parent)
QVector< QVariant > mAttributeWidgetCaches
virtual int columnCount(const QModelIndex &parent) const=0
virtual bool setData(const QModelIndex &index, const QVariant &value, int role)
qint64 QgsFeatureId
Definition: qgsfeature.h:31
const QgsAttributeEditorContext & editorContext() const
Returns the context in which this table is shown.
virtual Qt::ItemFlags flags(const QModelIndex &index) const
Represents a vector layer which manages a vector based data sets.
An action which can run on map layers.
QHash< int, QList< QgsConditionalStyle > > mRowStylesMap
typedef ItemFlags