Skip to content
Permalink
Browse files

[QgsActionMenu] use ActionData to trigger actions

  • Loading branch information
3nids committed Aug 28, 2014
1 parent ab22890 commit f85a883976b6b79887893352f703d7158b4ed6f1
Showing with 52 additions and 63 deletions.
  1. +33 −42 src/gui/qgsactionmenu.cpp
  2. +19 −21 src/gui/qgsactionmenu.h
@@ -21,8 +21,6 @@
QgsActionMenu::QgsActionMenu( QgsVectorLayer* layer, const QgsFeature* feature, QWidget* parent )
: QMenu( parent )
, mLayer( layer )
, mAttributeActionSignalMapper( 0 )
, mMapLayerActionSignalMapper( 0 )
, mActions( 0 )
, mFeature( feature )
, mFeatureId( feature->id() )
@@ -32,14 +30,12 @@ QgsActionMenu::QgsActionMenu( QgsVectorLayer* layer, const QgsFeature* feature,
}

QgsActionMenu::QgsActionMenu( QgsVectorLayer* layer, const QgsFeatureId fid, QWidget* parent )
: QMenu( parent )
, mLayer( layer )
, mAttributeActionSignalMapper( 0 )
, mMapLayerActionSignalMapper( 0 )
, mActions( 0 )
, mFeature( 0 )
, mFeatureId( fid )
, mOwnsFeature( false )
: QMenu( parent )
, mLayer( layer )
, mActions( 0 )
, mFeature( 0 )
, mFeatureId( fid )
, mOwnsFeature( false )
{
init();
}
@@ -48,7 +44,7 @@ void QgsActionMenu::init()
{
setTitle( tr( "&Actions" ) );

connect( QgsMapLayerActionRegistry::instance(), SIGNAL(changed()), this, SLOT(reloadActions()) );
connect( QgsMapLayerActionRegistry::instance(), SIGNAL( changed() ), this, SLOT( reloadActions() ) );

reloadActions();
}
@@ -88,42 +84,42 @@ void QgsActionMenu::setFeature( QgsFeature* feature )
mFeature = feature;
}

void QgsActionMenu::triggerAttributeAction( int index )
void QgsActionMenu::triggerAction()
{
if ( feature() )
{
mActions->doAction( index, *feature() );
}
else
if ( !feature() )
return;

QAction* action = qobject_cast<QAction*>( sender() );
if ( !action )
return;

if ( !action->data().isValid() || !action->data().canConvert<ActionData>() )
return;

ActionData data = action->data().value<ActionData>();

if ( data.actionType == Invalid )
return;

if ( data.actionType == MapLayerAction )
{
QgsDebugMsg( QString( "Trying to run an action on a non-existing feature with fid %1" ).arg( mFeatureId ) );
QgsMapLayerAction* mapLayerAction = data.actionId.action;
mapLayerAction->triggerForFeature( data.mapLayer, feature() );
}
}

void QgsActionMenu::triggerMapLayerAction( int index )
{
if ( feature() )
else if ( data.actionType == AttributeAction )
{
QgsMapLayerAction* action = QgsMapLayerActionRegistry::instance()->mapLayerActions( mLayer, QgsMapLayerAction::Feature ).at( index );

action->triggerForFeature( mLayer, feature() );
mActions->doAction( data.actionId.id, *feature() );
}
}

void QgsActionMenu::reloadActions()
{
delete mAttributeActionSignalMapper;
mAttributeActionSignalMapper = new QSignalMapper( this );
delete mMapLayerActionSignalMapper;
mMapLayerActionSignalMapper = new QSignalMapper( this );

connect( mAttributeActionSignalMapper, SIGNAL(mapped(int)), this, SLOT(triggerAttributeAction(int)) );
connect( mMapLayerActionSignalMapper, SIGNAL(mapped(int)), this, SLOT(triggerMapLayerAction(int)) );
clear();

delete mActions;
mActions = new QgsAttributeAction( *mLayer->actions() );

for( int idx = 0; idx < mActions->size(); ++idx )
for ( int idx = 0; idx < mActions->size(); ++idx )
{
const QgsAction& qaction( mActions->at( idx ) );

@@ -140,11 +136,7 @@ void QgsActionMenu::reloadActions()
{
action->setToolTip( qaction.action() );
}

mAttributeActionSignalMapper->setMapping( action, idx );

connect( action, SIGNAL(triggered()), mAttributeActionSignalMapper, SLOT(map()) );

connect( action, SIGNAL( triggered() ), this, SLOT( triggerAction() ) );
addAction( action );
}

@@ -159,10 +151,9 @@ void QgsActionMenu::reloadActions()
{
QgsMapLayerAction* qaction = mapLayerActions.at( i );
QAction* action = new QAction( qaction->text(), this );
action->setData( QVariant::fromValue<ActionData>( ActionData( MapLayerAction, mFeatureId, mLayer ) ) );
mMapLayerActionSignalMapper->setMapping( action, i );
action->setData( QVariant::fromValue<ActionData>( ActionData( qaction, mFeatureId, mLayer ) ) );
addAction( action );
connect( action, SIGNAL(triggered()), mMapLayerActionSignalMapper, SLOT(map()) );
connect( action, SIGNAL( triggered() ), this, SLOT( triggerAction() ) );
}
}

@@ -22,6 +22,7 @@
#include "qgsattributeaction.h"
#include "qgsmaplayeractionregistry.h"


/**
* This class is a menu that is populated automatically with the actions defined for a given layer.
*/
@@ -40,37 +41,37 @@ class QgsActionMenu : public QMenu

struct ActionData
{
ActionData()
ActionData()
: actionType( Invalid )
, actionId( 0 )
{}
{}

ActionData( int actionId, QgsFeatureId featureId, QgsMapLayer* mapLayer )
ActionData( int actionId, QgsFeatureId featureId, QgsMapLayer* mapLayer )
: actionType( AttributeAction )
, actionId( actionId )
, featureId( featureId )
, mapLayer( mapLayer )
{}
{}

ActionData( QgsMapLayerAction* action, QgsFeatureId featureId, QgsMapLayer* mapLayer )
: actionType( AttributeAction )
ActionData( QgsMapLayerAction* action, QgsFeatureId featureId, QgsMapLayer* mapLayer )
: actionType( MapLayerAction )
, actionId( action )
, featureId( featureId )
, mapLayer( mapLayer )
{}
{}

ActionType actionType;
ActionType actionType;

union aid
{
aid( int i ) : id( i ) {}
aid( QgsMapLayerAction* a ) : action( a ) {}
int id;
QgsMapLayerAction* action;
} actionId;
union aid
{
aid( int i ) : id( i ) {}
aid( QgsMapLayerAction* a ) : action( a ) {}
int id;
QgsMapLayerAction* action;
} actionId;

QgsFeatureId featureId;
QgsMapLayer* mapLayer;
QgsFeatureId featureId;
QgsMapLayer* mapLayer;
};


@@ -114,8 +115,7 @@ class QgsActionMenu : public QMenu
void setFeature( QgsFeatureId feature );

private slots:
void triggerAttributeAction( int index );
void triggerMapLayerAction( int index );
void triggerAction();
void reloadActions();

signals:
@@ -126,8 +126,6 @@ class QgsActionMenu : public QMenu
const QgsFeature* feature();

QgsVectorLayer* mLayer;
QSignalMapper* mAttributeActionSignalMapper;
QSignalMapper* mMapLayerActionSignalMapper;
QgsAttributeAction* mActions;
const QgsFeature* mFeature;
QgsFeatureId mFeatureId;

0 comments on commit f85a883

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