21
21
QgsActionMenu::QgsActionMenu ( QgsVectorLayer* layer, const QgsFeature* feature, QWidget* parent )
22
22
: QMenu( parent )
23
23
, mLayer( layer )
24
- , mAttributeActionSignalMapper( 0 )
25
- , mMapLayerActionSignalMapper( 0 )
26
24
, mActions( 0 )
27
25
, mFeature( feature )
28
26
, mFeatureId( feature->id () )
@@ -32,14 +30,12 @@ QgsActionMenu::QgsActionMenu( QgsVectorLayer* layer, const QgsFeature* feature,
32
30
}
33
31
34
32
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 )
43
39
{
44
40
init ();
45
41
}
@@ -48,7 +44,7 @@ void QgsActionMenu::init()
48
44
{
49
45
setTitle ( tr ( " &Actions" ) );
50
46
51
- connect ( QgsMapLayerActionRegistry::instance (), SIGNAL (changed ()), this , SLOT (reloadActions ()) );
47
+ connect ( QgsMapLayerActionRegistry::instance (), SIGNAL ( changed () ), this , SLOT ( reloadActions () ) );
52
48
53
49
reloadActions ();
54
50
}
@@ -88,42 +84,42 @@ void QgsActionMenu::setFeature( QgsFeature* feature )
88
84
mFeature = feature;
89
85
}
90
86
91
- void QgsActionMenu::triggerAttributeAction ( int index )
87
+ void QgsActionMenu::triggerAction ( )
92
88
{
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 )
98
105
{
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 () );
100
108
}
101
- }
102
-
103
- void QgsActionMenu::triggerMapLayerAction ( int index )
104
- {
105
- if ( feature () )
109
+ else if ( data.actionType == AttributeAction )
106
110
{
107
- QgsMapLayerAction* action = QgsMapLayerActionRegistry::instance ()->mapLayerActions ( mLayer , QgsMapLayerAction::Feature ).at ( index );
108
-
109
- action->triggerForFeature ( mLayer , feature () );
111
+ mActions ->doAction ( data.actionId .id , *feature () );
110
112
}
111
113
}
112
114
113
115
void QgsActionMenu::reloadActions ()
114
116
{
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 ();
122
118
123
119
delete mActions ;
124
120
mActions = new QgsAttributeAction ( *mLayer ->actions () );
125
121
126
- for ( int idx = 0 ; idx < mActions ->size (); ++idx )
122
+ for ( int idx = 0 ; idx < mActions ->size (); ++idx )
127
123
{
128
124
const QgsAction& qaction ( mActions ->at ( idx ) );
129
125
@@ -140,11 +136,7 @@ void QgsActionMenu::reloadActions()
140
136
{
141
137
action->setToolTip ( qaction.action () );
142
138
}
143
-
144
- mAttributeActionSignalMapper ->setMapping ( action, idx );
145
-
146
- connect ( action, SIGNAL (triggered ()), mAttributeActionSignalMapper , SLOT (map ()) );
147
-
139
+ connect ( action, SIGNAL ( triggered () ), this , SLOT ( triggerAction () ) );
148
140
addAction ( action );
149
141
}
150
142
@@ -159,10 +151,9 @@ void QgsActionMenu::reloadActions()
159
151
{
160
152
QgsMapLayerAction* qaction = mapLayerActions.at ( i );
161
153
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 ) ) );
164
155
addAction ( action );
165
- connect ( action, SIGNAL (triggered ()), mMapLayerActionSignalMapper , SLOT (map () ) );
156
+ connect ( action, SIGNAL ( triggered () ), this , SLOT ( triggerAction () ) );
166
157
}
167
158
}
168
159
0 commit comments