Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Disambiguate use of QgsMapCanvas::fullExtent and
QgsMapCanvas::zoomToFullExtent

In 3.18 these methods changed their meaning from the "full extent
of all layers visible in the canvas" to "the full extent of the
associated project".

These are actually two different, equally valid use cases, so
disambiguate by adding new explicit methods projectExtent()
and zoomToProjectExtent(), and revert fullExtent() and
zoomToFullExtent() to their pre-3.18 behavior.

Fixes #43303
  • Loading branch information
nyalldawson committed May 21, 2021
1 parent dc50988 commit 7bf85a0
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 8 deletions.
34 changes: 32 additions & 2 deletions python/gui/auto_generated/qgsmapcanvas.sip.in
Expand Up @@ -191,9 +191,27 @@ Returns the mapUnitsPerPixel (map units per pixel) for the canvas
%Docstring
Returns the current zoom extent of the map canvas
%End

QgsRectangle fullExtent() const;
%Docstring
Returns the combined extent for all layers on the map canvas
Returns the combined extent for all layers on the map canvas.

This method returns the combined extent for all layers which are currently visible in the map canvas.
The returned extent will be in the same CRS as the map canvas.

.. seealso:: :py:func:`projectExtent`
%End

QgsRectangle projectExtent() const;
%Docstring
Returns the associated project's full extent, in the canvas' CRS.

This method returns the full extent for the project associated with this canvas.
Unlike :py:func:`~QgsMapCanvas.fullExtent`, this method does NOT consider which layers are actually visible in the map canvas.

.. seealso:: :py:func:`fullExtent`

.. versionadded:: 3.20
%End

void setExtent( const QgsRectangle &r, bool magnified = false );
Expand Down Expand Up @@ -250,7 +268,19 @@ Gets map center, in geographical coordinates

void zoomToFullExtent();
%Docstring
Zoom to the full extent of all layers
Zoom to the full extent of all layers currently visible in the canvas.

.. seealso:: :py:func:`zoomToProjectExtent`
%End

void zoomToProjectExtent();
%Docstring
Zoom to the full extent the project associated with this canvas.

This method zooms to the full extent for the project associated with this canvas.
Unlike :py:func:`~QgsMapCanvas.zoomToFullExtent`, this method does NOT consider which layers are actually visible in the map canvas.

.. versionadded:: 3.20
%End

void zoomToPreviousExtent();
Expand Down
25 changes: 21 additions & 4 deletions src/gui/qgsmapcanvas.cpp
Expand Up @@ -90,6 +90,7 @@ email : sherman at mrcc.com
#include "qgsmaplayerelevationproperties.h"
#include "qgscoordinatereferencesystemregistry.h"
#include "qgslabelingresults.h"
#include "qgsmaplayerutils.h"

/**
* \ingroup gui
Expand Down Expand Up @@ -1105,8 +1106,13 @@ QgsRectangle QgsMapCanvas::extent() const

QgsRectangle QgsMapCanvas::fullExtent() const
{
const QgsReferencedRectangle extent = QgsProject::instance()->viewSettings()->fullExtent();
QgsCoordinateTransform ct( extent.crs(), mapSettings().destinationCrs(), QgsProject::instance()->transformContext() );
return QgsMapLayerUtils::combinedExtent( mSettings.layers(), mapSettings().destinationCrs(), QgsProject::instance()->transformContext() );
}

QgsRectangle QgsMapCanvas::projectExtent() const
{
const QgsReferencedRectangle extent = mProject ? mProject->viewSettings()->fullExtent() : QgsProject::instance()->viewSettings()->fullExtent();
QgsCoordinateTransform ct( extent.crs(), mapSettings().destinationCrs(), mProject ? mProject->transformContext() : QgsProject::instance()->transformContext() );
ct.setBallparkTransformsAreAppropriate( true );
QgsRectangle rect;
try
Expand Down Expand Up @@ -1253,7 +1259,6 @@ void QgsMapCanvas::updateScale()
emit scaleChanged( mapSettings().scale() );
}


void QgsMapCanvas::zoomToFullExtent()
{
QgsRectangle extent = fullExtent();
Expand All @@ -1265,9 +1270,21 @@ void QgsMapCanvas::zoomToFullExtent()
setExtent( extent );
}
refresh();
}

} // zoomToFullExtent
void QgsMapCanvas::zoomToProjectExtent()
{
QgsRectangle extent = projectExtent();

// If the full extent is an empty set, don't do the zoom
if ( !extent.isEmpty() )
{
// Add a 5% margin around the full extent
extent.scale( 1.05 );
setExtent( extent );
}
refresh();
}

void QgsMapCanvas::zoomToPreviousExtent()
{
Expand Down
37 changes: 35 additions & 2 deletions src/gui/qgsmapcanvas.h
Expand Up @@ -234,9 +234,28 @@ class GUI_EXPORT QgsMapCanvas : public QGraphicsView

//! Returns the current zoom extent of the map canvas
QgsRectangle extent() const;
//! Returns the combined extent for all layers on the map canvas

/**
* Returns the combined extent for all layers on the map canvas.
*
* This method returns the combined extent for all layers which are currently visible in the map canvas.
* The returned extent will be in the same CRS as the map canvas.
*
* \see projectExtent()
*/
QgsRectangle fullExtent() const;

/**
* Returns the associated project's full extent, in the canvas' CRS.
*
* This method returns the full extent for the project associated with this canvas.
* Unlike fullExtent(), this method does NOT consider which layers are actually visible in the map canvas.
*
* \see fullExtent()
* \since QGIS 3.20
*/
QgsRectangle projectExtent() const;

/**
* Sets the extent of the map canvas to the specified rectangle.
*
Expand Down Expand Up @@ -284,9 +303,23 @@ class GUI_EXPORT QgsMapCanvas : public QGraphicsView
*/
QgsPointXY center() const;

//! Zoom to the full extent of all layers
/**
* Zoom to the full extent of all layers currently visible in the canvas.
*
* \see zoomToProjectExtent()
*/
void zoomToFullExtent();

/**
* Zoom to the full extent the project associated with this canvas.
*
* This method zooms to the full extent for the project associated with this canvas.
* Unlike zoomToFullExtent(), this method does NOT consider which layers are actually visible in the map canvas.
*
* \since QGIS 3.20
*/
void zoomToProjectExtent();

//! Zoom to the previous extent (view)
void zoomToPreviousExtent();

Expand Down

0 comments on commit 7bf85a0

Please sign in to comment.