Skip to content

Commit

Permalink
Add method to retrieve map extent to QgsRenderContext
Browse files Browse the repository at this point in the history
Previously only a "layer clipping extent" was available for retrieval
from a QgsRenderContext instance, yet there's a need for rendering
operations to have access to the original full extent of the map
being rendered.
  • Loading branch information
nyalldawson authored and nirvn committed May 12, 2019
1 parent 883d235 commit 0e401cc
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 1 deletion.
32 changes: 32 additions & 0 deletions python/core/auto_generated/qgsrendercontext.sip.in
Expand Up @@ -184,6 +184,22 @@ than the actual visible portion of that layer.
should never be used to determine the actual visible extent of a map render.

.. seealso:: :py:func:`setExtent`

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

QgsRectangle mapExtent() const;
%Docstring
Returns the original extent of the map being rendered.

Unlike extent(), this extent is always in the final destination CRS for the map
render and represents the exact bounds of the map being rendered.

.. seealso:: :py:func:`extent`

.. seealso:: :py:func:`setMapExtent`

.. versionadded:: 3.4.8
%End

const QgsMapToPixel &mapToPixel() const;
Expand Down Expand Up @@ -263,6 +279,22 @@ to the map. It may be larger than the actual visible area, but MUST contain at l
entire visible area.

.. seealso:: :py:func:`setExtent`

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

void setMapExtent( const QgsRectangle &extent );
%Docstring
Sets the original ``extent`` of the map being rendered.

Unlike setExtent(), this extent is always in the final destination CRS for the map
render and represents the exact bounds of the map being rendered.

.. seealso:: :py:func:`mapExtent`

.. seealso:: :py:func:`setExtent`

.. versionadded:: 3.4.8
%End

void setDrawEditingInformation( bool b );
Expand Down
3 changes: 3 additions & 0 deletions src/core/qgsrendercontext.cpp
Expand Up @@ -43,6 +43,7 @@ QgsRenderContext::QgsRenderContext( const QgsRenderContext &rh )
, mCoordTransform( rh.mCoordTransform )
, mDistanceArea( rh.mDistanceArea )
, mExtent( rh.mExtent )
, mOriginalMapExtent( rh.mOriginalMapExtent )
, mMapToPixel( rh.mMapToPixel )
, mRenderingStopped( rh.mRenderingStopped )
, mScaleFactor( rh.mScaleFactor )
Expand Down Expand Up @@ -70,6 +71,7 @@ QgsRenderContext &QgsRenderContext::operator=( const QgsRenderContext &rh )
mPainter = rh.mPainter;
mCoordTransform = rh.mCoordTransform;
mExtent = rh.mExtent;
mOriginalMapExtent = rh.mOriginalMapExtent;
mMapToPixel = rh.mMapToPixel;
mRenderingStopped = rh.mRenderingStopped;
mScaleFactor = rh.mScaleFactor;
Expand Down Expand Up @@ -157,6 +159,7 @@ QgsRenderContext QgsRenderContext::fromMapSettings( const QgsMapSettings &mapSet
QgsRenderContext ctx;
ctx.setMapToPixel( mapSettings.mapToPixel() );
ctx.setExtent( mapSettings.visibleExtent() );
ctx.setMapExtent( mapSettings.visibleExtent() );
ctx.setFlag( DrawEditingInfo, mapSettings.testFlag( QgsMapSettings::DrawEditingInfo ) );
ctx.setFlag( ForceVectorOutput, mapSettings.testFlag( QgsMapSettings::ForceVectorOutput ) );
ctx.setFlag( UseAdvancedEffects, mapSettings.testFlag( QgsMapSettings::UseAdvancedEffects ) );
Expand Down
27 changes: 27 additions & 0 deletions src/core/qgsrendercontext.h
Expand Up @@ -234,9 +234,22 @@ class CORE_EXPORT QgsRenderContext
* should never be used to determine the actual visible extent of a map render.
*
* \see setExtent()
* \see mapExtent()
*/
const QgsRectangle &extent() const { return mExtent; }

/**
* Returns the original extent of the map being rendered.
*
* Unlike extent(), this extent is always in the final destination CRS for the map
* render and represents the exact bounds of the map being rendered.
*
* \see extent()
* \see setMapExtent()
* \since QGIS 3.4.8
*/
QgsRectangle mapExtent() const { return mOriginalMapExtent; }

const QgsMapToPixel &mapToPixel() const {return mMapToPixel;}

/**
Expand Down Expand Up @@ -313,9 +326,22 @@ class CORE_EXPORT QgsRenderContext
* entire visible area.
*
* \see setExtent()
* \see setMapExtent()
*/
void setExtent( const QgsRectangle &extent ) {mExtent = extent;}

/**
* Sets the original \a extent of the map being rendered.
*
* Unlike setExtent(), this extent is always in the final destination CRS for the map
* render and represents the exact bounds of the map being rendered.
*
* \see mapExtent()
* \see setExtent()
* \since QGIS 3.4.8
*/
void setMapExtent( const QgsRectangle &extent ) { mOriginalMapExtent = extent; }

void setDrawEditingInformation( bool b );

void setRenderingStopped( bool stopped ) {mRenderingStopped = stopped;}
Expand Down Expand Up @@ -512,6 +538,7 @@ class CORE_EXPORT QgsRenderContext
QgsDistanceArea mDistanceArea;

QgsRectangle mExtent;
QgsRectangle mOriginalMapExtent;

QgsMapToPixel mMapToPixel;

Expand Down
13 changes: 12 additions & 1 deletion tests/src/python/test_qgsrendercontext.py
Expand Up @@ -21,7 +21,8 @@
QgsCoordinateReferenceSystem,
QgsMapUnitScale,
QgsUnitTypes,
QgsProject)
QgsProject,
QgsRectangle)
from qgis.PyQt.QtCore import QSize
from qgis.PyQt.QtGui import QPainter, QImage
from qgis.testing import start_app, unittest
Expand All @@ -45,16 +46,21 @@ def testGettersSetters(self):
c.setTextRenderFormat(QgsRenderContext.TextFormatAlwaysOutlines)
self.assertEqual(c.textRenderFormat(), QgsRenderContext.TextFormatAlwaysOutlines)

