Skip to content

Commit a0360a6

Browse files
committedAug 10, 2016
Remove substitution map from QgsExpression::replaceExpressionText
Since expression context variables should now be used instead. Also remove all the deprecated methods which relied on this API.
1 parent c39088b commit a0360a6

12 files changed

+27
-280
lines changed
 

‎doc/api_break.dox

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,15 @@ corresponding counterparts in QgsUnitTypes should be used instead.</li>
128128
<li>fileMenu() has been removed, use projectMenu() instead.
129129
</ul>
130130

131+
\subsection qgis_api_break_3_0_QgsActionManager QgsActionManager
132+
133+
<ul>
134+
<li>doAction() no longer accepts a substitution map. Use expression context variables instead.</li>
135+
<li>The doAction() variant which takes a QgsFeature along has been removed. Use the expression context
136+
variant instead.</li>
137+
<li>expandAction() has been removed. Use QgsExpression::replaceExpressionText() instead.</li>
138+
</ul>
139+
131140
\subsection qgis_api_break_3_0_QgsAtlasComposition QgsAtlasComposition
132141

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

158+
\subsection qgis_api_break_3_0_QgsComposerLabel QgsComposerLabel
159+
160+
<ul>
161+
<li>setExpressionContext() has been removed. Setup the composition using an atlas and with
162+
expression variables in the composer label item instead.</li>
163+
<li>setSubstitutions has been removed. Use expression context variables in the composer
164+
label item instead.</li>
165+
</ul>
166+
149167
\subsection qgis_api_break_3_0_QgsComposerLegend QgsComposerLegend
150168

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

318337
\subsection qgis_api_break_3_0_QgsFeature QgsFeature

‎python/core/composer/qgscomposerlabel.sip

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,16 +28,6 @@ class QgsComposerLabel : QgsComposerItem
2828
/** Returns the text as it appears on screen (with replaced data field) */
2929
QString displayText() const;
3030

31-
/** Sets the current feature, the current layer and a list of local variable substitutions for evaluating expressions.
32-
* @deprecated use atlas features and setSubstitutions() instead
33-
*/
34-
void setExpressionContext( QgsFeature* feature, QgsVectorLayer* layer, const QMap<QString, QVariant>& substitutions = QMap<QString, QVariant>() ) /Deprecated/;
35-
36-
/** Sets the list of local variable substitutions for evaluating expressions in label text.
37-
* @note added in QGIS 2.12
38-
*/
39-
void setSubstitutions( const QMap<QString, QVariant>& substitutions = QMap<QString, QVariant>() );
40-
4131
QFont font() const;
4232
void setFont( const QFont& f );
4333
/** Accessor for the vertical alignment of the label

‎python/core/qgsactionmanager.sip

Lines changed: 1 addition & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -75,24 +75,10 @@ class QgsActionManager
7575
* @param index action index
7676
* @param feature feature to run action for
7777
* @param context expression context to evalute expressions under
78-
* @param substitutionMap deprecated - kept for compatibility with projects, will be removed for 3.0
7978
*/
80-
// TODO QGIS 3.0 remove substition map - force use of expression variables
8179
void doAction( int index,
8280
const QgsFeature& feature,
83-
const QgsExpressionContext& context,
84-
const QMap<QString, QVariant> *substitutionMap = nullptr );
85-
86-
87-
/** Does the action using the expression builder to expand it
88-
* and getting values from the passed feature attribute map.
89-
* substitutionMap is used to pass custom substitutions, to replace
90-
* each key in the map with the associated value
91-
* @note available in python bindings as doActionFeatureWithSubstitution
92-
*/
93-
void doAction( int index,
94-
const QgsFeature &feat,
95-
const QMap<QString, QVariant> *substitutionMap ) /PyName=doActionFeatureWithSubstitution/;
81+
const QgsExpressionContext& context );
9682

9783
//! Removes all actions
9884
void clearActions();
@@ -103,26 +89,6 @@ class QgsActionManager
10389
//! Return the layer
10490
QgsVectorLayer* layer() const;
10591

