QGIS API Documentation  2.8.2-Wien
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
qgsfeaturerequest.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsfeaturerequest.cpp
3  ---------------------
4  begin : Mai 2012
5  copyright : (C) 2012 by Martin Dobias
6  email : wonder dot sk at gmail dot com
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 #include "qgsfeaturerequest.h"
16 
17 #include "qgsfield.h"
18 #include "qgsgeometry.h"
19 
20 #include <QStringList>
21 
22 //constants
23 const QString QgsFeatureRequest::AllAttributes = QString( "#!allattributes!#" );
24 
26  : mFilter( FilterNone )
27  , mFilterFid( -1 )
28  , mFilterExpression( 0 )
29  , mFlags( 0 )
30 {
31 }
32 
34  : mFilter( FilterFid )
35  , mFilterFid( fid )
36  , mFilterExpression( 0 )
37  , mFlags( 0 )
38 {
39 }
40 
42  : mFilter( FilterRect )
43  , mFilterRect( rect )
44  , mFilterFid( -1 )
45  , mFilterExpression( 0 )
46  , mFlags( 0 )
47 {
48 }
49 
51  : mFilter( FilterExpression )
52  , mFilterFid( -1 )
53  , mFilterExpression( new QgsExpression( expr.expression() ) )
54  , mFlags( 0 )
55 {
56 }
57 
59 {
60  operator=( rh );
61 }
62 
64 {
65  mFlags = rh.mFlags;
66  mFilter = rh.mFilter;
70  if ( rh.mFilterExpression )
71  {
73  }
74  else
75  {
77  }
78  mAttrs = rh.mAttrs;
80  return *this;
81 }
82 
84 {
85  delete mFilterExpression;
86 }
87 
89 {
91  mFilterRect = rect;
92  return *this;
93 }
94 
96 {
98  mFilterFid = fid;
99  return *this;
100 }
101 
103 {
105  mFilterFids = fids;
106  return *this;
107 }
108 
110 {
112  delete mFilterExpression;
113  mFilterExpression = new QgsExpression( expression );
114  return *this;
115 }
116 
117 QgsFeatureRequest& QgsFeatureRequest::setFlags( QgsFeatureRequest::Flags flags )
118 {
119  mFlags = flags;
120  return *this;
121 }
122 
124 {
126  mAttrs = attrs;
127  return *this;
128 }
129 
130 QgsFeatureRequest& QgsFeatureRequest::setSubsetOfAttributes( const QStringList& attrNames, const QgsFields& fields )
131 {
132  if ( attrNames.contains( QgsFeatureRequest::AllAttributes ) )
133  {
134  //attribute string list contains the all attributes flag, so we must fetch all attributes
135  return *this;
136  }
137 
139  mAttrs.clear();
140 
141  foreach ( const QString& attrName, attrNames )
142  {
143  int attrNum = fields.fieldNameIndex( attrName );
144  if ( attrNum != -1 && !mAttrs.contains( attrNum ) )
145  mAttrs.append( attrNum );
146  }
147 
148  return *this;
149 }
150 
152 {
154  return *this;
155 }
156 
158 {
159  switch ( mFilter )
160  {
162  return true;
163  break;
164 
166  if ( feature.geometry() && feature.geometry()->intersects( mFilterRect ) )
167  return true;
168  else
169  return false;
170  break;
171 
173  if ( feature.id() == mFilterFid )
174  return true;
175  else
176  return false;
177  break;
178 
180  if ( mFilterExpression->evaluate( feature ).toBool() )
181  return true;
182  else
183  return false;
184  break;
185 
187  if ( mFilterFids.contains( feature.id() ) )
188  return true;
189  else
190  return false;
191  break;
192  }
193 
194  return true;
195 }
196 
197 #include "qgsfeatureiterator.h"
198 #include "qgslogger.h"
199 
201 {
202  while ( !mActiveIterators.empty() )
203  {
205  QgsDebugMsg( "closing active iterator" );
206  it->close();
207  }
208 }
209 
211 {
212  mActiveIterators.insert( it );
213 }
214 
216 {
217  mActiveIterators.remove( it );
218 }
219 
220