Skip to content
Permalink
Browse files

Port attribute actions to expression contexts

  • Loading branch information
nyalldawson committed Aug 22, 2015
1 parent 3b6f591 commit f82c641a3d19232bc8757527d2f1ec4c5fd308af
Showing with 22 additions and 3 deletions.
  1. +18 −2 src/core/qgsattributeaction.cpp
  2. +4 −1 src/core/qgsattributeaction.h
@@ -81,7 +81,9 @@ void QgsAttributeAction::doAction( int index, const QgsFeature &feat, const QMap
return;

// search for expressions while expanding actions
QString expandedAction = QgsExpression::replaceExpressionText( action.action(), &feat, mLayer, substitutionMap );
QgsExpressionContext context = createExpressionContext();
context.setFeature( feat );
QString expandedAction = QgsExpression::replaceExpressionText( action.action(), &context, substitutionMap );
if ( expandedAction.isEmpty() )
return;

@@ -125,6 +127,17 @@ void QgsAttributeAction::runAction( const QgsAction &action, void ( *executePyth
}
}

QgsExpressionContext QgsAttributeAction::createExpressionContext() const
{
QgsExpressionContext context;
context << QgsExpressionContextUtils::globalScope()
<< QgsExpressionContextUtils::projectScope();
if ( mLayer )
context << QgsExpressionContextUtils::layerScope( mLayer );

return context;
}

QString QgsAttributeAction::expandAction( QString action, const QgsAttributeMap &attributes,
uint clickedOnValue )
{
@@ -215,7 +228,10 @@ QString QgsAttributeAction::expandAction( QString action, QgsFeature &feat, cons
continue;
}

QVariant result = exp.evaluate( &feat, mLayer->fields() );
QgsExpressionContext context = createExpressionContext();
context.setFeature( feat );

QVariant result = exp.evaluate( &context );
if ( exp.hasEvalError() )
{
QgsDebugMsg( "Expression parser eval error: " + exp.evalErrorString() );
@@ -27,7 +27,8 @@
#include <QString>
#include <QIcon>

#include <qgsfeature.h>
#include "qgsfeature.h"
#include "qgsexpressioncontext.h"

class QDomNode;
class QDomDocument;
@@ -202,6 +203,8 @@ class CORE_EXPORT QgsAttributeAction
void ( *executePython )( const QString & ) = 0 );

int mDefaultAction;

QgsExpressionContext createExpressionContext() const;
};

#endif

0 comments on commit f82c641

Please sign in to comment.
You can’t perform that action at this time.