|
Quantum GIS API Documentation
master-ce49b66
|
00001 /*************************************************************************** 00002 qgsvectorlayercache.h 00003 Cache features of a vector layer 00004 ------------------- 00005 begin : January 2013 00006 copyright : (C) Matthias Kuhn 00007 email : matthias dot kuhn at gmx dot ch 00008 00009 *************************************************************************** 00010 * * 00011 * This program is free software; you can redistribute it and/or modify * 00012 * it under the terms of the GNU General Public License as published by * 00013 * the Free Software Foundation; either version 2 of the License, or * 00014 * (at your option) any later version. * 00015 * * 00016 ***************************************************************************/ 00017 00018 00019 #ifndef QgsVectorLayerCache_H 00020 #define QgsVectorLayerCache_H 00021 00022 #include <QCache> 00023 00024 #include "qgsvectorlayer.h" 00025 00026 class QgsCachedFeatureIterator; 00027 class QgsAbstractCacheIndex; 00028 00038 class CORE_EXPORT QgsVectorLayerCache : public QObject 00039 { 00040 Q_OBJECT 00041 00042 private: 00048 class QgsCachedFeature 00049 { 00050 public: 00057 QgsCachedFeature( const QgsFeature& feat, QgsVectorLayerCache* vlCache ) 00058 : mCache( vlCache ) 00059 { 00060 mFeature = new QgsFeature( feat ); 00061 } 00062 00063 ~QgsCachedFeature() 00064 { 00065 // That's the reason we need this wrapper: 00066 // Inform the cache that this feature has been removed 00067 mCache->featureRemoved( mFeature->id() ); 00068 delete mFeature; 00069 } 00070 00071 inline const QgsFeature* feature() { return mFeature; } 00072 00073 private: 00074 QgsFeature* mFeature; 00075 QgsVectorLayerCache* mCache; 00076 00077 friend class QgsVectorLayerCache; 00078 }; 00079 00080 public: 00081 QgsVectorLayerCache( QgsVectorLayer* layer, int cacheSize, QObject* parent = NULL ); 00082 00089 void setCacheSize( int cacheSize ); 00090 00098 int cacheSize(); 00099 00105 void setCacheGeometry( bool cacheGeometry ); 00106 00107 00113 void setCacheSubsetOfAttributes( const QgsAttributeList& attributes ); 00114 00121 void setCacheAddedAttributes( bool cacheAddedAttributes ); 00122 00133 void setFullCache( bool fullCache ); 00134 00142 void addCacheIndex( QgsAbstractCacheIndex *cacheIndex ); 00143 00153 QgsFeatureIterator getFeatures( const QgsFeatureRequest& featureRequest ); 00154 00160 bool isFidCached( const QgsFeatureId fid ); 00161 00169 bool featureAtId( QgsFeatureId featureId, QgsFeature &feature, bool skipCache = false ); 00170 00176 bool removeCachedFeature( QgsFeatureId fid ); 00177 00181 QgsVectorLayer* layer(); 00182 00183 protected: 00192 void requestCompleted( QgsFeatureRequest featureRequest, QgsFeatureIds fids ); 00193 00201 void featureRemoved( QgsFeatureId fid ); 00202 00213 bool checkInformationCovered( const QgsFeatureRequest& featureRequest ); 00214 00215 00216 signals: 00217 00225 void progress( int i, bool& cancel ); 00226 00230 void finished(); 00231 00237 void cachedLayerDeleted(); 00238 00239 private slots: 00240 void attributeValueChanged( QgsFeatureId fid, int field, const QVariant& value ); 00241 void featureDeleted( QgsFeatureId fid ); 00242 void featureAdded( QgsFeatureId fid ); 00243 void attributeAdded( int field ); 00244 void attributeDeleted( int field ); 00245 void geometryChanged( QgsFeatureId fid, QgsGeometry& geom ); 00246 void layerDeleted(); 00247 void updatedFields(); 00248 00249 private: 00250 00251 inline void cacheFeature( QgsFeature& feat ) 00252 { 00253 QgsCachedFeature* cachedFeature = new QgsCachedFeature( feat, this ); 00254 mCache.insert( feat.id(), cachedFeature ); 00255 } 00256 00257 QgsVectorLayer* mLayer; 00258 QCache< QgsFeatureId, QgsCachedFeature > mCache; 00259 00260 bool mCacheGeometry; 00261 bool mFullCache; 00262 QList<QgsAbstractCacheIndex*> mCacheIndices; 00263 00264 QgsAttributeList mCachedAttributes; 00265 00266 friend class QgsCachedFeatureIterator; 00267 friend class QgsCachedFeatureWriterIterator; 00268 friend class QgsCachedFeature; 00269 }; 00270 #endif // QgsVectorLayerCache_H