106-
/** Expands the given action, replacing all %'s with the value as
107-
* given.
108-
* @deprecated use QgsExpression::replaceExpressionText() instead
109-
*/
110-
QString expandAction( QString action, const QMap<int, QVariant> &attributes, uint defaultValueIndex ) /Deprecated/;
111-
112-
/** Expands the given action using the expression builder
113-
* This function currently replaces each expression between [% and %]
114-
* placeholders in the action with the result of its evaluation on
115-
* the feature passed as argument.
116-
*
117-
* Additional substitutions can be passed through the substitutionMap
118-
* parameter
119-
* @deprecated use QgsExpression::replaceExpressionText() instead
120-
*/
121-
QString expandAction( const QString& action,
122-
QgsFeature &feat,
123-
const QMap<QString, QVariant> *substitutionMap = 0 ) /Deprecated/;
124-
125-
12692
//! Writes the actions out in XML format
12793
bool writeXml( QDomNode& layer_node, QDomDocument& doc ) const;
12894

‎python/core/qgsexpression.sip

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,13 +140,11 @@ class QgsExpression
140140
* Additional substitutions can be passed through the substitutionMap parameter
141141
* @param action
142142
* @param context expression context
143-
* @param substitutionMap
144143
* @param distanceArea optional QgsDistanceArea. If specified, the QgsDistanceArea is used for distance
145144
* and area conversion
146145
* @note added in QGIS 2.12
147146
*/
148147
static QString replaceExpressionText( const QString &action, const QgsExpressionContext* context,
149-
const QMap<QString, QVariant> *substitutionMap = 0,
150148
const QgsDistanceArea* distanceArea = 0 );
151149

152150
/** Attempts to evaluate a text string as an expression to a resultant double

‎src/app/qgsmaptoolfeatureaction.cpp

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -138,14 +138,6 @@ bool QgsMapToolFeatureAction::doAction( QgsVectorLayer *layer, int x, int y )
138138
if ( layer->actions()->defaultAction() >= 0 )
139139
{
140140
// define custom substitutions: layer id and clicked coords
141-
142-
// TODO QGIS 3.0 - remove these deprecated global expression variables!
143-
QMap<QString, QVariant> substitutionMap;
144-
substitutionMap.insert( "$layerid", layer->id() );
145-
point = toLayerCoordinates( layer, point );
146-
substitutionMap.insert( "$clickx", point.x() );
147-
substitutionMap.insert( "$clicky", point.y() );
148-
149141
QgsExpressionContext context;
150142
context << QgsExpressionContextUtils::globalScope()
151143
<< QgsExpressionContextUtils::projectScope()
@@ -156,7 +148,7 @@ bool QgsMapToolFeatureAction::doAction( QgsVectorLayer *layer, int x, int y )
156148
context << actionScope;
157149

158150
int actionIdx = layer->actions()->defaultAction();
159-
layer->actions()->doAction( actionIdx, feat, context, &substitutionMap );
151+
layer->actions()->doAction( actionIdx, feat, context );
160152
}
161153
else
162154
{

‎src/core/composer/qgscomposerhtml.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ void QgsComposerHtml::loadHtml( const bool useCache, const QgsExpressionContext
179179
//evaluate expressions
180180
if ( mEvaluateExpressions )
181181
{
182-
loadedHtml = QgsExpression::replaceExpressionText( loadedHtml, evalContext, nullptr, mDistanceArea );
182+
loadedHtml = QgsExpression::replaceExpressionText( loadedHtml, evalContext, mDistanceArea );
183183
}
184184

185185
mLoaded = false;

‎src/core/composer/qgscomposerlabel.cpp

Lines changed: 1 addition & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -240,38 +240,6 @@ void QgsComposerLabel::setHtmlState( int state )
240240
}
241241
}
242242

243-
void QgsComposerLabel::setExpressionContext( QgsFeature *feature, QgsVectorLayer* layer, const QMap<QString, QVariant>& substitutions )
244-
{
245-
mExpressionFeature.reset( feature ? new QgsFeature( *feature ) : nullptr );
246-
mExpressionLayer = layer;
247-
mSubstitutions = substitutions;
248-
249-
//setup distance area conversion
250-
if ( layer )
251-
{
252-
mDistanceArea->setSourceCrs( layer->crs().srsid() );
253-
}
254-
else if ( mComposition )
255-
{
256-
//set to composition's mapsettings' crs
257-
mDistanceArea->setSourceCrs( mComposition->mapSettings().destinationCrs().srsid() );
258-
}
259-
if ( mComposition )
260-
{
261-
mDistanceArea->setEllipsoidalMode( mComposition->mapSettings().hasCrsTransformEnabled() );
262-
}
263-
mDistanceArea->setEllipsoid( QgsProject::instance()->readEntry( "Measure", "/Ellipsoid", GEO_NONE ) );
264-
contentChanged();
265-
266-
// Force label to redraw -- fixes label printing for labels with blend modes when used with atlas
267-
update();
268-
}
269-
270-
void QgsComposerLabel::setSubstitutions( const QMap<QString, QVariant>& substitutions )
271-
{
272-
mSubstitutions = substitutions;
273-
}
274-
275243
void QgsComposerLabel::refreshExpressionContext()
276244
{
277245
mExpressionLayer = nullptr;
@@ -307,7 +275,6 @@ QString QgsComposerLabel::displayText() const
307275
{
308276
QString displayText = mText;
309277
replaceDateText( displayText );
310-
QMap<QString, QVariant> subs = mSubstitutions;
311278

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

320-
return QgsExpression::replaceExpressionText( displayText, context.data(), &subs, mDistanceArea );
287+
return QgsExpression::replaceExpressionText( displayText, context.data(), mDistanceArea );
321288
}
322289

323290
void QgsComposerLabel::replaceDateText( QString& text ) const

‎src/core/composer/qgscomposerlabel.h

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -53,16 +53,6 @@ class CORE_EXPORT QgsComposerLabel: public QgsComposerItem
5353
/** Returns the text as it appears on screen (with replaced data field) */
5454
QString displayText() const;
5555

