Skip to content

Commit

Permalink
[QgsActionMenu] use ActionData to trigger actions
Browse files Browse the repository at this point in the history
  • Loading branch information
3nids committed Sep 16, 2014
1 parent ab22890 commit f85a883
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 63 deletions.
75 changes: 33 additions & 42 deletions src/gui/qgsactionmenu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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() )
Expand All @@ -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();
}
Expand All @@ -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();
}
Expand Down Expand Up @@ -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 ) );

Expand All @@ -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 );
}

Expand All @@ -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() ) );
}
}

Expand Down
40 changes: 19 additions & 21 deletions src/gui/qgsactionmenu.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand All @@ -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;
};


Expand Down Expand Up @@ -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:
Expand All @@ -126,8 +126,6 @@ class QgsActionMenu : public QMenu
const QgsFeature* feature();

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

0 comments on commit f85a883

Please sign in to comment.