QGIS API Documentation  2.2.0-Valmiera
 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"
29 
30 class QgsMapCanvas;
31 class QgsMapLayerAction;
32 
43 class GUI_EXPORT QgsAttributeTableModel: public QAbstractTableModel
44 {
45  Q_OBJECT
46 
47  public:
48  enum Role
49  {
50  SortRole = Qt::UserRole + 1,
51  FeatureIdRole = Qt::UserRole + 2,
52  FieldIndexRole = Qt::UserRole + 3
53  };
54 
55  public:
61  QgsAttributeTableModel( QgsVectorLayerCache *layerCache, QObject *parent = 0 );
62 
63  virtual ~QgsAttributeTableModel();
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 
197  void setRequest( const QgsFeatureRequest& request );
198 
199  signals:
203  void modelChanged();
204 
206  void progress( int i, bool &cancel );
207  void finished();
208 
209  private slots:
213  virtual void updatedFields();
214 
220  virtual void editCommandEnded();
221 
225  virtual void attributeDeleted( int idx );
226 
227  protected slots:
234  virtual void attributeValueChanged( QgsFeatureId fid, int idx, const QVariant &value );
239  virtual void featureDeleted( QgsFeatureId fid );
244  virtual void featureAdded( QgsFeatureId fid );
245 
249  virtual void layerDeleted();
250 
251  protected:
254 
255  mutable QgsFeature mFeat;
256 
258  QMap< int, const QMap<QString, QVariant> * > mValueMaps;
259 
260  QHash<QgsFeatureId, int> mIdRowMap;
261  QHash<int, QgsFeatureId> mRowIdMap;
262 
266  virtual void loadAttributes();
267 
268  private:
276  virtual bool loadFeatureAtId( QgsFeatureId fid ) const;
277 
279 
283  QHash<QgsFeatureId, QVariant> mFieldCache;
284 
293 };
294 
295 
296 #endif