QGIS API Documentation  3.0.2-Girona (307d082)
qgsaction.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsaction.cpp - QgsAction
3 
4  ---------------------
5  begin : 18.4.2016
6  copyright : (C) 2016 by Matthias Kuhn
7  email : [email protected]
8  ***************************************************************************
9  * *
10  * This program is free software; you can redistribute it and/or modify *
11  * it under the terms of the GNU General Public License as published by *
12  * the Free Software Foundation; either version 2 of the License, or *
13  * (at your option) any later version. *
14  * *
15  ***************************************************************************/
16 
17 #include "qgsaction.h"
18 
19 #include <QDesktopServices>
20 #include <QFileInfo>
21 #include <QUrl>
22 
23 #include "qgspythonrunner.h"
24 #include "qgsrunprocess.h"
25 #include "qgsexpressioncontext.h"
26 #include "qgsvectorlayer.h"
27 #include "qgslogger.h"
28 
29 bool QgsAction::runable() const
30 {
31  return mType == Generic ||
32  mType == GenericPython ||
33  mType == OpenUrl ||
34 #if defined(Q_OS_WIN)
35  mType == Windows
36 #elif defined(Q_OS_MAC)
37  mType == Mac
38 #else
39  mType == Unix
40 #endif
41  ;
42 }
43 
44 void QgsAction::run( QgsVectorLayer *layer, const QgsFeature &feature, const QgsExpressionContext &expressionContext ) const
45 {
46  QgsExpressionContext actionContext( expressionContext );
47 
48  actionContext << QgsExpressionContextUtils::layerScope( layer );
49  actionContext.setFeature( feature );
50 
51  run( actionContext );
52 }
53 
54 void QgsAction::run( const QgsExpressionContext &expressionContext ) const
55 {
56  if ( !isValid() )
57  {
58  QgsDebugMsg( "Invalid action cannot be run" );
59  return;
60  }
61 
62  QgsExpressionContextScope *scope = new QgsExpressionContextScope( mExpressionContextScope );
63  QgsExpressionContext context( expressionContext );
64  context << scope;
65 
66  QString expandedAction = QgsExpression::replaceExpressionText( mCommand, &context );
67 
68  if ( mType == QgsAction::OpenUrl )
69  {
70  QFileInfo finfo( expandedAction );
71  if ( finfo.exists() && finfo.isFile() )
72  QDesktopServices::openUrl( QUrl::fromLocalFile( expandedAction ) );
73  else
74  QDesktopServices::openUrl( QUrl( expandedAction, QUrl::TolerantMode ) );
75  }
76  else if ( mType == QgsAction::GenericPython )
77  {
78  // TODO: capture output from QgsPythonRunner (like QgsRunProcess does)
79  QgsPythonRunner::run( expandedAction );
80  }
81  else
82  {
83  // The QgsRunProcess instance created by this static function
84  // deletes itself when no longer needed.
85  QgsRunProcess::create( expandedAction, mCaptureOutput );
86  }
87 }
88 
89 QSet<QString> QgsAction::actionScopes() const
90 {
91  return mActionScopes;
92 }
93 
94 void QgsAction::setActionScopes( const QSet<QString> &actionScopes )
95 {
96  mActionScopes = actionScopes;
97 }
98 
99 void QgsAction::readXml( const QDomNode &actionNode )
100 {
101  QDomElement actionElement = actionNode.toElement();
102  QDomNodeList actionScopeNodes = actionElement.elementsByTagName( QStringLiteral( "actionScope" ) );
103 
104  if ( actionScopeNodes.isEmpty() )
105  {
106  mActionScopes
107  << QStringLiteral( "Canvas" )
108  << QStringLiteral( "Field" )
109  << QStringLiteral( "Feature" );
110  }
111  else
112  {
113  for ( int j = 0; j < actionScopeNodes.length(); ++j )
114  {
115  QDomElement actionScopeElem = actionScopeNodes.item( j ).toElement();
116  mActionScopes << actionScopeElem.attribute( QStringLiteral( "id" ) );
117  }
118  }
119 
120  mType = static_cast< QgsAction::ActionType >( actionElement.attributeNode( QStringLiteral( "type" ) ).value().toInt() );
121  mDescription = actionElement.attributeNode( QStringLiteral( "name" ) ).value();
122  mCommand = actionElement.attributeNode( QStringLiteral( "action" ) ).value();
123  mIcon = actionElement.attributeNode( QStringLiteral( "icon" ) ).value();
124  mCaptureOutput = actionElement.attributeNode( QStringLiteral( "capture" ) ).value().toInt() != 0;
125  mShortTitle = actionElement.attributeNode( QStringLiteral( "shortTitle" ) ).value();
126  mNotificationMessage = actionElement.attributeNode( QStringLiteral( "notificationMessage" ) ).value();
127  mIsEnabledOnlyWhenEditable = actionElement.attributeNode( QStringLiteral( "isEnabledOnlyWhenEditable" ) ).value().toInt() != 0;
128  mId = QUuid( actionElement.attributeNode( QStringLiteral( "id" ) ).value() );
129  if ( mId.isNull() )
130  mId = QUuid::createUuid();
131 }
132 
133 void QgsAction::writeXml( QDomNode &actionsNode ) const
134 {
135  QDomElement actionSetting = actionsNode.ownerDocument().createElement( QStringLiteral( "actionsetting" ) );
136  actionSetting.setAttribute( QStringLiteral( "type" ), mType );
137  actionSetting.setAttribute( QStringLiteral( "name" ), mDescription );
138  actionSetting.setAttribute( QStringLiteral( "shortTitle" ), mShortTitle );
139  actionSetting.setAttribute( QStringLiteral( "icon" ), mIcon );
140  actionSetting.setAttribute( QStringLiteral( "action" ), mCommand );
141  actionSetting.setAttribute( QStringLiteral( "capture" ), mCaptureOutput );
142  actionSetting.setAttribute( QStringLiteral( "notificationMessage" ), mNotificationMessage );
143  actionSetting.setAttribute( QStringLiteral( "isEnabledOnlyWhenEditable" ), mIsEnabledOnlyWhenEditable );
144  actionSetting.setAttribute( QStringLiteral( "id" ), mId.toString() );
145 
146  Q_FOREACH ( const QString &scope, mActionScopes )
147  {
148  QDomElement actionScopeElem = actionsNode.ownerDocument().createElement( QStringLiteral( "actionScope" ) );
149  actionScopeElem.setAttribute( QStringLiteral( "id" ), scope );
150  actionSetting.appendChild( actionScopeElem );
151  }
152 
153  actionsNode.appendChild( actionSetting );
154 }
155 
157 {
158  mExpressionContextScope = scope;
159 }
160 
162 {
163  return mExpressionContextScope;
164 };
void setFeature(const QgsFeature &feature)
Convenience function for setting a feature for the context.
#define QgsDebugMsg(str)
Definition: qgslogger.h:38
QSet< QString > actionScopes() const
The action scopes define where an action will be available.
Definition: qgsaction.cpp:89
void readXml(const QDomNode &actionNode)
Reads an XML definition from actionNode into this object.
Definition: qgsaction.cpp:99
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
Definition: qgsfeature.h:62
bool isValid() const
Returns true if this action was a default constructed one.
Definition: qgsaction.h:141
void run(QgsVectorLayer *layer, const QgsFeature &feature, const QgsExpressionContext &expressionContext) const
Run this action.
Definition: qgsaction.cpp:44
void writeXml(QDomNode &actionsNode) const
Appends an XML definition for this action as a new child node to actionsNode.
Definition: qgsaction.cpp:133
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
bool runable() const
Checks if the action is runable on the current platform.
Definition: qgsaction.cpp:29
Single scope for storing variables and functions for use within a QgsExpressionContext.
static bool run(const QString &command, const QString &messageOnError=QString())
Execute a Python statement.
static QgsRunProcess * create(const QString &action, bool capture)
Definition: qgsrunprocess.h:50
static QgsExpressionContextScope * layerScope(const QgsMapLayer *layer)
Creates a new scope which contains variables and functions relating to a QgsMapLayer.
QgsExpressionContextScope expressionContextScope() const
Returns an expression context scope used for running the action.
Definition: qgsaction.cpp:161
void setExpressionContextScope(const QgsExpressionContextScope &scope)
Sets an expression context scope to use for running the action.
Definition: qgsaction.cpp:156
Represents a vector layer which manages a vector based data sets.
void setActionScopes(const QSet< QString > &actionScopes)
The action scopes define where an action will be available.
Definition: qgsaction.cpp:94