Skip to content

Commit

Permalink
start action for single feature directly
Browse files Browse the repository at this point in the history
  • Loading branch information
signedav committed Oct 18, 2018
1 parent 5bbb1c4 commit b38e183
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 16 deletions.
40 changes: 25 additions & 15 deletions src/app/qgsmaptoolfeatureaction.cpp
Expand Up @@ -75,7 +75,7 @@ void QgsMapToolFeatureAction::canvasReleaseEvent( QgsMapMouseEvent *e )
return;
}

if ( !doAction( vlayer, e->x(), e->y() ) )
if ( !doAction( vlayer, e->x(), e->y(), e->pixelPoint() ) )
QgisApp::instance()->statusBarIface()->showMessage( tr( "No features at this position found." ) );
}

Expand All @@ -89,7 +89,7 @@ void QgsMapToolFeatureAction::deactivate()
QgsMapTool::deactivate();
}

bool QgsMapToolFeatureAction::doAction( QgsVectorLayer *layer, int x, int y )
bool QgsMapToolFeatureAction::doAction( QgsVectorLayer *layer, int x, int y, QPoint pixelpos )
{
if ( !layer )
return false;
Expand Down Expand Up @@ -120,23 +120,33 @@ bool QgsMapToolFeatureAction::doAction( QgsVectorLayer *layer, int x, int y )
QgsDebugMsg( QStringLiteral( "Caught CRS exception %1" ).arg( cse.what() ) );
}

QMenu *featureMenu = new QMenu();
QgsFeature f;
QgsFeatureList features;
QgsFeatureIterator fit = layer->getFeatures( QgsFeatureRequest().setFilterRect( r ).setFlags( QgsFeatureRequest::ExactIntersect ) );
QgsFeature feat;

int numberOfFeatures = 0;
while ( fit.nextFeature( feat ) )
while ( fit.nextFeature( f ) )
{
QAction *featureAction = featureMenu->addAction( FID_TO_STRING( feat.id() ) );
connect( featureAction, &QAction::triggered, this, [ = ] { doActionForFeature( layer, feat, point );} );
numberOfFeatures++;
features.append( f );
}

if ( numberOfFeatures == 0 )
return false;
else
featureMenu->exec( point.toQPointF().toPoint() );
return true;
if ( !features.isEmpty() )
{
if ( features.count() == 1 )
{
doActionForFeature( layer, features.first(), point );
}
else
{
QMenu *featureMenu = new QMenu();
for ( int i = 0; i < features.count(); i++ )
{
QAction *featureAction = featureMenu->addAction( FID_TO_STRING( features.at( i ).id() ) );
connect( featureAction, &QAction::triggered, this, [ = ] { doActionForFeature( layer, features.at( i ), point );} );
}
featureMenu->exec( pixelpos );
}
return true;
}
return false;
}

void QgsMapToolFeatureAction::doActionForFeature( QgsVectorLayer *layer, QgsFeature feat, QgsPointXY point )
Expand Down
2 changes: 1 addition & 1 deletion src/app/qgsmaptoolfeatureaction.h
Expand Up @@ -52,7 +52,7 @@ class APP_EXPORT QgsMapToolFeatureAction : public QgsMapTool
void deactivate() override;

private:
bool doAction( QgsVectorLayer *layer, int x, int y );
bool doAction( QgsVectorLayer *layer, int x, int y, QPoint pixelpos );
void doActionForFeature( QgsVectorLayer *layer, QgsFeature feat, QgsPointXY point );
};

Expand Down

0 comments on commit b38e183

Please sign in to comment.