QGIS API Documentation  2.14.0-Essen
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 at opengis 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  mActionSelectIntersect->setIcon( QgsApplication::getThemeIcon( "/mIconSelectIntersect.svg" ) );
34 
35  mButtonSelect->addAction( mActionSelect );
36  mButtonSelect->addAction( mActionAddToSelection );
37  mButtonSelect->addAction( mActionRemoveFromSelection );
38  mButtonSelect->addAction( mActionSelectIntersect );
39  mButtonSelect->setDefaultAction( mActionSelect );
40 
41  mExpressionBuilder->setLayer( layer );
42  mExpressionBuilder->setExpressionText( startText );
43  mExpressionBuilder->loadFieldNames();
44  mExpressionBuilder->loadRecent( "Selection" );
45 
46  QgsExpressionContext context;
50  mExpressionBuilder->setExpressionContext( context );
51 
52  QSettings settings;
53  restoreGeometry( settings.value( "/Windows/ExpressionSelectionDialog/geometry" ).toByteArray() );
54 }
55 
57 {
58  return mExpressionBuilder;
59 }
60 
62 {
63  mExpressionBuilder->setExpressionText( text );
64 }
65 
67 {
68  return mExpressionBuilder->expressionText();
69 }
70 
72 {
73  // Store in child widget only.
74  mExpressionBuilder->setGeomCalculator( da );
75 }
76 
78 {
79  QgsFeatureIds newSelection;
80  QgsExpression* expression = new QgsExpression( mExpressionBuilder->expressionText() );
81 
82  QgsExpressionContext context;
86 
87  QgsFeatureRequest request = QgsFeatureRequest().setFilterExpression( mExpressionBuilder->expressionText() ).setExpressionContext( context );
88  QgsFeatureIterator features = mLayer->getFeatures( request );
89 
90  QgsFeature feat;
91  while ( features.nextFeature( feat ) )
92  {
93  newSelection << feat.id();
94  }
95 
96  features.close();
97 
98  mLayer->setSelectedFeatures( newSelection );
99 
100  delete expression;
101  saveRecent();
102 }
103 
105 {
106  QgsFeatureIds newSelection = mLayer->selectedFeaturesIds();
107  QgsExpression* expression = new QgsExpression( mExpressionBuilder->expressionText() );
108 
109  QgsExpressionContext context;
113 
114  QgsFeatureRequest request = QgsFeatureRequest().setFilterExpression( mExpressionBuilder->expressionText() ).setExpressionContext( context );
115  QgsFeatureIterator features = mLayer->getFeatures( request );
116 
117  QgsFeature feat;
118  while ( features.nextFeature( feat ) )
119  {
120  newSelection << feat.id();
121  }
122 
123  features.close();
124 
125  mLayer->setSelectedFeatures( newSelection );
126 
127  delete expression;
128  saveRecent();
129 }
130 
132 {
133  const QgsFeatureIds &oldSelection = mLayer->selectedFeaturesIds();
134  QgsFeatureIds newSelection;
135 
136  QgsExpression* expression = new QgsExpression( mExpressionBuilder->expressionText() );
137 
138  QgsExpressionContext context;
142 
143  expression->prepare( &context );
144 
145  QgsFeature feat;
146  Q_FOREACH ( const QgsFeatureId fid, oldSelection )
147  {
148  QgsFeatureIterator features = mLayer->getFeatures( QgsFeatureRequest().setFilterFid( fid ) );
149 
150  if ( features.nextFeature( feat ) )
151  {
152  context.setFeature( feat );
153  if ( expression->evaluate( &context ).toBool() )
154  {
155  newSelection << feat.id();
156  }
157  }
158  else
159  {
160  Q_ASSERT( false );
161  }
162 
163  features.close();
164  }
165 
166  mLayer->setSelectedFeatures( newSelection );
167 
168  delete expression;
169  saveRecent();
170 }
171 
173 {
174  const QgsFeatureIds &oldSelection = mLayer->selectedFeaturesIds();
175  QgsFeatureIds newSelection = mLayer->selectedFeaturesIds();
176 
177  QgsExpression* expression = new QgsExpression( mExpressionBuilder->expressionText() );
178 
179  QgsExpressionContext context;
183 
184  expression->prepare( &context );
185 
186  QgsFeature feat;
187  Q_FOREACH ( const QgsFeatureId fid, oldSelection )
188  {
189  QgsFeatureIterator features = mLayer->getFeatures( QgsFeatureRequest().setFilterFid( fid ) );
190 
191  if ( features.nextFeature( feat ) )
192  {
193  context.setFeature( feat );
194  if ( expression->evaluate( &context ).toBool() )
195  {
196  newSelection.remove( feat.id() );
197  }
198  }
199  else
200  {
201  Q_ASSERT( false );
202  }
203 
204  features.close();
205  }
206 
207  mLayer->setSelectedFeatures( newSelection );
208 
209  delete expression;
210 
211  saveRecent();
212 }
213 
215 {
216  QDialog::closeEvent( closeEvent );
217 
218  QSettings settings;
219  settings.setValue( "/Windows/ExpressionSelectionDialog/geometry", saveGeometry() );
220 }
221 
223 {
224  close();
225 }
226 
228 {
229  QDialog::done( r );
230  close();
231 }
232 
233 void QgsExpressionSelectionDialog::saveRecent()
234 {
235  mExpressionBuilder->saveToRecent( "Selection" );
236 }
QgsFeatureId id() const
Get the feature ID for this feature.
Definition: qgsfeature.cpp:65
Class for parsing and evaluation of expressions (formerly called "search strings").
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)
Q_DECL_DEPRECATED QVariant evaluate(const QgsFeature *f)
Evaluate the feature and return the result.
QString name() const
Get the display name of the layer.
Q_DECL_DEPRECATED 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.
QgsExpressionSelectionDialog(QgsVectorLayer *layer, const QString &startText=QString(), QWidget *parent=nullptr)
Creates a new selection dialog.
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:187
void setValue(const QString &key, const QVariant &value)
QgsFeatureRequest & setFilterExpression(const QString &expression)
Set the filter expression.
static QgsExpressionContextScope * globalScope()
Creates a new scope which contains variables and functions relating to the global QGIS context...
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.
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
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
static QgsExpressionContextScope * projectScope()
Creates a new scope which contains variables and functions relating to the current QGIS project...
static QgsExpressionContextScope * layerScope(const QgsMapLayer *layer)
Creates a new scope which contains variables and functions relating to a QgsMapLayer.
bool nextFeature(QgsFeature &f)
Represents a vector layer which manages a vector based data sets.