56-
/** Sets the current feature, the current layer and a list of local variable substitutions for evaluating expressions.
57-
* @deprecated use atlas features and setSubstitutions() instead
58-
*/
59-
Q_DECL_DEPRECATED void setExpressionContext( QgsFeature* feature, QgsVectorLayer* layer, const QMap<QString, QVariant>& substitutions = ( QMap<QString, QVariant>() ) );
60-
61-
/** Sets the list of local variable substitutions for evaluating expressions in label text.
62-
* @note added in QGIS 2.12
63-
*/
64-
void setSubstitutions( const QMap<QString, QVariant>& substitutions = ( QMap<QString, QVariant>() ) );
65-
6656
QFont font() const;
6757
void setFont( const QFont& f );
6858
/** Accessor for the vertical alignment of the label
@@ -213,7 +203,6 @@ class CORE_EXPORT QgsComposerLabel: public QgsComposerItem
213203

214204
QScopedPointer<QgsFeature> mExpressionFeature;
215205
QgsVectorLayer* mExpressionLayer;
216-
QMap<QString, QVariant> mSubstitutions;
217206
QgsDistanceArea* mDistanceArea;
218207

219208
QgsWebPage* mWebPage;

‎src/core/qgsactionmanager.cpp

Lines changed: 2 additions & 140 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ void QgsActionManager::doAction( int index, const QgsFeature& feat, int defaultV
7272
doAction( index, feat, context );
7373
}
7474

75-
void QgsActionManager::doAction( int index, const QgsFeature& feat, const QgsExpressionContext& context, const QMap<QString, QVariant> *substitutionMap )
75+
void QgsActionManager::doAction( int index, const QgsFeature& feat, const QgsExpressionContext& context )
7676
{
7777
if ( index < 0 || index >= size() )
7878
return;
@@ -87,27 +87,7 @@ void QgsActionManager::doAction( int index, const QgsFeature& feat, const QgsExp
8787
actionContext << QgsExpressionContextUtils::layerScope( mLayer );
8888
actionContext.setFeature( feat );
8989

90-
QString expandedAction = QgsExpression::replaceExpressionText( action.action(), &actionContext, substitutionMap );
91-
if ( expandedAction.isEmpty() )
92-
return;
93-
94-
QgsAction newAction( action.type(), action.name(), expandedAction, action.capture() );
95-
runAction( newAction );
96-
}
97-
98-
void QgsActionManager::doAction( int index, const QgsFeature &feat, const QMap<QString, QVariant> *substitutionMap )
99-
{
100-
if ( index < 0 || index >= size() )
101-
return;
102-
103-
const QgsAction &action = at( index );
104-
if ( !action.runable() )
105-
return;
106-
107-
// search for expressions while expanding actions
108-
QgsExpressionContext context = createExpressionContext();
109-
context.setFeature( feat );
110-
QString expandedAction = QgsExpression::replaceExpressionText( action.action(), &context, substitutionMap );
90+
QString expandedAction = QgsExpression::replaceExpressionText( action.action(), &actionContext );
11191
if ( expandedAction.isEmpty() )
11292
return;
11393

@@ -172,124 +152,6 @@ QgsExpressionContext QgsActionManager::createExpressionContext() const
172152
return context;
173153
}
174154

175-
QString QgsActionManager::expandAction( QString action, const QgsAttributeMap &attributes,
176-
uint clickedOnValue )
177-
{
178-
// This function currently replaces all %% characters in the action
179-
// with the value from values[clickedOnValue].second, and then
180-
// searches for all strings that go %attribute_name, where
181-
// attribute_name is found in values[x].first, and replaces any that
182-
// it finds by values[s].second.
183-
184-
// Additional substitutions could include symbols for $CWD, $HOME,
185-
// etc (and their OSX and Windows equivalents)
186-
187-
// This function will potentially fall apart if any of the
188-
// substitutions produce text that could match another
189-
// substitution. May be better to adopt a two pass approach - identify
190-
// all matches and their substitutions and then do a second pass
191-
// for the actual substitutions.
192-
193-
QString expanded_action;
194-
if ( attributes.contains( clickedOnValue ) )
195-
expanded_action = action.replace( "%%", attributes[clickedOnValue].toString() );
196-
else
197-
expanded_action = action;
198-
199-
const QgsFields &fields = mLayer->fields();
200-
201-
for ( int i = 0; i < 4; i++ )
202-
{
203-
for ( QgsAttributeMap::const_iterator it = attributes.begin(); it != attributes.end(); ++it )
204-
{
205-
int attrIdx = it.key();
206-
if ( attrIdx < 0 || attrIdx >= fields.count() )
207-
continue;
208-
209-
QString to_replace;
210-
switch ( i )
211-
{
212-
case 0:
213-
to_replace = "[%" + fields.at( attrIdx ).name() + ']';
214-
break;
215-
case 1:
216-
to_replace = "[%" + mLayer->attributeDisplayName( attrIdx ) + ']';
217-
break;
218-
case 2:
219-
to_replace = '%' + fields.at( attrIdx ).name();
220-
break;
221-
case 3:
222-
to_replace = '%' + mLayer->attributeDisplayName( attrIdx );
223-
break;
224-
}
225-
226-
expanded_action = expanded_action.replace( to_replace, it.value().toString() );
227-
}
228-
}
229-
230-
return expanded_action;
231-
}
232-
233-
QString QgsActionManager::expandAction( const QString& action, QgsFeature &feat, const QMap<QString, QVariant> *substitutionMap )
234-
{
235-
// This function currently replaces each expression between [% and %]
236-
// in the action with the result of its evaluation on the feature
237-
// passed as argument.
238-
239-
// Additional substitutions can be passed through the substitutionMap
240-
// parameter
241-
242-
QString expr_action;
243-
244-
int index = 0;
245-
while ( index < action.size() )
246-
{
247-
QRegExp rx = QRegExp( "\\[%([^\\]]+)%\\]" );
248-
249-
int pos = rx.indexIn( action, index );
250-
if ( pos < 0 )
251-
break;
252-
253-
int start = index;
254-
index = pos + rx.matchedLength();
255-
256-
QString to_replace = rx.cap( 1 ).trimmed();
257-
QgsDebugMsg( "Found expression: " + to_replace );
258-
259-
if ( substitutionMap && substitutionMap->contains( to_replace ) )
260-
{
261-
expr_action += action.mid( start, pos - start ) + substitutionMap->value( to_replace ).toString();
262-
continue;
263-
}
264-
265-
QgsExpression exp( to_replace );
266-
if ( exp.hasParserError() )
267-
{
268-
QgsDebugMsg( "Expression parser error: " + exp.parserErrorString() );
269-
expr_action += action.midRef( start, index - start );
270-
continue;
271-
}
272-
273-
QgsExpressionContext context = createExpressionContext();
274-
context.setFeature( feat );
275-
276-
QVariant result = exp.evaluate( &context );
277-
if ( exp.hasEvalError() )
278-
{
279-
QgsDebugMsg( "Expression parser eval error: " + exp.evalErrorString() );
280-
expr_action += action.midRef( start, index - start );
281-
continue;
282-
}
283-
284-
QgsDebugMsg( "Expression result is: " + result.toString() );
285-
expr_action += action.mid( start, pos - start ) + result.toString();
286-
}
287-
288-
expr_action += action.midRef( index );
289-
return expr_action;
290-
}
291-
292-
293155
bool QgsActionManager::writeXml( QDomNode& layer_node, QDomDocument& doc ) const
294156
{
295157
QDomElement aActions = doc.createElement( "attributeactions" );

‎src/core/qgsactionmanager.h

Lines changed: 1 addition & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -90,24 +90,10 @@ class CORE_EXPORT QgsActionManager
9090
* @param index action index
9191
* @param feature feature to run action for
9292
* @param context expression context to evalute expressions under
93-
* @param substitutionMap deprecated - kept for compatibility with projects, will be removed for 3.0
9493
*/
95-
// TODO QGIS 3.0 remove substition map - force use of expression variables
9694
void doAction( int index,
9795
const QgsFeature& feature,
98-
const QgsExpressionContext& context,
99-
const QMap<QString, QVariant> *substitutionMap = nullptr );
100-
101-
/** Does the action using the expression builder to expand it
102-
* and getting values from the passed feature attribute map.
103-
* substitutionMap is used to pass custom substitutions, to replace
104-
* each key in the map with the associated value
105-
* @note available in python bindings as doActionFeatureWithSubstitution
106-
* @deprecated use QgsExpressionContext variant instead
107-
*/
108-
Q_DECL_DEPRECATED void doAction( int index,
109-
const QgsFeature &feat,
110-
const QMap<QString, QVariant> *substitutionMap );
96+
const QgsExpressionContext& context );
11197

