QGIS API Documentation
3.14.0-Pi (9f7028fd23)
|
Go to the documentation of this file.
36 #include <QStringList>
37 #include <QDomElement>
39 #include <QDesktopServices>
43 #include <QRegularExpression>
67 if ( !mOnNotifyConnected )
69 QgsDebugMsg( QStringLiteral(
"connecting to notify" ) );
71 mOnNotifyConnected =
true;
76 void QgsActionManager::onNotifyRunActions(
const QString &message )
78 for (
const QgsAction &act : qgis::as_const( mActions ) )
80 if ( !act.notificationMessage().isEmpty() && QRegularExpression( act.notificationMessage() ).match( message ).hasMatch() )
82 if ( !act.isValid() || !act.runable() )
92 if ( expandedAction.isEmpty() )
94 runAction(
QgsAction( act.type(), act.name(), expandedAction, act.capture() ) );
106 mActions.removeAt( i );
112 if ( mOnNotifyConnected )
114 bool hasActionOnNotify =
false;
117 if ( !hasActionOnNotify && mLayer && mLayer->
dataProvider() )
122 mOnNotifyConnected =
false;
132 if ( defaultValueIndex >= 0 && defaultValueIndex < feature.
fields().
size() )
135 context << actionScope;
136 doAction( actionId, feature, context );
153 if ( expandedAction.isEmpty() )
157 runAction( newAction );
163 if ( mOnNotifyConnected && mLayer && mLayer->
dataProvider() )
168 mOnNotifyConnected =
false;
174 if ( actionScope.isNull() )
190 void 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 ) );
226 QDomElement aActions = layer_node.ownerDocument().createElement( QStringLiteral(
"attributeactions" ) );
229 QDomElement defaultActionElement = layer_node.ownerDocument().createElement( QStringLiteral(
"defaultAction" ) );
230 defaultActionElement.setAttribute( QStringLiteral(
"key" ),
defaultAction.key() );
231 defaultActionElement.setAttribute( QStringLiteral(
"value" ),
defaultAction.value().toString() );
232 aActions.appendChild( defaultActionElement );
239 layer_node.appendChild( aActions );
248 QDomNode aaNode = layer_node.namedItem( QStringLiteral(
"attributeactions" ) );
250 if ( !aaNode.isNull() )
252 QDomNodeList actionsettings = aaNode.toElement().elementsByTagName( QStringLiteral(
"actionsetting" ) );
253 for (
int i = 0; i < actionsettings.size(); ++i )
260 QDomNodeList defaultActionNodes = aaNode.toElement().elementsByTagName( QStringLiteral(
"defaultAction" ) );
262 for (
int i = 0; i < defaultActionNodes.size(); ++i )
264 QDomElement defaultValueElem = defaultActionNodes.at( i ).toElement();
265 mDefaultActions.insert( defaultValueElem.attribute( QStringLiteral(
"key" ) ), defaultValueElem.attribute( QStringLiteral(
"value" ) ) );
284 mDefaultActions[ actionScope ] = actionId;
289 return action( mDefaultActions.value( actionScope ) );
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
QUuid addAction(QgsAction::ActionType type, const QString &name, const QString &command, bool capture=false)
Add an action with the given name and action details.
QString notificationMessage() const
Returns the notification message that triggers the action.
void doAction(QUuid actionId, const QgsFeature &feature, int defaultValueIndex=0, const QgsExpressionContextScope &scope=QgsExpressionContextScope())
Does the given action.
QgsVectorDataProvider * dataProvider() FINAL
Returns the layer's data provider, it may be nullptr.
static QgsExpressionContextScope * globalScope()
Creates a new scope which contains variables and functions relating to the global QGIS context.
void addVariable(const QgsExpressionContextScope::StaticVariable &variable)
Adds a variable into the context scope.
QString name() const
The name of the action. This may be a longer description.
static QgsExpressionContextScope * notificationScope(const QString &message=QString())
Creates a new scope which contains variables and functions relating to provider notifications.
static QgsExpressionContextScope * layerScope(const QgsMapLayer *layer)
Creates a new scope which contains variables and functions relating to a QgsMapLayer.
static QgsProject * instance()
Returns the QgsProject singleton instance.
bool writeXml(QDomNode &layer_node) const
Writes the actions out in XML format.
void notify(const QString &msg)
Emitted when the datasource issues a notification.
QgsAction defaultAction(const QString &actionScope)
Each scope can have a default action.
void removeAction(QUuid actionId)
Remove an action by its id.
bool isValid() const
Returns true if this action was a default constructed one.
static QgsExpressionContextScope * projectScope(const QgsProject *project)
Creates a new scope which contains variables and functions relating to a QGIS project.
static QgsRunProcess * create(const QString &action, bool capture)
ActionType type() const
The action type.
QList< QgsAction > actions(const QString &actionScope=QString()) const
Returns a list of actions that are available in the given action scope.
int size() const
Returns number of items.
QVariant attribute(const QString &name) const
Lookup attribute value from attribute name.
Single scope for storing variables and functions for use within a QgsExpressionContext....
bool capture() const
Whether to capture output for display when this action is run.
bool readXml(const QDomNode &layer_node)
Reads the actions in in XML format.
bool runable() const
Checks if the action is runable on the current platform.
QSet< QString > actionScopes() const
The action scopes define where an action will be available.
QUuid id() const
Returns a unique id for this action.
void writeXml(QDomNode &actionsNode) const
Appends an XML definition for this action as a new child node to actionsNode.
void setDefaultAction(const QString &actionScope, QUuid actionId)
Each scope can have a default action.
static bool run(const QString &command, const QString &messageOnError=QString())
Execute a Python statement.
void readXml(const QDomNode &actionNode)
Reads an XML definition from actionNode into this object.
QgsAction action(QUuid id)
Gets an action by its id.
void clearActions()
Removes all actions.
QString command() const
Returns the command that is executed by this action.
Single variable definition for use within a QgsExpressionContextScope.
QgsField at(int i) const
Gets field at particular index (must be in range 0..N-1)
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...
virtual void setListening(bool isListening)
Set whether the provider will listen to datasource notifications If set, the provider will issue noti...
void setFeature(const QgsFeature &feature)
Convenience function for setting a feature for the context.