QGIS API Documentation  2.15.0-Master (5f66276)
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  const QgsFeatureRequest& request() const;
222 
229  void setEditorContext( const QgsAttributeEditorContext& context ) { mEditorContext = context; }
230 
237  const QgsAttributeEditorContext& editorContext() const { return mEditorContext; }
238 
243  int extraColumns() const;
244 
249  void setExtraColumns( int extraColumns );
250 
251  public slots:
256  virtual void loadLayer();
257 
262  void fieldConditionalStyleChanged( const QString& fieldName );
263 
264  signals:
268  void modelChanged();
269 
271  void progress( int i, bool &cancel );
272  void finished();
273 
274  private slots:
278  virtual void updatedFields();
279 
285  virtual void editCommandEnded();
286 
290  virtual void attributeDeleted( int idx );
291 
292  protected slots:
299  virtual void attributeValueChanged( QgsFeatureId fid, int idx, const QVariant &value );
304  virtual void featuresDeleted( const QgsFeatureIds& fids );
309  virtual void featureAdded( QgsFeatureId fid );
310 
314  virtual void layerDeleted();
315 
316  protected:
319 
320  mutable QgsFeature mFeat;
321 
326 
330 
332 
336  virtual void loadAttributes();
337 
338  private:
346  virtual bool loadFeatureAtId( QgsFeatureId fid ) const;
347 
348  QgsFeatureRequest mFeatureRequest;
349 
351  QgsExpression mSortCacheExpression;
352  QgsAttributeList mSortCacheAttributes;
355 
363  QRect mChangedCellBounds;
364 
365  QgsAttributeEditorContext mEditorContext;
366 
367  int mExtraColumns;
368 };
369 
370 
371 #endif
Class for parsing and evaluation of expressions (formerly called "search strings").
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)...
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