[FEATURE] Add zoom to features shortcut in select by form dialog

Allows very quick navigation to features which match the criteria
in the form
This commit is contained in:
Nyall Dawson 2016-12-15 20:47:58 +10:00
parent 0bfc9bb6e3
commit 80b757b6db
8 changed files with 114 additions and 8 deletions

View File

@ -164,6 +164,12 @@ class QgsAttributeForm : QWidget
*/
void closed();
/**
* Emitted when the user chooses to zoom to a filtered set of features.
* @note added in QGIS 3.0
*/
void zoomToFeatures( const QString& filter );
public slots:
/**
* Call this to change the content of a given attribute. Will update the editor(s) related to this field.

View File

@ -231,6 +231,10 @@ class QgsMapCanvas : QGraphicsView
//! Zooms in/out with a given center
void zoomWithCenter( int x, int y, bool zoomIn );
//! Zooms to feature extent. Adds a small margin around the extent
//! and does a pan if rect is empty (point extent)
void zoomToFeatureExtent( QgsRectangle& rect );
//! Returns whether the scale is locked, so zooming can be performed using magnication.
//! @note added in 2.16
//! @see setScaleLocked()
@ -518,10 +522,6 @@ class QgsMapCanvas : QGraphicsView
//! called when panning is in action, reset indicates end of panning
void moveCanvasContents( bool reset = false );
//! Zooms to feature extent. Adds a small margin around the extent
//! and does a pan if rect is empty (point extent)
void zoomToFeatureExtent( QgsRectangle& rect );
//! called on resize or changed extent to notify canvas items to change their rectangle
void updateCanvasItemPositions();

View File

@ -7520,6 +7520,7 @@ void QgisApp::selectByForm()
QgsSelectByFormDialog* dlg = new QgsSelectByFormDialog( vlayer, context, this );
dlg->setMessageBar( messageBar() );
dlg->setMapCanvas( mapCanvas() );
dlg->setAttribute( Qt::WA_DeleteOnClose );
dlg->show();
}

View File

@ -15,11 +15,14 @@
#include "qgsselectbyformdialog.h"
#include "qgsattributeform.h"
#include "qgsmapcanvas.h"
#include <QLayout>
#include <QSettings>
QgsSelectByFormDialog::QgsSelectByFormDialog( QgsVectorLayer* layer, const QgsAttributeEditorContext& context, QWidget* parent, Qt::WindowFlags fl )
: QDialog( parent, fl )
, mLayer( layer )
, mMessageBar( nullptr )
{
QgsAttributeEditorContext dlgContext = context;
dlgContext.setFormMode( QgsAttributeEditorContext::StandaloneDialog );
@ -51,5 +54,66 @@ QgsSelectByFormDialog::~QgsSelectByFormDialog()
void QgsSelectByFormDialog::setMessageBar( QgsMessageBar* messageBar )
{
mMessageBar = messageBar;
mForm->setMessageBar( messageBar );
}
void QgsSelectByFormDialog::setMapCanvas( QgsMapCanvas* canvas )
{
mMapCanvas = canvas;
connect( mForm, &QgsAttributeForm::zoomToFeatures, this, &QgsSelectByFormDialog::zoomToFeatures );
}
void QgsSelectByFormDialog::zoomToFeatures( const QString& filter )
{
QgsFeatureIds ids;
QgsExpressionContext context;
context << QgsExpressionContextUtils::globalScope()
<< QgsExpressionContextUtils::projectScope()
<< QgsExpressionContextUtils::layerScope( mLayer );
QgsFeatureRequest request = QgsFeatureRequest().setFilterExpression( filter )
.setExpressionContext( context )
.setSubsetOfAttributes( QgsAttributeList() );
QgsFeatureIterator features = mLayer->getFeatures( request );
QgsRectangle bbox;
bbox.setMinimal();
QgsFeature feat;
int featureCount = 0;
while ( features.nextFeature( feat ) )
{
QgsGeometry geom = feat.geometry();
if ( geom.isEmpty() || geom.geometry()->isEmpty() )
continue;
QgsRectangle r = mMapCanvas->mapSettings().layerExtentToOutputExtent( mLayer, geom.boundingBox() );
bbox.combineExtentWith( r );
featureCount++;
}
features.close();
QSettings settings;
int timeout = settings.value( QStringLiteral( "/qgis/messageTimeout" ), 5 ).toInt();
if ( featureCount > 0 )
{
mMapCanvas->zoomToFeatureExtent( bbox );
if ( mMessageBar )
{
mMessageBar->pushMessage( QString(),
tr( "Zoomed to %1 matching %2" ).arg( featureCount )
.arg( featureCount == 1 ? tr( "feature" ) : tr( "features" ) ),
QgsMessageBar::INFO,
timeout );
}
}
else if ( mMessageBar )
{
mMessageBar->pushMessage( QString(),
tr( "No matching features found" ),
QgsMessageBar::INFO,
timeout );
}
}

View File

@ -22,6 +22,7 @@
class QgsAttributeForm;
class QgsMessageBar;
class QgsVectorLayer;
class QgsMapCanvas;
/** \ingroup app
* \class QgsSelectByFormDialog
@ -54,9 +55,21 @@ class APP_EXPORT QgsSelectByFormDialog : public QDialog
*/
void setMessageBar( QgsMessageBar* messageBar );
/**
* Sets a map canvas associated with the dialog.
*/
void setMapCanvas( QgsMapCanvas* canvas );
private slots:
void zoomToFeatures( const QString& filter );
private:
QgsAttributeForm* mForm;
QgsVectorLayer* mLayer;
QgsMessageBar* mMessageBar;
QgsMapCanvas* mMapCanvas;
};

