QGIS API Documentation  3.16.0-Hannover (43b64b13f3)
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( tr( "%1 — Select by Expression" ).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 
59  mExpressionBuilder->initWithLayer( layer, context, QStringLiteral( "selection" ) );
60  mExpressionBuilder->setExpressionText( startText );
61 
62  // by default, zoom to features is hidden, shown only if canvas is set
63  mButtonZoomToFeatures->setVisible( false );
64 
65  connect( buttonBox, &QDialogButtonBox::helpRequested, this, &QgsExpressionSelectionDialog::showHelp );
66 }
67 
69 {
70  return mExpressionBuilder;
71 }
72 
74 {
75  mExpressionBuilder->setExpressionText( text );
76 }
77 
79 {
80  return mExpressionBuilder->expressionText();
81 }
82 
84 {
85  // Store in child widget only.
86  mExpressionBuilder->setGeomCalculator( da );
87 }
88 
90 {
91  mMessageBar = messageBar;
92 }
93 
95 {
96  mMapCanvas = canvas;
97  mButtonZoomToFeatures->setVisible( true );
98 }
99 
100 void QgsExpressionSelectionDialog::mActionSelect_triggered()
101 {
102  mLayer->selectByExpression( mExpressionBuilder->expressionText(),
104  pushSelectedFeaturesMessage();
105  saveRecent();
106 }
107 
108 void QgsExpressionSelectionDialog::mActionAddToSelection_triggered()
109 {
110  mLayer->selectByExpression( mExpressionBuilder->expressionText(),
112  pushSelectedFeaturesMessage();
113  saveRecent();
114 }
115 
116 void QgsExpressionSelectionDialog::mActionSelectIntersect_triggered()
117 {
118  mLayer->selectByExpression( mExpressionBuilder->expressionText(),
120  pushSelectedFeaturesMessage();
121  saveRecent();
122 }
123 
124 void QgsExpressionSelectionDialog::mActionRemoveFromSelection_triggered()
125 {
126  mLayer->selectByExpression( mExpressionBuilder->expressionText(),
128  pushSelectedFeaturesMessage();
129  saveRecent();
130 }
131 
132 void QgsExpressionSelectionDialog::pushSelectedFeaturesMessage()
133 {
134  if ( !mMessageBar )
135  return;
136 
137  const int timeout = QgsSettings().value( QStringLiteral( "qgis/messageTimeout" ), 5 ).toInt();
138  const int count = mLayer->selectedFeatureCount();
139  if ( count > 0 )
140  {
141  mMessageBar->pushMessage( QString(),
142  tr( "%n matching feature(s) selected", "matching features", count ),
143  Qgis::Info, timeout );
144  }
145  else
146  {
147  mMessageBar->pushMessage( QString(),
148  tr( "No matching features found" ),
149  Qgis::Warning, timeout );
150  }
151 }
152 
153 void QgsExpressionSelectionDialog::mButtonZoomToFeatures_clicked()
154 {
155  if ( mExpressionBuilder->expressionText().isEmpty() || !mMapCanvas )
156  return;
157 
159 
160  QgsFeatureRequest request = QgsFeatureRequest().setFilterExpression( mExpressionBuilder->expressionText() )
161  .setExpressionContext( context )
162  .setNoAttributes();
163 
164  QgsFeatureIterator features = mLayer->getFeatures( request );
165 
166  QgsRectangle bbox;
167  bbox.setMinimal();
168  QgsFeature feat;
169  int featureCount = 0;
170  while ( features.nextFeature( feat ) )
171  {
172  QgsGeometry geom = feat.geometry();
173  if ( geom.isNull() || geom.constGet()->isEmpty() )
174  continue;
175 
176  QgsRectangle r = mMapCanvas->mapSettings().layerExtentToOutputExtent( mLayer, geom.boundingBox() );
177  bbox.combineExtentWith( r );
178  featureCount++;
179  }
180  features.close();
181 
182  QgsSettings settings;
183  int timeout = settings.value( QStringLiteral( "qgis/messageTimeout" ), 5 ).toInt();
184  if ( featureCount > 0 )
185  {
186  mMapCanvas->zoomToFeatureExtent( bbox );
187  if ( mMessageBar )
188  {
189  mMessageBar->pushMessage( QString(),
190  tr( "Zoomed to %n matching feature(s)", "number of matching features", featureCount ),
191  Qgis::Info,
192  timeout );
193  }
194  }
195  else if ( mMessageBar )
196  {
197  mMessageBar->pushMessage( QString(),
198  tr( "No matching features found" ),
199  Qgis::Info,
200  timeout );
201  }
202  saveRecent();
203 }
204 
205 void QgsExpressionSelectionDialog::closeEvent( QCloseEvent *closeEvent )
206 {
207  QDialog::closeEvent( closeEvent );
208 }
209 
210 void QgsExpressionSelectionDialog::mPbnClose_clicked()
211 {
212  close();
213 }
214 
216 {
217  QDialog::done( r );
218  close();
219 }
220 
221 void QgsExpressionSelectionDialog::saveRecent()
222 {
223  mExpressionBuilder->expressionTree()->saveToRecent( mExpressionBuilder->expressionText(), QStringLiteral( "selection" ) );
224 }
225 
226 void QgsExpressionSelectionDialog::showHelp()
227 {
228  QgsHelp::openHelp( QStringLiteral( "introduction/general_tools.html#automatic-selection" ) );
229 }
QgsVectorLayer::getFeatures
QgsFeatureIterator getFeatures(const QgsFeatureRequest &request=QgsFeatureRequest()) const FINAL
Queries the layer for features specified in request.
Definition: qgsvectorlayer.cpp:993
QgsExpressionContext
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
Definition: qgsexpressioncontext.h:370
qgsexpressioncontextutils.h
QgsApplication::getThemeIcon
static QIcon getThemeIcon(const QString &name)
Helper to get a theme icon.
Definition: qgsapplication.cpp:626
QgsRectangle::combineExtentWith
void combineExtentWith(const QgsRectangle &rect)
Expands the rectangle so that it covers both the original rectangle and the given rectangle.
Definition: qgsrectangle.h:359
QgsSettings::value
QVariant value(const QString &key, const QVariant &defaultValue=QVariant(), Section section=NoSection) const
Returns the value for setting key.
Definition: qgssettings.cpp:174
QgsExpressionSelectionDialog::setGeomCalculator
void setGeomCalculator(const QgsDistanceArea &da)
Sets geometry calculator used in distance/area calculations.
Definition: qgsexpressionselectiondialog.cpp:83
qgsmapcanvas.h
QgsExpressionBuilderWidget
A reusable widget that can be used to build a expression string.
Definition: qgsexpressionbuilderwidget.h:43
QgsExpressionSelectionDialog::QgsExpressionSelectionDialog
QgsExpressionSelectionDialog(QgsVectorLayer *layer, const QString &startText=QString(), QWidget *parent=nullptr)
Creates a new selection dialog.
Definition: qgsexpressionselectiondialog.cpp:29
qgsexpression.h
QgsVectorLayer::RemoveFromSelection
@ RemoveFromSelection
Remove from current selection.
Definition: qgsvectorlayer.h:416
QgsMapCanvas::mapSettings
const QgsMapSettings & mapSettings() const
Gets access to properties used for map rendering.
Definition: qgsmapcanvas.cpp:391
QgsGeometry::isNull
Q_GADGET bool isNull
Definition: qgsgeometry.h:126
qgsgui.h
Qgis::Warning
@ Warning
Definition: qgis.h:91
QgsExpressionSelectionDialog::setMapCanvas
void setMapCanvas(QgsMapCanvas *canvas)
Sets a map canvas associated with the dialog.
Definition: qgsexpressionselectiondialog.cpp:94
QgsMapCanvas
Map canvas is a class for displaying all GIS data types on a canvas.
Definition: qgsmapcanvas.h:85
QgsFeature::geometry
QgsGeometry geometry
Definition: qgsfeature.h:67
QgsRectangle::setMinimal
void setMinimal() SIP_HOLDGIL
Set a rectangle so that min corner is at max and max corner is at min.
Definition: qgsrectangle.h:151
QgsMapSettings::layerExtentToOutputExtent
QgsRectangle layerExtentToOutputExtent(const QgsMapLayer *layer, QgsRectangle extent) const
transform bounding box from layer's CRS to output CRS
Definition: qgsmapsettings.cpp:433
QgsVectorLayer::selectByExpression
Q_INVOKABLE void selectByExpression(const QString &expression, QgsVectorLayer::SelectBehavior behavior=QgsVectorLayer::SetSelection)
Selects matching features using an expression.
Definition: qgsvectorlayer.cpp:463
QgsExpressionContextUtils::globalProjectLayerScopes
static QList< QgsExpressionContextScope * > globalProjectLayerScopes(const QgsMapLayer *layer)
Creates a list of three scopes: global, layer's project and layer.
Definition: qgsexpressioncontextutils.cpp:307
QgsSettings
This class is a composition of two QSettings instances:
Definition: qgssettings.h:62
QgsExpressionSelectionDialog::expressionText
QString expressionText()
Returns the current expression text.
Definition: qgsexpressionselectiondialog.cpp:78
QgsRectangle
A rectangle specified with double values.
Definition: qgsrectangle.h:42
QgsFeatureRequest::setExpressionContext
QgsFeatureRequest & setExpressionContext(const QgsExpressionContext &context)
Sets the expression context used to evaluate filter expressions.
Definition: qgsfeaturerequest.cpp:144
QgsFeatureRequest::setFilterExpression
QgsFeatureRequest & setFilterExpression(const QString &expression)
Set the filter expression.
Definition: qgsfeaturerequest.cpp:124
qgsapplication.h
QgsGui::enableAutoGeometryRestore
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:139
QgsFeatureRequest
This class wraps a request for features to a vector layer (or directly its vector data provider).
Definition: qgsfeaturerequest.h:76
QgsVectorLayer::AddToSelection
@ AddToSelection
Add selection to current selection.
Definition: qgsvectorlayer.h:414
Qgis::Info
@ Info
Definition: qgis.h:90
QgsFeatureIterator::close
bool close()
Definition: qgsfeatureiterator.h:387
QgsExpressionSelectionDialog::expressionBuilder
QgsExpressionBuilderWidget * expressionBuilder()
The builder widget that is used by the dialog.
Definition: qgsexpressionselectiondialog.cpp:68
QgsMessageBar
A bar for displaying non-blocking messages to the user.
Definition: qgsmessagebar.h:61
qgsmessagebar.h
qgsexpressionselectiondialog.h
QgsFeatureRequest::setNoAttributes
QgsFeatureRequest & setNoAttributes()
Set that no attributes will be fetched.
Definition: qgsfeaturerequest.cpp:192
QgsAbstractGeometry::isEmpty
virtual bool isEmpty() const
Returns true if the geometry is empty.
Definition: qgsabstractgeometry.cpp:298
QgsGeometry::constGet
const QgsAbstractGeometry * constGet() const SIP_HOLDGIL
Returns a non-modifiable (const) reference to the underlying abstract geometry primitive.
Definition: qgsgeometry.cpp:128
QgsExpressionSelectionDialog::closeEvent
void closeEvent(QCloseEvent *closeEvent) override
Implementation for closeEvent Saves the window geometry.
Definition: qgsexpressionselectiondialog.cpp:205
QgsMapCanvas::zoomToFeatureExtent
void zoomToFeatureExtent(QgsRectangle &rect)
Zooms to feature extent.
Definition: qgsmapcanvas.cpp:1319
qgsvectorlayer.h
QgsVectorLayer::SetSelection
@ SetSelection
Set selection, removing any existing selection.
Definition: qgsvectorlayer.h:413
qgsgeometry.h
QgsVectorLayer::IntersectSelection
@ IntersectSelection
Modify current selection to include only select features which match.
Definition: qgsvectorlayer.h:415
QgsFeatureIterator::nextFeature
bool nextFeature(QgsFeature &f)
Definition: qgsfeatureiterator.h:374
QgsGeometry
A geometry is the spatial representation of a feature.
Definition: qgsgeometry.h:124
QgsHelp::openHelp
static void openHelp(const QString &key)
Opens help topic for the given help key using default system web browser.
Definition: qgshelp.cpp:36
QgsExpressionSelectionDialog::setExpressionText
void setExpressionText(const QString &text)
Sets the current expression text.
Definition: qgsexpressionselectiondialog.cpp:73
QgsVectorLayer
Represents a vector layer which manages a vector based data sets.
Definition: qgsvectorlayer.h:387
QgsExpressionSelectionDialog::done
void done(int r) override
Implementation for done (default behavior when pressing esc) Calls close, so the window geometry gets...
Definition: qgsexpressionselectiondialog.cpp:215
qgssettings.h
QgsExpressionSelectionDialog::setMessageBar
void setMessageBar(QgsMessageBar *messageBar)
Sets the message bar to display feedback from the dialog.
Definition: qgsexpressionselectiondialog.cpp:89
QgsMapLayer::name
QString name
Definition: qgsmaplayer.h:86
QgsGeometry::boundingBox
QgsRectangle boundingBox() const
Returns the bounding box of the geometry.
Definition: qgsgeometry.cpp:996
QgsDistanceArea
A general purpose distance and area calculator, capable of performing ellipsoid based calculations.
Definition: qgsdistancearea.h:50
QgsFeature
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
Definition: qgsfeature.h:56
QgsVectorLayer::selectedFeatureCount
int selectedFeatureCount() const
Returns the number of features that are selected in this layer.
Definition: qgsvectorlayer.cpp:3436
QgsFeatureIterator
Wrapper for iterator of features from vector data provider or vector layer.
Definition: qgsfeatureiterator.h:265
QgsMessageBar::pushMessage
void pushMessage(const QString &text, Qgis::MessageLevel level=Qgis::Info, int duration=5)
A convenience method for pushing a message with the specified text to the bar.
Definition: qgsmessagebar.cpp:379