QGIS API Documentation  2.2.0-Valmiera
 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 
23  : mFilter( FilterNone )
24  , mFilterExpression( 0 )
25  , mFlags( 0 )
26 {
27 }
28 
30  : mFilter( FilterFid )
31  , mFilterFid( fid )
32  , mFilterExpression( 0 )
33  , mFlags( 0 )
34 {
35 }
36 
38  : mFilter( FilterRect )
39  , mFilterRect( rect )
40  , mFilterExpression( 0 )
41  , mFlags( 0 )
42 {
43 }
44 
46  : mFilter( FilterExpression )
47  , mFilterExpression( new QgsExpression( expr.expression() ) )
48  , mFlags( 0 )
49 {
50 }
51 
53 {
54  operator=( rh );
55 }
56 
58 {
59  mFlags = rh.mFlags;
60  mFilter = rh.mFilter;
64  if ( rh.mFilterExpression )
65  {
67  }
68  else
69  {
71  }
72  mAttrs = rh.mAttrs;
74  return *this;
75 }
76 
78 {
79  delete mFilterExpression;
80 }
81 
83 {
85  mFilterRect = rect;
86  return *this;
87 }
88 
90 {
92  mFilterFid = fid;
93  return *this;
94 }
95 
97 {
99  mFilterFids = fids;
100  return *this;
101 }
102 
104 {
106  delete mFilterExpression;
107  mFilterExpression = new QgsExpression( expression );
108  return *this;
109 }
110 
111 QgsFeatureRequest& QgsFeatureRequest::setFlags( QgsFeatureRequest::Flags flags )
112 {
113  mFlags = flags;
114  return *this;
115 }
116 
118 {
120  mAttrs = attrs;
121  return *this;
122 }
123 
124 QgsFeatureRequest& QgsFeatureRequest::setSubsetOfAttributes( const QStringList& attrNames, const QgsFields& fields )
125 {
127  mAttrs.clear();
128 
129  for ( int idx = 0; idx < fields.count(); ++idx )
130  {
131  if ( attrNames.contains( fields[idx].name() ) )
132  mAttrs.append( idx );
133  }
134 
135  return *this;
136 }
137 
139 {
141  return *this;
142 }
143 
145 {
146  switch ( mFilter )
147  {
149  return true;
150  break;
151 
153  if ( feature.geometry() && feature.geometry()->intersects( mFilterRect ) )
154  return true;
155  else
156  return false;
157  break;
158 
160  if ( feature.id() == mFilterFid )
161  return true;
162  else
163  return false;
164  break;
165 
167  if ( mFilterExpression->evaluate( feature ).toBool() )
168  return true;
169  else
170  return false;
171  break;
172 
174  if ( mFilterFids.contains( feature.id() ) )
175  return true;
176  else
177  return false;
178  break;
179  }
180 
181  return true;
182 }