start action for single feature directly

This commit is contained in:
signedav 2018-10-18 14:37:33 +02:00
parent 5bbb1c484b
commit b38e183793
2 changed files with 26 additions and 16 deletions

View File

@ -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." ) );
}
@ -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;
@ -120,24 +120,34 @@ 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;
if ( !features.isEmpty() )
{
if ( features.count() == 1 )
{
doActionForFeature( layer, features.first(), point );
}
else
featureMenu->exec( point.toQPointF().toPoint() );
{
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 )
{

View File

@ -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 );
};