Skip to content

Commit

Permalink
Implement responsive map refresh when canvas z range changes
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Nov 28, 2020
1 parent 17cc0d4 commit 89a7e3c
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 2 deletions.
Expand Up @@ -37,6 +37,11 @@ how an individual QgsMapLayer behaves with relation to z values or elevations.
QgsMapLayerElevationProperties( QObject *parent /TransferThis/ );
%Docstring
Constructor for QgsMapLayerElevationProperties, with the specified ``parent`` object.
%End

virtual bool hasElevation() const;
%Docstring
Returns ``True`` if the layer has an elevation or z component.
%End

virtual QDomElement writeXml( QDomElement &element, QDomDocument &doc, const QgsReadWriteContext &context ) = 0;
Expand Down
15 changes: 14 additions & 1 deletion python/gui/auto_generated/qgsmapcanvas.sip.in
Expand Up @@ -979,6 +979,8 @@ Returns the range of z-values which will be visible in the map.

.. seealso:: :py:func:`setZRange`

.. seealso:: :py:func:`zRangeChanged`

.. versionadded:: 3.18
%End

Expand All @@ -988,6 +990,8 @@ Sets the ``range`` of z-values which will be visible in the map.

.. seealso:: :py:func:`zRange`

.. seealso:: :py:func:`zRangeChanged`

.. versionadded:: 3.18
%End

Expand Down Expand Up @@ -1171,6 +1175,16 @@ Emitted when the map canvas temporal range changes.
.. versionadded:: 3.14
%End

void zRangeChanged();
%Docstring
Emitted when the map canvas z (elevation) range changes.

.. seealso:: :py:func:`zRange`

.. seealso:: :py:func:`setZRange`

.. versionadded:: 3.18
%End

void contextMenuAboutToShow( QMenu *menu, QgsMapMouseEvent *event );
%Docstring
Expand All @@ -1180,7 +1194,6 @@ Can be used to extend the context menu.
.. versionadded:: 3.16
%End


protected:

virtual bool event( QEvent *e );
Expand Down
5 changes: 5 additions & 0 deletions src/core/qgsmaplayerelevationproperties.cpp
Expand Up @@ -22,6 +22,11 @@ QgsMapLayerElevationProperties::QgsMapLayerElevationProperties( QObject *parent
{
}

bool QgsMapLayerElevationProperties::hasElevation() const
{
return false;
}

bool QgsMapLayerElevationProperties::isVisibleInZRange( const QgsDoubleRange & ) const
{
return true;
Expand Down
5 changes: 5 additions & 0 deletions src/core/qgsmaplayerelevationproperties.h
Expand Up @@ -58,6 +58,11 @@ class CORE_EXPORT QgsMapLayerElevationProperties : public QObject
*/
QgsMapLayerElevationProperties( QObject *parent SIP_TRANSFERTHIS );

/**
* Returns TRUE if the layer has an elevation or z component.
*/
virtual bool hasElevation() const;

/**
* Writes the properties to a DOM \a element, to be used later with readXml().
*
Expand Down
32 changes: 32 additions & 0 deletions src/gui/qgsmapcanvas.cpp
Expand Up @@ -84,6 +84,7 @@ email : sherman at mrcc.com
#include "qgsruntimeprofiler.h"
#include "qgsprojectionselectiondialog.h"
#include "qgsannotationlayer.h"
#include "qgsmaplayerelevationproperties.h"

/**
* \ingroup gui
Expand Down Expand Up @@ -731,6 +732,7 @@ void QgsMapCanvas::rendererJobFinished()
{
mRefreshAfterJob = false;
clearTemporalCache();
clearElevationCache();
refresh();
}
}
Expand Down Expand Up @@ -806,6 +808,24 @@ void QgsMapCanvas::clearTemporalCache()
}
}

void QgsMapCanvas::clearElevationCache()
{
if ( mCache )
{
const QList<QgsMapLayer *> layerList = mapSettings().layers();
for ( QgsMapLayer *layer : layerList )
{
if ( layer->elevationProperties() && layer->elevationProperties()->hasElevation() )
{
if ( layer->elevationProperties()->flags() & QgsMapLayerElevationProperties::FlagDontInvalidateCachedRendersWhenRangeChanges )
continue;

mCache->invalidateCacheForLayer( layer );
}
}
}
}

void QgsMapCanvas::showContextMenu( QgsMapMouseEvent *event )
{
const QgsPointXY mapPoint = event->originalMapPoint();
Expand Down Expand Up @@ -1333,7 +1353,19 @@ QgsDoubleRange QgsMapCanvas::zRange() const

void QgsMapCanvas::setZRange( const QgsDoubleRange &range )
{
if ( zRange() == range )
return;

mSettings.setZRange( range );

emit zRangeChanged();

// we need to discard any previously cached images which are elevation aware, so that these will be updated when
// the canvas is redrawn
if ( !mJob )
clearElevationCache();

autoRefreshTriggered();
}

void QgsMapCanvas::zoomToFeatureExtent( QgsRectangle &rect )
Expand Down
19 changes: 18 additions & 1 deletion src/gui/qgsmapcanvas.h
Expand Up @@ -883,6 +883,8 @@ class GUI_EXPORT QgsMapCanvas : public QGraphicsView
* Returns the range of z-values which will be visible in the map.
*
* \see setZRange()
* \see zRangeChanged()
*
* \since QGIS 3.18
*/
QgsDoubleRange zRange() const;
Expand All @@ -891,6 +893,8 @@ class GUI_EXPORT QgsMapCanvas : public QGraphicsView
* Sets the \a range of z-values which will be visible in the map.
*
* \see zRange()
* \see zRangeChanged()
*
* \since QGIS 3.18
*/
void setZRange( const QgsDoubleRange &range );
Expand Down Expand Up @@ -1060,6 +1064,15 @@ class GUI_EXPORT QgsMapCanvas : public QGraphicsView
*/
void temporalRangeChanged();

/**
* Emitted when the map canvas z (elevation) range changes.
*
* \see zRange()
* \see setZRange()
*
* \since QGIS 3.18
*/
void zRangeChanged();

/**
* Emitted before the map canvas context menu will be shown.
Expand All @@ -1069,7 +1082,6 @@ class GUI_EXPORT QgsMapCanvas : public QGraphicsView
*/
void contextMenuAboutToShow( QMenu *menu, QgsMapMouseEvent *event );


protected:

bool event( QEvent *e ) override;
Expand Down Expand Up @@ -1329,6 +1341,11 @@ class GUI_EXPORT QgsMapCanvas : public QGraphicsView
*/
void clearTemporalCache();

/**
* Removes any rendered images of elevation aware layers from cache
*/
void clearElevationCache();

void showContextMenu( QgsMapMouseEvent *event );

friend class TestQgsMapCanvas;
Expand Down

0 comments on commit 89a7e3c

Please sign in to comment.