Skip to content

Commit f85a883

Browse files
committedSep 16, 2014
[QgsActionMenu] use ActionData to trigger actions
1 parent ab22890 commit f85a883

File tree

2 files changed

+52
-63
lines changed

2 files changed

+52
-63
lines changed
 

‎src/gui/qgsactionmenu.cpp

Lines changed: 33 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,6 @@
2121
QgsActionMenu::QgsActionMenu( QgsVectorLayer* layer, const QgsFeature* feature, QWidget* parent )
2222
: QMenu( parent )
2323
, mLayer( layer )
24-
, mAttributeActionSignalMapper( 0 )
25-
, mMapLayerActionSignalMapper( 0 )
2624
, mActions( 0 )
2725
, mFeature( feature )
2826
, mFeatureId( feature->id() )
@@ -32,14 +30,12 @@ QgsActionMenu::QgsActionMenu( QgsVectorLayer* layer, const QgsFeature* feature,
3230
}
3331

3432
QgsActionMenu::QgsActionMenu( QgsVectorLayer* layer, const QgsFeatureId fid, QWidget* parent )
35-
: QMenu( parent )
36-
, mLayer( layer )
37-
, mAttributeActionSignalMapper( 0 )
38-
, mMapLayerActionSignalMapper( 0 )
39-
, mActions( 0 )
40-
, mFeature( 0 )
41-
, mFeatureId( fid )
42-
, mOwnsFeature( false )
33+
: QMenu( parent )
34+
, mLayer( layer )
35+
, mActions( 0 )
36+
, mFeature( 0 )
37+
, mFeatureId( fid )
38+
, mOwnsFeature( false )
4339
{
4440
init();
4541
}
@@ -48,7 +44,7 @@ void QgsActionMenu::init()
4844
{
4945
setTitle( tr( "&Actions" ) );
5046

51-
connect( QgsMapLayerActionRegistry::instance(), SIGNAL(changed()), this, SLOT(reloadActions()) );
47+
connect( QgsMapLayerActionRegistry::instance(), SIGNAL( changed() ), this, SLOT( reloadActions() ) );
5248

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

91-
void QgsActionMenu::triggerAttributeAction( int index )
87+
void QgsActionMenu::triggerAction()
9288
{
93-
if ( feature() )
94-
{
95-
mActions->doAction( index, *feature() );
96-
}
97-
else
89+
if ( !feature() )
90+
return;
91+
92+
QAction* action = qobject_cast<QAction*>( sender() );
93+
if ( !action )
94+
return;
95+
96+
if ( !action->data().isValid() || !action->data().canConvert<ActionData>() )
97+
return;
98+
99+
ActionData data = action->data().value<ActionData>();
100+
101+
if ( data.actionType == Invalid )
102+
return;
103+
104+
if ( data.actionType == MapLayerAction )
98105
{
99-
QgsDebugMsg( QString( "Trying to run an action on a non-existing feature with fid %1" ).arg( mFeatureId ) );
106+
QgsMapLayerAction* mapLayerAction = data.actionId.action;
107+
mapLayerAction->triggerForFeature( data.mapLayer, feature() );
100108
}
101-
}
102-
103-
void QgsActionMenu::triggerMapLayerAction( int index )
104-
{
105-
if ( feature() )
109+
else if ( data.actionType == AttributeAction )
106110
{
107-
QgsMapLayerAction* action = QgsMapLayerActionRegistry::instance()->mapLayerActions( mLayer, QgsMapLayerAction::Feature ).at( index );
108-
109-
action->triggerForFeature( mLayer, feature() );
111+
mActions->doAction( data.actionId.id, *feature() );
110112
}
111113
}
112114

113115
void QgsActionMenu::reloadActions()
114116
{
115-
delete mAttributeActionSignalMapper;
116-
mAttributeActionSignalMapper = new QSignalMapper( this );
117-
delete mMapLayerActionSignalMapper;
118-
mMapLayerActionSignalMapper = new QSignalMapper( this );
119-
120-
connect( mAttributeActionSignalMapper, SIGNAL(mapped(int)), this, SLOT(triggerAttributeAction(int)) );
121-
connect( mMapLayerActionSignalMapper, SIGNAL(mapped(int)), this, SLOT(triggerMapLayerAction(int)) );
117+
clear();
122118

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

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

@@ -140,11 +136,7 @@ void QgsActionMenu::reloadActions()
140136
{
141137
action->setToolTip( qaction.action() );
142138
}
143-
144-
mAttributeActionSignalMapper->setMapping( action, idx );
145-
146-
connect( action, SIGNAL(triggered()), mAttributeActionSignalMapper, SLOT(map()) );
147-
139+
connect( action, SIGNAL( triggered() ), this, SLOT( triggerAction() ) );
148140
addAction( action );
149141
}
150142

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

‎src/gui/qgsactionmenu.h

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include "qgsattributeaction.h"
2323
#include "qgsmaplayeractionregistry.h"
2424

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

4142
struct ActionData
4243
{
43-
ActionData()
44+
ActionData()
4445
: actionType( Invalid )
4546
, actionId( 0 )
46-
{}
47+
{}
4748

48-
ActionData( int actionId, QgsFeatureId featureId, QgsMapLayer* mapLayer )
49+
ActionData( int actionId, QgsFeatureId featureId, QgsMapLayer* mapLayer )
4950
: actionType( AttributeAction )
5051
, actionId( actionId )
5152
, featureId( featureId )
5253
, mapLayer( mapLayer )
53-
{}
54+
{}
5455

55-
ActionData( QgsMapLayerAction* action, QgsFeatureId featureId, QgsMapLayer* mapLayer )
56-
: actionType( AttributeAction )
56+
ActionData( QgsMapLayerAction* action, QgsFeatureId featureId, QgsMapLayer* mapLayer )
57+
: actionType( MapLayerAction )
5758
, actionId( action )
5859
, featureId( featureId )
5960
, mapLayer( mapLayer )
60-
{}
61+
{}
6162

62-
ActionType actionType;
63+
ActionType actionType;
6364

64-
union aid
65-
{
66-
aid( int i ) : id( i ) {}
67-
aid( QgsMapLayerAction* a ) : action( a ) {}
68-
int id;
69-
QgsMapLayerAction* action;
70-
} actionId;
65+
union aid
66+
{
67+
aid( int i ) : id( i ) {}
68+
aid( QgsMapLayerAction* a ) : action( a ) {}
69+
int id;
70+
QgsMapLayerAction* action;
71+
} actionId;
7172

72-
QgsFeatureId featureId;
73-
QgsMapLayer* mapLayer;
73+
QgsFeatureId featureId;
74+
QgsMapLayer* mapLayer;
7475
};
7576

7677

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

116117
private slots:
117-
void triggerAttributeAction( int index );
118-
void triggerMapLayerAction( int index );
118+
void triggerAction();
119119
void reloadActions();
120120

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

128128
QgsVectorLayer* mLayer;
129-
QSignalMapper* mAttributeActionSignalMapper;
130-
QSignalMapper* mMapLayerActionSignalMapper;
131129
QgsAttributeAction* mActions;
132130
const QgsFeature* mFeature;
133131
QgsFeatureId mFeatureId;

0 commit comments

Comments
 (0)
Please sign in to comment.