39#include <QDesktopServices>
42#include <QRegularExpression>
65 if ( !mOnNotifyConnected )
67 QgsDebugMsg( QStringLiteral(
"connecting to notify" ) );
69 mOnNotifyConnected =
true;
74void QgsActionManager::onNotifyRunActions(
const QString &message )
76 for (
const QgsAction &act : std::as_const( mActions ) )
78 if ( !act.notificationMessage().isEmpty() && QRegularExpression( act.notificationMessage() ).match( message ).hasMatch() )
80 if ( !act.isValid() || !act.runable() )
90 if ( expandedAction.isEmpty() )
92 runAction(
QgsAction( act.type(), act.name(), expandedAction, act.capture() ) );
104 mActions.removeAt( i );
110 if ( mOnNotifyConnected )
112 bool hasActionOnNotify =
false;
115 if ( !hasActionOnNotify && mLayer && mLayer->
dataProvider() )
120 mOnNotifyConnected =
false;
130 if ( defaultValueIndex >= 0 && defaultValueIndex < feature.
fields().
size() )
133 context << actionScope;
134 doAction( actionId, feature, context );
151 if ( expandedAction.isEmpty() )
155 runAction( newAction );
161 if ( mOnNotifyConnected && mLayer && mLayer->
dataProvider() )
166 mOnNotifyConnected =
false;
172 if ( actionScope.isNull() )
188void QgsActionManager::runAction(
const QgsAction &action )
195 if ( finfo.exists() && finfo.isFile() )
196 QDesktopServices::openUrl( QUrl::fromLocalFile(
action.
command() ) );
198 QDesktopServices::openUrl( QUrl(
action.
command(), QUrl::TolerantMode ) );
234 QDomElement aActions = layer_node.ownerDocument().createElement( QStringLiteral(
"attributeactions" ) );
237 QDomElement defaultActionElement = layer_node.ownerDocument().createElement( QStringLiteral(
"defaultAction" ) );
238 defaultActionElement.setAttribute( QStringLiteral(
"key" ),
defaultAction.key() );
239 defaultActionElement.setAttribute( QStringLiteral(
"value" ),
defaultAction.value().toString() );
240 aActions.appendChild( defaultActionElement );
247 layer_node.appendChild( aActions );
256 QDomNode aaNode = layer_node.namedItem( QStringLiteral(
"attributeactions" ) );
258 if ( !aaNode.isNull() )
260 QDomNodeList actionsettings = aaNode.toElement().elementsByTagName( QStringLiteral(
"actionsetting" ) );
261 for (
int i = 0; i < actionsettings.size(); ++i )
268 QDomNodeList defaultActionNodes = aaNode.toElement().elementsByTagName( QStringLiteral(
"defaultAction" ) );
270 for (
int i = 0; i < defaultActionNodes.size(); ++i )
272 QDomElement defaultValueElem = defaultActionNodes.at( i ).toElement();
273 mDefaultActions.insert( defaultValueElem.attribute( QStringLiteral(
"key" ) ), QUuid( defaultValueElem.attribute( QStringLiteral(
"value" ) ) ) );
303 mDefaultActions[ actionScope ] = actionId;
308 return action( mDefaultActions.value( actionScope ) );
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.
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(QgsAction::ActionType 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.
QgsAction defaultAction(const QString &actionScope)
Each scope can have a default action.
bool readXml(const QDomNode &layer_node)
Reads the actions in in XML format.
QgsAction action(QUuid id) const
Gets an action by its id.
Utility class that encapsulates an action based on vector attributes.
QString notificationMessage() const
Returns the notification message that triggers the action.
QString name() const
The name of the action. This may be a longer description.
QSet< QString > actionScopes() const
The action scopes define where an action will be available.
void readXml(const QDomNode &actionNode)
Reads an XML definition from actionNode into this object.
void run(QgsVectorLayer *layer, const QgsFeature &feature, const QgsExpressionContext &expressionContext) const
Run this action.
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.
ActionType type() const
The action type.
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 writeXml(QDomNode &actionsNode) const
Appends an XML definition for this action as a new child node to actionsNode.
QUuid id() const
Returns a unique id for this action.
@ SubmitUrlEncoded
POST data to an URL, using "application/x-www-form-urlencoded" or "application/json" if the body is v...
@ SubmitUrlMultipart
POST data to an URL using "multipart/form-data".
virtual void setListening(bool isListening)
Set whether the provider will listen to datasource notifications If set, the provider will issue noti...
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...
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.
static QgsRunProcess * create(const QString &action, bool capture)
QgsVectorDataProvider * dataProvider() FINAL
Returns the layer's data provider, it may be nullptr.
Single variable definition for use within a QgsExpressionContextScope.