Skip to content
Permalink
Browse files

Merge pull request #38861 from pblottiere/maptips_willrenderfeature

Check if feature is rendered before displaying its map tips
  • Loading branch information
pblottiere committed Oct 15, 2020
2 parents 252976b + dd6b20e commit b38ea535b2ff0dde6b338876a8730f16da146219
Showing with 30 additions and 0 deletions.
  1. +30 −0 src/gui/qgsmaptip.cpp
@@ -23,6 +23,7 @@
#include "qgswebview.h"
#include "qgswebframe.h"
#include "qgsapplication.h"
#include "qgsrenderer.h"
#include "qgsexpressioncontextutils.h"

// Qt includes
@@ -212,12 +213,38 @@ QString QgsMapTip::fetchFeature( QgsMapLayer *layer, QgsPointXY &mapPosition, Qg
exp.prepare( &context );
request.setSubsetOfAttributes( exp.referencedColumns(), vlayer->fields() );
}

QgsRenderContext renderCtx = QgsRenderContext::fromMapSettings( mapCanvas->mapSettings() );
renderCtx.expressionContext() << QgsExpressionContextUtils::layerScope( vlayer );

bool filter = false;
std::unique_ptr< QgsFeatureRenderer > renderer;
if ( vlayer->renderer() )
{
renderer.reset( vlayer->renderer()->clone() );
renderer->startRender( renderCtx, vlayer->fields() );
filter = renderer->capabilities() & QgsFeatureRenderer::Filter;

const QString filterExpression = renderer->filter( vlayer->fields() );
if ( ! filterExpression.isEmpty() )
{
request.setFilterExpression( filterExpression );
}
}

QgsFeatureIterator it = vlayer->getFeatures( request );
QElapsedTimer timer;
timer.start();
while ( it.nextFeature( feature ) )
{
context.setFeature( feature );

renderCtx.expressionContext().setFeature( feature );
if ( filter && renderer && !renderer->willRenderFeature( feature, renderCtx ) )
{
continue;
}

if ( !mapTip.isEmpty() )
{
tipString = QgsExpression::replaceExpressionText( mapTip, &context );
@@ -233,6 +260,9 @@ QString QgsMapTip::fetchFeature( QgsMapLayer *layer, QgsPointXY &mapPosition, Qg
}
}

if ( renderer )
renderer->stopRender( renderCtx );

return tipString;
}

0 comments on commit b38ea53

Please sign in to comment.
You can’t perform that action at this time.