Showing with 35 additions and 21 deletions.
  1. +24 −13 src/app/qgsmaptoolfeatureaction.cpp
  2. +11 −8 src/core/qgsattributeaction.cpp
37 changes: 24 additions & 13 deletions src/app/qgsmaptoolfeatureaction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,12 @@ void QgsMapToolFeatureAction::canvasReleaseEvent( QMouseEvent *e )
return;
}

if ( !mCanvas->layers().contains( layer ) )
{
// do not run actions on hidden layers
return;
}

QgsVectorLayer *vlayer = qobject_cast<QgsVectorLayer *>( layer );
if ( vlayer->actions()->size() == 0 )
{
Expand Down Expand Up @@ -110,7 +116,7 @@ bool QgsMapToolFeatureAction::doAction( QgsVectorLayer *layer, int x, int y )
if ( identifyValue <= 0.0 )
identifyValue = QGis::DEFAULT_IDENTIFY_RADIUS;

QgsFeature feat;
QgsFeatureList featList;

// toLayerCoordinates will throw an exception for an 'invalid' point.
// For example, if you project a world map onto a globe using EPSG 2163
Expand All @@ -130,10 +136,8 @@ bool QgsMapToolFeatureAction::doAction( QgsVectorLayer *layer, int x, int y )

layer->select( layer->pendingAllAttributesList(), r, true, true );
QgsFeature f;
if ( layer->nextFeature( f ) )
feat = QgsFeature( f );
else
return false;
while ( layer->nextFeature( f ) )
featList << QgsFeature( f );
}
catch ( QgsCsException & cse )
{
Expand All @@ -142,15 +146,22 @@ bool QgsMapToolFeatureAction::doAction( QgsVectorLayer *layer, int x, int y )
QgsDebugMsg( QString( "Caught CRS exception %1" ).arg( cse.what() ) );
}

int action = layer->actions()->defaultAction();
if ( featList.size() == 0 )
return false;

// define custom substitutions: layer id and clicked coords
QMap<QString, QVariant> substitutionMap;
substitutionMap.insert( "$layerid", layer->id() );
point = toLayerCoordinates( layer, point );
substitutionMap.insert( "$clickx", point.x() );
substitutionMap.insert( "$clicky", point.y() );
foreach ( QgsFeature feat, featList )
{
int actionIdx = layer->actions()->defaultAction();

// define custom substitutions: layer id and clicked coords
QMap<QString, QVariant> substitutionMap;
substitutionMap.insert( "$layerid", layer->id() );
point = toLayerCoordinates( layer, point );
substitutionMap.insert( "$clickx", point.x() );
substitutionMap.insert( "$clicky", point.y() );

layer->actions()->doAction( actionIdx, feat, &substitutionMap );
}

layer->actions()->doAction( action, feat, &substitutionMap );
return true;
}
19 changes: 11 additions & 8 deletions src/core/qgsattributeaction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,10 @@ void QgsAttributeAction::doAction( int index, QgsFeature &feat, int defaultValue
{
QMap<QString, QVariant> substitutionMap;
if ( defaultValueIndex >= 0 )
substitutionMap.insert( "$currfield", QVariant( defaultValueIndex ) );
{
if ( feat.attributeMap().contains( defaultValueIndex ) )
substitutionMap.insert( "$currfield", feat.attributeMap()[ defaultValueIndex ] );
}

doAction( index, feat, &substitutionMap );
}
Expand Down Expand Up @@ -203,26 +206,26 @@ QString QgsAttributeAction::expandAction( QString action, QgsFeature &feat, cons
index = pos + rx.matchedLength();

QString to_replace = rx.cap( 1 ).trimmed();
QgsDebugMsg( "Found expression:" + to_replace );
QgsDebugMsg( "Found expression: " + to_replace );

if ( substitutionMap && substitutionMap->contains( to_replace ) )
{
expr_action += action.mid( start, pos - start ) + substitutionMap->value( to_replace ).toString();
continue;
}

QgsExpression* exp = new QgsExpression( to_replace );
if ( exp->hasParserError() )
QgsExpression exp( to_replace );
if ( exp.hasParserError() )
{
QgsDebugMsg( "Expression parser error:" + exp->parserErrorString() );
QgsDebugMsg( "Expression parser error: " + exp.parserErrorString() );
expr_action += action.mid( start, index - start );
continue;
}

QVariant result = exp->evaluate( &feat, mLayer->pendingFields() );
if ( exp->hasEvalError() )
QVariant result = exp.evaluate( &feat, mLayer->pendingFields() );
if ( exp.hasEvalError() )
{
QgsDebugMsg( "Expression parser eval error:" + exp->evalErrorString() );
QgsDebugMsg( "Expression parser eval error: " + exp.evalErrorString() );
expr_action += action.mid( start, index - start );
continue;
}
Expand Down