diff --git a/python/gui/qgsmapcanvas.sip b/python/gui/qgsmapcanvas.sip index 3c624618407..2bce86391d4 100644 --- a/python/gui/qgsmapcanvas.sip +++ b/python/gui/qgsmapcanvas.sip @@ -168,10 +168,10 @@ class QgsMapCanvas : QGraphicsView @param layer optionally specify different than current layer */ void zoomToSelected( QgsVectorLayer* layer = NULL ); - /** Set canvas extent to the bounding box of a feature + /** Set canvas extent to the bounding box of a set of features @param layer the vector layer - @param id the feature id*/ - void zoomToFeatureId( QgsVectorLayer* layer, QgsFeatureId id ); + @param ids the feature ids*/ + void zoomToFeatureIds( QgsVectorLayer* layer, const QgsFeatureIds& ids ); /** Pan to the selected features of current (vector) layer keeping same extent. */ void panToSelected( QgsVectorLayer* layer = NULL ); diff --git a/src/gui/attributetable/qgsdualview.cpp b/src/gui/attributetable/qgsdualview.cpp index e5784c7b2cc..6b9b01e01e2 100644 --- a/src/gui/attributetable/qgsdualview.cpp +++ b/src/gui/attributetable/qgsdualview.cpp @@ -409,11 +409,12 @@ void QgsDualView::zoomToCurrentFeature() return; } - QgsFeatureId id = mFilterModel->rowToId( currentIndex ); + QgsFeatureIds ids; + ids.insert( mFilterModel->rowToId( currentIndex ) ); QgsMapCanvas* canvas = mFilterModel->mapCanvas(); if ( canvas ) { - canvas->zoomToFeatureId( mLayerCache->layer(), id ); + canvas->zoomToFeatureIds( mLayerCache->layer(), ids ); } } diff --git a/src/gui/qgsmapcanvas.cpp b/src/gui/qgsmapcanvas.cpp index 585281ce67d..65b88088a3d 100644 --- a/src/gui/qgsmapcanvas.cpp +++ b/src/gui/qgsmapcanvas.cpp @@ -1071,38 +1071,45 @@ void QgsMapCanvas::zoomToFeatureExtent( QgsRectangle& rect ) refresh(); } -void QgsMapCanvas::zoomToFeatureId( QgsVectorLayer* layer, QgsFeatureId id ) +void QgsMapCanvas::zoomToFeatureIds( QgsVectorLayer* layer, const QgsFeatureIds& ids ) { if ( !layer ) { return; } - QgsFeatureIterator it = layer->getFeatures( QgsFeatureRequest().setFilterFid( id ).setSubsetOfAttributes( QgsAttributeList() ) ); + QgsFeatureIterator it = layer->getFeatures( QgsFeatureRequest().setFilterFids( ids ).setSubsetOfAttributes( QgsAttributeList() ) ); + QgsRectangle rect; + rect.setMinimal(); QgsFeature fet; - if ( !it.nextFeature( fet ) ) + int featureCount = 0; + while ( it.nextFeature( fet ) ) + { + QgsGeometry* geom = fet.geometry(); + QString errorMessage; + if ( !geom || !geom->geometry() ) + { + errorMessage = tr( "Feature does not have a geometry" ); + } + else if ( geom->geometry()->isEmpty() ) + { + errorMessage = tr( "Feature geometry is empty" ); + } + if ( !errorMessage.isEmpty() ) + { + emit messageEmitted( tr( "Zoom to feature id failed" ), errorMessage, QgsMessageBar::WARNING ); + return; + } + QgsRectangle r = mapSettings().layerExtentToOutputExtent( layer, geom->boundingBox() ); + rect.combineExtentWith( &r ); + featureCount++; + } + + if ( featureCount != ids.count() ) { return; } - QgsGeometry* geom = fet.geometry(); - - QString errorMessage; - if ( !geom || !geom->geometry() ) - { - errorMessage = tr( "Feature does not have a geometry" ); - } - else if ( geom->geometry()->isEmpty() ) - { - errorMessage = tr( "Feature geometry is empty" ); - } - if ( !errorMessage.isEmpty() ) - { - emit messageEmitted( tr( "Zoom to feature id failed" ), errorMessage, QgsMessageBar::WARNING ); - return; - } - - QgsRectangle rect = mapSettings().layerExtentToOutputExtent( layer, geom->boundingBox() ); zoomToFeatureExtent( rect ); } diff --git a/src/gui/qgsmapcanvas.h b/src/gui/qgsmapcanvas.h index 0c82bb54863..d3cb06c47bc 100644 --- a/src/gui/qgsmapcanvas.h +++ b/src/gui/qgsmapcanvas.h @@ -238,10 +238,10 @@ class GUI_EXPORT QgsMapCanvas : public QGraphicsView @param layer optionally specify different than current layer */ void zoomToSelected( QgsVectorLayer* layer = nullptr ); - /** Set canvas extent to the bounding box of a feature + /** Set canvas extent to the bounding box of a set of features @param layer the vector layer - @param id the feature id*/ - void zoomToFeatureId( QgsVectorLayer* layer, QgsFeatureId id ); + @param ids the feature ids*/ + void zoomToFeatureIds( QgsVectorLayer* layer, const QgsFeatureIds& ids ); /** Pan to the selected features of current (vector) layer keeping same extent. */ void panToSelected( QgsVectorLayer* layer = nullptr );