28#include "moc_qgsactionmenu.cpp"
30using namespace Qt::StringLiterals;
36 , mFeatureId( feature.id() )
37 , mActionScope( actionScope )
46 , mActionScope( actionScope )
53 mContextGenerator = generator;
57void QgsActionMenu::init()
59 setTitle( tr(
"&Actions" ) );
72 if ( !mFeature.isValid() )
74 mLayer->getFeatures( QgsFeatureRequest( mFeatureId ) ).nextFeature( mFeature );
91void QgsActionMenu::triggerAction()
93 if ( !feature().isValid() )
96 QAction *action = qobject_cast<QAction *>( sender() );
100 if ( !action->data().isValid() || !action->data().canConvert<ActionData>() )
103 const ActionData data = action->data().value<ActionData>();
105 switch ( data.actionType )
122 const QgsAction act = data.actionData.value<QgsAction>();
123 switch ( act.
type() )
149 QgsExpressionContext context = mLayer->createExpressionContext();
152 QgsExpressionContextScope *actionScope =
new QgsExpressionContextScope();
153 actionScope->
addVariable( QgsExpressionContextScope::StaticVariable( u
"action_scope"_s, mActionScope,
true ) );
154 context << actionScope;
161void QgsActionMenu::reloadActions()
165 mVisibleActionCount = 0;
167 mActions = mLayer->actions()->actions( mActionScope );
169 const auto constMActions = mActions;
170 for (
const QgsAction &action : constMActions )
172 if ( !mLayer->isEditable() && action.isEnabledOnlyWhenEditable() )
178 QgsAction act( action );
181 QAction *qAction =
new QAction( action.icon(), action.name(),
this );
182 qAction->setData( QVariant::fromValue<ActionData>(
ActionData( act, mFeatureId, mLayer ) ) );
183 qAction->setIcon( action.icon() );
186 if ( !action.runable() )
188 qAction->setEnabled(
false );
189 qAction->setToolTip( tr(
"Not supported on your platform" ) );
193 qAction->setToolTip( action.command() );
195 connect( qAction, &QAction::triggered,
this, &QgsActionMenu::triggerAction );
196 addAction( qAction );
198 mVisibleActionCount++;
203 if ( !mapLayerActions.isEmpty() )
206 if ( mVisibleActionCount > 0 )
209 for (
int i = 0; i < mapLayerActions.size(); ++i )
211 QgsMapLayerAction *mapLayerAction = mapLayerActions.at( i );
216 QAction *qAction =
new QAction( mapLayerAction->icon(), mapLayerAction->text(),
this );
217 qAction->setData( QVariant::fromValue<ActionData>(
ActionData( mapLayerAction, mFeatureId, mLayer ) ) );
218 addAction( qAction );
219 connect( qAction, &QAction::triggered,
this, &QgsActionMenu::triggerAction );
220 mVisibleActionCount++;
227void QgsActionMenu::layerWillBeDeleted()
255 mExpressionContextScope = scope;
261 return mExpressionContextScope;
271 return mVisibleActionCount == 0;
Provides global constants and enumerations for use throughout the application.
@ OpenUrl
Open URL action.
@ SubmitUrlMultipart
POST data to an URL using "multipart/form-data".
@ Windows
Windows specific.
@ SubmitUrlEncoded
POST data to an URL, using "application/x-www-form-urlencoded" or "application/json" if the body is v...
@ Warning
Warning message.
@ AttributeAction
Custom actions (manually defined in layer properties), corresponds to QgsAction class.
@ MapLayerAction
Standard actions (defined by core or plugins), corresponds to QgsMapLayerAction class.
@ SingleFeature
Action targets a single feature from a layer.
Utility class that encapsulates an action based on vector attributes.
Qgis::AttributeActionType type() const
The action type.
void run(QgsVectorLayer *layer, const QgsFeature &feature, const QgsExpressionContext &expressionContext) const
Run this action.
void setExpressionContextScope(const QgsExpressionContextScope &scope)
Sets an expression context scope to use for running the action.
@ IdentifyMode
Identify the feature.
Single scope for storing variables and functions for use within a QgsExpressionContext.
void addVariable(const QgsExpressionContextScope::StaticVariable &variable)
Adds a variable into the context scope.
void setFeature(const QgsFeature &feature)
Convenience function for setting a feature for the context.
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
static QgsMapLayerActionRegistry * mapLayerActionRegistry()
Returns the global map layer action registry, used for registering map layer actions.
static bool allowExecutionOfEmbeddedScripts(QgsProject *project, QgsMessageBar *messageBar=nullptr)
Returns true if python embedded in a project is currently allowed to be loaded.
An interface for objects which can create a QgsMapLayerActionContext.
virtual QgsMapLayerActionContext createActionContext()=0
Creates a QgsMapLayerActionContext.
Encapsulates the context in which a QgsMapLayerAction action is executed.
void changed()
Triggered when an action is added or removed from the registry.
QList< QgsMapLayerAction * > mapLayerActions(QgsMapLayer *layer, Qgis::MapLayerActionTargets targets=Qgis::MapLayerActionTarget::AllActions, const QgsMapLayerActionContext &context=QgsMapLayerActionContext())
Returns the map layer actions which can run on the specified layer.
An action which can run on map layers.
bool isEnabledOnlyWhenEditable() const
Returns true if the action is only enabled for layers in editable mode.
virtual Q_DECL_DEPRECATED void triggerForFeature(QgsMapLayer *layer, const QgsFeature &feature)
Triggers the action with the specified layer and feature.
Base class for all map layer types.
void editingStopped()
Emitted when edited changes have been successfully written to the data provider.
void editingStarted()
Emitted when editing on this layer has started.
void willBeDeleted()
Emitted in the destructor when the layer is about to be deleted, but it is still in a perfectly valid...
static QgsProject * instance()
Returns the QgsProject singleton instance.
Represents a vector layer which manages a vector based dataset.
void readOnlyChanged()
Emitted when the read only state of this layer is changed.
#define Q_NOWARN_DEPRECATED_POP
#define Q_NOWARN_DEPRECATED_PUSH
qint64 QgsFeatureId
64 bit feature ids negative numbers are used for uncommitted/newly added features