Skip to content

Commit

Permalink
Add a context for QgsMapLayerAction
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Nov 23, 2022
1 parent 45ed5f7 commit b6badb6
Show file tree
Hide file tree
Showing 17 changed files with 269 additions and 32 deletions.
Expand Up @@ -170,7 +170,7 @@ Returns the layer cache this model uses as backend.
Execute an action
%End

void executeMapLayerAction( QgsMapLayerAction *action, const QModelIndex &idx ) const;
void executeMapLayerAction( QgsMapLayerAction *action, const QModelIndex &idx, const QgsMapLayerActionContext &context = QgsMapLayerActionContext() ) const;
%Docstring
Execute a :py:class:`QgsMapLayerAction`
%End
Expand Down
90 changes: 84 additions & 6 deletions python/gui/auto_generated/qgsmaplayeractionregistry.sip.in
Expand Up @@ -10,6 +10,23 @@



class QgsMapLayerActionContext
{
%Docstring(signature="appended")
Encapsulates the context in which a :py:class:`QgsMapLayerAction` action is executed.

.. versionadded:: 3.30
%End

%TypeHeaderCode
#include "qgsmaplayeractionregistry.h"
%End
public:


};


class QgsMapLayerAction : QAction
{
%Docstring(signature="appended")
Expand Down Expand Up @@ -74,19 +91,49 @@ Layer behavior flags.
True if action can run using the specified layer
%End

virtual void triggerForFeatures( QgsMapLayer *layer, const QList<QgsFeature> &featureList );
virtual void triggerForFeatures( QgsMapLayer *layer, const QList<QgsFeature> &featureList ) /Deprecated/;
%Docstring
Triggers the action with the specified layer and list of feature.

.. deprecated::
use the version with :py:class:`QgsMapLayerActionContext` instead.
%End

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

.. deprecated::
use the version with :py:class:`QgsMapLayerActionContext` instead.
%End

virtual void triggerForLayer( QgsMapLayer *layer );
virtual void triggerForLayer( QgsMapLayer *layer ) /Deprecated/;
%Docstring
Triggers the action with the specified layer.

.. deprecated::
use the version with :py:class:`QgsMapLayerActionContext` instead.
%End

virtual void triggerForFeatures( QgsMapLayer *layer, const QList<QgsFeature> &featureList, const QgsMapLayerActionContext &context );
%Docstring
Triggers the action with the specified layer and list of feature.

.. versionadded:: 3.30
%End

virtual void triggerForFeature( QgsMapLayer *layer, const QgsFeature &feature, const QgsMapLayerActionContext &context );
%Docstring
Triggers the action with the specified layer and feature.

.. versionadded:: 3.30
%End

virtual void triggerForLayer( QgsMapLayer *layer, const QgsMapLayerActionContext &context );
%Docstring
Triggers the action with the specified layer.

.. versionadded:: 3.30
%End

void setTargets( Targets targets );
Expand All @@ -106,19 +153,50 @@ Returns ``True`` if the action is only enabled for layers in editable mode.
%End

signals:
void triggeredForFeatures( QgsMapLayer *layer, const QList<QgsFeature> &featureList );

void triggeredForFeatures( QgsMapLayer *layer, const QList<QgsFeature> &featureList ) /Deprecated/;
%Docstring
Triggered when action has been run for a specific list of features

.. deprecated::
use the version with :py:class:`QgsMapLayerActionContext` instead.
%End

void triggeredForFeature( QgsMapLayer *layer, const QgsFeature &feature );
void triggeredForFeature( QgsMapLayer *layer, const QgsFeature &feature ) /Deprecated/;
%Docstring
Triggered when action has been run for a specific feature

.. deprecated::
use the version with :py:class:`QgsMapLayerActionContext` instead.
%End

void triggeredForLayer( QgsMapLayer *layer );
void triggeredForLayer( QgsMapLayer *layer ) /Deprecated/;
%Docstring
Triggered when action has been run for a specific layer

.. deprecated::
use the version with :py:class:`QgsMapLayerActionContext` instead.
%End

void triggeredForFeaturesV2( QgsMapLayer *layer, const QList<QgsFeature> &featureList, const QgsMapLayerActionContext &context );
%Docstring
Triggered when action has been run for a specific list of features

.. versionadded:: 3.30
%End

void triggeredForFeatureV2( QgsMapLayer *layer, const QgsFeature &feature, const QgsMapLayerActionContext &context );
%Docstring
Triggered when action has been run for a specific feature.

.. versionadded:: 3.30
%End

void triggeredForLayerV2( QgsMapLayer *layer, const QgsMapLayerActionContext &context );
%Docstring
Triggered when action has been run for a specific layer.

.. versionadded:: 3.30
%End

};
Expand Down
6 changes: 3 additions & 3 deletions src/app/qgisapp.cpp
Expand Up @@ -9213,7 +9213,7 @@ void QgisApp::setupDuplicateFeaturesAction()
QgsApplication::getThemeIcon( QStringLiteral( "/mActionDuplicateFeature.svg" ) ), QgsMapLayerAction::EnabledOnlyWhenEditable ) );

