Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Cleanup composer label atlas handling
  • Loading branch information
nyalldawson committed Aug 12, 2015
1 parent 19a833c commit e9ff061
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 27 deletions.
47 changes: 32 additions & 15 deletions src/core/composer/qgscomposerlabel.cpp
Expand Up @@ -68,13 +68,9 @@ QgsComposerLabel::QgsComposerLabel( QgsComposition *composition )
//default to no background
setBackgroundEnabled( false );

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

if ( mComposition )
{
Expand Down Expand Up @@ -236,7 +232,7 @@ void QgsComposerLabel::setHtmlState( int state )

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

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

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

void QgsComposerLabel::refreshExpressionContext()
{
QgsVectorLayer * vl = 0;
QgsFeature feature;
mExpressionLayer = 0;
mExpressionFeature.reset();

if ( !mComposition )
return;

if ( mComposition->atlasComposition().enabled() )
{
vl = mComposition->atlasComposition().coverageLayer();
mExpressionLayer = mComposition->atlasComposition().coverageLayer();
if ( mComposition->atlasMode() != QgsComposition::AtlasOff )
{
mExpressionFeature.reset( new QgsFeature( mComposition->atlasComposition().feature() ) );
}
}

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

setExpressionContext( &feature, vl );
update();
}

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

void QgsComposerLabel::replaceDateText( QString& text ) const
Expand Down
13 changes: 10 additions & 3 deletions src/core/composer/qgscomposerlabel.h
Expand Up @@ -52,8 +52,15 @@ 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 */
void setExpressionContext( QgsFeature* feature, QgsVectorLayer* layer, QMap<QString, QVariant> substitutions = ( QMap<QString, QVariant>() ) );
/** 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, 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( QMap<QString, QVariant> substitutions = ( QMap<QString, QVariant>() ) );

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

QgsFeature mExpressionFeature;
QScopedPointer<QgsFeature> mExpressionFeature;
QgsVectorLayer* mExpressionLayer;
QMap<QString, QVariant> mSubstitutions;
QgsDistanceArea* mDistanceArea;
Expand Down
22 changes: 13 additions & 9 deletions tests/src/core/testqgscomposerlabel.cpp
Expand Up @@ -79,6 +79,7 @@ void TestQgsComposerLabel::initTestCase()
mMapSettings->setCrsTransformEnabled( false );
mComposition = new QgsComposition( *mMapSettings );
mComposition->setPaperSize( 297, 210 ); //A4 landscape
mComposition->atlasComposition().setCoverageLayer( mVectorLayer );

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

void TestQgsComposerLabel::init()
{
mComposerLabel = new QgsComposerLabel( mComposition );
mComposition->addComposerLabel( mComposerLabel );
}

void TestQgsComposerLabel::cleanup()
{
mComposition->removeItem( mComposerLabel );
delete mComposerLabel;
mComposerLabel = 0;
}

void TestQgsComposerLabel::evaluation()
Expand Down Expand Up @@ -140,22 +146,20 @@ void TestQgsComposerLabel::evaluation()

void TestQgsComposerLabel::feature_evaluation()
{
QgsFeatureIterator fit = mVectorLayer->getFeatures();
QgsFeature feat;

fit.nextFeature( feat );
mComposition->atlasComposition().setEnabled( true );
mComposition->setAtlasMode( QgsComposition::ExportAtlas );
mComposition->atlasComposition().updateFeatures();
mComposition->atlasComposition().prepareForFeature( 0 );
{
// evaluation with a feature
mComposerLabel->setExpressionContext( &feat, mVectorLayer );
mComposerLabel->setText( "[%\"NAME_1\"||'_ok'%]" );
QString evaluated = mComposerLabel->displayText();
QString expected = "Basse-Normandie_ok";
QCOMPARE( evaluated, expected );
}
fit.nextFeature( feat );
mComposition->atlasComposition().prepareForFeature( 1 );
{
// evaluation with a feature
mComposerLabel->setExpressionContext( &feat, mVectorLayer );
mComposerLabel->setText( "[%\"NAME_1\"||'_ok'%]" );
QString evaluated = mComposerLabel->displayText();
QString expected = "Bretagne_ok";
Expand All @@ -165,13 +169,13 @@ void TestQgsComposerLabel::feature_evaluation()
// evaluation with a feature and local variables
QMap<QString, QVariant> locals;
locals.insert( "$test", "OK" );

mComposerLabel->setExpressionContext( &feat, mVectorLayer, locals );
mComposerLabel->setSubstitutions( locals );
mComposerLabel->setText( "[%\"NAME_1\"||$test%]" );
QString evaluated = mComposerLabel->displayText();
QString expected = "BretagneOK";
QCOMPARE( evaluated, expected );
}
mComposition->atlasComposition().setEnabled( false );
}

void TestQgsComposerLabel::page_evaluation()
Expand Down

0 comments on commit e9ff061

Please sign in to comment.