QGIS API Documentation  2.6.0-Brighton
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
qgsvectorlayercache.h
Go to the documentation of this file.
1 /***************************************************************************
2  qgsvectorlayercache.h
3  Cache features of a vector layer
4  -------------------
5  begin : January 2013
6  copyright : (C) Matthias Kuhn
7  email : matthias dot kuhn at gmx dot ch
8 
9  ***************************************************************************
10  * *
11  * This program is free software; you can redistribute it and/or modify *
12  * it under the terms of the GNU General Public License as published by *
13  * the Free Software Foundation; either version 2 of the License, or *
14  * (at your option) any later version. *
15  * *
16  ***************************************************************************/
17 
18 
19 #ifndef QgsVectorLayerCache_H
20 #define QgsVectorLayerCache_H
21 
22 #include <QCache>
23 
24 #include "qgsvectorlayer.h"
25 
28 
38 class CORE_EXPORT QgsVectorLayerCache : public QObject
39 {
40  Q_OBJECT
41 
42  private:
48  class QgsCachedFeature
49  {
50  public:
57  QgsCachedFeature( const QgsFeature& feat, QgsVectorLayerCache* vlCache )
58  : mCache( vlCache )
59  {
60  mFeature = new QgsFeature( feat );
61  }
62 
63  ~QgsCachedFeature()
64  {
65  // That's the reason we need this wrapper:
66  // Inform the cache that this feature has been removed
67  mCache->featureRemoved( mFeature->id() );
68  delete mFeature;
69  }
70 
71  inline const QgsFeature* feature() { return mFeature; }
72 
73  private:
74  QgsFeature* mFeature;
75  QgsVectorLayerCache* mCache;
76 
77  friend class QgsVectorLayerCache;
78  };
79 
80  public:
81  QgsVectorLayerCache( QgsVectorLayer* layer, int cacheSize, QObject* parent = NULL );
83 
90  void setCacheSize( int cacheSize );
91 
99  int cacheSize();
100 
106  void setCacheGeometry( bool cacheGeometry );
107 
108 
114  void setCacheSubsetOfAttributes( const QgsAttributeList& attributes );
115 
122  void setCacheAddedAttributes( bool cacheAddedAttributes );
123 
134  void setFullCache( bool fullCache );
135 
144  void addCacheIndex( QgsAbstractCacheIndex *cacheIndex );
145 
156 
162  bool isFidCached( const QgsFeatureId fid );
163 
171  bool featureAtId( QgsFeatureId featureId, QgsFeature &feature, bool skipCache = false );
172 
178  bool removeCachedFeature( QgsFeatureId fid );
179 
184 
185  protected:
194  void requestCompleted( QgsFeatureRequest featureRequest, QgsFeatureIds fids );
195 
203  void featureRemoved( QgsFeatureId fid );
204 
215  bool checkInformationCovered( const QgsFeatureRequest& featureRequest );
216 
217 
218  signals:
219 
229  void progress( int i, bool& cancel );
230 
234  void finished();
235 
241  void cachedLayerDeleted();
242 
247  void attributeValueChanged( const QgsFeatureId& fid, const int& field, const QVariant &value );
248 
256  void featureAdded( QgsFeatureId fid );
257 
258  private slots:
259  void onAttributeValueChanged( QgsFeatureId fid, int field, const QVariant& value );
260  void featureDeleted( QgsFeatureId fid );
261  void onFeatureAdded( QgsFeatureId fid );
262  void attributeAdded( int field );
263  void attributeDeleted( int field );
264  void geometryChanged( QgsFeatureId fid, QgsGeometry& geom );
265  void layerDeleted();
266  void updatedFields();
267 
268  private:
269 
270  inline void cacheFeature( QgsFeature& feat )
271  {
272  QgsCachedFeature* cachedFeature = new QgsCachedFeature( feat, this );
273  mCache.insert( feat.id(), cachedFeature );
274  }
275 
276  QgsVectorLayer* mLayer;
277  QCache< QgsFeatureId, QgsCachedFeature > mCache;
278 
279  bool mCacheGeometry;
280  bool mFullCache;
281  QList<QgsAbstractCacheIndex*> mCacheIndices;
282 
283  QgsAttributeList mCachedAttributes;
284 
287  friend class QgsCachedFeature;
288 };
289 #endif // QgsVectorLayerCache_H