QgsGui::mapLayerActionRegistry()->addMapLayerAction( mDuplicateFeatureAction.get() );
connect( mDuplicateFeatureAction.get(), &QgsMapLayerAction::triggeredForFeature, this, [this]( QgsMapLayer * layer, const QgsFeature & feat )
connect( mDuplicateFeatureAction.get(), &QgsMapLayerAction::triggeredForFeatureV2, this, [this]( QgsMapLayer * layer, const QgsFeature & feat, const QgsMapLayerActionContext & )
{
duplicateFeatures( layer, feat );
}
Expand All @@ -9224,7 +9224,7 @@ void QgisApp::setupDuplicateFeaturesAction()
QgsApplication::getThemeIcon( QStringLiteral( "/mActionDuplicateFeatureDigitized.svg" ) ), QgsMapLayerAction::EnabledOnlyWhenEditable ) );

QgsGui::mapLayerActionRegistry()->addMapLayerAction( mDuplicateFeatureDigitizeAction.get() );
connect( mDuplicateFeatureDigitizeAction.get(), &QgsMapLayerAction::triggeredForFeature, this, [this]( QgsMapLayer * layer, const QgsFeature & feat )
connect( mDuplicateFeatureDigitizeAction.get(), &QgsMapLayerAction::triggeredForFeatureV2, this, [this]( QgsMapLayer * layer, const QgsFeature & feat, const QgsMapLayerActionContext & )
{
duplicateFeatureDigitized( layer, feat );
}
Expand All @@ -9249,7 +9249,7 @@ void QgisApp::setupAtlasMapLayerAction( QgsPrintLayout *layout, bool enableActio
QgsApplication::getThemeIcon( QStringLiteral( "/mIconAtlas.svg" ) ) );
mAtlasFeatureActions.insert( layout, action );
QgsGui::mapLayerActionRegistry()->addMapLayerAction( action );
connect( action, &QgsMapLayerAction::triggeredForFeature, this, [this, layout]( QgsMapLayer * layer, const QgsFeature & feat )
connect( action, &QgsMapLayerAction::triggeredForFeatureV2, this, [this, layout]( QgsMapLayer * layer, const QgsFeature & feat, const QgsMapLayerActionContext & )
{
Q_UNUSED( layer )
setLayoutAtlasFeature( layout, feat );
Expand Down
17 changes: 15 additions & 2 deletions src/app/qgsapplayertreeviewmenuprovider.cpp
Expand Up @@ -417,15 +417,28 @@ QMenu *QgsAppLayerTreeViewMenuProvider::createContextMenu()
if ( !constRegisteredActions.isEmpty() )
{
QMenu *actionMenu = menu->addMenu( tr( "Actions on Selection (%1)" ).arg( selectionCount ) );
QgsMapLayerActionContext context;
for ( QgsMapLayerAction *action : constRegisteredActions )
{
if ( target == QgsMapLayerAction::Target::SingleFeature )
{
actionMenu->addAction( action->text(), action, [ = ]() { action->triggerForFeature( vlayer, vlayer->selectedFeatures().at( 0 ) ); } );
actionMenu->addAction( action->text(), action, [ = ]()
{
Q_NOWARN_DEPRECATED_PUSH
action->triggerForFeature( vlayer, vlayer->selectedFeatures().at( 0 ) );
Q_NOWARN_DEPRECATED_POP
action->triggerForFeature( vlayer, vlayer->selectedFeatures().at( 0 ), context );
} );
}
else if ( target == QgsMapLayerAction::Target::MultipleFeatures )
{
actionMenu->addAction( action->text(), action, [ = ]() {action->triggerForFeatures( vlayer, vlayer->selectedFeatures() );} );
actionMenu->addAction( action->text(), action, [ = ]()
{
Q_NOWARN_DEPRECATED_PUSH
action->triggerForFeatures( vlayer, vlayer->selectedFeatures() );
Q_NOWARN_DEPRECATED_POP
action->triggerForFeatures( vlayer, vlayer->selectedFeatures(), context );
} );
}
}
}
Expand Down
1 change: 1 addition & 0 deletions src/app/qgsfeatureaction.h
Expand Up @@ -23,6 +23,7 @@
#include <QPair>
#include <QAction>
#include <QUuid>
#include <QPointer>
#include "qgis_app.h"
#include "qgshighlight.h"

Expand Down
8 changes: 8 additions & 0 deletions src/app/qgsidentifyresultsdialog.cpp
Expand Up @@ -1749,7 +1749,11 @@ void QgsIdentifyResultsDialog::doMapLayerAction( QTreeWidgetItem *item, QgsMapLa
return;

const QgsFeature feat = featItem->data( 0, FeatureRole ).value< QgsFeature >();
QgsMapLayerActionContext context;
Q_NOWARN_DEPRECATED_PUSH
action->triggerForFeature( layer, feat );
Q_NOWARN_DEPRECATED_POP
action->triggerForFeature( layer, feat, context );
}

QTreeWidgetItem *QgsIdentifyResultsDialog::featureItem( QTreeWidgetItem *item )
Expand Down Expand Up @@ -2531,7 +2535,11 @@ void QgsIdentifyResultsDialog::formatChanged( int index )

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

void QgsIdentifyResultsDialog::showHelp()
Expand Down
4 changes: 4 additions & 0 deletions src/app/qgsmaptoolfeatureaction.cpp
Expand Up @@ -191,7 +191,11 @@ void QgsMapToolFeatureAction::doActionForFeature( QgsVectorLayer *layer, const Q
QgsMapLayerAction *mapLayerAction = QgsGui::mapLayerActionRegistry()->defaultActionForLayer( layer );
if ( mapLayerAction )
{
QgsMapLayerActionContext context;
Q_NOWARN_DEPRECATED_PUSH
mapLayerAction->triggerForFeature( layer, feature );
Q_NOWARN_DEPRECATED_POP
mapLayerAction->triggerForFeature( layer, feature, context );
}
}
}
4 changes: 2 additions & 2 deletions src/app/qgsmaptoolidentifyaction.cpp
Expand Up @@ -49,7 +49,7 @@ QgsMapToolIdentifyAction::QgsMapToolIdentifyAction( QgsMapCanvas *canvas )
connect( this, &QgsMapToolIdentify::changedRasterResults, this, &QgsMapToolIdentifyAction::handleChangedRasterResults );
mIdentifyMenu->setAllowMultipleReturn( true );
QgsMapLayerAction *attrTableAction = new QgsMapLayerAction( tr( "Show Attribute Table" ), mIdentifyMenu, QgsMapLayerType::VectorLayer, QgsMapLayerAction::MultipleFeatures );
connect( attrTableAction, &QgsMapLayerAction::triggeredForFeatures, this, &QgsMapToolIdentifyAction::showAttributeTable );
connect( attrTableAction, &QgsMapLayerAction::triggeredForFeaturesV2, this, &QgsMapToolIdentifyAction::showAttributeTable );
identifyMenu()->addCustomAction( attrTableAction );
mSelectionHandler = new QgsMapToolSelectionHandler( canvas, QgsMapToolSelectionHandler::SelectSimple );
connect( mSelectionHandler, &QgsMapToolSelectionHandler::geometryChanged, this, &QgsMapToolIdentifyAction::identifyFromGeometry );
Expand Down Expand Up @@ -82,7 +82,7 @@ QgsIdentifyResultsDialog *QgsMapToolIdentifyAction::resultsDialog()
return mResultsDialog;
}

void QgsMapToolIdentifyAction::showAttributeTable( QgsMapLayer *layer, const QList<QgsFeature> &featureList )
void QgsMapToolIdentifyAction::showAttributeTable( QgsMapLayer *layer, const QList<QgsFeature> &featureList, const QgsMapLayerActionContext & )
{
resultsDialog()->clear();

Expand Down
3 changes: 2 additions & 1 deletion src/app/qgsmaptoolidentifyaction.h
Expand Up @@ -30,6 +30,7 @@ class QgsMapToolSelectionHandler;
class QgsRasterLayer;
class QgsVectorLayer;
class QgsFeatureStore;
class QgsMapLayerActionContext;

/**
* \brief Map tool for identifying features layers and showing results
Expand Down Expand Up @@ -83,7 +84,7 @@ class APP_EXPORT QgsMapToolIdentifyAction : public QgsMapToolIdentify
void copyToClipboard( QgsFeatureStore & );

private slots:
void showAttributeTable( QgsMapLayer *layer, const QList<QgsFeature> &featureList );
void showAttributeTable( QgsMapLayer *layer, const QList<QgsFeature> &featureList, const QgsMapLayerActionContext &context );

void identifyFromGeometry();

Expand Down
5 changes: 4 additions & 1 deletion src/gui/attributetable/qgsattributetablemodel.cpp
Expand Up @@ -909,10 +909,13 @@ void QgsAttributeTableModel::executeAction( QUuid action, const QModelIndex &idx
mLayer->actions()->doAction( action, f, fieldIdx( idx.column() ) );
}

void QgsAttributeTableModel::executeMapLayerAction( QgsMapLayerAction *action, const QModelIndex &idx ) const
void QgsAttributeTableModel::executeMapLayerAction( QgsMapLayerAction *action, const QModelIndex &idx, const QgsMapLayerActionContext &context ) const
{
const QgsFeature f = feature( idx );
Q_NOWARN_DEPRECATED_PUSH
action->triggerForFeature( mLayer, f );
Q_NOWARN_DEPRECATED_POP
action->triggerForFeature( mLayer, f, context );
}

QgsFeature QgsAttributeTableModel::feature( const QModelIndex &idx ) const
Expand Down
4 changes: 2 additions & 2 deletions src/gui/attributetable/qgsattributetablemodel.h
Expand Up @@ -27,10 +27,10 @@

#include "qgsconditionalstyle.h"
#include "qgsattributeeditorcontext.h"
#include "qgsmaplayeractionregistry.h"
#include "qgis_gui.h"

class QgsMapCanvas;
class QgsMapLayerAction;
class QgsEditorWidgetFactory;
class QgsFieldFormatter;
class QgsVectorLayerCache;
Expand Down Expand Up @@ -180,7 +180,7 @@ class GUI_EXPORT QgsAttributeTableModel: public QAbstractTableModel
/**
* Execute a QgsMapLayerAction
*/
void executeMapLayerAction( QgsMapLayerAction *action, const QModelIndex &idx ) const;
void executeMapLayerAction( QgsMapLayerAction *action, const QModelIndex &idx, const QgsMapLayerActionContext &context = QgsMapLayerActionContext() ) const;

/**
* Returns the feature attributes at given model index
Expand Down
4 changes: 4 additions & 0 deletions src/gui/attributetable/qgsattributetableview.cpp
Expand Up @@ -497,7 +497,11 @@ void QgsAttributeTableView::actionTriggered()
QgsMapLayerAction *layerAction = qobject_cast<QgsMapLayerAction *>( object );
if ( layerAction )
{
QgsMapLayerActionContext context;
Q_NOWARN_DEPRECATED_PUSH
layerAction->triggerForFeature( mFilterModel->layer(), f );
Q_NOWARN_DEPRECATED_POP
layerAction->triggerForFeature( mFilterModel->layer(), f, context );
}
}
}
Expand Down
12 changes: 10 additions & 2 deletions src/gui/attributetable/qgsdualview.cpp
Expand Up @@ -897,9 +897,16 @@ void QgsDualView::viewWillShowContextMenu( QMenu *menu, const QModelIndex &maste
QAction *action = menu->addAction( tr( "Actions on Selection (%1)" ).arg( mLayer->selectedFeatureCount() ) );
action->setEnabled( false );

QgsMapLayerActionContext context;
for ( QgsMapLayerAction *action : registeredActions )
{
menu->addAction( action->text(), action, [ = ]() {action->triggerForFeatures( mLayer, mLayer->selectedFeatures() );} );
menu->addAction( action->text(), action, [ = ]()
{
Q_NOWARN_DEPRECATED_PUSH
action->triggerForFeatures( mLayer, mLayer->selectedFeatures() );
Q_NOWARN_DEPRECATED_POP
action->triggerForFeatures( mLayer, mLayer->selectedFeatures(), context );
} );
}
}
}
Expand Down Expand Up @@ -1347,5 +1354,6 @@ void QgsAttributeTableAction::featureForm()

void QgsAttributeTableMapLayerAction::execute()
{
mDualView->masterModel()->executeMapLayerAction( mAction, mFieldIdx );
QgsMapLayerActionContext context;
mDualView->masterModel()->executeMapLayerAction( mAction, mFieldIdx, context );
}
5 changes: 5 additions & 0 deletions src/gui/qgsactionmenu.cpp
Expand Up @@ -93,7 +93,12 @@ void QgsActionMenu::triggerAction()
if ( data.actionType == MapLayerAction )
{
QgsMapLayerAction *mapLayerAction = data.actionData.value<QgsMapLayerAction *>();

QgsMapLayerActionContext context;
Q_NOWARN_DEPRECATED_PUSH
mapLayerAction->triggerForFeature( data.mapLayer, mFeature );
Q_NOWARN_DEPRECATED_POP
mapLayerAction->triggerForFeature( data.mapLayer, mFeature, context );
}
else if ( data.actionType == AttributeAction )
{
Expand Down

0 comments on commit b6badb6

Please sign in to comment.