QGIS API Documentation  2.0.1-Dufour
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
qgsexpressionselectiondialog.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgisexpressionselectiondialog.cpp
3  --------------------------------------
4  Date : 24.1.2013
5  Copyright : (C) 2013 by 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 "qgsapplication.h"
18 #include "qgsexpression.h"
19 
20 #include <QSettings>
21 
23  : QDialog( parent )
24  , mLayer( layer )
25 {
26  setupUi( this );
27 
28  mActionSelect->setIcon( QgsApplication::getThemeIcon( "/mIconExpressionSelect.svg" ) );
29  mActionAddToSelection->setIcon( QgsApplication::getThemeIcon( "/mIconSelectAdd.svg" ) );
30  mActionRemoveFromSelection->setIcon( QgsApplication::getThemeIcon( "/mIconSelectRemove.svg" ) );
31  mActionSelectInstersect->setIcon( QgsApplication::getThemeIcon( "/mIconSelectIntersect.svg" ) );
32 
33  mButtonSelect->addAction( mActionSelect );
34  mButtonSelect->addAction( mActionAddToSelection );
35  mButtonSelect->addAction( mActionRemoveFromSelection );
36  mButtonSelect->addAction( mActionSelectInstersect );
37  mButtonSelect->setDefaultAction( mActionSelect );
38 
39  mExpressionBuilder->setLayer( layer );
40  mExpressionBuilder->setExpressionText( startText );
41  mExpressionBuilder->loadFieldNames();
42 
43  QSettings settings;
44  restoreGeometry( settings.value( "/Windows/ExpressionSelectionDialog/geometry" ).toByteArray() );
45 }
46 
48 {
49  return mExpressionBuilder;
50 }
51 
53 {
54  mExpressionBuilder->setExpressionText( text );
55 }
56 
58 {
59  return mExpressionBuilder->expressionText();
60 }
61 
63 {
64  // Store in child widget only.
65  mExpressionBuilder->setGeomCalculator( da );
66 }
67 
69 {
70  QgsFeatureIds newSelection;
71  QgsExpression* expression = new QgsExpression( mExpressionBuilder->expressionText() );
72 
73  const QgsFields fields = mLayer->pendingFields();
74 
76 
77  expression->prepare( fields );
78 
79  QgsFeature feat;
80  while ( features.nextFeature( feat ) )
81  {
82  if ( expression->evaluate( &feat, fields ).toBool() )
83  {
84  newSelection << feat.id();
85  }
86  }
87 
88  features.close();
89 
90  mLayer->setSelectedFeatures( newSelection );
91 
92  delete expression;
93 }
94 
96 {
97  QgsFeatureIds newSelection = mLayer->selectedFeaturesIds();
98  QgsExpression* expression = new QgsExpression( mExpressionBuilder->expressionText() );
99 
100  const QgsFields fields = mLayer->pendingFields();
101 
102  QgsFeatureIterator features = mLayer->getFeatures();
103 
104  expression->prepare( fields );
105 
106  QgsFeature feat;
107  while ( features.nextFeature( feat ) )
108  {
109  if ( expression->evaluate( &feat, fields ).toBool() )
110  {
111  newSelection << feat.id();
112  }
113  }
114 
115  features.close();
116 
117  mLayer->setSelectedFeatures( newSelection );
118 
119  delete expression;
120 }
121 
123 {
124  const QgsFeatureIds &oldSelection = mLayer->selectedFeaturesIds();
125  QgsFeatureIds newSelection;
126 
127  QgsExpression* expression = new QgsExpression( mExpressionBuilder->expressionText() );
128 
129  const QgsFields fields = mLayer->pendingFields();
130 
131  expression->prepare( fields );
132 
133  QgsFeature feat;
134  foreach ( const QgsFeatureId fid, oldSelection )
135  {
136  QgsFeatureIterator features = mLayer->getFeatures( QgsFeatureRequest().setFilterFid( fid ) );
137 
138  if ( features.nextFeature( feat ) )
139  {
140  if ( expression->evaluate( &feat, fields ).toBool() )
141  {
142  newSelection << feat.id();
143  }
144  }
145  else
146  {
147  Q_ASSERT( false );
148  }
149 
150  features.close();
151  }
152 
153  mLayer->setSelectedFeatures( newSelection );
154 
155  delete expression;
156 }
157 
159 {
160  const QgsFeatureIds &oldSelection = mLayer->selectedFeaturesIds();
161  QgsFeatureIds newSelection = mLayer->selectedFeaturesIds();
162 
163  QgsExpression* expression = new QgsExpression( mExpressionBuilder->expressionText() );
164 
165  const QgsFields fields = mLayer->pendingFields();
166 
167  expression->prepare( fields );
168 
169  QgsFeature feat;
170  foreach ( const QgsFeatureId fid, oldSelection )
171  {
172  QgsFeatureIterator features = mLayer->getFeatures( QgsFeatureRequest().setFilterFid( fid ) );
173 
174  if ( features.nextFeature( feat ) )
175  {
176  if ( expression->evaluate( &feat, fields ).toBool() )
177  {
178  newSelection.remove( feat.id() );
179  }
180  }
181  else
182  {
183  Q_ASSERT( false );
184  }
185 
186  features.close();
187  }
188 
189  mLayer->setSelectedFeatures( newSelection );
190 
191  delete expression;
192 }
193 
194 void QgsExpressionSelectionDialog::closeEvent( QCloseEvent *closeEvent )
195 {
196  QDialog::closeEvent( closeEvent );
197 
198  QSettings settings;
199  settings.setValue( "/Windows/ExpressionSelectionDialog/geometry", saveGeometry() );
200 }
201 
203 {
204  close();
205 }
206 
208 {
209  QDialog::done( r );
210  close();
211 }