c.setMapExtent(QgsRectangle(1, 2, 3, 4))
self.assertEqual(c.mapExtent(), QgsRectangle(1, 2, 3, 4))

def testCopyConstructor(self):
"""
Test the copy constructor
"""
c1 = QgsRenderContext()

c1.setTextRenderFormat(QgsRenderContext.TextFormatAlwaysText)
c1.setMapExtent(QgsRectangle(1, 2, 3, 4))

c2 = QgsRenderContext(c1)
self.assertEqual(c2.textRenderFormat(), QgsRenderContext.TextFormatAlwaysText)
self.assertEqual(c2.mapExtent(), QgsRectangle(1, 2, 3, 4))

c1.setTextRenderFormat(QgsRenderContext.TextFormatAlwaysOutlines)
c2 = QgsRenderContext(c1)
Expand Down Expand Up @@ -92,6 +98,9 @@ def testFromMapSettings(self):
test QgsRenderContext.fromMapSettings()
"""
ms = QgsMapSettings()
ms.setOutputSize(QSize(1000, 1000))
ms.setDestinationCrs(QgsCoordinateReferenceSystem('EPSG:3111'))
ms.setExtent(QgsRectangle(10000, 20000, 30000, 40000))

ms.setTextRenderFormat(QgsRenderContext.TextFormatAlwaysText)
rc = QgsRenderContext.fromMapSettings(ms)
Expand All @@ -101,6 +110,8 @@ def testFromMapSettings(self):
rc = QgsRenderContext.fromMapSettings(ms)
self.assertEqual(rc.textRenderFormat(), QgsRenderContext.TextFormatAlwaysOutlines)

self.assertEqual(rc.mapExtent(), QgsRectangle(10000, 20000, 30000, 40000))

def testRenderMetersInMapUnits(self):

crs_wsg84 = QgsCoordinateReferenceSystem.fromOgcWmsCrs('EPSG:4326')
Expand Down

0 comments on commit 0e401cc

Please sign in to comment.