Skip to content

Commit

Permalink
Don't show label hover highlights while a map redraw is in progress,
Browse files Browse the repository at this point in the history
because these will show for outdated label positions
  • Loading branch information
nyalldawson committed Mar 15, 2021
1 parent 879f892 commit 5bceda8
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 13 deletions.
7 changes: 5 additions & 2 deletions python/gui/auto_generated/qgsmapcanvas.sip.in
Expand Up @@ -106,9 +106,12 @@ Resets the ``flags`` for the canvas' map settings.
.. versionadded:: 3.0
%End

const QgsLabelingResults *labelingResults() const;
const QgsLabelingResults *labelingResults( bool allowOutdatedResults = true ) const;
%Docstring
Gets access to the labeling results (may be ``None``)
Gets access to the labeling results (may be ``None``).

Since QGIS 3.20, if the ``allowOutdatedResults`` flag is ``False`` then outdated labeling results (e.g.
as a result of an ongoing canvas render) will not be returned, and instead ``None`` will be returned.

.. versionadded:: 2.4
%End
Expand Down
4 changes: 2 additions & 2 deletions src/app/labeling/qgsmaptoollabel.cpp
Expand Up @@ -62,7 +62,7 @@ void QgsMapToolLabel::deactivate()
bool QgsMapToolLabel::labelAtPosition( QMouseEvent *e, QgsLabelPosition &p )
{
QgsPointXY pt = toMapCoordinates( e->pos() );
const QgsLabelingResults *labelingResults = mCanvas->labelingResults();
const QgsLabelingResults *labelingResults = mCanvas->labelingResults( false );
if ( !labelingResults )
return false;

Expand Down Expand Up @@ -123,7 +123,7 @@ bool QgsMapToolLabel::labelAtPosition( QMouseEvent *e, QgsLabelPosition &p )
bool QgsMapToolLabel::calloutAtPosition( QMouseEvent *e, QgsCalloutPosition &p, bool &isOrigin )
{
QgsPointXY pt = toMapCoordinates( e->pos() );
const QgsLabelingResults *labelingResults = mCanvas->labelingResults();
const QgsLabelingResults *labelingResults = mCanvas->labelingResults( false );
if ( !labelingResults )
return false;

Expand Down
6 changes: 2 additions & 4 deletions src/app/labeling/qgsmaptoolpinlabels.cpp
Expand Up @@ -167,10 +167,9 @@ void QgsMapToolPinLabels::highlightPinnedLabels()
QgsDebugMsg( QStringLiteral( "Highlighting pinned labels" ) );

// get list of all drawn labels from all layers within given extent
const QgsLabelingResults *labelingResults = mCanvas->labelingResults();
const QgsLabelingResults *labelingResults = mCanvas->labelingResults( false );
if ( !labelingResults )
{
QgsDebugMsg( QStringLiteral( "No labeling engine" ) );
return;
}

Expand Down Expand Up @@ -238,10 +237,9 @@ void QgsMapToolPinLabels::pinUnpinLabels( const QgsRectangle &ext, QMouseEvent *
bool toggleUnpinOrPin = e->modifiers() & Qt::ControlModifier;

// get list of all drawn labels from all layers within, or touching, chosen extent
const QgsLabelingResults *labelingResults = mCanvas->labelingResults();
const QgsLabelingResults *labelingResults = mCanvas->labelingResults( false );
if ( !labelingResults )
{
QgsDebugMsg( QStringLiteral( "No labeling engine" ) );
return;
}

Expand Down
3 changes: 1 addition & 2 deletions src/app/labeling/qgsmaptoolshowhidelabels.cpp
Expand Up @@ -284,10 +284,9 @@ bool QgsMapToolShowHideLabels::selectedLabelFeatures( QgsVectorLayer *vlayer,
listPos.clear();

// get list of all drawn labels from current layer that intersect rubberband
const QgsLabelingResults *labelingResults = mCanvas->labelingResults();
const QgsLabelingResults *labelingResults = mCanvas->labelingResults( false );
if ( !labelingResults )
{
QgsDebugMsg( QStringLiteral( "No labeling engine" ) );
return false;
}

Expand Down
8 changes: 7 additions & 1 deletion src/gui/qgsmapcanvas.cpp
Expand Up @@ -469,8 +469,11 @@ void QgsMapCanvas::setMapSettingsFlags( QgsMapSettings::Flags flags )
refresh();
}

const QgsLabelingResults *QgsMapCanvas::labelingResults() const
const QgsLabelingResults *QgsMapCanvas::labelingResults( bool allowOutdatedResults ) const
{
if ( !allowOutdatedResults && mLabelingResultsOutdated )
return nullptr;

return mLabelingResults.get();
}

Expand Down Expand Up @@ -567,6 +570,8 @@ void QgsMapCanvas::refresh()

// schedule a refresh
mRefreshTimer->start( 1 );

mLabelingResultsOutdated = true;
}

void QgsMapCanvas::refreshMap()
Expand Down Expand Up @@ -692,6 +697,7 @@ void QgsMapCanvas::rendererJobFinished()
{
mLabelingResults.reset( mJob->takeLabelingResults() );
}
mLabelingResultsOutdated = false;

QImage img = mJob->renderedImage();

Expand Down
11 changes: 9 additions & 2 deletions src/gui/qgsmapcanvas.h
Expand Up @@ -161,10 +161,14 @@ class GUI_EXPORT QgsMapCanvas : public QGraphicsView
void setMapSettingsFlags( QgsMapSettings::Flags flags );

/**
* Gets access to the labeling results (may be NULLPTR)
* Gets access to the labeling results (may be NULLPTR).
*
* Since QGIS 3.20, if the \a allowOutdatedResults flag is FALSE then outdated labeling results (e.g.
* as a result of an ongoing canvas render) will not be returned, and instead NULLPTR will be returned.
*
* \since QGIS 2.4
*/
const QgsLabelingResults *labelingResults() const;
const QgsLabelingResults *labelingResults( bool allowOutdatedResults = true ) const;

/**
* Set whether to cache images of rendered layers
Expand Down Expand Up @@ -1255,6 +1259,9 @@ class GUI_EXPORT QgsMapCanvas : public QGraphicsView
//! Labeling results from the recently rendered map
std::unique_ptr< QgsLabelingResults > mLabelingResults;

//! TRUE if the labeling results stored in mLabelingResults are outdated (e.g. as a result of an ongoing canvas render)
bool mLabelingResultsOutdated = false;

//! Whether layers are rendered sequentially or in parallel
bool mUseParallelRendering = false;

Expand Down

0 comments on commit 5bceda8

Please sign in to comment.