Skip to content

Commit

Permalink
action only starting on one feature
Browse files Browse the repository at this point in the history
with selection context menu (with wrong position)
  • Loading branch information
signedav committed Oct 18, 2018
1 parent 4010e79 commit 5bbb1c4
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 26 deletions.
64 changes: 38 additions & 26 deletions src/app/qgsmaptoolfeatureaction.cpp
Expand Up @@ -15,7 +15,6 @@

#include "qgsmaptoolfeatureaction.h"

#include "qgsfeature.h"
#include "qgsfeatureiterator.h"
#include "qgsfields.h"
#include "qgsgeometry.h"
Expand Down Expand Up @@ -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 );
}
}
}
2 changes: 2 additions & 0 deletions src/app/qgsmaptoolfeatureaction.h
Expand Up @@ -18,6 +18,7 @@

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

#include <QObject>
#include <QPointer>
Expand Down Expand Up @@ -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.