PageRenderTime 35ms CodeModel.GetById 5ms RepoModel.GetById 1ms app.codeStats 0ms

/src/core/qgsaction.cpp

http://github.com/qgis/Quantum-GIS
C++ | 166 lines | 127 code | 21 blank | 18 comment | 20 complexity | db736aaf169ce1ac715ae8bedd53cd01 MD5 | raw file
Possible License(s): LGPL-2.0, GPL-3.0, GPL-2.0, CC-BY-SA-3.0, MIT, 0BSD, BSD-3-Clause
  1. /***************************************************************************
  2. qgsaction.cpp - QgsAction
  3. ---------------------
  4. begin : 18.4.2016
  5. copyright : (C) 2016 by Matthias Kuhn
  6. email : matthias@opengis.ch
  7. ***************************************************************************
  8. * *
  9. * This program is free software; you can redistribute it and/or modify *
  10. * it under the terms of the GNU General Public License as published by *
  11. * the Free Software Foundation; either version 2 of the License, or *
  12. * (at your option) any later version. *
  13. * *
  14. ***************************************************************************/
  15. #include "qgsaction.h"
  16. #include <QDesktopServices>
  17. #include <QFileInfo>
  18. #include <QUrl>
  19. #include "qgspythonrunner.h"
  20. #include "qgsrunprocess.h"
  21. #include "qgsexpressioncontext.h"
  22. #include "qgsvectorlayer.h"
  23. #include "qgslogger.h"
  24. #include "qgsexpressioncontextutils.h"
  25. bool QgsAction::runable() const
  26. {
  27. return mType == Generic ||
  28. mType == GenericPython ||
  29. mType == OpenUrl ||
  30. #if defined(Q_OS_WIN)
  31. mType == Windows
  32. #elif defined(Q_OS_MAC)
  33. mType == Mac
  34. #else
  35. mType == Unix
  36. #endif
  37. ;
  38. }
  39. void QgsAction::run( QgsVectorLayer *layer, const QgsFeature &feature, const QgsExpressionContext &expressionContext ) const
  40. {
  41. QgsExpressionContext actionContext( expressionContext );
  42. actionContext << QgsExpressionContextUtils::layerScope( layer );
  43. actionContext.setFeature( feature );
  44. run( actionContext );
  45. }
  46. void QgsAction::run( const QgsExpressionContext &expressionContext ) const
  47. {
  48. if ( !isValid() )
  49. {
  50. QgsDebugMsg( QStringLiteral( "Invalid action cannot be run" ) );
  51. return;
  52. }
  53. QgsExpressionContextScope *scope = new QgsExpressionContextScope( mExpressionContextScope );
  54. QgsExpressionContext context( expressionContext );
  55. context << scope;
  56. QString expandedAction = QgsExpression::replaceExpressionText( mCommand, &context );
  57. if ( mType == QgsAction::OpenUrl )
  58. {
  59. QFileInfo finfo( expandedAction );
  60. if ( finfo.exists() && finfo.isFile() )
  61. QDesktopServices::openUrl( QUrl::fromLocalFile( expandedAction ) );
  62. else
  63. QDesktopServices::openUrl( QUrl( expandedAction, QUrl::TolerantMode ) );
  64. }
  65. else if ( mType == QgsAction::GenericPython )
  66. {
  67. // TODO: capture output from QgsPythonRunner (like QgsRunProcess does)
  68. QgsPythonRunner::run( expandedAction );
  69. }
  70. else
  71. {
  72. // The QgsRunProcess instance created by this static function
  73. // deletes itself when no longer needed.
  74. QgsRunProcess::create( expandedAction, mCaptureOutput );
  75. }
  76. }
  77. QSet<QString> QgsAction::actionScopes() const
  78. {
  79. return mActionScopes;
  80. }
  81. void QgsAction::setActionScopes( const QSet<QString> &actionScopes )
  82. {
  83. mActionScopes = actionScopes;
  84. }
  85. void QgsAction::readXml( const QDomNode &actionNode )
  86. {
  87. QDomElement actionElement = actionNode.toElement();
  88. QDomNodeList actionScopeNodes = actionElement.elementsByTagName( QStringLiteral( "actionScope" ) );
  89. if ( actionScopeNodes.isEmpty() )
  90. {
  91. mActionScopes
  92. << QStringLiteral( "Canvas" )
  93. << QStringLiteral( "Field" )
  94. << QStringLiteral( "Feature" );
  95. }
  96. else
  97. {
  98. for ( int j = 0; j < actionScopeNodes.length(); ++j )
  99. {
  100. QDomElement actionScopeElem = actionScopeNodes.item( j ).toElement();
  101. mActionScopes << actionScopeElem.attribute( QStringLiteral( "id" ) );
  102. }
  103. }
  104. mType = static_cast< QgsAction::ActionType >( actionElement.attributeNode( QStringLiteral( "type" ) ).value().toInt() );
  105. mDescription = actionElement.attributeNode( QStringLiteral( "name" ) ).value();
  106. mCommand = actionElement.attributeNode( QStringLiteral( "action" ) ).value();
  107. mIcon = actionElement.attributeNode( QStringLiteral( "icon" ) ).value();
  108. mCaptureOutput = actionElement.attributeNode( QStringLiteral( "capture" ) ).value().toInt() != 0;
  109. mShortTitle = actionElement.attributeNode( QStringLiteral( "shortTitle" ) ).value();
  110. mNotificationMessage = actionElement.attributeNode( QStringLiteral( "notificationMessage" ) ).value();
  111. mIsEnabledOnlyWhenEditable = actionElement.attributeNode( QStringLiteral( "isEnabledOnlyWhenEditable" ) ).value().toInt() != 0;
  112. mId = QUuid( actionElement.attributeNode( QStringLiteral( "id" ) ).value() );
  113. if ( mId.isNull() )
  114. mId = QUuid::createUuid();
  115. }
  116. void QgsAction::writeXml( QDomNode &actionsNode ) const
  117. {
  118. QDomElement actionSetting = actionsNode.ownerDocument().createElement( QStringLiteral( "actionsetting" ) );
  119. actionSetting.setAttribute( QStringLiteral( "type" ), mType );
  120. actionSetting.setAttribute( QStringLiteral( "name" ), mDescription );
  121. actionSetting.setAttribute( QStringLiteral( "shortTitle" ), mShortTitle );
  122. actionSetting.setAttribute( QStringLiteral( "icon" ), mIcon );
  123. actionSetting.setAttribute( QStringLiteral( "action" ), mCommand );
  124. actionSetting.setAttribute( QStringLiteral( "capture" ), mCaptureOutput );
  125. actionSetting.setAttribute( QStringLiteral( "notificationMessage" ), mNotificationMessage );
  126. actionSetting.setAttribute( QStringLiteral( "isEnabledOnlyWhenEditable" ), mIsEnabledOnlyWhenEditable );
  127. actionSetting.setAttribute( QStringLiteral( "id" ), mId.toString() );
  128. const auto constMActionScopes = mActionScopes;
  129. for ( const QString &scope : constMActionScopes )
  130. {
  131. QDomElement actionScopeElem = actionsNode.ownerDocument().createElement( QStringLiteral( "actionScope" ) );
  132. actionScopeElem.setAttribute( QStringLiteral( "id" ), scope );
  133. actionSetting.appendChild( actionScopeElem );
  134. }
  135. actionsNode.appendChild( actionSetting );
  136. }
  137. void QgsAction::setExpressionContextScope( const QgsExpressionContextScope &scope )
  138. {
  139. mExpressionContextScope = scope;
  140. }
  141. QgsExpressionContextScope QgsAction::expressionContextScope() const
  142. {
  143. return mExpressionContextScope;
  144. };