31 #include "qgsexpression.h" 35 #include <QStringList> 36 #include <QDomElement> 38 #include <QDesktopServices> 42 #include <QRegularExpression> 62 mActions.append( action );
66 if ( !mOnNotifyConnected )
70 mOnNotifyConnected =
true;
75 void QgsActionManager::onNotifyRunActions(
const QString &message )
77 for (
const QgsAction &act : qgis::as_const( mActions ) )
79 if ( !act.notificationMessage().isEmpty() && QRegularExpression( act.notificationMessage() ).match( message ).hasMatch() )
81 if ( !act.isValid() || !act.runable() )
90 QString expandedAction = QgsExpression::replaceExpressionText( act.command(), &context );
91 if ( expandedAction.isEmpty() )
93 runAction(
QgsAction( act.type(), act.name(), expandedAction, act.capture() ) );
105 mActions.removeAt( i );
111 if ( mOnNotifyConnected )
113 bool hasActionOnNotify =
false;
116 if ( !hasActionOnNotify && mLayer && mLayer->
dataProvider() )
121 mOnNotifyConnected =
false;
131 if ( defaultValueIndex >= 0 && defaultValueIndex < feature.
fields().
size() )
134 context << actionScope;
135 doAction( actionId, feature, context );
151 QString expandedAction = QgsExpression::replaceExpressionText( act.
command(), &actionContext );
152 if ( expandedAction.isEmpty() )
156 runAction( newAction );
162 if ( mOnNotifyConnected && mLayer && mLayer->
dataProvider() )
167 mOnNotifyConnected =
false;
173 if ( actionScope.isNull() )
193 QFileInfo finfo( action.
command() );
194 if ( finfo.exists() && finfo.isFile() )
195 QDesktopServices::openUrl( QUrl::fromLocalFile( action.
command() ) );
197 QDesktopServices::openUrl( QUrl( action.
command(), QUrl::TolerantMode ) );
225 QDomElement aActions = layer_node.ownerDocument().createElement( QStringLiteral(
"attributeactions" ) );
228 QDomElement defaultActionElement = layer_node.ownerDocument().createElement( QStringLiteral(
"defaultAction" ) );
229 defaultActionElement.setAttribute( QStringLiteral(
"key" ),
defaultAction.key() );
230 defaultActionElement.setAttribute( QStringLiteral(
"value" ),
defaultAction.value().toString() );
231 aActions.appendChild( defaultActionElement );
234 for (
const QgsAction &action : qgis::as_const( mActions ) )
238 layer_node.appendChild( aActions );
247 QDomNode aaNode = layer_node.namedItem( QStringLiteral(
"attributeactions" ) );
249 if ( !aaNode.isNull() )
251 QDomNodeList actionsettings = aaNode.toElement().elementsByTagName( QStringLiteral(
"actionsetting" ) );
252 for (
int i = 0; i < actionsettings.size(); ++i )
255 action.
readXml( actionsettings.item( i ) );
259 QDomNodeList defaultActionNodes = aaNode.toElement().elementsByTagName( QStringLiteral(
"defaultAction" ) );
261 for (
int i = 0; i < defaultActionNodes.size(); ++i )
263 QDomElement defaultValueElem = defaultActionNodes.at( i ).toElement();
264 mDefaultActions.insert( defaultValueElem.attribute( QStringLiteral(
"key" ) ), defaultValueElem.attribute( QStringLiteral(
"value" ) ) );
272 for (
const QgsAction &action : qgis::as_const( mActions ) )
274 if ( action.
id() == id )
283 mDefaultActions[ actionScope ] = actionId;
288 return action( mDefaultActions.value( actionScope ) );
Single variable definition for use within a QgsExpressionContextScope.
void doAction(QUuid actionId, const QgsFeature &feature, int defaultValueIndex=0, const QgsExpressionContextScope &scope=QgsExpressionContextScope())
Does the given action.
int size() const
Return number of items.
QgsAction action(QUuid id)
Get an action by its id.
bool writeXml(QDomNode &layer_node) const
Writes the actions out in XML format.
bool capture() const
Whether to capture output for display when this action is run.
void setDefaultAction(const QString &actionScope, QUuid actionId)
Each scope can have a default action.
ActionType type() const
The action type.
void setFeature(const QgsFeature &feature)
Convenience function for setting a feature for the context.
QString notificationMessage() const
Returns the notification message that triggers the action.
QSet< QString > actionScopes() const
The action scopes define where an action will be available.
QList< QgsAction > actions(const QString &actionScope=QString()) const
Return a list of actions that are available in the given action scope.
void readXml(const QDomNode &actionNode)
Reads an XML definition from actionNode into this object.
static QgsExpressionContextScope * projectScope(const QgsProject *project)
Creates a new scope which contains variables and functions relating to a QGIS project.
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
bool isValid() const
Returns true if this action was a default constructed one.
void notify(const QString &msg)
Emitted when datasource issues a notification.
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 addVariable(const QgsExpressionContextScope::StaticVariable &variable)
Adds a variable into the context scope.
QgsField at(int i) const
Get field at particular index (must be in range 0..N-1)
bool readXml(const QDomNode &layer_node)
Reads the actions in in XML format.
static QgsExpressionContextScope * globalScope()
Creates a new scope which contains variables and functions relating to the global QGIS context...
virtual void setListening(bool isListening)
Set whether the provider will listen to datasource notifications If set, the provider will issue noti...
void writeXml(QDomNode &actionsNode) const
Appends an XML definition for this action as a new child node to actionsNode.
Utility class that encapsulates an action based on vector attributes.
static QgsExpressionContextScope * notificationScope(const QString &message=QString())
Creates a new scope which contains variables and functions relating to provider notifications.
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
void clearActions()
Removes all actions.
bool runable() const
Checks if the action is runable on the current platform.
QString name() const
The name of the action. This may be a longer description.
Single scope for storing variables and functions for use within a QgsExpressionContext.
QgsAction defaultAction(const QString &actionScope)
Each scope can have a default action.
void removeAction(QUuid actionId)
Remove an action by its id.
static bool run(const QString &command, const QString &messageOnError=QString())
Execute a Python statement.
static QgsProject * instance()
Returns the QgsProject singleton instance.
QgsVectorDataProvider * dataProvider() override
Returns the layer's data provider.
QUuid id() const
Returns a unique id for this action.
static QgsRunProcess * create(const QString &action, bool capture)
static QgsExpressionContextScope * layerScope(const QgsMapLayer *layer)
Creates a new scope which contains variables and functions relating to a QgsMapLayer.
QVariant attribute(const QString &name) const
Lookup attribute value from attribute name.
QString command() const
Returns the command that is executed by this action.