Skip to content

Commit b38e183

Browse files
committed
start action for single feature directly
1 parent 5bbb1c4 commit b38e183

File tree

2 files changed

+26
-16
lines changed

2 files changed

+26
-16
lines changed

src/app/qgsmaptoolfeatureaction.cpp

+25-15
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ void QgsMapToolFeatureAction::canvasReleaseEvent( QgsMapMouseEvent *e )
7575
return;
7676
}
7777

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

@@ -89,7 +89,7 @@ void QgsMapToolFeatureAction::deactivate()
8989
QgsMapTool::deactivate();
9090
}
9191

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

123-
QMenu *featureMenu = new QMenu();
123+
QgsFeature f;
124+
QgsFeatureList features;
124125
QgsFeatureIterator fit = layer->getFeatures( QgsFeatureRequest().setFilterRect( r ).setFlags( QgsFeatureRequest::ExactIntersect ) );
125-
QgsFeature feat;
126-
127-
int numberOfFeatures = 0;
128-
while ( fit.nextFeature( feat ) )
126+
while ( fit.nextFeature( f ) )
129127
{
130-
QAction *featureAction = featureMenu->addAction( FID_TO_STRING( feat.id() ) );
131-
connect( featureAction, &QAction::triggered, this, [ = ] { doActionForFeature( layer, feat, point );} );
132-
numberOfFeatures++;
128+
features.append( f );
133129
}
134130

135-
if ( numberOfFeatures == 0 )
136-
return false;
137-
else
138-
featureMenu->exec( point.toQPointF().toPoint() );
139-
return true;
131+
if ( !features.isEmpty() )
132+
{
133+
if ( features.count() == 1 )
134+
{
135+
doActionForFeature( layer, features.first(), point );
136+
}
137+
else
138+
{
139+
QMenu *featureMenu = new QMenu();
140+
for ( int i = 0; i < features.count(); i++ )
141+
{
142+
QAction *featureAction = featureMenu->addAction( FID_TO_STRING( features.at( i ).id() ) );
143+
connect( featureAction, &QAction::triggered, this, [ = ] { doActionForFeature( layer, features.at( i ), point );} );
144+
}
145+
featureMenu->exec( pixelpos );
146+
}
147+
return true;
148+
}
149+
return false;
140150
}
141151

142152
void QgsMapToolFeatureAction::doActionForFeature( QgsVectorLayer *layer, QgsFeature feat, QgsPointXY point )

src/app/qgsmaptoolfeatureaction.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ class APP_EXPORT QgsMapToolFeatureAction : public QgsMapTool
5252
void deactivate() override;
5353

5454
private:
55-
bool doAction( QgsVectorLayer *layer, int x, int y );
55+
bool doAction( QgsVectorLayer *layer, int x, int y, QPoint pixelpos );
5656
void doActionForFeature( QgsVectorLayer *layer, QgsFeature feat, QgsPointXY point );
5757
};
5858

0 commit comments

Comments
 (0)