QGIS API Documentation  2.99.0-Master (314842d)
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 "qgis_core.h"
23 #include <QCache>
24 
25 #include "qgsvectorlayer.h"
26 
29 
39 class CORE_EXPORT QgsVectorLayerCache : public QObject
40 {
41  Q_OBJECT
42 
43  private:
44 
50  class QgsCachedFeature
51  {
52  public:
53 
60  QgsCachedFeature( const QgsFeature &feat, QgsVectorLayerCache *vlCache )
61  : mCache( vlCache )
62  {
63  mFeature = new QgsFeature( feat );
64  }
65 
66  ~QgsCachedFeature()
67  {
68  // That's the reason we need this wrapper:
69  // Inform the cache that this feature has been removed
70  mCache->featureRemoved( mFeature->id() );
71  delete mFeature;
72  }
73 
74  inline const QgsFeature *feature() { return mFeature; }
75 
76  private:
77  QgsFeature *mFeature = nullptr;
78  QgsVectorLayerCache *mCache = nullptr;
79 
80  friend class QgsVectorLayerCache;
81  Q_DISABLE_COPY( QgsCachedFeature )
82  };
83 
84  public:
85  QgsVectorLayerCache( QgsVectorLayer *layer, int cacheSize, QObject *parent = nullptr );
87 
94  void setCacheSize( int cacheSize );
95 
103  int cacheSize();
104 
111  void setCacheGeometry( bool cacheGeometry );
112 
118  bool cacheGeometry() const { return mCacheGeometry; }
119 
125  void setCacheSubsetOfAttributes( const QgsAttributeList &attributes );
126 
133  void setCacheAddedAttributes( bool cacheAddedAttributes );
134 
148  void setFullCache( bool fullCache );
149 
156  bool hasFullCache() const { return mFullCache; }
157 
166  void addCacheIndex( QgsAbstractCacheIndex *cacheIndex );
167 
178 
182  inline QgsFeatureIterator getFeatures( const QString &expression )
183  {
184  return getFeatures( QgsFeatureRequest( expression ) );
185  }
186 
192  {
193  QgsFeature feature;
194  getFeatures( QgsFeatureRequest( fid ) ).nextFeature( feature );
195  return feature;
196  }
197 
202  {
203  return getFeatures( QgsFeatureRequest( fids ) );
204  }
205 
209  inline QgsFeatureIterator getFeatures( const QgsRectangle &rectangle )
210  {
211  return getFeatures( QgsFeatureRequest( rectangle ) );
212  }
213 
220  bool isFidCached( const QgsFeatureId fid ) const;
221 
226  QgsFeatureIds cachedFeatureIds() const { return mCache.keys().toSet(); }
227 
235  bool featureAtId( QgsFeatureId featureId, QgsFeature &feature, bool skipCache = false );
236 
242  bool removeCachedFeature( QgsFeatureId fid );
243 
248 
249  protected:
250 
259  void requestCompleted( const QgsFeatureRequest &featureRequest, const QgsFeatureIds &fids );
260 
268  void featureRemoved( QgsFeatureId fid );
269 
280  bool checkInformationCovered( const QgsFeatureRequest &featureRequest );
281 
282 
283  signals:
284 
294  void progress( int i, bool &cancel );
295 
299  void finished();
300 
306  void cachedLayerDeleted();
307 
312  void attributeValueChanged( QgsFeatureId fid, int field, const QVariant &value );
313 
321  void featureAdded( QgsFeatureId fid );
322 
328  void invalidated();
329 
330  private slots:
331  void onAttributeValueChanged( QgsFeatureId fid, int field, const QVariant &value );
332  void featureDeleted( QgsFeatureId fid );
333  void onFeatureAdded( QgsFeatureId fid );
334  void attributeAdded( int field );
335  void attributeDeleted( int field );
336  void geometryChanged( QgsFeatureId fid, const QgsGeometry &geom );
337  void layerDeleted();
338  void invalidate();
339 
340  private:
341 
342  inline void cacheFeature( QgsFeature &feat )
343  {
344  QgsCachedFeature *cachedFeature = new QgsCachedFeature( feat, this );
345  mCache.insert( feat.id(), cachedFeature );
346  }
347 
348  QgsVectorLayer *mLayer = nullptr;
349  QCache< QgsFeatureId, QgsCachedFeature > mCache;
350 
351  bool mCacheGeometry;
352  bool mFullCache;
353  QList<QgsAbstractCacheIndex *> mCacheIndices;
354 
355  QgsAttributeList mCachedAttributes;
356 
359  friend class QgsCachedFeature;
360 
368  bool canUseCacheForRequest( const QgsFeatureRequest &featureRequest, QgsFeatureIterator &it );
369 
370  friend class TestVectorLayerCache;
371 };
372 #endif // QgsVectorLayerCache_H
QgsFeatureIterator getFeatures(const QString &expression)
Query the layer for features matching a given expression.
QgsFeatureId id
Definition: qgsfeature.h:140
Wrapper for iterator of features from vector data provider or vector layer.
A rectangle specified with double values.
Definition: qgsrectangle.h:36
QgsVectorLayerCache(QgsVectorLayer *layer, int cacheSize, QObject *parent=nullptr)
QgsFeatureIterator getFeatures(const QgsFeatureIds &fids)
Query the layer for the features with the given ids.
void invalidated()
The cache has been invalidated and cleared.
bool cacheGeometry() const
Returns true if the cache will fetch and cache feature geometries.
QSet< QgsFeatureId > QgsFeatureIds
Definition: qgsfeature.h:358
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:79
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:136
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)...
QList< int > QgsAttributeList
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.
This class caches features of a given QgsVectorLayer.
Abstract base class for cache indices.
Definition: qgscacheindex.h:30
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:33
QgsFeatureIterator getFeatures(const QgsRectangle &rectangle)
Query the layer for the features which intersect the specified rectangle.
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.
QgsFeature getFeature(QgsFeatureId fid)
Query the layer for the feature with the given id.
bool nextFeature(QgsFeature &f)
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...