Skip to content

Commit f82c641

Browse files
committedAug 22, 2015
Port attribute actions to expression contexts
1 parent 3b6f591 commit f82c641

File tree

2 files changed

+22
-3
lines changed

2 files changed

+22
-3
lines changed
 

‎src/core/qgsattributeaction.cpp

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,9 @@ void QgsAttributeAction::doAction( int index, const QgsFeature &feat, const QMap
8181
return;
8282

8383
// search for expressions while expanding actions
84-
QString expandedAction = QgsExpression::replaceExpressionText( action.action(), &feat, mLayer, substitutionMap );
84+
QgsExpressionContext context = createExpressionContext();
85+
context.setFeature( feat );
86+
QString expandedAction = QgsExpression::replaceExpressionText( action.action(), &context, substitutionMap );
8587
if ( expandedAction.isEmpty() )
8688
return;
8789

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

130+
QgsExpressionContext QgsAttributeAction::createExpressionContext() const
131+
{
132+
QgsExpressionContext context;
133+
context << QgsExpressionContextUtils::globalScope()
134+
<< QgsExpressionContextUtils::projectScope();
135+
if ( mLayer )
136+
context << QgsExpressionContextUtils::layerScope( mLayer );
137+
138+
return context;
139+
}
140+
128141
QString QgsAttributeAction::expandAction( QString action, const QgsAttributeMap &attributes,
129142
uint clickedOnValue )
130143
{
@@ -215,7 +228,10 @@ QString QgsAttributeAction::expandAction( QString action, QgsFeature &feat, cons
215228
continue;
216229
}
217230

218-
QVariant result = exp.evaluate( &feat, mLayer->fields() );
231+
QgsExpressionContext context = createExpressionContext();
232+
context.setFeature( feat );
233+
234+
QVariant result = exp.evaluate( &context );
219235
if ( exp.hasEvalError() )
220236
{
221237
QgsDebugMsg( "Expression parser eval error: " + exp.evalErrorString() );

‎src/core/qgsattributeaction.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@
2727
#include <QString>
2828
#include <QIcon>
2929

30-
#include <qgsfeature.h>
30+
#include "qgsfeature.h"
31+
#include "qgsexpressioncontext.h"
3132

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

204205
int mDefaultAction;
206+
207+
QgsExpressionContext createExpressionContext() const;
205208
};
206209

207210
#endif

0 commit comments

Comments
 (0)
Please sign in to comment.