QGIS API Documentation 3.99.0-Master (26c88405ac0)
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
18#include "qgsapplication.h"
19#include "qgsexpression.h"
21#include "qgsgeometry.h"
22#include "qgsgui.h"
23#include "qgsmapcanvas.h"
24#include "qgsmessagebar.h"
25#include "qgssettings.h"
26#include "qgsvectorlayer.h"
27
28#include "moc_qgsexpressionselectiondialog.cpp"
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(), Qgis::SelectBehavior::SetSelection );
105 pushSelectedFeaturesMessage();
106 saveRecent();
107}
108
109void QgsExpressionSelectionDialog::mActionAddToSelection_triggered()
110{
111 mLayer->selectByExpression( mExpressionBuilder->expressionText(), Qgis::SelectBehavior::AddToSelection );
112 pushSelectedFeaturesMessage();
113 saveRecent();
114}
115
116void QgsExpressionSelectionDialog::mActionSelectIntersect_triggered()
117{
118 mLayer->selectByExpression( mExpressionBuilder->expressionText(), Qgis::SelectBehavior::IntersectSelection );
119 pushSelectedFeaturesMessage();
120 saveRecent();
121}
122
123void QgsExpressionSelectionDialog::mActionRemoveFromSelection_triggered()
124{
125 mLayer->selectByExpression( mExpressionBuilder->expressionText(), Qgis::SelectBehavior::RemoveFromSelection );
126 pushSelectedFeaturesMessage();
127 saveRecent();
128}
129
130void QgsExpressionSelectionDialog::pushSelectedFeaturesMessage()
131{
132 if ( !mMessageBar )
133 return;
134
135 const int count = mLayer->selectedFeatureCount();
136 if ( count > 0 )
137 {
138 mMessageBar->pushMessage( QString(), tr( "%n matching feature(s) selected", "matching features", count ), Qgis::MessageLevel::Info );
139 }
140 else
141 {
142 mMessageBar->pushMessage( QString(), tr( "No matching features found" ), Qgis::MessageLevel::Info );
143 }
144}
145
146void QgsExpressionSelectionDialog::mButtonZoomToFeatures_clicked()
147{
148 if ( mExpressionBuilder->expressionText().isEmpty() || !mMapCanvas )
149 return;
150
151 const QgsExpressionContext context( QgsExpressionContextUtils::globalProjectLayerScopes( mLayer ) );
152
153 const QgsFeatureRequest request = QgsFeatureRequest().setFilterExpression( mExpressionBuilder->expressionText() ).setExpressionContext( context ).setNoAttributes();
154
155 QgsFeatureIterator features = mLayer->getFeatures( request );
156
157 QgsRectangle bbox;
158 bbox.setNull();
159 QgsFeature feat;
160 int featureCount = 0;
161 while ( features.nextFeature( feat ) )
162 {
163 const QgsGeometry geom = feat.geometry();
164 if ( geom.isNull() || geom.constGet()->isEmpty() )
165 continue;
166
167 const QgsRectangle r = mMapCanvas->mapSettings().layerExtentToOutputExtent( mLayer, geom.boundingBox() );
168 bbox.combineExtentWith( r );
169 featureCount++;
170 }
171 features.close();
172
173 if ( featureCount > 0 )
174 {
175 mMapCanvas->zoomToFeatureExtent( bbox );
176 if ( mMessageBar )
177 {
178 mMessageBar->pushMessage( QString(), tr( "Zoomed to %n matching feature(s)", "number of matching features", featureCount ), Qgis::MessageLevel::Info );
179 }
180 }
181 else if ( mMessageBar )
182 {
183 mMessageBar->pushMessage( QString(), tr( "No matching features found" ), Qgis::MessageLevel::Info );
184 }
185 saveRecent();
186}
187
189{
190 QDialog::closeEvent( closeEvent );
191}
192
193void QgsExpressionSelectionDialog::mPbnClose_clicked()
194{
195 close();
196}
197
199{
200 QDialog::done( r );
201 close();
202}
203
204void QgsExpressionSelectionDialog::saveRecent()
205{
206 mExpressionBuilder->expressionTree()->saveToRecent( mExpressionBuilder->expressionText(), QStringLiteral( "selection" ) );
207}
208
209void QgsExpressionSelectionDialog::showHelp()
210{
211 QgsHelp::openHelp( QStringLiteral( "introduction/general_tools.html#automatic-selection" ) );
212}
@ Info
Information message.
Definition qgis.h:157
@ SetSelection
Set selection, removing any existing selection.
Definition qgis.h:1772
@ AddToSelection
Add selection to current selection.
Definition qgis.h:1773
@ IntersectSelection
Modify current selection to include only select features which match.
Definition qgis.h:1774
@ RemoveFromSelection
Remove from current selection.
Definition qgis.h:1775
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 an 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...
bool nextFeature(QgsFeature &f)
Fetch next feature and stores in f, returns true on success.
bool close()
Call to end the iteration.
QgsGeometry geometry
Definition qgsfeature.h:69
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:221
static void openHelp(const QString &key)
Opens help topic for the given help key using default system web browser.
Definition qgshelp.cpp:38
Map canvas is a class for displaying all GIS data types on a canvas.
QString name
Definition qgsmaplayer.h:84
void willBeDeleted()
Emitted in the destructor when the layer is about to be deleted, but it is still in a perfectly valid...
A bar for displaying non-blocking messages to the user.
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 dataset.
Q_INVOKABLE void selectByExpression(const QString &expression, Qgis::SelectBehavior behavior=Qgis::SelectBehavior::SetSelection, QgsExpressionContext *context=nullptr)
Selects matching features using an expression.