Skip to content

Commit

Permalink
Remove substitution map from QgsExpression::replaceExpressionText
Browse files Browse the repository at this point in the history
Since expression context variables should now be used instead. Also
remove all the deprecated methods which relied on this API.
  • Loading branch information
nyalldawson committed Aug 10, 2016
1 parent c39088b commit a0360a6
Show file tree
Hide file tree
Showing 12 changed files with 27 additions and 280 deletions.
19 changes: 19 additions & 0 deletions doc/api_break.dox
Expand Up @@ -128,6 +128,15 @@ corresponding counterparts in QgsUnitTypes should be used instead.</li>
<li>fileMenu() has been removed, use projectMenu() instead.
</ul>

\subsection qgis_api_break_3_0_QgsActionManager QgsActionManager

<ul>
<li>doAction() no longer accepts a substitution map. Use expression context variables instead.</li>
<li>The doAction() variant which takes a QgsFeature along has been removed. Use the expression context
variant instead.</li>
<li>expandAction() has been removed. Use QgsExpression::replaceExpressionText() instead.</li>
</ul>

\subsection qgis_api_break_3_0_QgsAtlasComposition QgsAtlasComposition

<ul>
Expand All @@ -146,6 +155,15 @@ corresponding counterparts in QgsUnitTypes should be used instead.</li>
<li>GenericDataSourceURI has been renamed to GenericDataSourceUri</li>
</ul>

\subsection qgis_api_break_3_0_QgsComposerLabel QgsComposerLabel

<ul>
<li>setExpressionContext() has been removed. Setup the composition using an atlas and with
expression variables in the composer label item instead.</li>
<li>setSubstitutions has been removed. Use expression context variables in the composer
label item instead.</li>
</ul>

\subsection qgis_api_break_3_0_QgsComposerLegend QgsComposerLegend

<ul>
Expand Down Expand Up @@ -313,6 +331,7 @@ version instead.</li>
<li>QgsExpression::Function::func has been modified to use a QgsExpressionContext argument rather than a QgsFeature.</li>
<li>The QgsExpression::Node::eval and prepare versions which take a QgsFeature has been removed, use the QgsExpressionContext versions instead.</li>
<li>QgsExpression::Interval has been removed. Use QgsInterval instead.</li>
<li>replaceExpressionText() no longer accepts a substitution map parameter. Use expression context variables instead.</li>
</ul>

\subsection qgis_api_break_3_0_QgsFeature QgsFeature
Expand Down
10 changes: 0 additions & 10 deletions python/core/composer/qgscomposerlabel.sip
Expand Up @@ -28,16 +28,6 @@ class QgsComposerLabel : QgsComposerItem
/** Returns the text as it appears on screen (with replaced data field) */
QString displayText() const;

/** Sets the current feature, the current layer and a list of local variable substitutions for evaluating expressions.
* @deprecated use atlas features and setSubstitutions() instead
*/
void setExpressionContext( QgsFeature* feature, QgsVectorLayer* layer, const QMap<QString, QVariant>& substitutions = QMap<QString, QVariant>() ) /Deprecated/;

/** Sets the list of local variable substitutions for evaluating expressions in label text.
* @note added in QGIS 2.12
*/
void setSubstitutions( const QMap<QString, QVariant>& substitutions = QMap<QString, QVariant>() );

QFont font() const;
void setFont( const QFont& f );
/** Accessor for the vertical alignment of the label
Expand Down
36 changes: 1 addition & 35 deletions python/core/qgsactionmanager.sip
Expand Up @@ -75,24 +75,10 @@ class QgsActionManager
* @param index action index
* @param feature feature to run action for
* @param context expression context to evalute expressions under
* @param substitutionMap deprecated - kept for compatibility with projects, will be removed for 3.0
*/
// TODO QGIS 3.0 remove substition map - force use of expression variables
void doAction( int index,
const QgsFeature& feature,
const QgsExpressionContext& context,
const QMap<QString, QVariant> *substitutionMap = nullptr );


