QGIS API Documentation  2.0.1-Dufour
 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:
49  {
50  public:
58  : mCache( vlCache )
59  {
60  mFeature = new QgsFeature( feat );
61  }
62 
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:
76 
77  friend class QgsVectorLayerCache;
78  };
79 
80  public:
81  QgsVectorLayerCache( QgsVectorLayer* layer, int cacheSize, QObject* parent = NULL );
82 
89  void setCacheSize( int cacheSize );
90 
98  int cacheSize();
99 
105  void setCacheGeometry( bool cacheGeometry );
106 
107 
113  void setCacheSubsetOfAttributes( const QgsAttributeList& attributes );
114 
121  void setCacheAddedAttributes( bool cacheAddedAttributes );
122 
133  void setFullCache( bool fullCache );
134 
142  void addCacheIndex( QgsAbstractCacheIndex *cacheIndex );
143 
153  QgsFeatureIterator getFeatures( const QgsFeatureRequest& featureRequest );
154 
160  bool isFidCached( const QgsFeatureId fid );
161 
169  bool featureAtId( QgsFeatureId featureId, QgsFeature &feature, bool skipCache = false );
170 
176  bool removeCachedFeature( QgsFeatureId fid );
177 
182 
183  protected:
192  void requestCompleted( QgsFeatureRequest featureRequest, QgsFeatureIds fids );
193 
201  void featureRemoved( QgsFeatureId fid );
202 
213  bool checkInformationCovered( const QgsFeatureRequest& featureRequest );
214 
215 
216  signals:
217 
225  void progress( int i, bool& cancel );
226 
230  void finished();
231 
237  void cachedLayerDeleted();
238 
243  void attributeValueChanged( const QgsFeatureId& fid, const int& field, const QVariant &value );
244 
245  private slots:
246  void onAttributeValueChanged( QgsFeatureId fid, int field, const QVariant& value );
247  void featureDeleted( QgsFeatureId fid );
248  void featureAdded( QgsFeatureId fid );
249  void attributeAdded( int field );
250  void attributeDeleted( int field );
251  void geometryChanged( QgsFeatureId fid, QgsGeometry& geom );
252  void layerDeleted();
253  void updatedFields();
254 
255  private:
256 
257  inline void cacheFeature( QgsFeature& feat )
258  {
259  QgsCachedFeature* cachedFeature = new QgsCachedFeature( feat, this );
260  mCache.insert( feat.id(), cachedFeature );
261  }
262 
264  QCache< QgsFeatureId, QgsCachedFeature > mCache;
265 
268  QList<QgsAbstractCacheIndex*> mCacheIndices;
269 
271 
274  friend class QgsCachedFeature;
275 };
276 #endif // QgsVectorLayerCache_H
QgsFeatureId id() const
Get the feature id for this feature.
Definition: qgsfeature.cpp:101
Wrapper for iterator of features from vector data provider or vector layer.
QList< QgsAbstractCacheIndex * > mCacheIndices
QgsAttributeList mCachedAttributes
QSet< QgsFeatureId > QgsFeatureIds
Definition: qgsfeature.h:321
bool removeCachedFeature(QgsFeatureId fid)
Removes the feature identified by fid from the cache if present.
bool isFidCached(const QgsFeatureId fid)
Check if a certain feature id is cached.
A geometry is the spatial representation of a feature.
Definition: qgsgeometry.h:72
QgsVectorLayer * layer()
Returns the layer to which this cache belongs.
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
Definition: qgsfeature.h:114
QCache< QgsFeatureId, QgsCachedFeature > mCache
void setCacheSubsetOfAttributes(const QgsAttributeList &attributes)
Set the subset of attributes to be cached.
void featureAdded(QgsFeatureId fid)
void attributeAdded(int field)
Uses another iterator as backend and writes features to the cache.
void progress(int i, bool &cancel)
When filling the cache, this signal gets emitted periodically to notify about the progress and to be ...
QgsFeatureIterator getFeatures(const QgsFeatureRequest &featureRequest)
Query this VectorLayerCache for features.
This class wraps a request for features to a vector layer (or directly its vector data provider)...
QList< int > QgsAttributeList
void featureDeleted(QgsFeatureId fid)
void cachedLayerDeleted()
Is emitted when the cached layer is deleted.
QgsCachedFeature(const QgsFeature &feat, QgsVectorLayerCache *vlCache)
Will create a new cached feature.
void geometryChanged(QgsFeatureId fid, QgsGeometry &geom)
bool checkInformationCovered(const QgsFeatureRequest &featureRequest)
Checks if the information required to complete the request is cached.
This class caches features of a given QgsVectorLayer.
void attributeValueChanged(const QgsFeatureId &fid, const int &field, const QVariant &value)
Is emitted when an attribute is changed.
Abstract base class for cache indices.
Definition: qgscacheindex.h:29
QgsVectorLayer * mLayer
void setCacheGeometry(bool cacheGeometry)
Enable or disable the caching of geometries.
void setFullCache(bool fullCache)
This enables or disables full caching.
bool featureAtId(QgsFeatureId featureId, QgsFeature &feature, bool skipCache=false)
Gets the feature at the given feature id.
void cacheFeature(QgsFeature &feat)
void setCacheSize(int cacheSize)
Sets the maximum number of features to keep in the cache.
void requestCompleted(QgsFeatureRequest featureRequest, QgsFeatureIds fids)
Gets called, whenever the full list of feature ids for a certain request is known.
qint64 QgsFeatureId
Definition: qgsfeature.h:30
This is a wrapper class around a cached QgsFeature, which will inform the cache, when it has been del...
QgsVectorLayerCache(QgsVectorLayer *layer, int cacheSize, QObject *parent=NULL)
Delivers features from the cache.
void featureRemoved(QgsFeatureId fid)
Gets called, whenever a feature has been removed.
int cacheSize()
Returns the maximum number of features this cache will hold.
void onAttributeValueChanged(QgsFeatureId fid, int field, const QVariant &value)
void addCacheIndex(QgsAbstractCacheIndex *cacheIndex)
Adds a QgsAbstractCacheIndex to this cache.
Represents a vector layer which manages a vector based data sets.
void finished()
When filling the cache, this signal gets emitted once the cache is fully initialized.
void setCacheAddedAttributes(bool cacheAddedAttributes)
If this is enabled, the subset of cached attributes will automatically be extended to also include ne...
void attributeDeleted(int field)