Skip to content
Permalink
Browse files

[composer] Fix data defined map style/layer set, add test

(fallout from expression contexts)
  • Loading branch information
nyalldawson committed Sep 15, 2015
1 parent 481d2c0 commit 59c43f194a963532b2c6316b2cc7cc7d17c25cdb
Showing with 47 additions and 10 deletions.
  1. +18 −8 src/core/composer/qgscomposermap.cpp
  2. +2 −2 src/core/composer/qgscomposermap.h
  3. +27 −0 tests/src/core/testqgscomposermap.cpp
@@ -198,6 +198,8 @@ QgsMapSettings QgsComposerMap::mapSettings( const QgsRectangle& extent, const QS
{
const QgsMapSettings &ms = mComposition->mapSettings();

QScopedPointer< QgsExpressionContext > expressionContext( createExpressionContext() );

QgsMapSettings jobMapSettings;
jobMapSettings.setExtent( extent );
jobMapSettings.setOutputSize( size.toSize() );
@@ -208,7 +210,7 @@ QgsMapSettings QgsComposerMap::mapSettings( const QgsRectangle& extent, const QS
jobMapSettings.setRotation( mEvaluatedMapRotation );

//set layers to render
QStringList theLayerSet = layersToRender();
QStringList theLayerSet = layersToRender( expressionContext.data() );
if ( -1 != mCurrentExportLayer )
{
//exporting with separate layers (eg, to svg layers), so we only want to render a single map layer
@@ -219,7 +221,7 @@ QgsMapSettings QgsComposerMap::mapSettings( const QgsRectangle& extent, const QS
: QStringList(); //exporting decorations such as map frame/grid/overview, so no map layers required
}
jobMapSettings.setLayers( theLayerSet );
jobMapSettings.setLayerStyleOverrides( layerStyleOverridesToRender() );
jobMapSettings.setLayerStyleOverrides( layerStyleOverridesToRender( *expressionContext ) );
jobMapSettings.setDestinationCrs( ms.destinationCrs() );
jobMapSettings.setCrsTransformEnabled( ms.hasCrsTransformEnabled() );
jobMapSettings.setFlags( ms.flags() );
@@ -523,12 +525,20 @@ const QgsMapRenderer *QgsComposerMap::mapRenderer() const
Q_NOWARN_DEPRECATED_POP
}

QStringList QgsComposerMap::layersToRender() const
QStringList QgsComposerMap::layersToRender( const QgsExpressionContext* context ) const
{
const QgsExpressionContext* evalContext = context;
QScopedPointer< QgsExpressionContext > scopedContext;
if ( !evalContext )
{
scopedContext.reset( createExpressionContext() );
evalContext = scopedContext.data();
}

QStringList renderLayerSet;

QVariant exprVal;
if ( dataDefinedEvaluate( QgsComposerObject::MapStylePreset, exprVal ) )
if ( dataDefinedEvaluate( QgsComposerObject::MapStylePreset, exprVal, *evalContext ) )
{
QString presetName = exprVal.toString();

@@ -549,7 +559,7 @@ QStringList QgsComposerMap::layersToRender() const
}
}

if ( dataDefinedEvaluate( QgsComposerObject::MapLayers, exprVal ) )
if ( dataDefinedEvaluate( QgsComposerObject::MapLayers, exprVal, *evalContext ) )
{
renderLayerSet.clear();

@@ -583,10 +593,10 @@ QStringList QgsComposerMap::layersToRender() const
return renderLayerSet;
}

QMap<QString, QString> QgsComposerMap::layerStyleOverridesToRender() const
QMap<QString, QString> QgsComposerMap::layerStyleOverridesToRender( const QgsExpressionContext& context ) const
{
QVariant exprVal;
if ( dataDefinedEvaluate( QgsComposerObject::MapStylePreset, exprVal ) )
if ( dataDefinedEvaluate( QgsComposerObject::MapStylePreset, exprVal, context ) )
{
QString presetName = exprVal.toString();

@@ -2175,7 +2185,7 @@ void QgsComposerMap::refreshDataDefinedProperty( const QgsComposerObject::DataDe
property == QgsComposerObject::AllProperties )
{
QgsRectangle beforeExtent = *currentMapExtent();
refreshMapExtents();
refreshMapExtents( evalContext );
emit itemChanged();
if ( *currentMapExtent() != beforeExtent )
{
@@ -921,10 +921,10 @@ class CORE_EXPORT QgsComposerMap : public QgsComposerItem
void updateToolTip();

/** Returns a list of the layers to render for this map item*/
QStringList layersToRender() const;
QStringList layersToRender( const QgsExpressionContext* context = 0 ) const;

/** Returns current layer style overrides for this map item*/
QMap<QString, QString> layerStyleOverridesToRender() const;
QMap<QString, QString> layerStyleOverridesToRender( const QgsExpressionContext& context ) const;

/** Returns extent that considers mOffsetX / mOffsetY (during content move)*/
QgsRectangle transformedExtent() const;
@@ -24,6 +24,7 @@
#include "qgsmultibandcolorrenderer.h"
#include "qgsrasterlayer.h"
#include "qgsvectorlayer.h"
#include "qgsvectordataprovider.h"
#include "qgsproject.h"
#include "qgsvisibilitypresetcollection.h"
#include <QObject>
@@ -288,6 +289,32 @@ void TestQgsComposerMap::dataDefinedLayers()
result = mComposerMap->layersToRender();
QVERIFY( result.isEmpty() );


//test with atlas feature evaluation
QgsVectorLayer* atlasLayer = new QgsVectorLayer( "Point?field=col1:string", "atlas", "memory" );
QVERIFY( atlasLayer->isValid() );
QgsFeature f1( atlasLayer->dataProvider()->fields(), 1 );
f1.setAttribute( "col1", mLinesLayer->name() );
QgsFeature f2( atlasLayer->dataProvider()->fields(), 1 );
f2.setAttribute( "col1", mPointsLayer->name() );
atlasLayer->dataProvider()->addFeatures( QgsFeatureList() << f1 << f2 );
mComposition->atlasComposition().setCoverageLayer( atlasLayer );
mComposition->atlasComposition().setEnabled( true );
mComposition->setAtlasMode( QgsComposition::ExportAtlas );
mComposition->atlasComposition().beginRender();
mComposition->atlasComposition().prepareForFeature( 0 );

mComposerMap->setDataDefinedProperty( QgsComposerObject::MapLayers, true, true, QString( "\"col1\"" ), QString() );
result = mComposerMap->layersToRender();
QCOMPARE( result.count(), 1 );
QCOMPARE( result.at( 0 ), mLinesLayer->id() );
mComposition->atlasComposition().prepareForFeature( 1 );
result = mComposerMap->layersToRender();
QCOMPARE( result.count(), 1 );
QCOMPARE( result.at( 0 ), mPointsLayer->id() );
mComposition->atlasComposition().setEnabled( false );
delete atlasLayer;

//render test
mComposerMap->setDataDefinedProperty( QgsComposerObject::MapLayers, true, true,
QString( "'%1|%2'" ).arg( mPolysLayer->name() ).arg( mPointsLayer->name() ), QString() );

0 comments on commit 59c43f1

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