11298
//! Removes all actions
11399
void clearActions();
@@ -120,26 +106,6 @@ class CORE_EXPORT QgsActionManager
120106
//! Return the layer
121107
QgsVectorLayer* layer() const { return mLayer; }
122108

123-
/** Expands the given action, replacing all %'s with the value as
124-
* given.
125-
* @deprecated use QgsExpression::replaceExpressionText() instead
126-
*/
127-
Q_DECL_DEPRECATED QString expandAction( QString action, const QgsAttributeMap &attributes, uint defaultValueIndex );
128-
129-
/** Expands the given action using the expression builder
130-
* This function currently replaces each expression between [% and %]
131-
* placeholders in the action with the result of its evaluation on
132-
* the feature passed as argument.
133-
*
134-
* Additional substitutions can be passed through the substitutionMap
135-
* parameter
136-
* @deprecated use QgsExpression::replaceExpressionText() instead
137-
*/
138-
Q_DECL_DEPRECATED QString expandAction( const QString& action,
139-
QgsFeature &feat,
140-
const QMap<QString, QVariant> *substitutionMap = nullptr );
141-
142-
143109
//! Writes the actions out in XML format
144110
bool writeXml( QDomNode& layer_node, QDomDocument& doc ) const;
145111

‎src/core/qgsexpression.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3604,7 +3604,7 @@ void QgsExpression::acceptVisitor( QgsExpression::Visitor& v ) const
36043604
d->mRootNode->accept( v );
36053605
}
36063606

3607-
QString QgsExpression::replaceExpressionText( const QString &action, const QgsExpressionContext *context, const QMap<QString, QVariant> *substitutionMap, const QgsDistanceArea *distanceArea )
3607+
QString QgsExpression::replaceExpressionText( const QString &action, const QgsExpressionContext *context, const QgsDistanceArea *distanceArea )
36083608
{
36093609
QString expr_action;
36103610

‎src/core/qgsexpression.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -262,13 +262,11 @@ class CORE_EXPORT QgsExpression
262262
* Additional substitutions can be passed through the substitutionMap parameter
263263
* @param action
264264
* @param context expression context
265-
* @param substitutionMap
266265
* @param distanceArea optional QgsDistanceArea. If specified, the QgsDistanceArea is used for distance
267266
* and area conversion
268267
* @note added in QGIS 2.12
269268
*/
270269
static QString replaceExpressionText( const QString &action, const QgsExpressionContext* context,
271-
const QMap<QString, QVariant> *substitutionMap = nullptr,
272270
const QgsDistanceArea* distanceArea = nullptr );
273271

274272
/** Attempts to evaluate a text string as an expression to a resultant double

0 commit comments

Comments
 (0)
Please sign in to comment.