/** Does the action using the expression builder to expand it
* and getting values from the passed feature attribute map.
* substitutionMap is used to pass custom substitutions, to replace
* each key in the map with the associated value
* @note available in python bindings as doActionFeatureWithSubstitution
*/
void doAction( int index,
const QgsFeature &feat,
const QMap<QString, QVariant> *substitutionMap ) /PyName=doActionFeatureWithSubstitution/;
const QgsExpressionContext& context );

//! Removes all actions
void clearActions();
Expand All @@ -103,26 +89,6 @@ class QgsActionManager
//! Return the layer
QgsVectorLayer* layer() const;

/** Expands the given action, replacing all %'s with the value as
* given.
* @deprecated use QgsExpression::replaceExpressionText() instead
*/
QString expandAction( QString action, const QMap<int, QVariant> &attributes, uint defaultValueIndex ) /Deprecated/;

/** Expands the given action using the expression builder
* This function currently replaces each expression between [% and %]
* placeholders in the action with the result of its evaluation on
* the feature passed as argument.
*
* Additional substitutions can be passed through the substitutionMap
* parameter
* @deprecated use QgsExpression::replaceExpressionText() instead
*/
QString expandAction( const QString& action,
QgsFeature &feat,
const QMap<QString, QVariant> *substitutionMap = 0 ) /Deprecated/;


//! Writes the actions out in XML format
bool writeXml( QDomNode& layer_node, QDomDocument& doc ) const;

Expand Down
2 changes: 0 additions & 2 deletions python/core/qgsexpression.sip
Expand Up @@ -140,13 +140,11 @@ class QgsExpression
* Additional substitutions can be passed through the substitutionMap parameter
* @param action
* @param context expression context
* @param substitutionMap
* @param distanceArea optional QgsDistanceArea. If specified, the QgsDistanceArea is used for distance
* and area conversion
* @note added in QGIS 2.12
*/
static QString replaceExpressionText( const QString &action, const QgsExpressionContext* context,
const QMap<QString, QVariant> *substitutionMap = 0,
const QgsDistanceArea* distanceArea = 0 );

