QGIS API Documentation  2.99.0-Master (e077efd)
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 "qgsconditionalstyle.h"
30 #include "qgsvectorlayercache.h"
31 
32 class QgsMapCanvas;
33 class QgsMapLayerAction;
35 
46 class GUI_EXPORT QgsAttributeTableModel: public QAbstractTableModel
47 {
48  Q_OBJECT
49 
50  public:
51  enum Role
52  {
53  SortRole = Qt::UserRole + 1,
56  UserRole
57  };
58 
59  public:
60 
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( const QUuid& 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:
253 
258  virtual void loadLayer();
259 
264  void fieldConditionalStyleChanged( const QString& fieldName );
265 
266  signals:
267 
271  void modelChanged();
272 
274  void progress( int i, bool &cancel );
275  void finished();
276 
277  private slots:
278 
282  virtual void updatedFields();
283 
289  virtual void editCommandEnded();
290 
294  virtual void attributeDeleted( int idx );
295 
296  protected slots:
297 
304  virtual void attributeValueChanged( QgsFeatureId fid, int idx, const QVariant &value );
305 
310  virtual void featuresDeleted( const QgsFeatureIds& fids );
311 
316  virtual void featureAdded( QgsFeatureId fid );
317 
321  virtual void layerDeleted();
322 
323  protected:
326 
327  mutable QgsFeature mFeat;
328 
330  QVector<QgsEditorWidgetFactory*> mWidgetFactories;
331  QVector<QVariant> mAttributeWidgetCaches;
332  QVector<QgsEditorWidgetConfig> mWidgetConfigs;
333 
334  QHash<QgsFeatureId, int> mIdRowMap;
335  QHash<int, QgsFeatureId> mRowIdMap;
336  mutable QHash<int, QList<QgsConditionalStyle> > mRowStylesMap;
337 
339 
343  virtual void loadAttributes();
344 
345  private:
346 
354  virtual bool loadFeatureAtId( QgsFeatureId fid ) const;
355 
356  QgsFeatureRequest mFeatureRequest;
357 
359  QgsExpression mSortCacheExpression;
360  QgsAttributeList mSortCacheAttributes;
362  int mSortFieldIndex;
364  QHash<QgsFeatureId, QVariant> mSortCache;
365 
373  QRect mChangedCellBounds;
374 
375  QgsAttributeEditorContext mEditorContext;
376 
377  int mExtraColumns;
378 };
379 
380 
381 #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
QHash< int, QgsFeatureId > mRowIdMap
Get the field index of this column.
QSet< QgsFeatureId > QgsFeatureIds
Definition: qgsfeature.h:355
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:135
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:106
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.
QList< int > QgsAttributeList
QVector< QgsEditorWidgetConfig > mWidgetConfigs
QHash< QgsFeatureId, int > mIdRowMap
This class caches features of a given QgsVectorLayer.
QVector< QVariant > mAttributeWidgetCaches
qint64 QgsFeatureId
Definition: qgsfeature.h:32
const QgsAttributeEditorContext & editorContext() const
Returns the context in which this table is shown.
Represents a vector layer which manages a vector based data sets.
An action which can run on map layers.
QHash< int, QList< QgsConditionalStyle > > mRowStylesMap