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 }
QgsFeatureId id() const
Get the feature id for this feature.
Definition: qgsfeature.cpp:101
Class for parsing and evaluation of expressions (formerly called "search strings").
Definition: qgsexpression.h:88
Wrapper for iterator of features from vector data provider or vector layer.
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.
QSet< QgsFeatureId > QgsFeatureIds
Definition: qgsfeature.h:321
QString expressionText()
Returns the current expression text.
QgsFeatureIterator getFeatures(const QgsFeatureRequest &request=QgsFeatureRequest())
Query the provider for features specified in request.
Container of fields for a vector layer.
Definition: qgsfield.h:162
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
Definition: qgsfeature.h:114
const QgsFeatureIds & selectedFeaturesIds() const
Return reference to identifiers of selected features.
void setGeomCalculator(const QgsDistanceArea &da)
Sets geometry calculator used in distance/area calculations.
QgsExpressionSelectionDialog(QgsVectorLayer *layer, QString startText=QString(), QWidget *parent=NULL)
Creates a new selection dialog.
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.
virtual void closeEvent(QCloseEvent *closeEvent)
Implementation for closeEvent Saves the window geometry.
General purpose distance and area calculator.
A reusable widget that can be used to build a expression string.
void setSelectedFeatures(const QgsFeatureIds &ids)
Change selection to the new set of features.
qint64 QgsFeatureId
Definition: qgsfeature.h:30
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.
virtual void done(int r)
Implementation for done (default behavior when pressing esc) Calls close, so the window geometry gets...
static QIcon getThemeIcon(const QString theName)
Helper to get a theme icon.