QGIS API Documentation  2.10.1-Pisa
 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 
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 }
QgsFeatureId id() const
Get the feature ID for this feature.
Definition: qgsfeature.cpp:51
QgsExpressionSelectionDialog(QgsVectorLayer *layer, QString startText=QString(), QWidget *parent=0)
Creates a new selection dialog.
Class for parsing and evaluation of expressions (formerly called "search strings").
Definition: qgsexpression.h:86
Wrapper for iterator of features from vector data provider or vector layer.
QByteArray toByteArray() const
bool close()
virtual void closeEvent(QCloseEvent *e)
void setupUi(QWidget *widget)
QVariant evaluate(const QgsFeature *f=NULL)
Evaluate the feature and return the result.
bool prepare(const QgsFields &fields)
Get the expression ready for evaluation - find out column indexes.
QString expressionText()
Returns the current expression text.
QgsFeatureIterator getFeatures(const QgsFeatureRequest &request=QgsFeatureRequest())
Query the provider for features specified in request.
static QIcon getThemeIcon(const QString &theName)
Helper to get a theme icon.
Container of fields for a vector layer.
Definition: qgsfield.h:173
virtual void done(int r)
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
Definition: qgsfeature.h:162
const QString & name() const
Get the display name of the layer.
void setValue(const QString &key, const QVariant &value)
const QgsFeatureIds & selectedFeaturesIds() const
Return reference to identifiers of selected features.
bool restoreGeometry(const QByteArray &geometry)
void setGeomCalculator(const QgsDistanceArea &da)
Sets geometry calculator used in distance/area calculations.
virtual void closeEvent(QCloseEvent *closeEvent) override
Implementation for closeEvent Saves the window geometry.
virtual void done(int r) override
Implementation for done (default behavior when pressing esc) Calls close, so the window geometry gets...
This class wraps a request for features to a vector layer (or directly its vector data provider)...
QgsExpressionBuilderWidget * expressionBuilder()
The builder widget that is used by the dialog.
void setExpressionText(const QString &text)
Sets the current expression text.
General purpose distance and area calculator.
A reusable widget that can be used to build a expression string.
QVariant value(const QString &key, const QVariant &defaultValue) const
bool remove(const T &value)
QByteArray saveGeometry() const
void setSelectedFeatures(const QgsFeatureIds &ids)
Change selection to the new set of features.
void setWindowTitle(const QString &)
bool toBool() const
qint64 QgsFeatureId
Definition: qgsfeature.h:31
const QgsFields & pendingFields() const
returns field list in the to-be-committed state
bool nextFeature(QgsFeature &f)
Represents a vector layer which manages a vector based data sets.