36#include <QDesktopServices>
40#include <QRegularExpression>
46#include "moc_qgsactionmanager.cpp"
48using namespace Qt::StringLiterals;
72 if ( mLayer && mLayer->dataProvider() && !
action.notificationMessage().isEmpty() )
74 mLayer->dataProvider()->setListening(
true );
75 if ( !mOnNotifyConnected )
79 mOnNotifyConnected =
true;
84void QgsActionManager::onNotifyRunActions(
const QString &message )
86 for (
const QgsAction &act : std::as_const( mActions ) )
88 if ( !act.notificationMessage().isEmpty() && QRegularExpression( act.notificationMessage() ).match( message ).hasMatch() )
90 if ( !act.isValid() || !act.runable() )
100 if ( expandedAction.isEmpty() )
102 runAction(
QgsAction( act.type(), act.name(), expandedAction, act.capture() ) );
112 if (
action.id() == actionId )
114 mActions.removeAt( i );
120 if ( mOnNotifyConnected )
122 bool hasActionOnNotify =
false;
124 hasActionOnNotify |= !
action.notificationMessage().isEmpty();
125 if ( !hasActionOnNotify && mLayer && mLayer->dataProvider() )
130 mOnNotifyConnected =
false;
140 if ( defaultValueIndex >= 0 && defaultValueIndex < feature.
fields().
size() )
143 context << actionScope;
144 doAction( actionId, feature, context );
161 if ( expandedAction.isEmpty() )
165 runAction( newAction );
171 if ( mOnNotifyConnected && mLayer && mLayer->dataProvider() )
176 mOnNotifyConnected =
false;
182 if ( actionScope.isNull() )
190 if (
action.actionScopes().contains( actionScope ) )
198void QgsActionManager::runAction(
const QgsAction &action )
205 if ( finfo.exists() && finfo.isFile() )
206 QDesktopServices::openUrl( QUrl::fromLocalFile(
action.
command() ) );
208 QDesktopServices::openUrl( QUrl(
action.
command(), QUrl::TolerantMode ) );
215 action.run( QgsExpressionContext() );
233 QgsExpressionContext context;
244 QDomElement aActions = layer_node.ownerDocument().createElement( u
"attributeactions"_s );
247 QDomElement defaultActionElement = layer_node.ownerDocument().createElement( u
"defaultAction"_s );
248 defaultActionElement.setAttribute( u
"key"_s,
defaultAction.key() );
249 defaultActionElement.setAttribute( u
"value"_s,
defaultAction.value().toString() );
250 aActions.appendChild( defaultActionElement );
255 action.writeXml( aActions );
257 layer_node.appendChild( aActions );
266 QDomNode aaNode = layer_node.namedItem( u
"attributeactions"_s );
268 if ( !aaNode.isNull() )
270 QDomNodeList actionsettings = aaNode.toElement().elementsByTagName( u
"actionsetting"_s );
271 for (
int i = 0; i < actionsettings.size(); ++i )
274 action.readXml( actionsettings.item( i ), context );
278 QDomNodeList defaultActionNodes = aaNode.toElement().elementsByTagName( u
"defaultAction"_s );
280 for (
int i = 0; i < defaultActionNodes.size(); ++i )
282 QDomElement defaultValueElem = defaultActionNodes.at( i ).toElement();
283 mDefaultActions.insert( defaultValueElem.attribute( u
"key"_s ), QUuid( defaultValueElem.attribute( u
"value"_s ) ) );
304 if (
action.id().toString() ==
id )
313 mDefaultActions[ actionScope ] = actionId;
318 return action( mDefaultActions.value( actionScope ) );
AttributeActionType
Attribute action types.
@ 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...
void removeAction(QUuid actionId)
Remove an action by its id.
bool writeXml(QDomNode &layer_node) const
Writes the actions out in XML format.
QList< QgsAction > actions(const QString &actionScope=QString()) const
Returns a list of actions that are available in the given action scope.
QgsVectorLayer * layer() const
Returns the layer.
void doAction(QUuid actionId, const QgsFeature &feature, int defaultValueIndex=0, const QgsExpressionContextScope &scope=QgsExpressionContextScope())
Does the given action.
void clearActions()
Removes all actions.
QUuid addAction(Qgis::AttributeActionType type, const QString &name, const QString &command, bool capture=false)
Add an action with the given name and action details.
void setDefaultAction(const QString &actionScope, QUuid actionId)
Each scope can have a default action.
QgsActionManager(QgsVectorLayer *layer)
Constructor.
QgsAction defaultAction(const QString &actionScope)
Each scope can have a default action.
QgsAction action(QUuid id) const
Gets an action by its id.
bool readXml(const QDomNode &layer_node, const QgsReadWriteContext &context=QgsReadWriteContext())
Reads the actions in in XML format.
Utility class that encapsulates an action based on vector attributes.
QString name() const
The name of the action. This may be a longer description.
Qgis::AttributeActionType type() const
The action type.
bool runable() const
Checks if the action is runable on the current platform.
bool isValid() const
Returns true if this action was a default constructed one.
QString command() const
Returns the command that is executed by this action.
bool capture() const
Whether to capture output for display when this action is run.
void notify(const QString &msg)
Emitted when the datasource issues a notification.
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.
static QgsExpressionContextScope * notificationScope(const QString &message=QString())
Creates a new scope which contains variables and functions relating to provider notifications.
static QgsExpressionContextScope * projectScope(const QgsProject *project)
Creates a new scope which contains variables and functions relating to a QGIS project.
static QgsExpressionContextScope * layerScope(const QgsMapLayer *layer)
Creates a new scope which contains variables and functions relating to a QgsMapLayer.
static QgsExpressionContextScope * globalScope()
Creates a new scope which contains variables and functions relating to the global QGIS context.
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
void setFeature(const QgsFeature &feature)
Convenience function for setting a feature for the context.
static QString replaceExpressionText(const QString &action, const QgsExpressionContext *context, const QgsDistanceArea *distanceArea=nullptr)
This function replaces each expression between [% and %] in the string with the result of its evaluat...
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
Q_INVOKABLE QVariant attribute(const QString &name) const
Lookup attribute value by attribute name.
int size() const
Returns number of items.
QgsField at(int i) const
Returns the field at particular index (must be in range 0..N-1).
static QgsProject * instance()
Returns the QgsProject singleton instance.
A container for the context for various read/write operations on objects.
static QgsRunProcess * create(const QString &action, bool capture)
Represents a vector layer which manages a vector based dataset.
#define QgsDebugMsgLevel(str, level)
Single variable definition for use within a QgsExpressionContextScope.