Skip to content
Permalink
Browse files

Cleanup composer label atlas handling

  • Loading branch information
nyalldawson committed Aug 12, 2015
1 parent 19a833c commit e9ff061f05fc1b02377243fcac3a2efd51caa37b
@@ -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 )
{
@@ -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;

@@ -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
@@ -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
@@ -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 );
@@ -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;
@@ -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 );
@@ -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()
@@ -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";
@@ -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()

0 comments on commit e9ff061

Please sign in to comment.
You can’t perform that action at this time.