QGIS API Documentation
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 
203  void setRequest( const QgsFeatureRequest& request );
204 
208  const QgsFeatureRequest &request() const;
209 
216  void setEditorContext( const QgsAttributeEditorContext& context ) { mEditorContext = context; }
217 
224  const QgsAttributeEditorContext& editorContext() const { return mEditorContext; }
225 
226  public slots:
231  virtual void loadLayer();
232 
237  void fieldConditionalStyleChanged( const QString& fieldName );
238 
239  signals:
243  void modelChanged();
244 
246  void progress( int i, bool &cancel );
247  void finished();
248 
249  private slots:
253  virtual void updatedFields();
254 
260  virtual void editCommandEnded();
261 
265  virtual void attributeDeleted( int idx );
266 
267  protected slots:
274  virtual void attributeValueChanged( QgsFeatureId fid, int idx, const QVariant &value );
279  virtual void featuresDeleted( const QgsFeatureIds& fids );
284  virtual void featureAdded( QgsFeatureId fid );
285 
289  virtual void layerDeleted();
290 
291  protected:
294 
295  mutable QgsFeature mFeat;
296 
301 
305 
307 
311  virtual void loadAttributes();
312 
313  private:
321  virtual bool loadFeatureAtId( QgsFeatureId fid ) const;
322 
323  QgsFeatureRequest mFeatureRequest;
324 
326  int mCachedField;
328  QHash<QgsFeatureId, QVariant> mFieldCache;
329 
337  QRect mChangedCellBounds;
338 
339  QgsAttributeEditorContext mEditorContext;
340 };
341 
342 
343 #endif
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:108
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)...
QVector< QgsEditorWidgetConfig > mWidgetConfigs
QHash< QgsFeatureId, int > mIdRowMap
virtual QVariant data(const QModelIndex &index, int role) const =0
const QgsAttributeEditorContext & editorContext() const
Returns the context in which this table is shown.
QgsVectorLayerCache * layerCache() const
Returns the layer cache this model uses as backend.
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
QgsVectorLayer * layer() const
Returns the layer this model uses as backend.
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
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