Skip to content
Permalink
Browse files

action only starting on one feature

with selection context menu (with wrong position)
  • Loading branch information
signedav committed Oct 18, 2018
1 parent 4010e79 commit 5bbb1c484b92a4efc692ee8bb52766045584d315
Showing with 40 additions and 26 deletions.
  1. +38 −26 src/app/qgsmaptoolfeatureaction.cpp
  2. +2 −0 src/app/qgsmaptoolfeatureaction.h
@@ -15,7 +15,6 @@

#include "qgsmaptoolfeatureaction.h"

#include "qgsfeature.h"
#include "qgsfeatureiterator.h"
#include "qgsfields.h"
#include "qgsgeometry.h"
@@ -121,36 +120,49 @@ bool QgsMapToolFeatureAction::doAction( QgsVectorLayer *layer, int x, int y )
QgsDebugMsg( QStringLiteral( "Caught CRS exception %1" ).arg( cse.what() ) );
}

QgsAction defaultAction = layer->actions()->defaultAction( QStringLiteral( "Canvas" ) );

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

int numberOfFeatures = 0;
while ( fit.nextFeature( feat ) )
{
if ( defaultAction.isValid() )
{
// define custom substitutions: layer id and clicked coords
QgsExpressionContext context;
context << QgsExpressionContextUtils::globalScope()
<< QgsExpressionContextUtils::projectScope( QgsProject::instance() )
<< QgsExpressionContextUtils::mapSettingsScope( mCanvas->mapSettings() );
QgsExpressionContextScope *actionScope = new QgsExpressionContextScope();
actionScope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "click_x" ), point.x(), true ) );
actionScope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "click_y" ), point.y(), true ) );
actionScope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "action_scope" ), QStringLiteral( "Canvas" ), true ) );
context << actionScope;

defaultAction.run( layer, feat, context );
}
else
{
QgsMapLayerAction *mapLayerAction = QgsGui::mapLayerActionRegistry()->defaultActionForLayer( layer );
if ( mapLayerAction )
{
mapLayerAction->triggerForFeature( layer, &feat );
}
}
QAction *featureAction = featureMenu->addAction( FID_TO_STRING( feat.id() ) );
connect( featureAction, &QAction::triggered, this, [ = ] { doActionForFeature( layer, feat, point );} );
numberOfFeatures++;
}

if ( numberOfFeatures == 0 )
return false;
else
featureMenu->exec( point.toQPointF().toPoint() );
return true;
}

void QgsMapToolFeatureAction::doActionForFeature( QgsVectorLayer *layer, QgsFeature feat, QgsPointXY point )
{
QgsAction defaultAction = layer->actions()->defaultAction( QStringLiteral( "Canvas" ) );
if ( defaultAction.isValid() )
{
// define custom substitutions: layer id and clicked coords
QgsExpressionContext context;
context << QgsExpressionContextUtils::globalScope()
<< QgsExpressionContextUtils::projectScope( QgsProject::instance() )
<< QgsExpressionContextUtils::mapSettingsScope( mCanvas->mapSettings() );
QgsExpressionContextScope *actionScope = new QgsExpressionContextScope();
actionScope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "click_x" ), point.x(), true ) );
actionScope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "click_y" ), point.y(), true ) );
actionScope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "action_scope" ), QStringLiteral( "Canvas" ), true ) );
context << actionScope;

defaultAction.run( layer, feat, context );
}
else
{
QgsMapLayerAction *mapLayerAction = QgsGui::mapLayerActionRegistry()->defaultActionForLayer( layer );
if ( mapLayerAction )
{
mapLayerAction->triggerForFeature( layer, &feat );
}
}
}
@@ -18,6 +18,7 @@

#include "qgis.h"
#include "qgsmaptool.h"
#include "qgsfeature.h"

#include <QObject>
#include <QPointer>
@@ -52,6 +53,7 @@ class APP_EXPORT QgsMapToolFeatureAction : public QgsMapTool

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

#endif

0 comments on commit 5bbb1c4

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