Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Add test for terrain theme + do not use project singleton
  • Loading branch information
wonder-sk committed Nov 21, 2018
1 parent 61cb715 commit afd3525
Show file tree
Hide file tree
Showing 7 changed files with 110 additions and 2 deletions.
3 changes: 3 additions & 0 deletions src/3d/qgs3dmapsettings.cpp
Expand Up @@ -49,6 +49,9 @@ Qgs3DMapSettings::Qgs3DMapSettings( const Qgs3DMapSettings &other )
, mSkyboxEnabled( other.mSkyboxEnabled )
, mSkyboxFileBase( other.mSkyboxFileBase )
, mSkyboxFileExtension( other.mSkyboxFileExtension )
, mTransformContext( other.mTransformContext )
, mPathResolver( other.mPathResolver )
, mMapThemes( other.mMapThemes )
{
Q_FOREACH ( QgsAbstract3DRenderer *renderer, other.mRenderers )
{
Expand Down
17 changes: 17 additions & 0 deletions src/3d/qgs3dmapsettings.h
Expand Up @@ -126,6 +126,21 @@ class _3D_EXPORT Qgs3DMapSettings : public QObject
*/
void setPathResolver( const QgsPathResolver &resolver ) { mPathResolver = resolver; }

/**
* Returns pointer to the collection of map themes. Normally this would be QgsProject::mapThemeCollection()
* of the currently used project. Without a valid map theme collection object it is not possible
* to resolve map themes from their names.
* \since QGIS 3.6
*/
QgsMapThemeCollection *mapThemeCollection() const { return mMapThemes; }

/**
* Sets pointer to the collection of map themes.
* \see mapThemeCollection()
* \since QGIS 3.6
*/
void setMapThemeCollection( QgsMapThemeCollection *mapThemes ) { mMapThemes = mapThemes; }

//! Sets background color of the 3D map view
void setBackgroundColor( const QColor &color );
//! Returns background color of the 3D map view
Expand Down Expand Up @@ -248,6 +263,7 @@ class _3D_EXPORT Qgs3DMapSettings : public QObject
/**
* Returns name of the map theme (from the active project) that will be used for terrain's texture.
* Empty map theme name means that the map theme is not overridden and the current map theme will be used.
* \note Support for map themes only works if mapThemeCollection() is a valid object (otherwise it is not possible to resolve map themes from names)
* \since QGIS 3.6
*/
QString terrainMapTheme() const { return mTerrainMapTheme; }
Expand Down Expand Up @@ -387,6 +403,7 @@ class _3D_EXPORT Qgs3DMapSettings : public QObject
//! Coordinate transform context
QgsCoordinateTransformContext mTransformContext;
QgsPathResolver mPathResolver;
QgsMapThemeCollection *mMapThemes = nullptr; //!< Pointer to map themes (e.g. from the current project) to resolve map theme content from the name
};


Expand Down
6 changes: 6 additions & 0 deletions src/3d/qgslayoutitem3dmap.cpp
Expand Up @@ -217,7 +217,13 @@ bool QgsLayoutItem3DMap::readPropertiesFromElement( const QDomElement &element,
mSettings.reset( new Qgs3DMapSettings );
mSettings->readXml( elemSettings, context );
if ( mLayout->project() )
{
mSettings->resolveReferences( *mLayout->project() );

mSettings->setTransformContext( mLayout->project()->transformContext() );
mSettings->setPathResolver( mLayout->project()->pathResolver() );
mSettings->setMapThemeCollection( mLayout->project()->mapThemeCollection() );
}
}

QDomElement elemCameraPose = element.firstChildElement( QStringLiteral( "camera-pose" ) );
Expand Down
4 changes: 2 additions & 2 deletions src/3d/terrain/qgsterraintexturegenerator_p.cpp
Expand Up @@ -136,9 +136,9 @@ QgsMapSettings QgsTerrainTextureGenerator::baseMapSettings()
mapSettings.setTransformContext( mMap.transformContext() );
mapSettings.setPathResolver( mMap.pathResolver() );

QgsMapThemeCollection *mapThemes = QgsProject::instance()->mapThemeCollection();
QgsMapThemeCollection *mapThemes = mMap.mapThemeCollection();
QString mapThemeName = mMap.terrainMapTheme();
if ( mapThemeName.isEmpty() || !mapThemes->hasMapTheme( mapThemeName ) )
if ( mapThemeName.isEmpty() || !mapThemes || !mapThemes->hasMapTheme( mapThemeName ) )
{
mapSettings.setLayers( mMap.layers() );
}
Expand Down
10 changes: 10 additions & 0 deletions src/app/qgisapp.cpp
Expand Up @@ -11128,8 +11128,10 @@ void QgisApp::new3DMapCanvas()
map->setSelectionColor( mMapCanvas->selectionColor() );
map->setBackgroundColor( mMapCanvas->canvasColor() );
map->setLayers( mMapCanvas->layers() );

map->setTransformContext( QgsProject::instance()->transformContext() );
map->setPathResolver( QgsProject::instance()->pathResolver() );
map->setMapThemeCollection( QgsProject::instance()->mapThemeCollection() );
connect( QgsProject::instance(), &QgsProject::transformContextChanged, map, [map]
{
map->setTransformContext( QgsProject::instance()->transformContext() );
Expand Down Expand Up @@ -13544,6 +13546,14 @@ void QgisApp::readProject( const QDomDocument &doc )
map->readXml( elem3D, readWriteContext );
map->resolveReferences( *QgsProject::instance() );

map->setTransformContext( QgsProject::instance()->transformContext() );
map->setPathResolver( QgsProject::instance()->pathResolver() );
map->setMapThemeCollection( QgsProject::instance()->mapThemeCollection() );
connect( QgsProject::instance(), &QgsProject::transformContextChanged, map, [map]
{
map->setTransformContext( QgsProject::instance()->transformContext() );
} );

// these things are not saved in project
map->setSelectionColor( mMapCanvas->selectionColor() );
map->setBackgroundColor( mMapCanvas->canvasColor() );
Expand Down
72 changes: 72 additions & 0 deletions tests/src/3d/testqgs3drendering.cpp
Expand Up @@ -16,8 +16,12 @@
#include "qgstest.h"
#include "qgsrenderchecker.h"

#include "qgsmaplayerstylemanager.h"
#include "qgsmapthemecollection.h"
#include "qgsproject.h"
#include "qgsrasterlayer.h"
#include "qgsrastershader.h"
#include "qgssinglebandpseudocolorrenderer.h"
#include "qgsvectorlayer.h"

#include "qgs3dmapscene.h"
Expand All @@ -43,6 +47,7 @@ class TestQgs3DRendering : public QObject
void testFlatTerrain();
void testDemTerrain();
void testExtrudedPolygons();
void testMapTheme();

private:
bool renderCheck( const QString &testName, QImage &image, int mismatchCount = 0 );
Expand Down Expand Up @@ -88,6 +93,39 @@ void TestQgs3DRendering::initTestCase()
mLayerBuildings->setRenderer3D( renderer3d );

mProject->setCrs( mLayerDtm->crs() );

//
// prepare styles for DTM layer
//

mLayerDtm->styleManager()->addStyleFromLayer( "grayscale" );

double vMin = 44, vMax = 198;
QColor cMin = Qt::red, cMax = Qt::yellow;

// change renderer for the new style
std::unique_ptr<QgsColorRampShader> colorRampShader( new QgsColorRampShader( vMin, vMax ) );
colorRampShader->setColorRampItemList( QList<QgsColorRampShader::ColorRampItem>()
<< QgsColorRampShader::ColorRampItem( vMin, cMin )
<< QgsColorRampShader::ColorRampItem( vMax, cMax ) );
std::unique_ptr<QgsRasterShader> shader( new QgsRasterShader( vMin, vMax ) );
shader->setRasterShaderFunction( colorRampShader.release() );
QgsSingleBandPseudoColorRenderer *r = new QgsSingleBandPseudoColorRenderer( mLayerDtm->renderer()->input(), 1, shader.release() );
mLayerDtm->setRenderer( r );
mLayerDtm->styleManager()->addStyleFromLayer( "my_style" );

mLayerDtm->styleManager()->setCurrentStyle( "grayscale" );

//
// add map theme
//

QgsMapThemeCollection::MapThemeLayerRecord layerRecord( mLayerDtm );
layerRecord.usingCurrentStyle = true;
layerRecord.currentStyle = "my_style";
QgsMapThemeCollection::MapThemeRecord record;
record.addLayerRecord( layerRecord );
mProject->mapThemeCollection()->insert( "theme_dtm", record );
}

//runs after all tests
Expand Down Expand Up @@ -204,6 +242,40 @@ void TestQgs3DRendering::testExtrudedPolygons()
QVERIFY( renderCheck( "polygon3d_extrusion", img, 40 ) );
}

void TestQgs3DRendering::testMapTheme()
{
QgsRectangle fullExtent = mLayerDtm->extent();

Qgs3DMapSettings *map = new Qgs3DMapSettings;
map->setCrs( mProject->crs() );
map->setOrigin( QgsVector3D( fullExtent.center().x(), fullExtent.center().y(), 0 ) );
map->setLayers( QList<QgsMapLayer *>() << mLayerRgb );

// set theme - this should override what we set in setLayers()
map->setMapThemeCollection( mProject->mapThemeCollection() );
map->setTerrainMapTheme( "theme_dtm" );

QgsFlatTerrainGenerator *flatTerrain = new QgsFlatTerrainGenerator;
flatTerrain->setCrs( map->crs() );
flatTerrain->setExtent( fullExtent );
map->setTerrainGenerator( flatTerrain );

QgsOffscreen3DEngine engine;
Qgs3DMapScene *scene = new Qgs3DMapScene( *map, &engine );
engine.setRootEntity( scene );

// look from the top
scene->cameraController()->setLookingAtPoint( QgsVector3D( 0, 0, 0 ), 2500, 0, 0 );

// When running the test on Travis, it would initially return empty rendered image.
// Capturing the initial image and throwing it away fixes that. Hopefully we will
// find a better fix in the future.
Qgs3DUtils::captureSceneImage( engine, scene );

QImage img = Qgs3DUtils::captureSceneImage( engine, scene );
QVERIFY( renderCheck( "terrain_theme", img, 40 ) );
}


bool TestQgs3DRendering::renderCheck( const QString &testName, QImage &image, int mismatchCount )
{
Expand Down
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit afd3525

Please sign in to comment.