QGIS API Documentation 3.41.0-Master (3440c17df1d)
Loading...
Searching...
No Matches
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#include "moc_qgsexpressionselectiondialog.cpp"
18
19#include "qgsapplication.h"
20#include "qgsexpression.h"
21#include "qgsgeometry.h"
22#include "qgsmapcanvas.h"
23#include "qgsmessagebar.h"
24#include "qgsvectorlayer.h"
25#include "qgssettings.h"
26#include "qgsgui.h"
28
29
30QgsExpressionSelectionDialog::QgsExpressionSelectionDialog( QgsVectorLayer *layer, const QString &startText, QWidget *parent )
31 : QDialog( parent )
32 , mLayer( layer )
33
34{
35 setupUi( this );
36
38
39 connect( mActionSelect, &QAction::triggered, this, &QgsExpressionSelectionDialog::mActionSelect_triggered );
40 connect( mActionAddToSelection, &QAction::triggered, this, &QgsExpressionSelectionDialog::mActionAddToSelection_triggered );
41 connect( mActionRemoveFromSelection, &QAction::triggered, this, &QgsExpressionSelectionDialog::mActionRemoveFromSelection_triggered );
42 connect( mActionSelectIntersect, &QAction::triggered, this, &QgsExpressionSelectionDialog::mActionSelectIntersect_triggered );
43 connect( mButtonZoomToFeatures, &QToolButton::clicked, this, &QgsExpressionSelectionDialog::mButtonZoomToFeatures_clicked );
44 connect( mPbnClose, &QPushButton::clicked, this, &QgsExpressionSelectionDialog::mPbnClose_clicked );
45 connect( mLayer, &QgsVectorLayer::willBeDeleted, this, &QgsExpressionSelectionDialog::close );
46
47 setWindowTitle( tr( "%1 — Select by Expression" ).arg( layer->name() ) );
48
49 mActionSelect->setIcon( QgsApplication::getThemeIcon( QStringLiteral( "/mIconExpressionSelect.svg" ) ) );
50 mActionAddToSelection->setIcon( QgsApplication::getThemeIcon( QStringLiteral( "/mIconSelectAdd.svg" ) ) );
51 mActionRemoveFromSelection->setIcon( QgsApplication::getThemeIcon( QStringLiteral( "/mIconSelectRemove.svg" ) ) );
52 mActionSelectIntersect->setIcon( QgsApplication::getThemeIcon( QStringLiteral( "/mIconSelectIntersect.svg" ) ) );
53
54 mButtonSelect->addAction( mActionSelect );
55 mButtonSelect->addAction( mActionAddToSelection );
56 mButtonSelect->addAction( mActionRemoveFromSelection );
57 mButtonSelect->addAction( mActionSelectIntersect );
58 mButtonSelect->setDefaultAction( mActionSelect );
59
61 mExpressionBuilder->initWithLayer( layer, context, QStringLiteral( "selection" ) );
62 mExpressionBuilder->setExpressionText( startText );
63
64 // by default, zoom to features is hidden, shown only if canvas is set
65 mButtonZoomToFeatures->setVisible( false );
66
67 connect( buttonBox, &QDialogButtonBox::helpRequested, this, &QgsExpressionSelectionDialog::showHelp );
68}
69
74
76{
77 mExpressionBuilder->setExpressionText( text );
78}
79
81{
82 return mExpressionBuilder->expressionText();
83}
84
86{
87 // Store in child widget only.
88 mExpressionBuilder->setGeomCalculator( da );
89}
90
92{
93 mMessageBar = messageBar;
94}
95
97{
98 mMapCanvas = canvas;
99 mButtonZoomToFeatures->setVisible( true );
100}
101
102void QgsExpressionSelectionDialog::mActionSelect_triggered()
103{
104 mLayer->selectByExpression( mExpressionBuilder->expressionText(),
106 pushSelectedFeaturesMessage();
107 saveRecent();
108}
109
110void QgsExpressionSelectionDialog::mActionAddToSelection_triggered()
111{
112 mLayer->selectByExpression( mExpressionBuilder->expressionText(),
114 pushSelectedFeaturesMessage();
115 saveRecent();
116}
117
118void QgsExpressionSelectionDialog::mActionSelectIntersect_triggered()
119{
120 mLayer->selectByExpression( mExpressionBuilder->expressionText(),
122 pushSelectedFeaturesMessage();
123 saveRecent();
124}
125
126void QgsExpressionSelectionDialog::mActionRemoveFromSelection_triggered()
127{
128 mLayer->selectByExpression( mExpressionBuilder->expressionText(),
130 pushSelectedFeaturesMessage();
131 saveRecent();
132}
133
134void QgsExpressionSelectionDialog::pushSelectedFeaturesMessage()
135{
136 if ( !mMessageBar )
137 return;
138
139 const int count = mLayer->selectedFeatureCount();
140 if ( count > 0 )
141 {
142 mMessageBar->pushMessage( QString(),
143 tr( "%n matching feature(s) selected", "matching features", count ),
145 }
146 else
147 {
148 mMessageBar->pushMessage( QString(),
149 tr( "No matching features found" ),
151 }
152}
153
154void QgsExpressionSelectionDialog::mButtonZoomToFeatures_clicked()
155{
156 if ( mExpressionBuilder->expressionText().isEmpty() || !mMapCanvas )
157 return;
158
160
161 const QgsFeatureRequest request = QgsFeatureRequest().setFilterExpression( mExpressionBuilder->expressionText() )
162 .setExpressionContext( context )
164
165 QgsFeatureIterator features = mLayer->getFeatures( request );
166
167 QgsRectangle bbox;
168 bbox.setNull();
169 QgsFeature feat;
170 int featureCount = 0;
171 while ( features.nextFeature( feat ) )
172 {
173 const QgsGeometry geom = feat.geometry();
174 if ( geom.isNull() || geom.constGet()->isEmpty() )
175 continue;
176
177 const QgsRectangle r = mMapCanvas->mapSettings().layerExtentToOutputExtent( mLayer, geom.boundingBox() );
178 bbox.combineExtentWith( r );
179 featureCount++;
180 }
181 features.close();
182
183 if ( featureCount > 0 )
184 {
185 mMapCanvas->zoomToFeatureExtent( bbox );
186 if ( mMessageBar )
187 {
188 mMessageBar->pushMessage( QString(),
189 tr( "Zoomed to %n matching feature(s)", "number of matching features", featureCount ),
191 }
192 }
193 else if ( mMessageBar )
194 {
195 mMessageBar->pushMessage( QString(),
196 tr( "No matching features found" ),
198 }
199 saveRecent();
200}
201
202void QgsExpressionSelectionDialog::closeEvent( QCloseEvent *closeEvent )
203{
204 QDialog::closeEvent( closeEvent );
205}
206
207void QgsExpressionSelectionDialog::mPbnClose_clicked()
208{
209 close();
210}
211
213{
214 QDialog::done( r );
215 close();
216}
217
218void QgsExpressionSelectionDialog::saveRecent()
219{
220 mExpressionBuilder->expressionTree()->saveToRecent( mExpressionBuilder->expressionText(), QStringLiteral( "selection" ) );
221}
222
223void QgsExpressionSelectionDialog::showHelp()
224{
225 QgsHelp::openHelp( QStringLiteral( "introduction/general_tools.html#automatic-selection" ) );
226}
@ Info
Information message.
Definition qgis.h:155
@ SetSelection
Set selection, removing any existing selection.
@ AddToSelection
Add selection to current selection.
@ IntersectSelection
Modify current selection to include only select features which match.
@ RemoveFromSelection
Remove from current selection.
virtual bool isEmpty() const
Returns true if the geometry is empty.
static QIcon getThemeIcon(const QString &name, const QColor &fillColor=QColor(), const QColor &strokeColor=QColor())
Helper to get a theme icon.
A general purpose distance and area calculator, capable of performing ellipsoid based calculations.
A reusable widget that can be used to build a expression string.
void setExpressionText(const QString &expression)
Sets the expression string for the widget.
static QList< QgsExpressionContextScope * > globalProjectLayerScopes(const QgsMapLayer *layer)
Creates a list of three scopes: global, layer's project and layer.
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
QgsExpressionBuilderWidget * expressionBuilder()
The builder widget that is used by the dialog.
QString expressionText()
Returns the current expression text.
void closeEvent(QCloseEvent *closeEvent) override
Implementation for closeEvent Saves the window geometry.
void setExpressionText(const QString &text)
Sets the current expression text.
QgsExpressionSelectionDialog(QgsVectorLayer *layer, const QString &startText=QString(), QWidget *parent=nullptr)
Creates a new selection dialog.
void setMapCanvas(QgsMapCanvas *canvas)
Sets a map canvas associated with the dialog.
void setMessageBar(QgsMessageBar *messageBar)
Sets the message bar to display feedback from the dialog.
void setGeomCalculator(const QgsDistanceArea &da)
Sets geometry calculator used in distance/area calculations.
void done(int r) override
Implementation for done (default behavior when pressing esc) Calls close, so the window geometry gets...
Wrapper for iterator of features from vector data provider or vector layer.
bool nextFeature(QgsFeature &f)
Fetch next feature and stores in f, returns true on success.
bool close()
Call to end the iteration.
This class wraps a request for features to a vector layer (or directly its vector data provider).
QgsFeatureRequest & setFilterExpression(const QString &expression)
Set the filter expression.
QgsFeatureRequest & setExpressionContext(const QgsExpressionContext &context)
Sets the expression context used to evaluate filter expressions.
QgsFeatureRequest & setNoAttributes()
Set that no attributes will be fetched.
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
Definition qgsfeature.h:58
QgsGeometry geometry
Definition qgsfeature.h:69
A geometry is the spatial representation of a feature.
const QgsAbstractGeometry * constGet() const
Returns a non-modifiable (const) reference to the underlying abstract geometry primitive.
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:209
static void openHelp(const QString &key)
Opens help topic for the given help key using default system web browser.
Definition qgshelp.cpp:39
Map canvas is a class for displaying all GIS data types on a canvas.
void zoomToFeatureExtent(QgsRectangle &rect)
Zooms to feature extent.
const QgsMapSettings & mapSettings() const
Gets access to properties used for map rendering.
QString name
Definition qgsmaplayer.h:80
void willBeDeleted()
Emitted in the destructor when the layer is about to be deleted, but it is still in a perfectly valid...
QgsRectangle layerExtentToOutputExtent(const QgsMapLayer *layer, QgsRectangle extent) const
transform bounding box from layer's CRS to output CRS
A bar for displaying non-blocking messages to the user.
void pushMessage(const QString &text, Qgis::MessageLevel level=Qgis::MessageLevel::Info, int duration=-1)
A convenience method for pushing a message with the specified text to the bar.
A rectangle specified with double values.
void combineExtentWith(const QgsRectangle &rect)
Expands the rectangle so that it covers both the original rectangle and the given rectangle.
void setNull()
Mark a rectangle as being null (holding no spatial information).
Represents a vector layer which manages a vector based data sets.
QgsFeatureIterator getFeatures(const QgsFeatureRequest &request=QgsFeatureRequest()) const FINAL
Queries the layer for features specified in request.
int selectedFeatureCount() const
Returns the number of features that are selected in this layer.
Q_INVOKABLE void selectByExpression(const QString &expression, Qgis::SelectBehavior behavior=Qgis::SelectBehavior::SetSelection, QgsExpressionContext *context=nullptr)
Selects matching features using an expression.