QGIS API Documentation  2.8.2-Wien
 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  setWindowTitle( QString( "Select by expression - %1" ).arg( layer->name() ) );
29 
30  mActionSelect->setIcon( QgsApplication::getThemeIcon( "/mIconExpressionSelect.svg" ) );
31  mActionAddToSelection->setIcon( QgsApplication::getThemeIcon( "/mIconSelectAdd.svg" ) );
32  mActionRemoveFromSelection->setIcon( QgsApplication::getThemeIcon( "/mIconSelectRemove.svg" ) );
33  mActionSelectInstersect->setIcon( QgsApplication::getThemeIcon( "/mIconSelectIntersect.svg" ) );
34 
35  mButtonSelect->addAction( mActionSelect );
36  mButtonSelect->addAction( mActionAddToSelection );
37  mButtonSelect->addAction( mActionRemoveFromSelection );
38  mButtonSelect->addAction( mActionSelectInstersect );
39  mButtonSelect->setDefaultAction( mActionSelect );
40 
41  mExpressionBuilder->setLayer( layer );
42  mExpressionBuilder->setExpressionText( startText );
43  mExpressionBuilder->loadFieldNames();
44  mExpressionBuilder->loadRecent( "Selection" );
45 
46  QSettings settings;
47  restoreGeometry( settings.value( "/Windows/ExpressionSelectionDialog/geometry" ).toByteArray() );
48 }
49 
51 {
52  return mExpressionBuilder;
53 }
54 
56 {
57  mExpressionBuilder->setExpressionText( text );
58 }
59 
61 {
62  return mExpressionBuilder->expressionText();
63 }
64 
66 {
67  // Store in child widget only.
68  mExpressionBuilder->setGeomCalculator( da );
69 }
70 
72 {
73  QgsFeatureIds newSelection;
74  QgsExpression* expression = new QgsExpression( mExpressionBuilder->expressionText() );
75 
76  const QgsFields fields = mLayer->pendingFields();
77 
78  QgsFeatureIterator features = mLayer->getFeatures();
79 
80  expression->prepare( fields );
81 
82  QgsFeature feat;
83  while ( features.nextFeature( feat ) )
84  {
85  if ( expression->evaluate( &feat, fields ).toBool() )
86  {
87  newSelection << feat.id();
88  }
89  }
90 
91  features.close();
92 
93  mLayer->setSelectedFeatures( newSelection );
94 
95  delete expression;
96  saveRecent();
97 }
98 
100 {
101  QgsFeatureIds newSelection = mLayer->selectedFeaturesIds();
102  QgsExpression* expression = new QgsExpression( mExpressionBuilder->expressionText() );
103 
104  const QgsFields fields = mLayer->pendingFields();
105 
106  QgsFeatureIterator features = mLayer->getFeatures();
107 
108  expression->prepare( fields );
109 
110  QgsFeature feat;
111  while ( features.nextFeature( feat ) )
112  {
113  if ( expression->evaluate( &feat, fields ).toBool() )
114  {
115  newSelection << feat.id();
116  }
117  }
118 
119  features.close();
120 
121  mLayer->setSelectedFeatures( newSelection );
122 
123  delete expression;
124  saveRecent();
125 }
126 
128 {
129  const QgsFeatureIds &oldSelection = mLayer->selectedFeaturesIds();
130  QgsFeatureIds newSelection;
131 
132  QgsExpression* expression = new QgsExpression( mExpressionBuilder->expressionText() );
133 
134  const QgsFields fields = mLayer->pendingFields();
135 
136  expression->prepare( fields );
137 
138  QgsFeature feat;
139  foreach ( const QgsFeatureId fid, oldSelection )
140  {
141  QgsFeatureIterator features = mLayer->getFeatures( QgsFeatureRequest().setFilterFid( fid ) );
142 
143  if ( features.nextFeature( feat ) )
144  {
145  if ( expression->evaluate( &feat, fields ).toBool() )
146  {
147  newSelection << feat.id();
148  }
149  }
150  else
151  {
152  Q_ASSERT( false );
153  }
154 
155  features.close();
156  }
157 
158  mLayer->setSelectedFeatures( newSelection );
159 
160  delete expression;
161  saveRecent();
162 }
163 
165 {
166  const QgsFeatureIds &oldSelection = mLayer->selectedFeaturesIds();
167  QgsFeatureIds newSelection = mLayer->selectedFeaturesIds();
168 
169  QgsExpression* expression = new QgsExpression( mExpressionBuilder->expressionText() );
170 
171  const QgsFields fields = mLayer->pendingFields();
172 
173  expression->prepare( fields );
174 
175  QgsFeature feat;
176  foreach ( const QgsFeatureId fid, oldSelection )
177  {
178  QgsFeatureIterator features = mLayer->getFeatures( QgsFeatureRequest().setFilterFid( fid ) );
179 
180  if ( features.nextFeature( feat ) )
181  {
182  if ( expression->evaluate( &feat, fields ).toBool() )
183  {
184  newSelection.remove( feat.id() );
185  }
186  }
187  else
188  {
189  Q_ASSERT( false );
190  }
191 
192  features.close();
193  }
194 
195  mLayer->setSelectedFeatures( newSelection );
196 
197  delete expression;
198 
199  saveRecent();
200 }
201 
202 void QgsExpressionSelectionDialog::closeEvent( QCloseEvent *closeEvent )
203 {
204  QDialog::closeEvent( closeEvent );
205 
206  QSettings settings;
207  settings.setValue( "/Windows/ExpressionSelectionDialog/geometry", saveGeometry() );
208 }
209 
211 {
212  close();
213 }
214 
216 {
217  QDialog::done( r );
218  close();
219 }
220 
221 void QgsExpressionSelectionDialog::saveRecent()
222 {
223  mExpressionBuilder->saveToRecent( "Selection" );
224 }