QGIS API Documentation  3.4.15-Madeira (e83d02e274)
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 at opengis 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"
19 #include "qgsactionmanager.h"
20 #include "qgsfeatureiterator.h"
21 #include "qgsgui.h"
22 
23 QgsActionMenu::QgsActionMenu( QgsVectorLayer *layer, const QgsFeature &feature, const QString &actionScope, QWidget *parent )
24  : QMenu( parent )
25  , mLayer( layer )
26  , mFeature( feature )
27  , mFeatureId( feature.id() )
28  , mActionScope( actionScope )
29 {
30  init();
31 }
32 
33 QgsActionMenu::QgsActionMenu( QgsVectorLayer *layer, const QgsFeatureId fid, const QString &actionScope, QWidget *parent )
34  : QMenu( parent )
35  , mLayer( layer )
36  , mFeatureId( fid )
37  , mActionScope( actionScope )
38 {
39  init();
40 }
41 
42 void QgsActionMenu::init()
43 {
44  setTitle( tr( "&Actions" ) );
45 
46  connect( QgsGui::mapLayerActionRegistry(), &QgsMapLayerActionRegistry::changed, this, &QgsActionMenu::reloadActions );
47  connect( mLayer, &QgsVectorLayer::editingStarted, this, &QgsActionMenu::reloadActions );
48  connect( mLayer, &QgsVectorLayer::editingStopped, this, &QgsActionMenu::reloadActions );
49  connect( mLayer, &QgsVectorLayer::readOnlyChanged, this, &QgsActionMenu::reloadActions );
50  connect( mLayer, &QgsMapLayer::willBeDeleted, this, &QgsActionMenu::layerWillBeDeleted );
51 
52  reloadActions();
53 }
54 
55 QgsFeature QgsActionMenu::feature()
56 {
57  if ( !mFeature.isValid() )
58  {
59  mLayer->getFeatures( QgsFeatureRequest( mFeatureId ) ).nextFeature( mFeature );
60  }
61 
62  return mFeature;
63 }
64 
65 void QgsActionMenu::setFeature( const QgsFeature &feature )
66 {
67  mFeature = feature;
68 }
69 
71 {
72  mMode = mode;
73  reloadActions();
74 }
75 
76 void QgsActionMenu::triggerAction()
77 {
78  if ( !feature().isValid() )
79  return;
80 
81  QAction *action = qobject_cast<QAction *>( sender() );
82  if ( !action )
83  return;
84 
85  if ( !action->data().isValid() || !action->data().canConvert<ActionData>() )
86  return;
87 
88  ActionData data = action->data().value<ActionData>();
89 
90  if ( data.actionType == Invalid )
91  return;
92 
93  if ( data.actionType == MapLayerAction )
94  {
95  QgsMapLayerAction *mapLayerAction = data.actionData.value<QgsMapLayerAction *>();
96  mapLayerAction->triggerForFeature( data.mapLayer, &mFeature );
97  }
98  else if ( data.actionType == AttributeAction )
99  {
100  // define custom substitutions: layer id and clicked coords
101  QgsExpressionContext context = mLayer->createExpressionContext();
102  context.setFeature( mFeature );
103 
105  actionScope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "action_scope" ), mActionScope, true ) );
106  context << actionScope;
107  QgsAction act = data.actionData.value<QgsAction>();
108  act.run( context );
109  }
110 }
111 
112 void QgsActionMenu::reloadActions()
113 {
114  clear();
115 
116  mActions = mLayer->actions()->actions( mActionScope );
117 
118  Q_FOREACH ( const QgsAction &action, mActions )
119  {
120  if ( !mLayer->isEditable() && action.isEnabledOnlyWhenEditable() )
121  continue;
122 
124  continue;
125 
126  QgsAction act( action );
127  act.setExpressionContextScope( mExpressionContextScope );
128 
129  QAction *qAction = new QAction( action.icon(), action.name(), this );
130  qAction->setData( QVariant::fromValue<ActionData>( ActionData( act, mFeatureId, mLayer ) ) );
131  qAction->setIcon( action.icon() );
132 
133  // Only enable items on supported platforms
134  if ( !action.runable() )
135  {
136  qAction->setEnabled( false );
137  qAction->setToolTip( tr( "Not supported on your platform" ) );
138  }
139  else
140  {
141  qAction->setToolTip( action.command() );
142  }
143  connect( qAction, &QAction::triggered, this, &QgsActionMenu::triggerAction );
144  addAction( qAction );
145  }
146 
147  QList<QgsMapLayerAction *> mapLayerActions = QgsGui::mapLayerActionRegistry()->mapLayerActions( mLayer, QgsMapLayerAction::SingleFeature );
148 
149  if ( !mapLayerActions.isEmpty() )
150  {
151  //add a separator between user defined and standard actions
152  addSeparator();
153 
154  for ( int i = 0; i < mapLayerActions.size(); ++i )
155  {
156  QgsMapLayerAction *qaction = mapLayerActions.at( i );
157 
159  continue;
160 
161  QAction *qAction = new QAction( qaction->icon(), qaction->text(), this );
162  qAction->setData( QVariant::fromValue<ActionData>( ActionData( qaction, mFeatureId, mLayer ) ) );
163  addAction( qAction );
164  connect( qAction, &QAction::triggered, this, &QgsActionMenu::triggerAction );
165  }
166  }
167 
168  emit reinit();
169 }
170 
171 void QgsActionMenu::layerWillBeDeleted()
172 {
173  // here we are just making sure that we are not going to have reloadActions() called again
174  // with a dangling pointer to a layer when actions get removed on QGIS exit
175  clear();
176  mLayer = nullptr;
177  disconnect( QgsGui::mapLayerActionRegistry(), &QgsMapLayerActionRegistry::changed, this, &QgsActionMenu::reloadActions );
178 }
179 
180 
182  : actionType( MapLayerAction )
183  , actionData( QVariant::fromValue<QgsMapLayerAction*>( action ) )
184  , featureId( featureId )
185  , mapLayer( mapLayer )
186 {}
187 
188 
191  , actionData( QVariant::fromValue<QgsAction>( action ) )
192  , featureId( featureId )
193  , mapLayer( mapLayer )
194 {}
195 
196 
198 {
199  mExpressionContextScope = scope;
200  reloadActions();
201 }
202 
204 {
205  return mExpressionContextScope;
206 }
void setFeature(const QgsFeature &feature)
Change the feature on which actions are performed.
QgsActionManager * actions()
Returns all layer actions defined on this layer.
Single variable definition for use within a QgsExpressionContextScope.
Base class for all map layer types.
Definition: qgsmaplayer.h:63
QgsExpressionContextScope expressionContextScope() const
Returns an expression context scope used to resolve underlying actions.
bool isValid() const
Returns the validity of this feature.
Definition: qgsfeature.cpp:183
void setFeature(const QgsFeature &feature)
Convenience function for setting a feature for the context.
void run(QgsVectorLayer *layer, const QgsFeature &feature, const QgsExpressionContext &expressionContext) const
Run this action.
Definition: qgsaction.cpp:44
void willBeDeleted()
Emitted in the destructor when the layer is about to be deleted, but it is still in a perfectly valid...
void readOnlyChanged()
Emitted when the read only state of this layer is changed.
QgsActionMenu(QgsVectorLayer *layer, const QgsFeature &feature, const QString &actionScope, QWidget *parent=nullptr)
Constructs a new QgsActionMenu.
qint64 QgsFeatureId
Definition: qgsfeatureid.h:25
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
Definition: qgsfeature.h:55
void addVariable(const QgsExpressionContextScope::StaticVariable &variable)
Adds a variable into the context scope.
Standard actions (defined by core or plugins)
Definition: qgsactionmenu.h:46
void setMode(QgsAttributeEditorContext::Mode mode)
Change the mode of the actions.
bool isEditable() const FINAL
Returns true if the provider is in editing mode.
QString name() const
The name of the action. This may be a longer description.
Definition: qgsaction.h:124
QList< QgsMapLayerAction * > mapLayerActions(QgsMapLayer *layer, QgsMapLayerAction::Targets targets=QgsMapLayerAction::AllActions)
Returns the map layer actions which can run on the specified layer.
Utility class that encapsulates an action based on vector attributes.
Definition: qgsaction.h:35
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
void editingStopped()
Is emitted, when edited changes successfully have been written to the data provider.
This class wraps a request for features to a vector layer (or directly its vector data provider)...
void changed()
Triggered when an action is added or removed from the registry.
Single scope for storing variables and functions for use within a QgsExpressionContext.
void editingStarted()
Is emitted, when editing on this layer has started.
QgsExpressionContext createExpressionContext() const FINAL
This method needs to be reimplemented in all classes which implement this interface and return an exp...
QString command() const
Returns the command that is executed by this action.
Definition: qgsaction.h:156
bool isEnabledOnlyWhenEditable() const
Returns true if the action is only enabled for layers in editable mode.
Custom actions (manually defined in layer properties)
Definition: qgsactionmenu.h:47
ActionData()=default
Constructor for ActionData.
bool runable() const
Checks if the action is runable on the current platform.
Definition: qgsaction.cpp:29
QgsFeatureIterator getFeatures(const QgsFeatureRequest &request=QgsFeatureRequest()) const FINAL
Query the layer for features specified in request.
QgsActionMenu::ActionType actionType
Definition: qgsactionmenu.h:60
bool nextFeature(QgsFeature &f)
QList< QgsAction > actions(const QString &actionScope=QString()) const
Returns a list of actions that are available in the given action scope.
void setExpressionContextScope(const QgsExpressionContextScope &scope)
Sets an expression context scope used to resolve underlying actions.
void setExpressionContextScope(const QgsExpressionContextScope &scope)
Sets an expression context scope to use for running the action.
Definition: qgsaction.cpp:156
Represents a vector layer which manages a vector based data sets.
static QgsMapLayerActionRegistry * mapLayerActionRegistry()
Returns the global map layer action registry, used for registering map layer actions.
Definition: qgsgui.cpp:78
void triggerForFeature(QgsMapLayer *layer, const QgsFeature *feature)
Triggers the action with the specified layer and feature.
bool isEnabledOnlyWhenEditable() const
Returns whether only enabled in editable mode.
Definition: qgsaction.h:173
An action which can run on map layers.
QIcon icon() const
The icon.
Definition: qgsaction.h:147