Skip to content
Permalink
Browse files

QgsMapLayerAction: add legend context menu entry for action on select…

…ion (#34354)

A new menu entry (Actions on selection) is added to the layer tree context menu
It is shown only when required (layer has selection and actions can be run on the given layer)
  • Loading branch information
3nids committed Feb 12, 2020
1 parent 990c0e2 commit 4980adbe19e06c47c21d62143a57b56dd070e4a8
@@ -76,7 +76,7 @@ True if action can run using the specified layer
Triggers the action with the specified layer and list of feature.
%End

void triggerForFeature( QgsMapLayer *layer, const QgsFeature *feature );
void triggerForFeature( QgsMapLayer *layer, const QgsFeature &feature );
%Docstring
Triggers the action with the specified layer and feature.
%End
@@ -12,16 +12,17 @@
* (at your option) any later version. *
* *
***************************************************************************/
#include <QClipboard>

#include "qgsapplayertreeviewmenuprovider.h"


#include "qgisapp.h"
#include "qgsapplication.h"
#include "qgsclipboard.h"
#include "qgscolorwidgets.h"
#include "qgscolorschemeregistry.h"
#include "qgscolorswatchgrid.h"
#include "qgsgui.h"
#include "qgslayertree.h"
#include "qgslayertreemodel.h"
#include "qgslayertreemodellegendnode.h"
@@ -42,7 +43,7 @@
#include "qgssymbollayerutils.h"
#include "qgsxmlutils.h"

#include <QClipboard>


QgsAppLayerTreeViewMenuProvider::QgsAppLayerTreeViewMenuProvider( QgsLayerTreeView *view, QgsMapCanvas *canvas )
: mView( view )
@@ -272,6 +273,34 @@ QMenu *QgsAppLayerTreeViewMenuProvider::createContextMenu()
menu->addAction( a );
}

// actions on the selection
if ( vlayer && vlayer->selectedFeatureCount() > 0 )
{
int selectionCount = vlayer->selectedFeatureCount();
QgsMapLayerAction::Target target;
if ( selectionCount == 1 )
target = QgsMapLayerAction::Target::SingleFeature;
else
target = QgsMapLayerAction::Target::MultipleFeatures;

const QList<QgsMapLayerAction *> constRegisteredActions = QgsGui::mapLayerActionRegistry()->mapLayerActions( vlayer, target );
if ( !constRegisteredActions.isEmpty() )
{
QMenu *actionMenu = menu->addMenu( tr( "Actions on Selection (%1)" ).arg( selectionCount ) );
for ( QgsMapLayerAction *action : constRegisteredActions )
{
if ( target == QgsMapLayerAction::Target::SingleFeature )
{
actionMenu->addAction( action->text(), action, [ = ]() { action->triggerForFeature( vlayer, vlayer->selectedFeatures().at( 0 ) ); } );
}
else if ( target == QgsMapLayerAction::Target::MultipleFeatures )
{
actionMenu->addAction( action->text(), action, [ = ]() {action->triggerForFeatures( vlayer, vlayer->selectedFeatures() );} );
}
}
}
}

menu->addSeparator();

if ( layer && layer->isSpatial() )
@@ -1479,7 +1479,7 @@ void QgsIdentifyResultsDialog::doMapLayerAction( QTreeWidgetItem *item, QgsMapLa
return;

int featIdx = featItem->data( 0, Qt::UserRole + 1 ).toInt();
action->triggerForFeature( layer, &mFeatures[ featIdx ] );
action->triggerForFeature( layer, mFeatures[ featIdx ] );
}

QTreeWidgetItem *QgsIdentifyResultsDialog::featureItem( QTreeWidgetItem *item )
@@ -2186,7 +2186,7 @@ void QgsIdentifyResultsDialog::formatChanged( int index )

void QgsIdentifyResultsDialogMapLayerAction::execute()
{
mAction->triggerForFeature( mLayer, mFeature );
mAction->triggerForFeature( mLayer, *mFeature );
}

void QgsIdentifyResultsDialog::showHelp()
@@ -182,7 +182,7 @@ void QgsMapToolFeatureAction::doActionForFeature( QgsVectorLayer *layer, const Q
QgsMapLayerAction *mapLayerAction = QgsGui::mapLayerActionRegistry()->defaultActionForLayer( layer );
if ( mapLayerAction )
{
mapLayerAction->triggerForFeature( layer, &feature );
mapLayerAction->triggerForFeature( layer, feature );
}
}
}
@@ -846,7 +846,7 @@ void QgsAttributeTableModel::executeAction( QUuid action, const QModelIndex &idx
void QgsAttributeTableModel::executeMapLayerAction( QgsMapLayerAction *action, const QModelIndex &idx ) const
{
QgsFeature f = feature( idx );
action->triggerForFeature( layer(), &f );
action->triggerForFeature( layer(), f );
}

QgsFeature QgsAttributeTableModel::feature( const QModelIndex &idx ) const
@@ -455,7 +455,7 @@ void QgsAttributeTableView::actionTriggered()
QgsMapLayerAction *layerAction = qobject_cast<QgsMapLayerAction *>( object );
if ( layerAction )
{
layerAction->triggerForFeature( mFilterModel->layer(), &f );
layerAction->triggerForFeature( mFilterModel->layer(), f );
}
}
}
@@ -93,7 +93,7 @@ void QgsActionMenu::triggerAction()
if ( data.actionType == MapLayerAction )
{
QgsMapLayerAction *mapLayerAction = data.actionData.value<QgsMapLayerAction *>();
mapLayerAction->triggerForFeature( data.mapLayer, &mFeature );
mapLayerAction->triggerForFeature( data.mapLayer, mFeature );
}
else if ( data.actionType == AttributeAction )
{
@@ -504,7 +504,7 @@ void QgsIdentifyMenu::triggerMapLayerAction()
{
if ( result.mFeature.id() == actData.mFeatureId )
{
actData.mMapLayerAction->triggerForFeature( actData.mLayer, new QgsFeature( result.mFeature ) );
actData.mMapLayerAction->triggerForFeature( actData.mLayer, result.mFeature );
return;
}
}
@@ -92,9 +92,9 @@ void QgsMapLayerAction::triggerForFeatures( QgsMapLayer *layer, const QList<QgsF
emit triggeredForFeatures( layer, featureList );
}

void QgsMapLayerAction::triggerForFeature( QgsMapLayer *layer, const QgsFeature *feature )
void QgsMapLayerAction::triggerForFeature( QgsMapLayer *layer, const QgsFeature &feature )
{
emit triggeredForFeature( layer, *feature );
emit triggeredForFeature( layer, feature );
}

void QgsMapLayerAction::triggerForLayer( QgsMapLayer *layer )
@@ -89,7 +89,7 @@ class GUI_EXPORT QgsMapLayerAction : public QAction
void triggerForFeatures( QgsMapLayer *layer, const QList<QgsFeature> &featureList );

//! Triggers the action with the specified layer and feature.
void triggerForFeature( QgsMapLayer *layer, const QgsFeature *feature );
void triggerForFeature( QgsMapLayer *layer, const QgsFeature &feature );

//! Triggers the action with the specified layer.
void triggerForLayer( QgsMapLayer *layer );

0 comments on commit 4980adb

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