QGIS API Documentation  2.8.2-Wien
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
qgsactionmenu.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsactionmenu.cpp
3  --------------------------------------
4  Date : 11.8.2014
5  Copyright : (C) 2014 Matthias Kuhn
6  Email : matthias dot kuhn at gmx dot ch
7  ***************************************************************************
8  * *
9  * This program is free software; you can redistribute it and/or modify *
10  * it under the terms of the GNU General Public License as published by *
11  * the Free Software Foundation; either version 2 of the License, or *
12  * (at your option) any later version. *
13  * *
14  ***************************************************************************/
15 
16 #include "qgsactionmenu.h"
17 #include "qgsvectorlayer.h"
18 
19 QgsActionMenu::QgsActionMenu( QgsVectorLayer* layer, const QgsFeature* feature, QWidget* parent )
20  : QMenu( parent )
21  , mLayer( layer )
22  , mActions( 0 )
23  , mFeature( feature )
24  , mFeatureId( feature->id() )
25  , mOwnsFeature( false )
26 {
27  init();
28 }
29 
30 QgsActionMenu::QgsActionMenu( QgsVectorLayer* layer, const QgsFeatureId fid, QWidget* parent )
31  : QMenu( parent )
32  , mLayer( layer )
33  , mActions( 0 )
34  , mFeature( 0 )
35  , mFeatureId( fid )
36  , mOwnsFeature( false )
37 {
38  init();
39 }
40 
41 void QgsActionMenu::init()
42 {
43  setTitle( tr( "&Actions" ) );
44 
45  connect( QgsMapLayerActionRegistry::instance(), SIGNAL( changed() ), this, SLOT( reloadActions() ) );
46 
47  reloadActions();
48 }
49 
50 const QgsFeature* QgsActionMenu::feature()
51 {
52  if ( !mFeature || !mFeature->isValid() )
53  {
54  QgsFeature* feat = new QgsFeature();
55  if ( mActions->layer()->getFeatures( QgsFeatureRequest( mFeatureId ) ).nextFeature( *feat ) )
56  {
57  mFeature = feat;
58  mOwnsFeature = true;
59  }
60  else
61  {
62  delete feat;
63  }
64  }
65 
66  return mFeature;
67 }
68 
70 {
71  delete mActions;
72 
73  if ( mOwnsFeature )
74  delete mFeature;
75 }
76 
78 {
79  if ( mOwnsFeature )
80  delete mFeature;
81  mOwnsFeature = false;
82  mFeature = feature;
83 }
84 
85 void QgsActionMenu::triggerAction()
86 {
87  if ( !feature() )
88  return;
89 
90  QAction* action = qobject_cast<QAction*>( sender() );
91  if ( !action )
92  return;
93 
94  if ( !action->data().isValid() || !action->data().canConvert<ActionData>() )
95  return;
96 
97  ActionData data = action->data().value<ActionData>();
98 
99  if ( data.actionType == Invalid )
100  return;
101 
102  if ( data.actionType == MapLayerAction )
103  {
104  QgsMapLayerAction* mapLayerAction = data.actionId.action;
105  mapLayerAction->triggerForFeature( data.mapLayer, feature() );
106  }
107  else if ( data.actionType == AttributeAction )
108  {
109  mActions->doAction( data.actionId.id, *feature() );
110  }
111 }
112 
113 void QgsActionMenu::reloadActions()
114 {
115  clear();
116 
117  delete mActions;
118  mActions = new QgsAttributeAction( *mLayer->actions() );
119 
120  for ( int idx = 0; idx < mActions->size(); ++idx )
121  {
122  const QgsAction& qaction( mActions->at( idx ) );
123 
124  QAction* action = new QAction( qaction.icon(), qaction.name(), this );
125  action->setData( QVariant::fromValue<ActionData>( ActionData( idx, mFeatureId, mLayer ) ) );
126  action->setIcon( qaction.icon() );
127 
128  // Only enable items on supported platforms
129  if ( !qaction.runable() )
130  {
131  action->setEnabled( false );
132  action->setToolTip( tr( "Not supported on your platform" ) );
133  }
134  else
135  {
136  action->setToolTip( qaction.action() );
137  }
138  connect( action, SIGNAL( triggered() ), this, SLOT( triggerAction() ) );
139  addAction( action );
140  }
141 
142  QList<QgsMapLayerAction*> mapLayerActions = QgsMapLayerActionRegistry::instance()->mapLayerActions( mLayer, QgsMapLayerAction::SingleFeature );
143 
144  if ( mapLayerActions.size() > 0 )
145  {
146  //add a separator between user defined and standard actions
147  addSeparator();
148 
149  for ( int i = 0; i < mapLayerActions.size(); ++i )
150  {
151  QgsMapLayerAction* qaction = mapLayerActions.at( i );
152  QAction* action = new QAction( qaction->icon(), qaction->text(), this );
153  action->setData( QVariant::fromValue<ActionData>( ActionData( qaction, mFeatureId, mLayer ) ) );
154  addAction( action );
155  connect( action, SIGNAL( triggered() ), this, SLOT( triggerAction() ) );
156  }
157  }
158 
159  emit reinit();
160 }
161