Skip to content

Commit

Permalink
Fix #12088 - selection tool - do not consider invisible features
Browse files Browse the repository at this point in the history
This avoids dangerous situations where the user inadvertently selects features
that are not rendered and does further actions (like to delete them)
  • Loading branch information
wonder-sk committed Feb 19, 2015
1 parent 7f2fac1 commit ad86ffe
Showing 1 changed file with 22 additions and 1 deletion.
23 changes: 22 additions & 1 deletion src/app/qgsmaptoolselectutils.cpp
Expand Up @@ -22,6 +22,7 @@ email : jpalmer at linz dot govt dot nz
#include "qgsvectorlayer.h"
#include "qgsfeature.h"
#include "qgsgeometry.h"
#include "qgsrendererv2.h"
#include "qgsrubberband.h"
#include "qgscsexception.h"
#include "qgslogger.h"
Expand Down Expand Up @@ -135,7 +136,20 @@ void QgsMapToolSelectUtils::setSelectFeatures( QgsMapCanvas* canvas,
QgsDebugMsg( "doContains: " + QString( doContains ? "T" : "F" ) );
QgsDebugMsg( "doDifference: " + QString( doDifference ? "T" : "F" ) );

QgsFeatureIterator fit = vlayer->getFeatures( QgsFeatureRequest().setFilterRect( selectGeomTrans.boundingBox() ).setFlags( QgsFeatureRequest::ExactIntersect ).setSubsetOfAttributes( QgsAttributeList() ) );
QgsRenderContext context = QgsRenderContext::fromMapSettings( canvas->mapSettings() );
QgsFeatureRendererV2* r = vlayer->rendererV2();
if ( r )
r->startRender( context, vlayer->pendingFields() );

QgsFeatureRequest request;
request.setFilterRect( selectGeomTrans.boundingBox() );
request.setFlags( QgsFeatureRequest::ExactIntersect );
if ( r )
request.setSubsetOfAttributes( r->usedAttributes(), vlayer->pendingFields() );
else
request.setSubsetOfAttributes( QgsAttributeList() );

QgsFeatureIterator fit = vlayer->getFeatures( request );

QgsFeatureIds newSelectedFeatures;
QgsFeature f;
Expand All @@ -144,6 +158,10 @@ void QgsMapToolSelectUtils::setSelectFeatures( QgsMapCanvas* canvas,
double closestFeatureDist = std::numeric_limits<double>::max();
while ( fit.nextFeature( f ) )
{
// make sure to only use features that are visible
if ( r && !r->willRenderFeature( f ) )
continue;

QgsGeometry* g = f.geometry();
if ( doContains )
{
Expand Down Expand Up @@ -175,6 +193,9 @@ void QgsMapToolSelectUtils::setSelectFeatures( QgsMapCanvas* canvas,
newSelectedFeatures.insert( closestFeatureId );
}

if ( r )
r->stopRender( context );

QgsDebugMsg( "Number of new selected features: " + QString::number( newSelectedFeatures.size() ) );

if ( doDifference )
Expand Down

1 comment on commit ad86ffe

@nirvn
Copy link
Contributor

@nirvn nirvn commented on ad86ffe Feb 19, 2015

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@wonder-sk , thanks for that!

Please sign in to comment.