View File

@ -405,6 +405,15 @@ void QgsAttributeForm::filterTriggered()
setMode( SingleEditMode );
}
void QgsAttributeForm::searchZoomTo()
{
QString filter = createFilterExpression();
if ( filter.isEmpty() )
return;
emit zoomToFeatures( filter );
}
void QgsAttributeForm::filterAndTriggered()
{
QString filter = createFilterExpression();
@ -1310,6 +1319,12 @@ void QgsAttributeForm::init()
boxLayout->addWidget( clearButton );
boxLayout->addStretch( 1 );
QPushButton* zoomButton = new QPushButton();
zoomButton->setSizePolicy( QSizePolicy::Minimum, QSizePolicy::Minimum );
zoomButton->setText( tr( "&Zoom to features" ) );
connect( zoomButton, &QToolButton::clicked, this, &QgsAttributeForm::searchZoomTo );
boxLayout->addWidget( zoomButton );
QToolButton* selectButton = new QToolButton();
selectButton->setSizePolicy( QSizePolicy::Minimum, QSizePolicy::Minimum );
selectButton->setText( tr( "&Select features" ) );

View File

@ -191,6 +191,12 @@ class GUI_EXPORT QgsAttributeForm : public QWidget
*/
void closed();
/**
* Emitted when the user chooses to zoom to a filtered set of features.
* @note added in QGIS 3.0
*/
void zoomToFeatures( const QString& filter );
public slots:
/**
@ -250,6 +256,7 @@ class GUI_EXPORT QgsAttributeForm : public QWidget
void filterOrTriggered();
void filterTriggered();
void searchZoomTo();
void searchSetSelection();
void searchAddToSelection();
void searchRemoveFromSelection();

View File

@ -288,6 +288,10 @@ class GUI_EXPORT QgsMapCanvas : public QGraphicsView
//! Zooms in/out with a given center
void zoomWithCenter( int x, int y, bool zoomIn );
//! Zooms to feature extent. Adds a small margin around the extent
//! and does a pan if rect is empty (point extent)
void zoomToFeatureExtent( QgsRectangle& rect );
//! Returns whether the scale is locked, so zooming can be performed using magnication.
//! @note added in 2.16
//! @see setScaleLocked()
@ -589,10 +593,6 @@ class GUI_EXPORT QgsMapCanvas : public QGraphicsView
//! called when panning is in action, reset indicates end of panning
void moveCanvasContents( bool reset = false );
//! Zooms to feature extent. Adds a small margin around the extent
//! and does a pan if rect is empty (point extent)
void zoomToFeatureExtent( QgsRectangle& rect );
//! called on resize or changed extent to notify canvas items to change their rectangle
void updateCanvasItemPositions();