QGIS API Documentation  2.99.0-Master (19b062c)
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 
30 {
31  setupUi( this );
32  connect( mActionSelect, &QAction::triggered, this, &QgsExpressionSelectionDialog::mActionSelect_triggered );
33  connect( mActionAddToSelection, &QAction::triggered, this, &QgsExpressionSelectionDialog::mActionAddToSelection_triggered );
34  connect( mActionRemoveFromSelection, &QAction::triggered, this, &QgsExpressionSelectionDialog::mActionRemoveFromSelection_triggered );
35  connect( mActionSelectIntersect, &QAction::triggered, this, &QgsExpressionSelectionDialog::mActionSelectIntersect_triggered );
36  connect( mButtonZoomToFeatures, &QToolButton::clicked, this, &QgsExpressionSelectionDialog::mButtonZoomToFeatures_clicked );
37  connect( mPbnClose, &QPushButton::clicked, this, &QgsExpressionSelectionDialog::mPbnClose_clicked );
38 
39  setWindowTitle( QStringLiteral( "Select by Expression - %1" ).arg( layer->name() ) );
40 
41  mActionSelect->setIcon( QgsApplication::getThemeIcon( QStringLiteral( "/mIconExpressionSelect.svg" ) ) );
42  mActionAddToSelection->setIcon( QgsApplication::getThemeIcon( QStringLiteral( "/mIconSelectAdd.svg" ) ) );
43  mActionRemoveFromSelection->setIcon( QgsApplication::getThemeIcon( QStringLiteral( "/mIconSelectRemove.svg" ) ) );
44  mActionSelectIntersect->setIcon( QgsApplication::getThemeIcon( QStringLiteral( "/mIconSelectIntersect.svg" ) ) );
45 
46  mButtonSelect->addAction( mActionSelect );
47  mButtonSelect->addAction( mActionAddToSelection );
48  mButtonSelect->addAction( mActionRemoveFromSelection );
49  mButtonSelect->addAction( mActionSelectIntersect );
50  mButtonSelect->setDefaultAction( mActionSelect );
51 
52  mExpressionBuilder->setLayer( layer );
53  mExpressionBuilder->setExpressionText( startText );
54  mExpressionBuilder->loadFieldNames();
55  mExpressionBuilder->loadRecent( QStringLiteral( "Selection" ) );
56 
58  mExpressionBuilder->setExpressionContext( context );
59 
60  // by default, zoom to features is hidden, shown only if canvas is set
61  mButtonZoomToFeatures->setVisible( false );
62 
63  QgsSettings settings;
64  restoreGeometry( settings.value( QStringLiteral( "Windows/ExpressionSelectionDialog/geometry" ) ).toByteArray() );
65 
66  connect( buttonBox, &QDialogButtonBox::helpRequested, this, &QgsExpressionSelectionDialog::showHelp );
67 }
68 
70 {
71  return mExpressionBuilder;
72 }
73 
75 {
76  mExpressionBuilder->setExpressionText( text );
77 }
78 
80 {
81  return mExpressionBuilder->expressionText();
82 }
83 
85 {
86  // Store in child widget only.
87  mExpressionBuilder->setGeomCalculator( da );
88 }
89 
91 {
92  mMessageBar = messageBar;
93 }
94 
96 {
97  mMapCanvas = canvas;
98  mButtonZoomToFeatures->setVisible( true );
99 }
100 
101 void QgsExpressionSelectionDialog::mActionSelect_triggered()
102 {
103  mLayer->selectByExpression( mExpressionBuilder->expressionText(),
105  saveRecent();
106 }
107 
108 void QgsExpressionSelectionDialog::mActionAddToSelection_triggered()
109 {
110  mLayer->selectByExpression( mExpressionBuilder->expressionText(),
112  saveRecent();
113 }
114 
115 void QgsExpressionSelectionDialog::mActionSelectIntersect_triggered()
116 {
117  mLayer->selectByExpression( mExpressionBuilder->expressionText(),
119  saveRecent();
120 }
121 
122 void QgsExpressionSelectionDialog::mActionRemoveFromSelection_triggered()
123 {
124  mLayer->selectByExpression( mExpressionBuilder->expressionText(),
126  saveRecent();
127 }
128 
129 void QgsExpressionSelectionDialog::mButtonZoomToFeatures_clicked()
130 {
131  if ( mExpressionBuilder->expressionText().isEmpty() || !mMapCanvas )
132  return;
133 
135 
136  QgsFeatureRequest request = QgsFeatureRequest().setFilterExpression( mExpressionBuilder->expressionText() )
137  .setExpressionContext( context )
139 
140  QgsFeatureIterator features = mLayer->getFeatures( request );
141 
142  QgsRectangle bbox;
143  bbox.setMinimal();
144  QgsFeature feat;
145  int featureCount = 0;
146  while ( features.nextFeature( feat ) )
147  {
148  QgsGeometry geom = feat.geometry();
149  if ( geom.isNull() || geom.constGet()->isEmpty() )
150  continue;
151 
152  QgsRectangle r = mMapCanvas->mapSettings().layerExtentToOutputExtent( mLayer, geom.boundingBox() );
153  bbox.combineExtentWith( r );
154  featureCount++;
155  }
156  features.close();
157 
158  QgsSettings settings;
159  int timeout = settings.value( QStringLiteral( "qgis/messageTimeout" ), 5 ).toInt();
160  if ( featureCount > 0 )
161  {
162  mMapCanvas->zoomToFeatureExtent( bbox );
163  if ( mMessageBar )
164  {
165  mMessageBar->pushMessage( QString(),
166  tr( "Zoomed to %n matching feature(s)", "number of matching features", featureCount ),
168  timeout );
169  }
170  }
171  else if ( mMessageBar )
172  {
173  mMessageBar->pushMessage( QString(),
174  tr( "No matching features found" ),
176  timeout );
177  }
178  saveRecent();
179 }
180 
182 {
183  QDialog::closeEvent( closeEvent );
184 
185  QgsSettings settings;
186  settings.setValue( QStringLiteral( "Windows/ExpressionSelectionDialog/geometry" ), saveGeometry() );
187 }
188 
189 void QgsExpressionSelectionDialog::mPbnClose_clicked()
190 {
191  close();
192 }
193 
195 {
196  QDialog::done( r );
197  close();
198 }
199 
200 void QgsExpressionSelectionDialog::saveRecent()
201 {
202  mExpressionBuilder->saveToRecent( QStringLiteral( "Selection" ) );
203 }
204 
205 void QgsExpressionSelectionDialog::showHelp()
206 {
207  QgsHelp::openHelp( QStringLiteral( "introduction/general_tools.html#automatic-selection" ) );
208 }
Wrapper for iterator of features from vector data provider or vector layer.
A rectangle specified with double values.
Definition: qgsrectangle.h:39
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:55
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:45
A geometry is the spatial representation of a feature.
Definition: qgsgeometry.h:111
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:62
Map canvas is a class for displaying all GIS data types on a canvas.
Definition: qgsmapcanvas.h:74
void pushMessage(const QString &text, MessageLevel level=INFO, int duration=5)
convenience method for pushing a message to the bar
Definition: qgsmessagebar.h:94
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'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 QgsAbstractGeometry * constGet() const
Returns a non-modifiable (const) reference to the underlying abstract geometry primitive.
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.
static void openHelp(const QString &key)
Opens help topic for the given help key using default system web browser.
Definition: qgshelp.cpp:34
QString name
Definition: qgsmaplayer.h:60
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.