QGIS API Documentation  3.12.1-BucureČ™ti (121cc00ff0)
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 "qgsmapcanvas.h"
22 #include "qgsmessagebar.h"
23 #include "qgsvectorlayer.h"
24 #include "qgssettings.h"
25 #include "qgsgui.h"
27 
28 
29 QgsExpressionSelectionDialog::QgsExpressionSelectionDialog( QgsVectorLayer *layer, const QString &startText, QWidget *parent )
30  : QDialog( parent )
31  , mLayer( layer )
32 
33 {
34  setupUi( this );
35 
37 
38  connect( mActionSelect, &QAction::triggered, this, &QgsExpressionSelectionDialog::mActionSelect_triggered );
39  connect( mActionAddToSelection, &QAction::triggered, this, &QgsExpressionSelectionDialog::mActionAddToSelection_triggered );
40  connect( mActionRemoveFromSelection, &QAction::triggered, this, &QgsExpressionSelectionDialog::mActionRemoveFromSelection_triggered );
41  connect( mActionSelectIntersect, &QAction::triggered, this, &QgsExpressionSelectionDialog::mActionSelectIntersect_triggered );
42  connect( mButtonZoomToFeatures, &QToolButton::clicked, this, &QgsExpressionSelectionDialog::mButtonZoomToFeatures_clicked );
43  connect( mPbnClose, &QPushButton::clicked, this, &QgsExpressionSelectionDialog::mPbnClose_clicked );
44 
45  setWindowTitle( QStringLiteral( "Select by Expression - %1" ).arg( layer->name() ) );
46 
47  mActionSelect->setIcon( QgsApplication::getThemeIcon( QStringLiteral( "/mIconExpressionSelect.svg" ) ) );
48  mActionAddToSelection->setIcon( QgsApplication::getThemeIcon( QStringLiteral( "/mIconSelectAdd.svg" ) ) );
49  mActionRemoveFromSelection->setIcon( QgsApplication::getThemeIcon( QStringLiteral( "/mIconSelectRemove.svg" ) ) );
50  mActionSelectIntersect->setIcon( QgsApplication::getThemeIcon( QStringLiteral( "/mIconSelectIntersect.svg" ) ) );
51 
52  mButtonSelect->addAction( mActionSelect );
53  mButtonSelect->addAction( mActionAddToSelection );
54  mButtonSelect->addAction( mActionRemoveFromSelection );
55  mButtonSelect->addAction( mActionSelectIntersect );
56  mButtonSelect->setDefaultAction( mActionSelect );
57 
58  mExpressionBuilder->setLayer( layer );
59  mExpressionBuilder->setExpressionText( startText );
60 
62  mExpressionBuilder->loadRecent( QStringLiteral( "selection" ) );
63  mExpressionBuilder->setExpressionContext( context );
64 
65  // by default, zoom to features is hidden, shown only if canvas is set
66  mButtonZoomToFeatures->setVisible( false );
67 
68  connect( buttonBox, &QDialogButtonBox::helpRequested, this, &QgsExpressionSelectionDialog::showHelp );
69 }
70 
72 {
73  return mExpressionBuilder;
74 }
75 
77 {
78  mExpressionBuilder->setExpressionText( text );
79 }
80 
82 {
83  return mExpressionBuilder->expressionText();
84 }
85 
87 {
88  // Store in child widget only.
89  mExpressionBuilder->setGeomCalculator( da );
90 }
91 
93 {
94  mMessageBar = messageBar;
95 }
96 
98 {
99  mMapCanvas = canvas;
100  mButtonZoomToFeatures->setVisible( true );
101 }
102 
103 void QgsExpressionSelectionDialog::mActionSelect_triggered()
104 {
105  mLayer->selectByExpression( mExpressionBuilder->expressionText(),
107  pushSelectedFeaturesMessage();
108  saveRecent();
109 }
110 
111 void QgsExpressionSelectionDialog::mActionAddToSelection_triggered()
112 {
113  mLayer->selectByExpression( mExpressionBuilder->expressionText(),
115  pushSelectedFeaturesMessage();
116  saveRecent();
117 }
118 
119 void QgsExpressionSelectionDialog::mActionSelectIntersect_triggered()
120 {
121  mLayer->selectByExpression( mExpressionBuilder->expressionText(),
123  pushSelectedFeaturesMessage();
124  saveRecent();
125 }
126 
127 void QgsExpressionSelectionDialog::mActionRemoveFromSelection_triggered()
128 {
129  mLayer->selectByExpression( mExpressionBuilder->expressionText(),
131  pushSelectedFeaturesMessage();
132  saveRecent();
133 }
134 
135 void QgsExpressionSelectionDialog::pushSelectedFeaturesMessage()
136 {
137  if ( !mMessageBar )
138  return;
139 
140  const int timeout = QgsSettings().value( QStringLiteral( "qgis/messageTimeout" ), 5 ).toInt();
141  const int count = mLayer->selectedFeatureCount();
142  if ( count > 0 )
143  {
144  mMessageBar->pushMessage( QString(),
145  tr( "%n matching feature(s) selected", "matching features", count ),
146  Qgis::Info, timeout );
147  }
148  else
149  {
150  mMessageBar->pushMessage( QString(),
151  tr( "No matching features found" ),
152  Qgis::Warning, timeout );
153  }
154 }
155 
156 void QgsExpressionSelectionDialog::mButtonZoomToFeatures_clicked()
157 {
158  if ( mExpressionBuilder->expressionText().isEmpty() || !mMapCanvas )
159  return;
160 
162 
163  QgsFeatureRequest request = QgsFeatureRequest().setFilterExpression( mExpressionBuilder->expressionText() )
164  .setExpressionContext( context )
165  .setNoAttributes();
166 
167  QgsFeatureIterator features = mLayer->getFeatures( request );
168 
169  QgsRectangle bbox;
170  bbox.setMinimal();
171  QgsFeature feat;
172  int featureCount = 0;
173  while ( features.nextFeature( feat ) )
174  {
175  QgsGeometry geom = feat.geometry();
176  if ( geom.isNull() || geom.constGet()->isEmpty() )
177  continue;
178 
179  QgsRectangle r = mMapCanvas->mapSettings().layerExtentToOutputExtent( mLayer, geom.boundingBox() );
180  bbox.combineExtentWith( r );
181  featureCount++;
182  }
183  features.close();
184 
185  QgsSettings settings;
186  int timeout = settings.value( QStringLiteral( "qgis/messageTimeout" ), 5 ).toInt();
187  if ( featureCount > 0 )
188  {
189  mMapCanvas->zoomToFeatureExtent( bbox );
190  if ( mMessageBar )
191  {
192  mMessageBar->pushMessage( QString(),
193  tr( "Zoomed to %n matching feature(s)", "number of matching features", featureCount ),
194  Qgis::Info,
195  timeout );
196  }
197  }
198  else if ( mMessageBar )
199  {
200  mMessageBar->pushMessage( QString(),
201  tr( "No matching features found" ),
202  Qgis::Info,
203  timeout );
204  }
205  saveRecent();
206 }
207 
209 {
210  QDialog::closeEvent( closeEvent );
211 }
212 
213 void QgsExpressionSelectionDialog::mPbnClose_clicked()
214 {
215  close();
216 }
217 
219 {
220  QDialog::done( r );
221  close();
222 }
223 
224 void QgsExpressionSelectionDialog::saveRecent()
225 {
226  mExpressionBuilder->saveToRecent( QStringLiteral( "selection" ) );
227 }
228 
229 void QgsExpressionSelectionDialog::showHelp()
230 {
231  QgsHelp::openHelp( QStringLiteral( "introduction/general_tools.html#automatic-selection" ) );
232 }
Wrapper for iterator of features from vector data provider or vector layer.
A rectangle specified with double values.
Definition: qgsrectangle.h:41
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.
Definition: qgsrectangle.h:151
This class is a composition of two QSettings instances:
Definition: qgssettings.h:58
QVariant value(const QString &key, const QVariant &defaultValue=QVariant(), Section section=NoSection) const
Returns the value for setting key.
QString expressionText()
Returns the current expression text.
int selectedFeatureCount() const
Returns the number of features that are selected in this layer.
void zoomToFeatureExtent(QgsRectangle &rect)
Zooms to feature extent.
Remove from current selection.
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:122
static QIcon getThemeIcon(const QString &name)
Helper to get a theme icon.
QgsExpressionSelectionDialog(QgsVectorLayer *layer, const QString &startText=QString(), QWidget *parent=nullptr)
Creates a new selection dialog.
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
Definition: qgsfeature.h:55
Map canvas is a class for displaying all GIS data types on a canvas.
Definition: qgsmapcanvas.h:75
QgsFeatureRequest & setFilterExpression(const QString &expression)
Set the filter expression.
void setGeomCalculator(const QgsDistanceArea &da)
Sets geometry calculator used in distance/area calculations.
QgsFeatureRequest & setNoAttributes()
Set that no attributes will be fetched.
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
void closeEvent(QCloseEvent *closeEvent) override
Implementation for closeEvent Saves the window geometry.
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
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.
void pushMessage(const QString &text, Qgis::MessageLevel level=Qgis::Info, int duration=5)
convenience method for pushing a message to the bar
Definition: qgsmessagebar.h:88
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.
const QgsAbstractGeometry * constGet() const
Returns a non-modifiable (const) reference to the underlying abstract geometry primitive.
const QgsMapSettings & mapSettings() const
Gets access to properties used for map rendering.
void combineExtentWith(const QgsRectangle &rect)
Expands the rectangle so that it covers both the original rectangle and the given rectangle...
Definition: qgsrectangle.h:359
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.
Q_INVOKABLE void selectByExpression(const QString &expression, QgsVectorLayer::SelectBehavior behavior=QgsVectorLayer::SetSelection)
Selects matching features using an expression.
QgsRectangle boundingBox() const
Returns the bounding box of the geometry.
static void enableAutoGeometryRestore(QWidget *widget, const QString &key=QString())
Register the widget to allow its position to be automatically saved and restored when open and closed...
Definition: qgsgui.cpp:133
static void openHelp(const QString &key)
Opens help topic for the given help key using default system web browser.
Definition: qgshelp.cpp:36
QgsFeatureIterator getFeatures(const QgsFeatureRequest &request=QgsFeatureRequest()) const FINAL
Queries the layer for features specified in request.
QString name
Definition: qgsmaplayer.h:83
QgsGeometry geometry
Definition: qgsfeature.h:67
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.