Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Port attribute actions to expression contexts
  • Loading branch information
nyalldawson committed Aug 22, 2015
1 parent 3b6f591 commit f82c641
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
20 changes: 18 additions & 2 deletions src/core/qgsattributeaction.cpp
Expand Up @@ -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;

Expand Down Expand Up @@ -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 )
{
Expand Down Expand Up @@ -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() );
Expand Down
5 changes: 4 additions & 1 deletion src/core/qgsattributeaction.h
Expand Up @@ -27,7 +27,8 @@
#include <QString>
#include <QIcon>

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

class QDomNode;
class QDomDocument;
Expand Down Expand Up @@ -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.