QGIS API Documentation  3.0.2-Girona (307d082)
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 "qgsmessagebar.h"
22 #include "qgsvectorlayer.h"
23 #include "qgssettings.h"
24 #include "qgsgui.h"
25 
26 
27 QgsExpressionSelectionDialog::QgsExpressionSelectionDialog( QgsVectorLayer *layer, const QString &startText, QWidget *parent )
28  : QDialog( parent )
29  , mLayer( layer )
30 
31 {
32  setupUi( this );
33 
35 
36  connect( mActionSelect, &QAction::triggered, this, &QgsExpressionSelectionDialog::mActionSelect_triggered );
37  connect( mActionAddToSelection, &QAction::triggered, this, &QgsExpressionSelectionDialog::mActionAddToSelection_triggered );
38  connect( mActionRemoveFromSelection, &QAction::triggered, this, &QgsExpressionSelectionDialog::mActionRemoveFromSelection_triggered );
39  connect( mActionSelectIntersect, &QAction::triggered, this, &QgsExpressionSelectionDialog::mActionSelectIntersect_triggered );
40  connect( mButtonZoomToFeatures, &QToolButton::clicked, this, &QgsExpressionSelectionDialog::mButtonZoomToFeatures_clicked );
41  connect( mPbnClose, &QPushButton::clicked, this, &QgsExpressionSelectionDialog::mPbnClose_clicked );
42 
43  setWindowTitle( QStringLiteral( "Select by Expression - %1" ).arg( layer->name() ) );
44 
45  mActionSelect->setIcon( QgsApplication::getThemeIcon( QStringLiteral( "/mIconExpressionSelect.svg" ) ) );
46  mActionAddToSelection->setIcon( QgsApplication::getThemeIcon( QStringLiteral( "/mIconSelectAdd.svg" ) ) );
47  mActionRemoveFromSelection->setIcon( QgsApplication::getThemeIcon( QStringLiteral( "/mIconSelectRemove.svg" ) ) );
48  mActionSelectIntersect->setIcon( QgsApplication::getThemeIcon( QStringLiteral( "/mIconSelectIntersect.svg" ) ) );
49 
50  mButtonSelect->addAction( mActionSelect );
51  mButtonSelect->addAction( mActionAddToSelection );
52  mButtonSelect->addAction( mActionRemoveFromSelection );
53  mButtonSelect->addAction( mActionSelectIntersect );
54  mButtonSelect->setDefaultAction( mActionSelect );
55 
56  mExpressionBuilder->setLayer( layer );
57  mExpressionBuilder->setExpressionText( startText );
58  mExpressionBuilder->loadFieldNames();
59  mExpressionBuilder->loadRecent( QStringLiteral( "Selection" ) );
60 
62  mExpressionBuilder->setExpressionContext( context );
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 
71 {
72  return mExpressionBuilder;
73 }
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 
102 void QgsExpressionSelectionDialog::mActionSelect_triggered()
103 {
104  mLayer->selectByExpression( mExpressionBuilder->expressionText(),
106  saveRecent();
107 }
108 
109 void QgsExpressionSelectionDialog::mActionAddToSelection_triggered()
110 {
111  mLayer->selectByExpression( mExpressionBuilder->expressionText(),
113  saveRecent();
114 }
115 
116 void QgsExpressionSelectionDialog::mActionSelectIntersect_triggered()
117 {
118  mLayer->selectByExpression( mExpressionBuilder->expressionText(),
120  saveRecent();
121 }
122 
123 void QgsExpressionSelectionDialog::mActionRemoveFromSelection_triggered()
124 {
125  mLayer->selectByExpression( mExpressionBuilder->expressionText(),
127  saveRecent();
128 }
129 
130 void QgsExpressionSelectionDialog::mButtonZoomToFeatures_clicked()
131 {
132  if ( mExpressionBuilder->expressionText().isEmpty() || !mMapCanvas )
133  return;
134 
136 
137  QgsFeatureRequest request = QgsFeatureRequest().setFilterExpression( mExpressionBuilder->expressionText() )
138  .setExpressionContext( context )
140 
141  QgsFeatureIterator features = mLayer->getFeatures( request );
142 
143  QgsRectangle bbox;
144  bbox.setMinimal();
145  QgsFeature feat;
146  int featureCount = 0;
147  while ( features.nextFeature( feat ) )
148  {
149  QgsGeometry geom = feat.geometry();
150  if ( geom.isNull() || geom.constGet()->isEmpty() )
151  continue;
152 
153  QgsRectangle r = mMapCanvas->mapSettings().layerExtentToOutputExtent( mLayer, geom.boundingBox() );
154  bbox.combineExtentWith( r );
155  featureCount++;
156  }
157  features.close();
158 
159  QgsSettings settings;
160  int timeout = settings.value( QStringLiteral( "qgis/messageTimeout" ), 5 ).toInt();
161  if ( featureCount > 0 )
162  {
163  mMapCanvas->zoomToFeatureExtent( bbox );
164  if ( mMessageBar )
165  {
166  mMessageBar->pushMessage( QString(),
167  tr( "Zoomed to %n matching feature(s)", "number of matching features", featureCount ),
168  Qgis::Info,
169  timeout );
170  }
171  }
172  else if ( mMessageBar )
173  {
174  mMessageBar->pushMessage( QString(),
175  tr( "No matching features found" ),
176  Qgis::Info,
177  timeout );
178  }
179  saveRecent();
180 }
181 
183 {
184  QDialog::closeEvent( closeEvent );
185 }
186 
187 void QgsExpressionSelectionDialog::mPbnClose_clicked()
188 {
189  close();
190 }
191 
193 {
194  QDialog::done( r );
195  close();
196 }
197 
198 void QgsExpressionSelectionDialog::saveRecent()
199 {
200  mExpressionBuilder->saveToRecent( QStringLiteral( "Selection" ) );
201 }
202 
203 void QgsExpressionSelectionDialog::showHelp()
204 {
205  QgsHelp::openHelp( QStringLiteral( "introduction/general_tools.html#automatic-selection" ) );
206 }
Wrapper for iterator of features from vector data provider or vector layer.
A rectangle specified with double values.
Definition: qgsrectangle.h:39
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.
bool isNull() const
Returns true if the geometry is null (ie, contains no underlying geometry accessible via geometry() )...
This class is a composition of two QSettings instances:
Definition: qgssettings.h:57
QString expressionText()
Returns the current expression text.
void zoomToFeatureExtent(QgsRectangle &rect)
Zooms to feature extent.
Remove from current selection.
QgsFeatureRequest & setSubsetOfAttributes(const QgsAttributeList &attrs)
Set a subset of attributes that will be fetched.
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:111
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:62
static QgsGui * instance()
Returns a pointer to the singleton instance.
Definition: qgsgui.cpp:35
Map canvas is a class for displaying all GIS data types on a canvas.
Definition: qgsmapcanvas.h:74
QgsFeatureRequest & setFilterExpression(const QString &expression)
Set the filter expression.
void setGeomCalculator(const QgsDistanceArea &da)
Sets geometry calculator used in distance/area calculations.
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
QgsGeometry geometry() const
Returns the geometry associated with this feature.
Definition: qgsfeature.cpp:101
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.
QgsFeatureIterator getFeatures(const QgsFeatureRequest &request=QgsFeatureRequest()) const override
Query the layer for features specified in request.
const QgsAbstractGeometry * constGet() const
Returns a non-modifiable (const) reference to the underlying abstract geometry primitive.
const QgsMapSettings & mapSettings() const
Get access to properties used for map rendering.
void combineExtentWith(const QgsRectangle &rect)
Expand the rectangle so that covers both the original rectangle and the given rectangle.
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.
void selectByExpression(const QString &expression, SelectBehavior behavior=SetSelection)
Select matching features using an expression.
QVariant value(const QString &key, const QVariant &defaultValue=QVariant(), const Section section=NoSection) const
Returns the value for setting key.
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:76
static void openHelp(const QString &key)
Opens help topic for the given help key using default system web browser.
Definition: qgshelp.cpp:35
QString name
Definition: qgsmaplayer.h:60
QList< int > QgsAttributeList
Definition: qgsfield.h:27
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.