Skip to content

Commit e9ff061

Browse files
committedAug 12, 2015
Cleanup composer label atlas handling
1 parent 19a833c commit e9ff061

File tree

3 files changed

+55
-27
lines changed

3 files changed

+55
-27
lines changed
 

‎src/core/composer/qgscomposerlabel.cpp

Lines changed: 32 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -68,13 +68,9 @@ QgsComposerLabel::QgsComposerLabel( QgsComposition *composition )
6868
//default to no background
6969
setBackgroundEnabled( false );
7070

71-
if ( mComposition && mComposition->atlasMode() == QgsComposition::PreviewAtlas )
72-
{
73-
//a label added while atlas preview is enabled needs to have the expression context set,
74-
//otherwise fields in the label aren't correctly evaluated until atlas preview feature changes (#9457)
75-
QgsFeature atlasFeature = mComposition->atlasComposition().feature();
76-
setExpressionContext( &atlasFeature, mComposition->atlasComposition().coverageLayer() );
77-
}
71+
//a label added while atlas preview is enabled needs to have the expression context set,
72+
//otherwise fields in the label aren't correctly evaluated until atlas preview feature changes (#9457)
73+
refreshExpressionContext();
7874

7975
if ( mComposition )
8076
{
@@ -236,7 +232,7 @@ void QgsComposerLabel::setHtmlState( int state )
236232

237233
void QgsComposerLabel::setExpressionContext( QgsFeature *feature, QgsVectorLayer* layer, QMap<QString, QVariant> substitutions )
238234
{
239-
mExpressionFeature = feature ? *feature : QgsFeature();
235+
mExpressionFeature.reset( feature ? new QgsFeature( *feature ) : 0 );
240236
mExpressionLayer = layer;
241237
mSubstitutions = substitutions;
242238

@@ -260,21 +256,42 @@ void QgsComposerLabel::setExpressionContext( QgsFeature *feature, QgsVectorLayer
260256
update();
261257
}
262258

259+
void QgsComposerLabel::setSubstitutions( QMap<QString, QVariant> substitutions )
260+
{
261+
mSubstitutions = substitutions;
262+
}
263+
263264
void QgsComposerLabel::refreshExpressionContext()
264265
{
265-
QgsVectorLayer * vl = 0;
266-
QgsFeature feature;
266+
mExpressionLayer = 0;
267+
mExpressionFeature.reset();
268+
269+
if ( !mComposition )
270+
return;
267271

268272
if ( mComposition->atlasComposition().enabled() )
269273
{
270-
vl = mComposition->atlasComposition().coverageLayer();
274+
mExpressionLayer = mComposition->atlasComposition().coverageLayer();
275+
if ( mComposition->atlasMode() != QgsComposition::AtlasOff )
276+
{
277+
mExpressionFeature.reset( new QgsFeature( mComposition->atlasComposition().feature() ) );
278+
}
279+
}
280+
281+
//setup distance area conversion
282+
if ( mExpressionLayer )
283+
{
284+
mDistanceArea->setSourceCrs( mExpressionLayer->crs().srsid() );
271285
}
272-
if ( mComposition->atlasMode() != QgsComposition::AtlasOff )
286+
else
273287
{
274-
feature = mComposition->atlasComposition().feature();
288+
//set to composition's mapsettings' crs
289+
mDistanceArea->setSourceCrs( mComposition->mapSettings().destinationCrs().srsid() );
275290
}
291+
mDistanceArea->setEllipsoidalMode( mComposition->mapSettings().hasCrsTransformEnabled() );
292+
mDistanceArea->setEllipsoid( QgsProject::instance()->readEntry( "Measure", "/Ellipsoid", GEO_NONE ) );
276293

277-
setExpressionContext( &feature, vl );
294+
update();
278295
}
279296

280297
QString QgsComposerLabel::displayText() const
@@ -283,7 +300,7 @@ QString QgsComposerLabel::displayText() const
283300
replaceDateText( displayText );
284301
QMap<QString, QVariant> subs = mSubstitutions;
285302
subs[ "$page" ] = QVariant(( int )mComposition->itemPageNumber( this ) + 1 );
286-
return QgsExpression::replaceExpressionText( displayText, &mExpressionFeature, mExpressionLayer, &subs, mDistanceArea );
303+
return QgsExpression::replaceExpressionText( displayText, mExpressionFeature.data(), mExpressionLayer, &subs, mDistanceArea );
287304
}
288305

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

‎src/core/composer/qgscomposerlabel.h

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,15 @@ class CORE_EXPORT QgsComposerLabel: public QgsComposerItem
5252
/** Returns the text as it appears on screen (with replaced data field) */
5353
QString displayText() const;
5454

55-
/** Sets the current feature, the current layer and a list of local variable substitutions for evaluating expressions */
56-
void setExpressionContext( QgsFeature* feature, QgsVectorLayer* layer, QMap<QString, QVariant> substitutions = ( QMap<QString, QVariant>() ) );
55+
/** Sets the current feature, the current layer and a list of local variable substitutions for evaluating expressions.
56+
* @deprecated use atlas features and setSubstitutions() instead
57+
*/
58+
Q_DECL_DEPRECATED void setExpressionContext( QgsFeature* feature, QgsVectorLayer* layer, QMap<QString, QVariant> substitutions = ( QMap<QString, QVariant>() ) );
59+
60+
/** Sets the list of local variable substitutions for evaluating expressions in label text.
61+
* @note added in QGIS 2.12
62+
*/
63+
void setSubstitutions( QMap<QString, QVariant> substitutions = ( QMap<QString, QVariant>() ) );
5764

5865
QFont font() const;
5966
void setFont( const QFont& f );
@@ -197,7 +204,7 @@ class CORE_EXPORT QgsComposerLabel: public QgsComposerItem
197204
/** Replaces replace '$CURRENT_DATE<(FORMAT)>' with the current date (e.g. $CURRENT_DATE(d 'June' yyyy)*/
198205
void replaceDateText( QString& text ) const;
199206

200-
QgsFeature mExpressionFeature;
207+
QScopedPointer<QgsFeature> mExpressionFeature;
201208
QgsVectorLayer* mExpressionLayer;
202209
QMap<QString, QVariant> mSubstitutions;
203210
QgsDistanceArea* mDistanceArea;

‎tests/src/core/testqgscomposerlabel.cpp

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ void TestQgsComposerLabel::initTestCase()
7979
mMapSettings->setCrsTransformEnabled( false );
8080
mComposition = new QgsComposition( *mMapSettings );
8181
mComposition->setPaperSize( 297, 210 ); //A4 landscape
82+
mComposition->atlasComposition().setCoverageLayer( mVectorLayer );
8283

8384
mComposerLabel = new QgsComposerLabel( mComposition );
8485
mComposition->addComposerLabel( mComposerLabel );
@@ -96,10 +97,15 @@ void TestQgsComposerLabel::cleanupTestCase()
9697

9798
void TestQgsComposerLabel::init()
9899
{
100+
mComposerLabel = new QgsComposerLabel( mComposition );
101+
mComposition->addComposerLabel( mComposerLabel );
99102
}
100103

101104
void TestQgsComposerLabel::cleanup()
102105
{
106+
mComposition->removeItem( mComposerLabel );
107+
delete mComposerLabel;
108+
mComposerLabel = 0;
103109
}
104110

105111
void TestQgsComposerLabel::evaluation()
@@ -140,22 +146,20 @@ void TestQgsComposerLabel::evaluation()
140146

141147
void TestQgsComposerLabel::feature_evaluation()
142148
{
143-
QgsFeatureIterator fit = mVectorLayer->getFeatures();
144-
QgsFeature feat;
145-
146-
fit.nextFeature( feat );
149+
mComposition->atlasComposition().setEnabled( true );
150+
mComposition->setAtlasMode( QgsComposition::ExportAtlas );
151+
mComposition->atlasComposition().updateFeatures();
152+
mComposition->atlasComposition().prepareForFeature( 0 );
147153
{
148154
// evaluation with a feature
149-
mComposerLabel->setExpressionContext( &feat, mVectorLayer );
150155
mComposerLabel->setText( "[%\"NAME_1\"||'_ok'%]" );
151156
QString evaluated = mComposerLabel->displayText();
152157
QString expected = "Basse-Normandie_ok";
153158
QCOMPARE( evaluated, expected );
154159
}
155-
fit.nextFeature( feat );
160+
mComposition->atlasComposition().prepareForFeature( 1 );
156161
{
157162
// evaluation with a feature
158-
mComposerLabel->setExpressionContext( &feat, mVectorLayer );
159163
mComposerLabel->setText( "[%\"NAME_1\"||'_ok'%]" );
160164
QString evaluated = mComposerLabel->displayText();
161165
QString expected = "Bretagne_ok";
@@ -165,13 +169,13 @@ void TestQgsComposerLabel::feature_evaluation()
165169
// evaluation with a feature and local variables
166170
QMap<QString, QVariant> locals;
167171
locals.insert( "$test", "OK" );
168-
169-
mComposerLabel->setExpressionContext( &feat, mVectorLayer, locals );
172+
mComposerLabel->setSubstitutions( locals );
170173
mComposerLabel->setText( "[%\"NAME_1\"||$test%]" );
171174
QString evaluated = mComposerLabel->displayText();
172175
QString expected = "BretagneOK";
173176
QCOMPARE( evaluated, expected );
174177
}
178+
mComposition->atlasComposition().setEnabled( false );
175179
}
176180

177181
void TestQgsComposerLabel::page_evaluation()

0 commit comments

Comments
 (0)
Please sign in to comment.