QGIS API Documentation  2.8.2-Wien
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
qgscachedfeatureiterator.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgscachedfeatureiterator.cpp
3  --------------------------------------
4  Date : 12.2.2013
5  Copyright : (C) 2013 Matthias Kuhn
6  Email : matthias dot kuhn at gmx dot ch
7  ***************************************************************************
8  * *
9  * This program is free software; you can redistribute it and/or modify *
10  * it under the terms of the GNU General Public License as published by *
11  * the Free Software Foundation; either version 2 of the License, or *
12  * (at your option) any later version. *
13  * *
14  ***************************************************************************/
15 
17 #include "qgsvectorlayercache.h"
18 
20  : QgsAbstractFeatureIterator( featureRequest )
21  , mFeatureIds( featureIds )
22  , mVectorLayerCache( vlCache )
23 {
24  mFeatureIdIterator = featureIds.constBegin();
25 
26  if ( mFeatureIdIterator == featureIds.constEnd() )
27  close();
28 }
29 
31  : QgsAbstractFeatureIterator( featureRequest )
32  , mVectorLayerCache( vlCache )
33 {
34  switch ( featureRequest.filterType() )
35  {
37  mFeatureIds = featureRequest.filterFids();
38  break;
39 
41  mFeatureIds = QgsFeatureIds() << featureRequest.filterFid();
42  break;
43 
44  default:
45  mFeatureIds = mVectorLayerCache->mCache.keys().toSet();
46  break;
47  }
48 
49  mFeatureIdIterator = mFeatureIds.constBegin();
50 
51  if ( mFeatureIdIterator == mFeatureIds.constEnd() )
52  close();
53 }
54 
56 {
57  if ( mClosed )
58  return false;
59 
60  while ( mFeatureIdIterator != mFeatureIds.constEnd() )
61  {
62  f = QgsFeature( *mVectorLayerCache->mCache[*mFeatureIdIterator]->feature() );
63  ++mFeatureIdIterator;
64  if ( mRequest.acceptFeature( f ) )
65  return true;
66  }
67  close();
68  return false;
69 }
70 
72 {
73  mFeatureIdIterator = mFeatureIds.constBegin();
74  return true;
75 }
76 
78 {
79  mClosed = true;
80  mFeatureIds.clear();
81  return true;
82 }
83 
85  : QgsAbstractFeatureIterator( featureRequest )
86  , mVectorLayerCache( vlCache )
87 {
88  mFeatIt = vlCache->layer()->getFeatures( featureRequest );
89 }
90 
92 {
93  if ( mFeatIt.nextFeature( f ) )
94  {
95  // As long as features can be fetched from the provider: Write them to cache
96  mVectorLayerCache->cacheFeature( f );
97  mFids.insert( f.id() );
98  return true;
99  }
100  else
101  {
102  // Once no more features can be fetched: Inform the cache, that
103  // the request has been completed
104  mVectorLayerCache->requestCompleted( mRequest, mFids );
105  return false;
106  }
107 }
108 
110 {
111  mFids.clear();
112  return mFeatIt.rewind();
113 }
114 
116 {
117  mClosed = true;
118  return mFeatIt.close();
119 }