Skip to content
Permalink
Browse files

Fix missing map settings expression variables in composer maps

Missing map extent related variables were causing stacking order
issues with the 25d renderer (fix #14604)
  • Loading branch information
nyalldawson committed Apr 11, 2016
1 parent 1ed39c4 commit ce2891b10e7c5634c900838e6f9e3b9364e699d0
@@ -2151,6 +2151,14 @@ QgsExpressionContext* QgsComposerMap::createExpressionContext() const
scope->addVariable( QgsExpressionContextScope::StaticVariable( "map_id", QgsComposerItem::id(), true ) );
scope->addVariable( QgsExpressionContextScope::StaticVariable( "map_rotation", mMapRotation, true ) );
scope->addVariable( QgsExpressionContextScope::StaticVariable( "map_scale", scale(), true ) );

QgsRectangle extent( *currentMapExtent() );
scope->addVariable( QgsExpressionContextScope::StaticVariable( "map_extent_width", extent.width(), true ) );
scope->addVariable( QgsExpressionContextScope::StaticVariable( "map_extent_height", extent.height(), true ) );
QgsGeometry* centerPoint = QgsGeometry::fromPoint( extent.center() );
scope->addVariable( QgsExpressionContextScope::StaticVariable( "map_extent_center", QVariant::fromValue( *centerPoint ), true ) );
delete centerPoint;

context->appendScope( scope );

return context;
@@ -713,6 +713,9 @@ void QgsExpressionContextUtils::setLayerVariables( QgsMapLayer* layer, const Qgs

QgsExpressionContextScope* QgsExpressionContextUtils::mapSettingsScope( const QgsMapSettings& mapSettings )
{
// IMPORTANT: ANY CHANGES HERE ALSO NEED TO BE MADE TO QgsComposerMap::createExpressionContext()
// (rationale is described in QgsComposerMap::createExpressionContext() )

QgsExpressionContextScope* scope = new QgsExpressionContextScope( QObject::tr( "Map Settings" ) );

//add known map settings context variables
@@ -84,6 +84,7 @@ ENDMACRO (ADD_QGIS_TEST)
#############################################################
# Tests:

ADD_QGIS_TEST(25drenderertest testqgs25drenderer.cpp)
ADD_QGIS_TEST(applicationtest testqgsapplication.cpp)
ADD_QGIS_TEST(atlascompositiontest testqgsatlascomposition.cpp)
ADD_QGIS_TEST(authcryptotest testqgsauthcrypto.cpp)
@@ -0,0 +1,163 @@
/***************************************************************************
testqgs25drenderer.cpp
--------------------------------------
Date : April 2016
Copyright : (C) 2016 by Nyall Dawson
Email : nyall dot dawson at gmail dot com
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#include <QtTest/QtTest>
#include <QObject>
#include <QString>
#include <QStringList>
#include <QApplication>
#include <QFileInfo>
#include <QDir>
#include <QDesktopServices>

//qgis includes...
#include <qgsmapsettings.h>
#include <qgsmaplayer.h>
#include <qgsvectorlayer.h>
#include <qgsapplication.h>
#include <qgsproviderregistry.h>
#include <qgsmaplayerregistry.h>
#include <qgssymbolv2.h>
#include <qgs25drenderer.h>
#include "qgscomposition.h"
#include "qgscomposermap.h"
#include "qgsmultirenderchecker.h"

/** \ingroup UnitTests
* This is a unit test for 25d renderer.
*/
class TestQgs25DRenderer : public QObject
{
Q_OBJECT
public:
TestQgs25DRenderer()
: mpPolysLayer( nullptr )
{}

private slots:
void initTestCase();// will be called before the first testfunction is executed.
void cleanupTestCase();// will be called after the last testfunction was executed.
void init() {} // will be called before each testfunction is executed.
void cleanup() {} // will be called after every testfunction.

void render();
void renderComposition();

private:
bool imageCheck( const QString& theType );
QgsMapSettings mMapSettings;
QgsVectorLayer * mpPolysLayer;
QString mTestDataDir;
QString mReport;
};


void TestQgs25DRenderer::initTestCase()
{
// init QGIS's paths - true means that all path will be inited from prefix
QgsApplication::init();
QgsApplication::initQgis();
QgsApplication::showSettings();

//create some objects that will be used in all tests...
QString myDataDir( TEST_DATA_DIR ); //defined in CmakeLists.txt
mTestDataDir = myDataDir + '/';

//
//create a poly layer that will be used in all tests...
//
QString myPolysFileName = mTestDataDir + "polys.shp";
QFileInfo myPolyFileInfo( myPolysFileName );
mpPolysLayer = new QgsVectorLayer( myPolyFileInfo.filePath(),
myPolyFileInfo.completeBaseName(), "ogr" );

QgsVectorSimplifyMethod simplifyMethod;
simplifyMethod.setSimplifyHints( QgsVectorSimplifyMethod::NoSimplification );
mpPolysLayer->setSimplifyMethod( simplifyMethod );

//need a very high height to check for stacking
QgsExpressionContextUtils::setLayerVariable( mpPolysLayer, "qgis_25d_height", 8 );
QgsExpressionContextUtils::setLayerVariable( mpPolysLayer, "qgis_25d_angle", 45 );

// Register the layer with the registry
QgsMapLayerRegistry::instance()->addMapLayers(
QList<QgsMapLayer *>() << mpPolysLayer );

mMapSettings.setLayers( QStringList() << mpPolysLayer->id() );
mReport += "<h1>25D Renderer Tests</h1>\n";

}
void TestQgs25DRenderer::cleanupTestCase()
{
QString myReportFile = QDir::tempPath() + "/qgistest.html";
QFile myFile( myReportFile );
if ( myFile.open( QIODevice::WriteOnly | QIODevice::Append ) )
{
QTextStream myQTextStream( &myFile );
myQTextStream << mReport;
myFile.close();
}

QgsApplication::exitQgis();
}

void TestQgs25DRenderer::render()
{
mReport += "<h2>Render</h2>\n";

//setup 25d renderer
Qgs25DRenderer* renderer = new Qgs25DRenderer( );
renderer->setShadowEnabled( false );
renderer->setWallShadingEnabled( false );
mpPolysLayer->setRendererV2( renderer );

QVERIFY( imageCheck( "25d_render" ) );
}

void TestQgs25DRenderer::renderComposition()
{
QgsComposition* composition = new QgsComposition( mMapSettings );
composition->setPaperSize( 297, 210 ); //A4 landscape
QgsComposerMap* map = new QgsComposerMap( composition, 20, 20, 200, 100 );
map->setFrameEnabled( true );
composition->addComposerMap( map );

map->setNewExtent( mpPolysLayer->extent() );
QgsCompositionChecker checker( "25d_composer", composition );
checker.setControlPathPrefix( "25d_renderer" );

QVERIFY( checker.testComposition( mReport, 0, 100 ) );
}

bool TestQgs25DRenderer::imageCheck( const QString& theTestType )
{
//use the QgsRenderChecker test utility class to
//ensure the rendered output matches our control image
mMapSettings.setExtent( mpPolysLayer->extent() );
mMapSettings.setOutputDpi( 96 );
QgsExpressionContext context;
context << QgsExpressionContextUtils::mapSettingsScope( mMapSettings );
mMapSettings.setExpressionContext( context );
QgsMultiRenderChecker myChecker;
myChecker.setControlPathPrefix( "25d_renderer" );
myChecker.setControlName( "expected_" + theTestType );
myChecker.setMapSettings( mMapSettings );
myChecker.setColorTolerance( 20 );
bool myResultFlag = myChecker.runTest( theTestType, 500 );
mReport += myChecker.report();
return myResultFlag;
}

QTEST_MAIN( TestQgs25DRenderer )
#include "testqgs25drenderer.moc"
Binary file not shown.
Binary file not shown.

1 comment on commit ce2891b

@m-kuhn

This comment has been minimized.

Copy link
Member

@m-kuhn m-kuhn commented on ce2891b Apr 12, 2016

Thanks @nyalldawson 👍

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