2121QgsActionMenu::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
3432QgsActionMenu::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
113115void 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
0 commit comments