Skip to content

Commit

Permalink
Merge pull request #8509 from wonder-sk/terrain-map-themes
Browse files Browse the repository at this point in the history
[FEATURE] Optionally use a map theme to render terrain textures
  • Loading branch information
wonder-sk committed Nov 22, 2018
2 parents fee3935 + afd3525 commit d4ef55d
Show file tree
Hide file tree
Showing 10 changed files with 201 additions and 6 deletions.
15 changes: 15 additions & 0 deletions src/3d/qgs3dmapsettings.cpp
Expand Up @@ -39,6 +39,7 @@ Qgs3DMapSettings::Qgs3DMapSettings( const Qgs3DMapSettings &other )
, mMaxTerrainGroundError( other.mMaxTerrainGroundError )
, mTerrainShadingEnabled( other.mTerrainShadingEnabled )
, mTerrainShadingMaterial( other.mTerrainShadingMaterial )
, mTerrainMapTheme( other.mTerrainMapTheme )
, mShowTerrainBoundingBoxes( other.mShowTerrainBoundingBoxes )
, mShowTerrainTileInfo( other.mShowTerrainTileInfo )
, mShowCameraViewCenter( other.mShowCameraViewCenter )
Expand All @@ -48,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 Expand Up @@ -87,6 +91,7 @@ void Qgs3DMapSettings::readXml( const QDomElement &elem, const QgsReadWriteConte
QDomElement elemTerrainShadingMaterial = elemTerrain.firstChildElement( QStringLiteral( "shading-material" ) );
if ( !elemTerrainShadingMaterial.isNull() )
mTerrainShadingMaterial.readXml( elemTerrainShadingMaterial );
mTerrainMapTheme = elemTerrain.attribute( QStringLiteral( "map-theme" ) );
mShowLabels = elemTerrain.attribute( QStringLiteral( "show-labels" ), QStringLiteral( "0" ) ).toInt();

mPointLights.clear();
Expand Down Expand Up @@ -204,6 +209,7 @@ QDomElement Qgs3DMapSettings::writeXml( QDomDocument &doc, const QgsReadWriteCon
QDomElement elemTerrainShadingMaterial = doc.createElement( QStringLiteral( "shading-material" ) );
mTerrainShadingMaterial.writeXml( elemTerrainShadingMaterial );
elemTerrain.appendChild( elemTerrainShadingMaterial );
elemTerrain.setAttribute( QStringLiteral( "map-theme" ), mTerrainMapTheme );
elemTerrain.setAttribute( QStringLiteral( "show-labels" ), mShowLabels ? 1 : 0 );

QDomElement elemPointLights = doc.createElement( QStringLiteral( "point-lights" ) );
Expand Down Expand Up @@ -432,6 +438,15 @@ void Qgs3DMapSettings::setTerrainShadingMaterial( const QgsPhongMaterialSettings
emit terrainShadingChanged();
}

void Qgs3DMapSettings::setTerrainMapTheme( const QString &theme )
{
if ( mTerrainMapTheme == theme )
return;

mTerrainMapTheme = theme;
emit terrainMapThemeChanged();
}

void Qgs3DMapSettings::setRenderers( const QList<QgsAbstract3DRenderer *> &renderers )
{
mRenderers = renderers;
Expand Down
53 changes: 51 additions & 2 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 All @@ -148,9 +163,16 @@ class _3D_EXPORT Qgs3DMapSettings : public QObject
//! Returns vertical scale (exaggeration) of terrain
double terrainVerticalScale() const;

//! Sets the list of map layers to be rendered as a texture of the terrain
/**
* Sets the list of map layers to be rendered as a texture of the terrain
* \note If terrain map theme is set, it has a priority over the list of layers specified here.
*/
void setLayers( const QList<QgsMapLayer *> &layers );
//! Returns the list of map layers to be rendered as a texture of the terrain

/**
* Returns the list of map layers to be rendered as a texture of the terrain
* \note If terrain map theme is set, it has a priority over the list of layers specified here.
*/
QList<QgsMapLayer *> layers() const;

/**
Expand Down Expand Up @@ -231,6 +253,25 @@ class _3D_EXPORT Qgs3DMapSettings : public QObject
*/
QgsPhongMaterialSettings terrainShadingMaterial() const { return mTerrainShadingMaterial; }

/**
* Sets name of the map theme.
* \see terrainMapTheme()
* \since QGIS 3.6
*/
void setTerrainMapTheme( const QString &theme );

/**
* 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; }

//
// misc configuration
//

//! Sets list of extra 3D renderers to use in the scene. Takes ownership of the objects.
void setRenderers( const QList<QgsAbstract3DRenderer *> &renderers SIP_TRANSFER );
//! Returns list of extra 3D renderers
Expand Down Expand Up @@ -310,6 +351,12 @@ class _3D_EXPORT Qgs3DMapSettings : public QObject
* \since QGIS 3.6
*/
void terrainShadingChanged();

/**
* Emitted when terrain's map theme has changed
* \since QGIS 3.6
*/
void terrainMapThemeChanged();
//! Emitted when the flag whether terrain's bounding boxes are shown has changed
void showTerrainBoundingBoxesChanged();
//! Emitted when the flag whether terrain's tile info is shown has changed
Expand Down Expand Up @@ -342,6 +389,7 @@ class _3D_EXPORT Qgs3DMapSettings : public QObject
float mMaxTerrainGroundError = 1.f; //!< Maximum allowed horizontal map error in map units (determines how many zoom levels will be used)
bool mTerrainShadingEnabled = false; //!< Whether terrain should be shaded taking lights into account
QgsPhongMaterialSettings mTerrainShadingMaterial; //!< Material to use for the terrain (if shading is enabled). Diffuse color is ignored.
QString mTerrainMapTheme; //!< Name of map theme used for terrain's texture (empty means use the current map theme)
bool mShowTerrainBoundingBoxes = false; //!< Whether to show bounding boxes of entities - useful for debugging
bool mShowTerrainTileInfo = false; //!< Whether to draw extra information about terrain tiles to the textures - useful for debugging
bool mShowCameraViewCenter = false; //!< Whether to show camera view center as a sphere - useful for debugging
Expand All @@ -355,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
1 change: 1 addition & 0 deletions src/3d/terrain/qgsterrainentity_p.cpp
Expand Up @@ -69,6 +69,7 @@ QgsTerrainEntity::QgsTerrainEntity( int maxLevel, const Qgs3DMapSettings &map, Q
connect( &map, &Qgs3DMapSettings::showLabelsChanged, this, &QgsTerrainEntity::invalidateMapImages );
connect( &map, &Qgs3DMapSettings::layersChanged, this, &QgsTerrainEntity::onLayersChanged );
connect( &map, &Qgs3DMapSettings::backgroundColorChanged, this, &QgsTerrainEntity::invalidateMapImages );
connect( &map, &Qgs3DMapSettings::terrainMapThemeChanged, this, &QgsTerrainEntity::invalidateMapImages );

connectToLayersRepaintRequest();

Expand Down
16 changes: 15 additions & 1 deletion src/3d/terrain/qgsterraintexturegenerator_p.cpp
Expand Up @@ -18,6 +18,7 @@
#include <qgsmaprenderercustompainterjob.h>
#include <qgsmaprenderersequentialjob.h>
#include <qgsmapsettings.h>
#include <qgsmapthemecollection.h>
#include <qgsproject.h>

#include "qgs3dmapsettings.h"
Expand Down Expand Up @@ -127,13 +128,26 @@ void QgsTerrainTextureGenerator::onRenderingFinished()
QgsMapSettings QgsTerrainTextureGenerator::baseMapSettings()
{
QgsMapSettings mapSettings;
mapSettings.setLayers( mMap.layers() );

mapSettings.setOutputSize( QSize( mMap.mapTileResolution(), mMap.mapTileResolution() ) );
mapSettings.setDestinationCrs( mMap.crs() );
mapSettings.setBackgroundColor( mMap.backgroundColor() );
mapSettings.setFlag( QgsMapSettings::DrawLabeling, mMap.showLabels() );
mapSettings.setTransformContext( mMap.transformContext() );
mapSettings.setPathResolver( mMap.pathResolver() );

QgsMapThemeCollection *mapThemes = mMap.mapThemeCollection();
QString mapThemeName = mMap.terrainMapTheme();
if ( mapThemeName.isEmpty() || !mapThemes || !mapThemes->hasMapTheme( mapThemeName ) )
{
mapSettings.setLayers( mMap.layers() );
}
else
{
mapSettings.setLayers( mapThemes->mapThemeVisibleLayers( mapThemeName ) );
mapSettings.setLayerStyleOverrides( mapThemes->mapThemeStyleOverrides( mapThemeName ) );
}

return mapSettings;
}

Expand Down
13 changes: 12 additions & 1 deletion src/app/3d/qgs3dmapconfigwidget.cpp
Expand Up @@ -21,8 +21,9 @@
#include "qgs3dutils.h"

#include "qgsmapcanvas.h"
#include "qgsmapthemecollection.h"
#include "qgsrasterlayer.h"
//#include "qgsproject.h"
#include "qgsproject.h"

Qgs3DMapConfigWidget::Qgs3DMapConfigWidget( Qgs3DMapSettings *map, QgsMapCanvas *mainCanvas, QWidget *parent )
: QWidget( parent )
Expand Down Expand Up @@ -73,6 +74,14 @@ Qgs3DMapConfigWidget::Qgs3DMapConfigWidget( Qgs3DMapSettings *map, QgsMapCanvas
widgetTerrainMaterial->setDiffuseVisible( false );
widgetTerrainMaterial->setMaterial( mMap->terrainShadingMaterial() );

// populate combo box with map themes
const QStringList mapThemeNames = QgsProject::instance()->mapThemeCollection()->mapThemes();
cboTerrainMapTheme->addItem( QString() ); // empty item for no map theme
for ( QString themeName : mapThemeNames )
cboTerrainMapTheme->addItem( themeName );

cboTerrainMapTheme->setCurrentText( mMap->terrainMapTheme() );

widgetLights->setPointLights( mMap->pointLights() );

connect( cboTerrainLayer, static_cast<void ( QComboBox::* )( int )>( &QgsMapLayerComboBox::currentIndexChanged ), this, &Qgs3DMapConfigWidget::onTerrainLayerChanged );
Expand Down Expand Up @@ -144,6 +153,8 @@ void Qgs3DMapConfigWidget::apply()
mMap->setTerrainShadingEnabled( groupTerrainShading->isChecked() );
mMap->setTerrainShadingMaterial( widgetTerrainMaterial->material() );

mMap->setTerrainMapTheme( cboTerrainMapTheme->currentText() );

mMap->setPointLights( widgetLights->pointLights() );
}

Expand Down
10 changes: 10 additions & 0 deletions src/app/qgisapp.cpp
Expand Up @@ -11141,8 +11141,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 @@ -13562,6 +13564,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
21 changes: 19 additions & 2 deletions src/ui/3d/map3dconfigwidget.ui
Expand Up @@ -7,15 +7,15 @@
<x>0</x>
<y>0</y>
<width>691</width>
<height>1135</height>
<height>1122</height>
</rect>
</property>
<property name="windowTitle">
<string>Configure 3D Map Rendering</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="QGroupBox" name="groupBox">
<widget class="QgsCollapsibleGroupBox" name="groupTerrain">
<property name="title">
<string>Terrain</string>
</property>
Expand Down Expand Up @@ -84,6 +84,20 @@
</property>
</widget>
</item>
<item row="4" column="0">
<widget class="QLabel" name="label_9">
<property name="text">
<string>Map theme</string>
</property>
</widget>
</item>
<item row="4" column="1" colspan="2">
<widget class="QComboBox" name="cboTerrainMapTheme">
<property name="editable">
<bool>false</bool>
</property>
</widget>
</item>
</layout>
</widget>
</item>
Expand Down Expand Up @@ -275,12 +289,15 @@
<tabstop>spinTerrainScale</tabstop>
<tabstop>spinTerrainResolution</tabstop>
<tabstop>spinTerrainSkirtHeight</tabstop>
<tabstop>cboTerrainMapTheme</tabstop>
<tabstop>groupTerrainShading</tabstop>
<tabstop>spinMapResolution</tabstop>
<tabstop>spinScreenError</tabstop>
<tabstop>spinGroundError</tabstop>
<tabstop>chkShowLabels</tabstop>
<tabstop>chkShowTileInfo</tabstop>
<tabstop>chkShowBoundingBoxes</tabstop>
<tabstop>chkShowCameraViewCenter</tabstop>
</tabstops>
<resources/>
<connections/>
Expand Down

0 comments on commit d4ef55d

Please sign in to comment.