15
15
16
16
#include " qgsmaptoolfeatureaction.h"
17
17
18
- #include " qgsfeature.h"
19
18
#include " qgsfeatureiterator.h"
20
19
#include " qgsfields.h"
21
20
#include " qgsgeometry.h"
@@ -96,6 +95,7 @@ bool QgsMapToolFeatureAction::doAction( QgsVectorLayer *layer, int x, int y )
96
95
return false ;
97
96
98
97
QgsPointXY point = mCanvas ->getCoordinateTransform ()->toMapCoordinates ( x, y );
98
+ QPoint position = mCanvas ->mapToGlobal ( QPoint ( x + 5 , y + 5 ) );
99
99
100
100
QgsRectangle r;
101
101
@@ -121,36 +121,67 @@ bool QgsMapToolFeatureAction::doAction( QgsVectorLayer *layer, int x, int y )
121
121
QgsDebugMsg ( QStringLiteral ( " Caught CRS exception %1" ).arg ( cse.what () ) );
122
122
}
123
123
124
- QgsAction defaultAction = layer-> actions ()-> defaultAction ( QStringLiteral ( " Canvas " ) ) ;
125
-
124
+ QgsFeature f ;
125
+ QgsFeatureList features;
126
126
QgsFeatureIterator fit = layer->getFeatures ( QgsFeatureRequest ().setFilterRect ( r ).setFlags ( QgsFeatureRequest::ExactIntersect ) );
127
- QgsFeature feat;
128
- while ( fit.nextFeature ( feat ) )
127
+ while ( fit.nextFeature ( f ) )
128
+ {
129
+ features.append ( f );
130
+ }
131
+
132
+ if ( !features.isEmpty () )
129
133
{
130
- if ( defaultAction. isValid () )
134
+ if ( features. count () == 1 )
131
135
{
132
- // define custom substitutions: layer id and clicked coords
133
- QgsExpressionContext context;
134
- context << QgsExpressionContextUtils::globalScope ()
135
- << QgsExpressionContextUtils::projectScope ( QgsProject::instance () )
136
- << QgsExpressionContextUtils::mapSettingsScope ( mCanvas ->mapSettings () );
137
- QgsExpressionContextScope *actionScope = new QgsExpressionContextScope ();
138
- actionScope->addVariable ( QgsExpressionContextScope::StaticVariable ( QStringLiteral ( " click_x" ), point.x (), true ) );
139
- actionScope->addVariable ( QgsExpressionContextScope::StaticVariable ( QStringLiteral ( " click_y" ), point.y (), true ) );
140
- actionScope->addVariable ( QgsExpressionContextScope::StaticVariable ( QStringLiteral ( " action_scope" ), QStringLiteral ( " Canvas" ), true ) );
141
- context << actionScope;
142
-
143
- defaultAction.run ( layer, feat, context );
136
+ doActionForFeature ( layer, features.first (), point );
144
137
}
145
138
else
146
139
{
147
- QgsMapLayerAction *mapLayerAction = QgsGui::mapLayerActionRegistry ()-> defaultActionForLayer ( layer );
148
- if ( mapLayerAction )
140
+ QMenu *featureMenu = new QMenu ( );
141
+ for ( const QgsFeature &feature : qgis::as_const ( features ) )
149
142
{
150
- mapLayerAction->triggerForFeature ( layer, &feat );
143
+ QAction *featureAction = featureMenu->addAction ( FID_TO_STRING ( feature.id () ) );
144
+ connect ( featureAction, &QAction::triggered, this , [ = ] { doActionForFeature ( layer, feature, point );} );
151
145
}
146
+ QAction *allFeatureAction = featureMenu->addAction ( tr ( " All Features" ) );
147
+ connect ( allFeatureAction, &QAction::triggered, this , [ = ]
148
+ {
149
+ for ( const QgsFeature &feature : qgis::as_const ( features ) )
150
+ {
151
+ doActionForFeature ( layer, feature, point );
152
+ }
153
+ } );
154
+ featureMenu->exec ( position );
152
155
}
156
+ return true ;
153
157
}
158
+ return false ;
159
+ }
154
160
155
- return true ;
161
+ void QgsMapToolFeatureAction::doActionForFeature ( QgsVectorLayer *layer, const QgsFeature &feature, const QgsPointXY &point )
162
+ {
163
+ QgsAction defaultAction = layer->actions ()->defaultAction ( QStringLiteral ( " Canvas" ) );
164
+ if ( defaultAction.isValid () )
165
+ {
166
+ // define custom substitutions: layer id and clicked coords
167
+ QgsExpressionContext context;
168
+ context << QgsExpressionContextUtils::globalScope ()
169
+ << QgsExpressionContextUtils::projectScope ( QgsProject::instance () )
170
+ << QgsExpressionContextUtils::mapSettingsScope ( mCanvas ->mapSettings () );
171
+ QgsExpressionContextScope *actionScope = new QgsExpressionContextScope ();
172
+ actionScope->addVariable ( QgsExpressionContextScope::StaticVariable ( QStringLiteral ( " click_x" ), point.x (), true ) );
173
+ actionScope->addVariable ( QgsExpressionContextScope::StaticVariable ( QStringLiteral ( " click_y" ), point.y (), true ) );
174
+ actionScope->addVariable ( QgsExpressionContextScope::StaticVariable ( QStringLiteral ( " action_scope" ), QStringLiteral ( " Canvas" ), true ) );
175
+ context << actionScope;
176
+
177
+ defaultAction.run ( layer, feature, context );
178
+ }
179
+ else
180
+ {
181
+ QgsMapLayerAction *mapLayerAction = QgsGui::mapLayerActionRegistry ()->defaultActionForLayer ( layer );
182
+ if ( mapLayerAction )
183
+ {
184
+ mapLayerAction->triggerForFeature ( layer, &feature );
185
+ }
186
+ }
156
187
}
0 commit comments