QGIS API Documentation  2.99.0-Master (7d4f81d)
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 
18 #include "qgsapplication.h"
19 #include "qgsexpression.h"
20 #include "qgsgeometry.h"
21 #include "qgsmessagebar.h"
22 #include "qgsvectorlayer.h"
23 #include "qgssettings.h"
24 
25 
26 QgsExpressionSelectionDialog::QgsExpressionSelectionDialog( QgsVectorLayer *layer, const QString &startText, QWidget *parent )
27  : QDialog( parent )
28  , mLayer( layer )
29  , mMessageBar( nullptr )
30  , mMapCanvas( nullptr )
31 {
32  setupUi( this );
33 
34  setWindowTitle( QStringLiteral( "Select by Expression - %1" ).arg( layer->name() ) );
35 
36  mActionSelect->setIcon( QgsApplication::getThemeIcon( QStringLiteral( "/mIconExpressionSelect.svg" ) ) );
37  mActionAddToSelection->setIcon( QgsApplication::getThemeIcon( QStringLiteral( "/mIconSelectAdd.svg" ) ) );
38  mActionRemoveFromSelection->setIcon( QgsApplication::getThemeIcon( QStringLiteral( "/mIconSelectRemove.svg" ) ) );
39  mActionSelectIntersect->setIcon( QgsApplication::getThemeIcon( QStringLiteral( "/mIconSelectIntersect.svg" ) ) );
40 
41  mButtonSelect->addAction( mActionSelect );
42  mButtonSelect->addAction( mActionAddToSelection );
43  mButtonSelect->addAction( mActionRemoveFromSelection );
44  mButtonSelect->addAction( mActionSelectIntersect );
45  mButtonSelect->setDefaultAction( mActionSelect );
46 
47  mExpressionBuilder->setLayer( layer );
48  mExpressionBuilder->setExpressionText( startText );
49  mExpressionBuilder->loadFieldNames();
50  mExpressionBuilder->loadRecent( QStringLiteral( "Selection" ) );
51 
53  mExpressionBuilder->setExpressionContext( context );
54 
55  // by default, zoom to features is hidden, shown only if canvas is set
56  mButtonZoomToFeatures->setVisible( false );
57 
58  QgsSettings settings;
59  restoreGeometry( settings.value( QStringLiteral( "Windows/ExpressionSelectionDialog/geometry" ) ).toByteArray() );
60 }
61 
63 {
64  return mExpressionBuilder;
65 }
66 
68 {
69  mExpressionBuilder->setExpressionText( text );
70 }
71 
73 {
74  return mExpressionBuilder->expressionText();
75 }
76 
78 {
79  // Store in child widget only.
80  mExpressionBuilder->setGeomCalculator( da );
81 }
82 
84 {
85  mMessageBar = messageBar;
86 }
87 
89 {
90  mMapCanvas = canvas;
91  mButtonZoomToFeatures->setVisible( true );
92 }
93 
94 void QgsExpressionSelectionDialog::on_mActionSelect_triggered()
95 {
96  mLayer->selectByExpression( mExpressionBuilder->expressionText(),
98  saveRecent();
99 }
100 
101 void QgsExpressionSelectionDialog::on_mActionAddToSelection_triggered()
102 {
103  mLayer->selectByExpression( mExpressionBuilder->expressionText(),
105  saveRecent();
106 }
107 
108 void QgsExpressionSelectionDialog::on_mActionSelectIntersect_triggered()
109 {
110  mLayer->selectByExpression( mExpressionBuilder->expressionText(),
112  saveRecent();
113 }
114 
115 void QgsExpressionSelectionDialog::on_mActionRemoveFromSelection_triggered()
116 {
117  mLayer->selectByExpression( mExpressionBuilder->expressionText(),
119  saveRecent();
120 }
121 
122 void QgsExpressionSelectionDialog::on_mButtonZoomToFeatures_clicked()
123 {
124  if ( mExpressionBuilder->expressionText().isEmpty() || !mMapCanvas )
125  return;
126 
127  QgsFeatureIds ids;
128 
130 
131  QgsFeatureRequest request = QgsFeatureRequest().setFilterExpression( mExpressionBuilder->expressionText() )
132  .setExpressionContext( context )
134 
135  QgsFeatureIterator features = mLayer->getFeatures( request );
136 
137  QgsRectangle bbox;
138  bbox.setMinimal();
139  QgsFeature feat;
140  int featureCount = 0;
141  while ( features.nextFeature( feat ) )
142  {
143  QgsGeometry geom = feat.geometry();
144  if ( geom.isNull() || geom.geometry()->isEmpty() )
145  continue;
146 
147  QgsRectangle r = mMapCanvas->mapSettings().layerExtentToOutputExtent( mLayer, geom.boundingBox() );
148  bbox.combineExtentWith( r );
149  featureCount++;
150  }
151  features.close();
152 
153  QgsSettings settings;
154  int timeout = settings.value( QStringLiteral( "qgis/messageTimeout" ), 5 ).toInt();
155  if ( featureCount > 0 )
156  {
157  mMapCanvas->zoomToFeatureExtent( bbox );
158  if ( mMessageBar )
159  {
160  mMessageBar->pushMessage( QString(),
161  tr( "Zoomed to %n matching feature(s)", "number of matching features", featureCount ),
163  timeout );
164  }
165  }
166  else if ( mMessageBar )
167  {
168  mMessageBar->pushMessage( QString(),
169  tr( "No matching features found" ),
171  timeout );
172  }
173  saveRecent();
174 }
175 
177 {
178  QDialog::closeEvent( closeEvent );
179 
180  QgsSettings settings;
181  settings.setValue( QStringLiteral( "Windows/ExpressionSelectionDialog/geometry" ), saveGeometry() );
182 }
183 
184 void QgsExpressionSelectionDialog::on_mPbnClose_clicked()
185 {
186  close();
187 }
188 
190 {
191  QDialog::done( r );
192  close();
193 }
194 
195 void QgsExpressionSelectionDialog::saveRecent()
196 {
197  mExpressionBuilder->saveToRecent( QStringLiteral( "Selection" ) );
198 }
Wrapper for iterator of features from vector data provider or vector layer.
A rectangle specified with double values.
Definition: qgsrectangle.h:38
virtual bool isEmpty() const
Returns true if the geometry is empty.
void setMinimal()
Set a rectangle so that min corner is at max and max corner is at min.
bool isNull() const
Returns true if the geometry is null (ie, contains no underlying geometry accessible via geometry() )...
This class is a composition of two QSettings instances:
Definition: qgssettings.h:54
QSet< QgsFeatureId > QgsFeatureIds
Definition: qgsfeature.h:519
QString expressionText()
Returns the current expression text.
void zoomToFeatureExtent(QgsRectangle &rect)
Zooms to feature extent.
Remove from current selection.
QgsFeatureRequest & setSubsetOfAttributes(const QgsAttributeList &attrs)
Set a subset of attributes that will be fetched.
A bar for displaying non-blocking messages to the user.
Definition: qgsmessagebar.h:44
A geometry is the spatial representation of a feature.
Definition: qgsgeometry.h:96
static QIcon getThemeIcon(const QString &name)
Helper to get a theme icon.
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
Definition: qgsfeature.h:61
Map canvas is a class for displaying all GIS data types on a canvas.
Definition: qgsmapcanvas.h:73
void pushMessage(const QString &text, MessageLevel level=INFO, int duration=5)
convenience method for pushing a message to the bar
Definition: qgsmessagebar.h:91
void setValue(const QString &key, const QVariant &value, const QgsSettings::Section section=QgsSettings::NoSection)
Sets the value of setting key to value.
QgsFeatureRequest & setFilterExpression(const QString &expression)
Set the filter expression.
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)...
QgsRectangle layerExtentToOutputExtent(const QgsMapLayer *layer, QgsRectangle extent) const
transform bounding box from layer&#39;s CRS to output CRS
QgsExpressionSelectionDialog(QgsVectorLayer *layer, const QString &startText=QString(), QWidget *parent=0)
Creates a new selection dialog.
void setMessageBar(QgsMessageBar *messageBar)
Sets the message bar to display feedback from the dialog.
static QList< QgsExpressionContextScope * > globalProjectLayerScopes(const QgsMapLayer *layer)
Creates a list of three scopes: global, layer&#39;s project and layer.
QgsGeometry geometry() const
Returns the geometry associated with this feature.
Definition: qgsfeature.cpp:101
Add selection to current selection.
QgsExpressionBuilderWidget * expressionBuilder()
The builder widget that is used by the dialog.
void setExpressionText(const QString &text)
Sets the current expression text.
Set selection, removing any existing selection.
QgsFeatureIterator getFeatures(const QgsFeatureRequest &request=QgsFeatureRequest()) const override
Query the layer for features specified in request.
const QgsMapSettings & mapSettings() const
Get access to properties used for map rendering.
void combineExtentWith(const QgsRectangle &rect)
Expand the rectangle so that covers both the original rectangle and the given rectangle.
A general purpose distance and area calculator, capable of performing ellipsoid based calculations...
Modify current selection to include only select features which match.
A reusable widget that can be used to build a expression string.
void selectByExpression(const QString &expression, SelectBehavior behavior=SetSelection)
Select matching features using an expression.
QVariant value(const QString &key, const QVariant &defaultValue=QVariant(), const Section section=NoSection) const
Returns the value for setting key.
QgsRectangle boundingBox() const
Returns the bounding box of the geometry.
QString name
Definition: qgsmaplayer.h:58
QList< int > QgsAttributeList
Definition: qgsfield.h:27
bool nextFeature(QgsFeature &f)
Represents a vector layer which manages a vector based data sets.
void setMapCanvas(QgsMapCanvas *canvas)
Sets a map canvas associated with the dialog.
QgsAbstractGeometry * geometry() const
Returns the underlying geometry store.