QGIS API Documentation  2.6.0-Brighton
 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;
76 
81  int columnCount( const QModelIndex &parent = QModelIndex() ) const;
82 
89  QVariant headerData( int section, Qt::Orientation orientation, int role = Qt::DisplayRole ) const;
90 
96  virtual QVariant data( const QModelIndex &index, int role ) const;
97 
104  virtual bool setData( const QModelIndex &index, const QVariant &value, int role = Qt::EditRole );
105 
110  Qt::ItemFlags flags( const QModelIndex &index ) const;
111 
117  void reload( const QModelIndex &index1, const QModelIndex &index2 );
118 
122  bool removeRows( int row, int count, const QModelIndex &parent = QModelIndex() );
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 
212  void setEditorContext( const QgsAttributeEditorContext& context ) { mEditorContext = context; }
213 
220  const QgsAttributeEditorContext& editorContext() const { return mEditorContext; }
221 
222  signals:
226  void modelChanged();
227 
229  void progress( int i, bool &cancel );
230  void finished();
231 
232  private slots:
236  virtual void updatedFields();
237 
243  virtual void editCommandEnded();
244 
248  virtual void attributeDeleted( int idx );
249 
250  protected slots:
257  virtual void attributeValueChanged( QgsFeatureId fid, int idx, const QVariant &value );
262  virtual void featureDeleted( QgsFeatureId fid );
267  virtual void featureAdded( QgsFeatureId fid );
268 
272  virtual void layerDeleted();
273 
274  protected:
277 
278  mutable QgsFeature mFeat;
279 
281  QVector<QgsEditorWidgetFactory*> mWidgetFactories;
282  QVector<QVariant> mAttributeWidgetCaches;
283  QVector<QgsEditorWidgetConfig> mWidgetConfigs;
284 
285  QHash<QgsFeatureId, int> mIdRowMap;
286  QHash<int, QgsFeatureId> mRowIdMap;
287 
291  virtual void loadAttributes();
292 
293  private:
301  virtual bool loadFeatureAtId( QgsFeatureId fid ) const;
302 
303  QgsFeatureRequest mFeatureRequest;
304 
306  int mCachedField;
308  QHash<QgsFeatureId, QVariant> mFieldCache;
309 
317  QRect mChangedCellBounds;
318 
319  QgsAttributeEditorContext mEditorContext;
320 };
321 
322 
323 #endif