QGIS API Documentation  2.0.1-Dufour
 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 
42 class GUI_EXPORT QgsAttributeTableModel: public QAbstractTableModel
43 {
44  Q_OBJECT
45 
46  public:
47  enum Role
48  {
49  SortRole = Qt::UserRole + 1,
50  FeatureIdRole = Qt::UserRole + 2,
51  FieldIndexRole = Qt::UserRole + 3
52  };
53 
54  public:
60  QgsAttributeTableModel( QgsVectorLayerCache *layerCache, QObject *parent = 0 );
61 
62  virtual ~QgsAttributeTableModel();
63 
68  virtual void loadLayer();
69 
74  virtual int rowCount( const QModelIndex &parent = QModelIndex() ) const;
75 
80  int columnCount( const QModelIndex &parent = QModelIndex() ) const;
81 
88  QVariant headerData( int section, Qt::Orientation orientation, int role = Qt::DisplayRole ) const;
89 
95  virtual QVariant data( const QModelIndex &index, int role ) const;
96 
103  virtual bool setData( const QModelIndex &index, const QVariant &value, int role = Qt::EditRole );
104 
109  Qt::ItemFlags flags( const QModelIndex &index ) const;
110 
116  void reload( const QModelIndex &index1, const QModelIndex &index2 );
117 
121  bool removeRows( int row, int count, const QModelIndex &parent = QModelIndex() );
122 
126  void resetModel();
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() : NULL; }
165 
169  inline QgsVectorLayerCache* layerCache() const { return mLayerCache; }
170 
174  void executeAction( int action, const QModelIndex &idx ) const;
175 
180  QgsFeature feature( const QModelIndex &idx ) const;
181 
189  void prefetchColumnData( int column );
190 
191  signals:
195  void modelChanged();
196 
198  void progress( int i, bool &cancel );
199  void finished();
200 
201  private slots:
205  virtual void updatedFields();
206 
210  virtual void attributeDeleted( int idx );
211 
212  protected slots:
219  virtual void attributeValueChanged( QgsFeatureId fid, int idx, const QVariant &value );
224  virtual void featureDeleted( QgsFeatureId fid );
230  virtual void featureAdded( QgsFeatureId fid, bool inOperation = true );
231 
235  virtual void layerDeleted();
236 
237  protected:
240 
241  mutable QgsFeature mFeat;
242 
244  QMap< int, const QMap<QString, QVariant> * > mValueMaps;
245 
246  QHash<QgsFeatureId, int> mIdRowMap;
247  QHash<int, QgsFeatureId> mRowIdMap;
248 
252  virtual void loadAttributes();
253 
254  private:
262  virtual bool loadFeatureAtId( QgsFeatureId fid ) const;
263 
265 
269  QHash<QgsFeatureId, QVariant> mFieldCache;
270 };
271 
272 
273 #endif