QGIS API Documentation  2.2.0-Valmiera
 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  mExpressionBuilder->loadRecent( "Selection" );
43 
44  QSettings settings;
45  restoreGeometry( settings.value( "/Windows/ExpressionSelectionDialog/geometry" ).toByteArray() );
46 }
47 
49 {
50  return mExpressionBuilder;
51 }
52 
54 {
55  mExpressionBuilder->setExpressionText( text );
56 }
57 
59 {
60  return mExpressionBuilder->expressionText();
61 }
62 
64 {
65  // Store in child widget only.
66  mExpressionBuilder->setGeomCalculator( da );
67 }
68 
70 {
71  QgsFeatureIds newSelection;
72  QgsExpression* expression = new QgsExpression( mExpressionBuilder->expressionText() );
73 
74  const QgsFields fields = mLayer->pendingFields();
75 
77 
78  expression->prepare( fields );
79 
80  QgsFeature feat;
81  while ( features.nextFeature( feat ) )
82  {
83  if ( expression->evaluate( &feat, fields ).toBool() )
84  {
85  newSelection << feat.id();
86  }
87  }
88 
89  features.close();
90 
91  mLayer->setSelectedFeatures( newSelection );
92 
93  delete expression;
94  saveRecent();
95 }
96 
98 {
99  QgsFeatureIds newSelection = mLayer->selectedFeaturesIds();
100  QgsExpression* expression = new QgsExpression( mExpressionBuilder->expressionText() );
101 
102  const QgsFields fields = mLayer->pendingFields();
103 
104  QgsFeatureIterator features = mLayer->getFeatures();
105 
106  expression->prepare( fields );
107 
108  QgsFeature feat;
109  while ( features.nextFeature( feat ) )
110  {
111  if ( expression->evaluate( &feat, fields ).toBool() )
112  {
113  newSelection << feat.id();
114  }
115  }
116 
117  features.close();
118 
119  mLayer->setSelectedFeatures( newSelection );
120 
121  delete expression;
122  saveRecent();
123 }
124 
126 {
127  const QgsFeatureIds &oldSelection = mLayer->selectedFeaturesIds();
128  QgsFeatureIds newSelection;
129 
130  QgsExpression* expression = new QgsExpression( mExpressionBuilder->expressionText() );
131 
132  const QgsFields fields = mLayer->pendingFields();
133 
134  expression->prepare( fields );
135 
136  QgsFeature feat;
137  foreach ( const QgsFeatureId fid, oldSelection )
138  {
139  QgsFeatureIterator features = mLayer->getFeatures( QgsFeatureRequest().setFilterFid( fid ) );
140 
141  if ( features.nextFeature( feat ) )
142  {
143  if ( expression->evaluate( &feat, fields ).toBool() )
144  {
145  newSelection << feat.id();
146  }
147  }
148  else
149  {
150  Q_ASSERT( false );
151  }
152 
153  features.close();
154  }
155 
156  mLayer->setSelectedFeatures( newSelection );
157 
158  delete expression;
159  saveRecent();
160 }
161 
163 {
164  const QgsFeatureIds &oldSelection = mLayer->selectedFeaturesIds();
165  QgsFeatureIds newSelection = mLayer->selectedFeaturesIds();
166 
167  QgsExpression* expression = new QgsExpression( mExpressionBuilder->expressionText() );
168 
169  const QgsFields fields = mLayer->pendingFields();
170 
171  expression->prepare( fields );
172 
173  QgsFeature feat;
174  foreach ( const QgsFeatureId fid, oldSelection )
175  {
176  QgsFeatureIterator features = mLayer->getFeatures( QgsFeatureRequest().setFilterFid( fid ) );
177 
178  if ( features.nextFeature( feat ) )
179  {
180  if ( expression->evaluate( &feat, fields ).toBool() )
181  {
182  newSelection.remove( feat.id() );
183  }
184  }
185  else
186  {
187  Q_ASSERT( false );
188  }
189 
190  features.close();
191  }
192 
193  mLayer->setSelectedFeatures( newSelection );
194 
195  delete expression;
196 
197  saveRecent();
198 }
199 
200 void QgsExpressionSelectionDialog::closeEvent( QCloseEvent *closeEvent )
201 {
202  QDialog::closeEvent( closeEvent );
203 
204  QSettings settings;
205  settings.setValue( "/Windows/ExpressionSelectionDialog/geometry", saveGeometry() );
206 }
207 
209 {
210  close();
211 }
212 
214 {
215  QDialog::done( r );
216  close();
217 }
218 
220 {
221  mExpressionBuilder->saveToRecent( "Selection" );
222 }