QGIS API Documentation  2.99.0-Master (0a63d1f)
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 #include "qgis_gui.h"
32 
33 class QgsMapCanvas;
34 class QgsMapLayerAction;
36 class QgsFieldFormatter;
37 
48 class GUI_EXPORT QgsAttributeTableModel: public QAbstractTableModel
49 {
50  Q_OBJECT
51 
52  public:
53  enum Role
54  {
55  SortRole = Qt::UserRole + 1,
58  UserRole
59  };
60 
61  public:
62 
68  QgsAttributeTableModel( QgsVectorLayerCache *layerCache, QObject *parent = nullptr );
69 
74  virtual int rowCount( const QModelIndex &parent = QModelIndex() ) const override;
75 
80  int columnCount( const QModelIndex &parent = QModelIndex() ) const override;
81 
88  QVariant headerData( int section, Qt::Orientation orientation, int role = Qt::DisplayRole ) const override;
89 
95  virtual QVariant data( const QModelIndex &index, int role ) const override;
96 
103  virtual bool setData( const QModelIndex &index, const QVariant &value, int role = Qt::EditRole ) override;
104 
109  Qt::ItemFlags flags( const QModelIndex &index ) const override;
110 
116  void reload( const QModelIndex &index1, const QModelIndex &index2 );
117 
121  bool removeRows( int row, int count, const QModelIndex &parent = QModelIndex() ) override;
122 
128  inline void resetModel() { loadLayer(); }
129 
134  int idToRow( QgsFeatureId id ) const;
135 
136  QModelIndex idToIndex( QgsFeatureId id ) const;
137 
138  QModelIndexList idToIndexList( QgsFeatureId id ) const;
139 
143  int fieldIdx( int col ) const;
144 
148  int fieldCol( int idx ) const;
149 
154  QgsFeatureId rowToId( int row ) const;
155 
161  void swapRows( QgsFeatureId a, QgsFeatureId b );
162 
166  inline QgsVectorLayer* layer() const { return mLayerCache ? mLayerCache->layer() : nullptr; }
167 
171  inline QgsVectorLayerCache* layerCache() const { return mLayerCache; }
172 
176  void executeAction( const QUuid& action, const QModelIndex &idx ) const;
177 
181  void executeMapLayerAction( QgsMapLayerAction* action, const QModelIndex &idx ) const;
182 
187  QgsFeature feature( const QModelIndex &idx ) const;
188 
196  void prefetchColumnData( int column );
197 
204  void prefetchSortData( const QString& expression );
205 
209  QString sortCacheExpression() const;
210 
218  void setRequest( const QgsFeatureRequest& request );
219 
223  // TODO QGIS 3: return copy instead of reference
224  const QgsFeatureRequest& request() const;
225 
232  void setEditorContext( const QgsAttributeEditorContext& context ) { mEditorContext = context; }
233 
240  const QgsAttributeEditorContext& editorContext() const { return mEditorContext; }
241 
246  int extraColumns() const;
247 
252  void setExtraColumns( int extraColumns );
253 
254  public slots:
255 
260  virtual void loadLayer();
261 
266  void fieldConditionalStyleChanged( const QString& fieldName );
267 
268  signals:
269 
273  void modelChanged();
274 
276  void progress( int i, bool &cancel );
277  void finished();
278 
279  private slots:
280 
284  virtual void updatedFields();
285 
291  virtual void editCommandEnded();
292 
296  virtual void attributeDeleted( int idx );
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  virtual void fieldFormatterRemoved( QgsFieldFormatter* fieldFormatter );
324 
325  private:
326  QgsVectorLayerCache *mLayerCache;
327  int mFieldCount;
328 
329  mutable QgsFeature mFeat;
330 
331  QgsAttributeList mAttributes;
332  QVector<QgsEditorWidgetFactory*> mWidgetFactories;
333  QVector<QgsFieldFormatter*> mFieldFormatters;
334  QVector<QVariant> mAttributeWidgetCaches;
335  QVector<QVariantMap> mWidgetConfigs;
336 
337  QHash<QgsFeatureId, int> mIdRowMap;
338  QHash<int, QgsFeatureId> mRowIdMap;
339  mutable QHash<int, QList<QgsConditionalStyle> > mRowStylesMap;
340 
341  mutable QgsExpressionContext mExpressionContext;
342 
346  virtual void loadAttributes();
347 
355  virtual bool loadFeatureAtId( QgsFeatureId fid ) const;
356 
357  QgsFeatureRequest mFeatureRequest;
358 
360  QgsExpression mSortCacheExpression;
361  QgsAttributeList mSortCacheAttributes;
363  int mSortFieldIndex;
365  QHash<QgsFeatureId, QVariant> mSortCache;
366 
374  QRect mChangedCellBounds;
375 
376  QgsAttributeEditorContext mEditorContext;
377 
378  int mExtraColumns;
379 };
380 
381 
382 #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
Get the field index of this column.
QSet< QgsFeatureId > QgsFeatureIds
Definition: qgsfeature.h:348
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:136
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.
Get the feature id of the feature in this row.
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
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:33
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.