/** Attempts to evaluate a text string as an expression to a resultant double
Expand Down
10 changes: 1 addition & 9 deletions src/app/qgsmaptoolfeatureaction.cpp
Expand Up @@ -138,14 +138,6 @@ bool QgsMapToolFeatureAction::doAction( QgsVectorLayer *layer, int x, int y )
if ( layer->actions()->defaultAction() >= 0 )
{
// define custom substitutions: layer id and clicked coords

// TODO QGIS 3.0 - remove these deprecated global expression variables!
QMap<QString, QVariant> substitutionMap;
substitutionMap.insert( "$layerid", layer->id() );
point = toLayerCoordinates( layer, point );
substitutionMap.insert( "$clickx", point.x() );
substitutionMap.insert( "$clicky", point.y() );

QgsExpressionContext context;
context << QgsExpressionContextUtils::globalScope()
<< QgsExpressionContextUtils::projectScope()
Expand All @@ -156,7 +148,7 @@ bool QgsMapToolFeatureAction::doAction( QgsVectorLayer *layer, int x, int y )
context << actionScope;

int actionIdx = layer->actions()->defaultAction();
layer->actions()->doAction( actionIdx, feat, context, &substitutionMap );
layer->actions()->doAction( actionIdx, feat, context );
}
else
{
Expand Down
2 changes: 1 addition & 1 deletion src/core/composer/qgscomposerhtml.cpp
Expand Up @@ -179,7 +179,7 @@ void QgsComposerHtml::loadHtml( const bool useCache, const QgsExpressionContext
//evaluate expressions
if ( mEvaluateExpressions )
{
loadedHtml = QgsExpression::replaceExpressionText( loadedHtml, evalContext, nullptr, mDistanceArea );
loadedHtml = QgsExpression::replaceExpressionText( loadedHtml, evalContext, mDistanceArea );
}

mLoaded = false;
Expand Down
35 changes: 1 addition & 34 deletions src/core/composer/qgscomposerlabel.cpp
Expand Up @@ -240,38 +240,6 @@ void QgsComposerLabel::setHtmlState( int state )
}
}

void QgsComposerLabel::setExpressionContext( QgsFeature *feature, QgsVectorLayer* layer, const QMap<QString, QVariant>& substitutions )
{
mExpressionFeature.reset( feature ? new QgsFeature( *feature ) : nullptr );
mExpressionLayer = layer;
mSubstitutions = substitutions;

//setup distance area conversion
if ( layer )
{
mDistanceArea->setSourceCrs( layer->crs().srsid() );
}
else if ( mComposition )
{
//set to composition's mapsettings' crs
mDistanceArea->setSourceCrs( mComposition->mapSettings().destinationCrs().srsid() );
}
if ( mComposition )
{
mDistanceArea->setEllipsoidalMode( mComposition->mapSettings().hasCrsTransformEnabled() );
}
mDistanceArea->setEllipsoid( QgsProject::instance()->readEntry( "Measure", "/Ellipsoid", GEO_NONE ) );
contentChanged();

// Force label to redraw -- fixes label printing for labels with blend modes when used with atlas
update();
}

void QgsComposerLabel::setSubstitutions( const QMap<QString, QVariant>& substitutions )
{
mSubstitutions = substitutions;
}

void QgsComposerLabel::refreshExpressionContext()
{
mExpressionLayer = nullptr;
Expand Down Expand Up @@ -307,7 +275,6 @@ QString QgsComposerLabel::displayText() const
{
QString displayText = mText;
replaceDateText( displayText );
QMap<QString, QVariant> subs = mSubstitutions;

QScopedPointer<QgsExpressionContext> context( createExpressionContext() );
//overwrite layer/feature if they have been set via setExpressionContext
Expand All @@ -317,7 +284,7 @@ QString QgsComposerLabel::displayText() const
if ( mExpressionLayer )
context->setFields( mExpressionLayer->fields() );

return QgsExpression::replaceExpressionText( displayText, context.data(), &subs, mDistanceArea );
return QgsExpression::replaceExpressionText( displayText, context.data(), mDistanceArea );
}

void QgsComposerLabel::replaceDateText( QString& text ) const
Expand Down
11 changes: 0 additions & 11 deletions src/core/composer/qgscomposerlabel.h
Expand Up @@ -53,16 +53,6 @@ class CORE_EXPORT QgsComposerLabel: public QgsComposerItem
/** Returns the text as it appears on screen (with replaced data field) */
QString displayText() const;

/** Sets the current feature, the current layer and a list of local variable substitutions for evaluating expressions.
* @deprecated use atlas features and setSubstitutions() instead
*/
Q_DECL_DEPRECATED void setExpressionContext( QgsFeature* feature, QgsVectorLayer* layer, const QMap<QString, QVariant>& substitutions = ( QMap<QString, QVariant>() ) );

/** Sets the list of local variable substitutions for evaluating expressions in label text.
* @note added in QGIS 2.12
*/
void setSubstitutions( const QMap<QString, QVariant>& substitutions = ( QMap<QString, QVariant>() ) );

QFont font() const;
void setFont( const QFont& f );
/** Accessor for the vertical alignment of the label
Expand Down Expand Up @@ -213,7 +203,6 @@ class CORE_EXPORT QgsComposerLabel: public QgsComposerItem

QScopedPointer<QgsFeature> mExpressionFeature;
QgsVectorLayer* mExpressionLayer;
QMap<QString, QVariant> mSubstitutions;
QgsDistanceArea* mDistanceArea;

QgsWebPage* mWebPage;
Expand Down

0 comments on commit a0360a6

Please sign in to comment.