QGIS API Documentation  2.18.21-Las Palmas (9fba24a)
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 at opengis 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  Q_DISABLE_COPY( QgsCachedFeature )
79  };
80 
81  public:
84 
91  void setCacheSize( int cacheSize );
92 
100  int cacheSize();
101 
108  void setCacheGeometry( bool cacheGeometry );
109 
115  bool cacheGeometry() const { return mCacheGeometry; }
116 
122  void setCacheSubsetOfAttributes( const QgsAttributeList& attributes );
123 
130  void setCacheAddedAttributes( bool cacheAddedAttributes );
131 
145  void setFullCache( bool fullCache );
146 
153  bool hasFullCache() const { return mFullCache; }
154 
163  void addCacheIndex( QgsAbstractCacheIndex *cacheIndex );
164 
175 
182  bool isFidCached( const QgsFeatureId fid ) const;
183 
188  QgsFeatureIds cachedFeatureIds() const { return mCache.keys().toSet(); }
189 
197  bool featureAtId( QgsFeatureId featureId, QgsFeature &feature, bool skipCache = false );
198 
204  bool removeCachedFeature( QgsFeatureId fid );
205 
210 
211  protected:
220  void requestCompleted( const QgsFeatureRequest& featureRequest, const QgsFeatureIds& fids );
221 
229  void featureRemoved( QgsFeatureId fid );
230 
241  bool checkInformationCovered( const QgsFeatureRequest& featureRequest );
242 
243 
244  signals:
245 
255  void progress( int i, bool& cancel );
256 
260  void finished();
261 
267  void cachedLayerDeleted();
268 
273  void attributeValueChanged( QgsFeatureId fid, int field, const QVariant &value );
274 
282  void featureAdded( QgsFeatureId fid );
283 
289  void invalidated();
290 
291  private slots:
292  void onAttributeValueChanged( QgsFeatureId fid, int field, const QVariant& value );
293  void featureDeleted( QgsFeatureId fid );
294  void onFeatureAdded( QgsFeatureId fid );
295  void attributeAdded( int field );
296  void attributeDeleted( int field );
297  void geometryChanged( QgsFeatureId fid, QgsGeometry& geom );
298  void layerDeleted();
299  void invalidate();
300 
301  private:
302 
303  inline void cacheFeature( QgsFeature& feat )
304  {
305  QgsCachedFeature* cachedFeature = new QgsCachedFeature( feat, this );
306  mCache.insert( feat.id(), cachedFeature );
307  }
308 
309  QgsVectorLayer* mLayer;
311 
312  bool mCacheGeometry;
313  bool mFullCache;
314  QList<QgsAbstractCacheIndex*> mCacheIndices;
315 
316  QgsAttributeList mCachedAttributes;
317 
320  friend class QgsCachedFeature;
321 
329  bool canUseCacheForRequest( const QgsFeatureRequest& featureRequest, QgsFeatureIterator& it );
330 
331  friend class TestVectorLayerCache;
332 };
333 #endif // QgsVectorLayerCache_H
Wrapper for iterator of features from vector data provider or vector layer.
QgsVectorLayerCache(QgsVectorLayer *layer, int cacheSize, QObject *parent=nullptr)
void invalidated()
The cache has been invalidated and cleared.
bool cacheGeometry() const
Returns true if the cache will fetch and cache feature geometries.
bool removeCachedFeature(QgsFeatureId fid)
Removes the feature identified by fid from the cache if present.
A geometry is the spatial representation of a feature.
Definition: qgsgeometry.h:76
void requestCompleted(const QgsFeatureRequest &featureRequest, const QgsFeatureIds &fids)
Gets called, whenever the full list of feature ids for a certain request is known.
QgsVectorLayer * layer()
Returns the layer to which this cache belongs.
bool isFidCached(const QgsFeatureId fid) const
Check if a certain feature id is cached.
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
Definition: qgsfeature.h:187
void attributeValueChanged(QgsFeatureId fid, int field, const QVariant &value)
Is emitted when an attribute is changed.
void setCacheSubsetOfAttributes(const QgsAttributeList &attributes)
Set the subset of attributes to be cached.
void featureAdded(QgsFeatureId fid)
Is emitted, when a new feature has been added to the layer and this cache.
Uses another iterator as backend and writes features to the cache.
QgsFeatureIds cachedFeatureIds() const
Returns the set of feature IDs for features which are cached.
void progress(int i, bool &cancel)
When filling the cache, this signal gets emitted periodically to notify about the progress and to be ...
This class wraps a request for features to a vector layer (or directly its vector data provider)...
void cachedLayerDeleted()
Is emitted when the cached layer is deleted.
bool checkInformationCovered(const QgsFeatureRequest &featureRequest)
Checks if the information required to complete the request is cached.
QgsFeatureId id() const
Get the feature ID for this feature.
Definition: qgsfeature.cpp:65
This class caches features of a given QgsVectorLayer.
Abstract base class for cache indices.
Definition: qgscacheindex.h:29
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 setCacheSize(int cacheSize)
Sets the maximum number of features to keep in the cache.
qint64 QgsFeatureId
Definition: qgsfeature.h:31
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.
QObject * parent() const
void addCacheIndex(QgsAbstractCacheIndex *cacheIndex)
Adds a QgsAbstractCacheIndex to this cache.
Represents a vector layer which manages a vector based data sets.
QgsFeatureIterator getFeatures(const QgsFeatureRequest &featureRequest=QgsFeatureRequest())
Query this VectorLayerCache for features.
void finished()
When filling the cache, this signal gets emitted once the cache is fully initialized.
bool hasFullCache() const
Returns true if the cache is complete, ie it contains all features.
void setCacheAddedAttributes(bool cacheAddedAttributes)
If this is enabled, the subset of cached attributes will automatically be extended to also include ne...