QGIS API Documentation 3.99.0-Master (e9821da5c6b)
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 <QString>
29
30#include "moc_qgsexpressionselectiondialog.cpp"
31
32using namespace Qt::StringLiterals;
33
34QgsExpressionSelectionDialog::QgsExpressionSelectionDialog( QgsVectorLayer *layer, const QString &startText, QWidget *parent )
35 : QDialog( parent )
36 , mLayer( layer )
37
38{
39 setupUi( this );
40
42
43 connect( mActionSelect, &QAction::triggered, this, &QgsExpressionSelectionDialog::mActionSelect_triggered );
44 connect( mActionAddToSelection, &QAction::triggered, this, &QgsExpressionSelectionDialog::mActionAddToSelection_triggered );
45 connect( mActionRemoveFromSelection, &QAction::triggered, this, &QgsExpressionSelectionDialog::mActionRemoveFromSelection_triggered );
46 connect( mActionSelectIntersect, &QAction::triggered, this, &QgsExpressionSelectionDialog::mActionSelectIntersect_triggered );
47 connect( mButtonZoomToFeatures, &QToolButton::clicked, this, &QgsExpressionSelectionDialog::mButtonZoomToFeatures_clicked );
48 connect( mPbnClose, &QPushButton::clicked, this, &QgsExpressionSelectionDialog::mPbnClose_clicked );
49 connect( mLayer, &QgsVectorLayer::willBeDeleted, this, &QgsExpressionSelectionDialog::close );
50
51 setWindowTitle( tr( "%1 — Select by Expression" ).arg( layer->name() ) );
52
53 mActionSelect->setIcon( QgsApplication::getThemeIcon( u"/mIconExpressionSelect.svg"_s ) );
54 mActionAddToSelection->setIcon( QgsApplication::getThemeIcon( u"/mIconSelectAdd.svg"_s ) );
55 mActionRemoveFromSelection->setIcon( QgsApplication::getThemeIcon( u"/mIconSelectRemove.svg"_s ) );
56 mActionSelectIntersect->setIcon( QgsApplication::getThemeIcon( u"/mIconSelectIntersect.svg"_s ) );
57
58 mButtonSelect->addAction( mActionSelect );
59 mButtonSelect->addAction( mActionAddToSelection );
60 mButtonSelect->addAction( mActionRemoveFromSelection );
61 mButtonSelect->addAction( mActionSelectIntersect );
62 mButtonSelect->setDefaultAction( mActionSelect );
63
65 mExpressionBuilder->initWithLayer( layer, context, u"selection"_s );
66 mExpressionBuilder->setExpressionText( startText );
67
68 // by default, zoom to features is hidden, shown only if canvas is set
69 mButtonZoomToFeatures->setVisible( false );
70
71 connect( buttonBox, &QDialogButtonBox::helpRequested, this, &QgsExpressionSelectionDialog::showHelp );
72}
73
78
80{
81 mExpressionBuilder->setExpressionText( text );
82}
83
85{
86 return mExpressionBuilder->expressionText();
87}
88
90{
91 // Store in child widget only.
92 mExpressionBuilder->setGeomCalculator( da );
93}
94
96{
97 mMessageBar = messageBar;
98}
99
101{
102 mMapCanvas = canvas;
103 mButtonZoomToFeatures->setVisible( true );
104}
105
106void QgsExpressionSelectionDialog::mActionSelect_triggered()
107{
108 mLayer->selectByExpression( mExpressionBuilder->expressionText(), Qgis::SelectBehavior::SetSelection );
109 pushSelectedFeaturesMessage();
110 saveRecent();
111}
112
113void QgsExpressionSelectionDialog::mActionAddToSelection_triggered()
114{
115 mLayer->selectByExpression( mExpressionBuilder->expressionText(), Qgis::SelectBehavior::AddToSelection );
116 pushSelectedFeaturesMessage();
117 saveRecent();
118}
119
120void QgsExpressionSelectionDialog::mActionSelectIntersect_triggered()
121{
122 mLayer->selectByExpression( mExpressionBuilder->expressionText(), Qgis::SelectBehavior::IntersectSelection );
123 pushSelectedFeaturesMessage();
124 saveRecent();
125}
126
127void QgsExpressionSelectionDialog::mActionRemoveFromSelection_triggered()
128{
129 mLayer->selectByExpression( mExpressionBuilder->expressionText(), Qgis::SelectBehavior::RemoveFromSelection );
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(), tr( "%n matching feature(s) selected", "matching features", count ), Qgis::MessageLevel::Info );
143 }
144 else
145 {
146 mMessageBar->pushMessage( QString(), tr( "No matching features found" ), Qgis::MessageLevel::Info );
147 }
148}
149
150void QgsExpressionSelectionDialog::mButtonZoomToFeatures_clicked()
151{
152 if ( mExpressionBuilder->expressionText().isEmpty() || !mMapCanvas )
153 return;
154
155 const QgsExpressionContext context( QgsExpressionContextUtils::globalProjectLayerScopes( mLayer ) );
156
157 const QgsFeatureRequest request = QgsFeatureRequest().setFilterExpression( mExpressionBuilder->expressionText() ).setExpressionContext( context ).setNoAttributes();
158
159 QgsFeatureIterator features = mLayer->getFeatures( request );
160
161 QgsRectangle bbox;
162 bbox.setNull();
163 QgsFeature feat;
164 int featureCount = 0;
165 while ( features.nextFeature( feat ) )
166 {
167 const QgsGeometry geom = feat.geometry();
168 if ( geom.isNull() || geom.constGet()->isEmpty() )
169 continue;
170
171 const QgsRectangle r = mMapCanvas->mapSettings().layerExtentToOutputExtent( mLayer, geom.boundingBox() );
172 bbox.combineExtentWith( r );
173 featureCount++;
174 }
175 features.close();
176
177 if ( featureCount > 0 )
178 {
179 mMapCanvas->zoomToFeatureExtent( bbox );
180 if ( mMessageBar )
181 {
182 mMessageBar->pushMessage( QString(), tr( "Zoomed to %n matching feature(s)", "number of matching features", featureCount ), Qgis::MessageLevel::Info );
183 }
184 }
185 else if ( mMessageBar )
186 {
187 mMessageBar->pushMessage( QString(), tr( "No matching features found" ), Qgis::MessageLevel::Info );
188 }
189 saveRecent();
190}
191
193{
194 QDialog::closeEvent( closeEvent );
195}
196
197void QgsExpressionSelectionDialog::mPbnClose_clicked()
198{
199 close();
200}
201
203{
204 QDialog::done( r );
205 close();
206}
207
208void QgsExpressionSelectionDialog::saveRecent()
209{
210 mExpressionBuilder->expressionTree()->saveToRecent( mExpressionBuilder->expressionText(), u"selection"_s );
211}
212
213void QgsExpressionSelectionDialog::showHelp()
214{
215 QgsHelp::openHelp( u"introduction/general_tools.html#automatic-selection"_s );
216}
@ Info
Information message.
Definition qgis.h:160
@ SetSelection
Set selection, removing any existing selection.
Definition qgis.h:1830
@ AddToSelection
Add selection to current selection.
Definition qgis.h:1831
@ IntersectSelection
Modify current selection to include only select features which match.
Definition qgis.h:1832
@ RemoveFromSelection
Remove from current selection.
Definition qgis.h:1833
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:71
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:224
static void openHelp(const QString &key)
Opens help topic for the given help key using default system web browser.
Definition qgshelp.cpp:41
Map canvas is a class for displaying all GIS data types on a canvas.
QString name
Definition qgsmaplayer.h:87
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.