QGIS API Documentation  2.8.2-Wien
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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"
30 
31 class QgsMapCanvas;
32 class QgsMapLayerAction;
34 
45 class GUI_EXPORT QgsAttributeTableModel: public QAbstractTableModel
46 {
47  Q_OBJECT
48 
49  public:
50  enum Role
51  {
52  SortRole = Qt::UserRole + 1,
53  FeatureIdRole = Qt::UserRole + 2,
54  FieldIndexRole = Qt::UserRole + 3
55  };
56 
57  public:
63  QgsAttributeTableModel( QgsVectorLayerCache *layerCache, QObject *parent = 0 );
64 
69  virtual void loadLayer();
70 
75  virtual int rowCount( const QModelIndex &parent = QModelIndex() ) const override;
76 
81  int columnCount( const QModelIndex &parent = QModelIndex() ) const override;
82 
89  QVariant headerData( int section, Qt::Orientation orientation, int role = Qt::DisplayRole ) const override;
90 
96  virtual QVariant data( const QModelIndex &index, int role ) const override;
97 
104  virtual bool setData( const QModelIndex &index, const QVariant &value, int role = Qt::EditRole ) override;
105 
110  Qt::ItemFlags flags( const QModelIndex &index ) const override;
111 
117  void reload( const QModelIndex &index1, const QModelIndex &index2 );
118 
122  bool removeRows( int row, int count, const QModelIndex &parent = QModelIndex() ) override;
123 
127  void resetModel();
128 
133  int idToRow( QgsFeatureId id ) const;
134 
135  QModelIndex idToIndex( QgsFeatureId id ) const;
136 
137  QModelIndexList idToIndexList( QgsFeatureId id ) const;
138 
142  int fieldIdx( int col ) const;
143 
147  int fieldCol( int idx ) const;
148 
153  QgsFeatureId rowToId( int row ) const;
154 
160  void swapRows( QgsFeatureId a, QgsFeatureId b );
161 
165  inline QgsVectorLayer* layer() const { return mLayerCache ? mLayerCache->layer() : NULL; }
166 
170  inline QgsVectorLayerCache* layerCache() const { return mLayerCache; }
171 
175  void executeAction( int action, const QModelIndex &idx ) const;
176 
180  void executeMapLayerAction( QgsMapLayerAction* action, const QModelIndex &idx ) const;
181 
186  QgsFeature feature( const QModelIndex &idx ) const;
187 
195  void prefetchColumnData( int column );
196 
204  void setRequest( const QgsFeatureRequest& request );
205 
209  const QgsFeatureRequest &request() const;
210 
217  void setEditorContext( const QgsAttributeEditorContext& context ) { mEditorContext = context; }
218 
225  const QgsAttributeEditorContext& editorContext() const { return mEditorContext; }
226 
227  signals:
231  void modelChanged();
232 
234  void progress( int i, bool &cancel );
235  void finished();
236 
237  private slots:
241  virtual void updatedFields();
242 
248  virtual void editCommandEnded();
249 
253  virtual void attributeDeleted( int idx );
254 
255  protected slots:
262  virtual void attributeValueChanged( QgsFeatureId fid, int idx, const QVariant &value );
267  virtual void featureDeleted( QgsFeatureId fid );
272  virtual void featureAdded( QgsFeatureId fid );
273 
277  virtual void layerDeleted();
278 
279  protected:
282 
283  mutable QgsFeature mFeat;
284 
286  QVector<QgsEditorWidgetFactory*> mWidgetFactories;
287  QVector<QVariant> mAttributeWidgetCaches;
288  QVector<QgsEditorWidgetConfig> mWidgetConfigs;
289 
290  QHash<QgsFeatureId, int> mIdRowMap;
291  QHash<int, QgsFeatureId> mRowIdMap;
292 
296  virtual void loadAttributes();
297 
298  private:
306  virtual bool loadFeatureAtId( QgsFeatureId fid ) const;
307 
308  QgsFeatureRequest mFeatureRequest;
309 
311  int mCachedField;
313  QHash<QgsFeatureId, QVariant> mFieldCache;
314 
322  QRect mChangedCellBounds;
323 
324  QgsAttributeEditorContext mEditorContext;
325 };
326 
327 
328 #endif