QGIS API Documentation  3.23.0-Master (b5237dafc3)
qgsvectorlayer3drenderer.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsvectorlayer3drenderer.cpp
3  --------------------------------------
4  Date : July 2017
5  Copyright : (C) 2017 by Martin Dobias
6  Email : wonder dot sk at gmail dot com
7  ***************************************************************************
8  * *
9  * This program is free software; you can redistribute it and/or modify *
10  * it under the terms of the GNU General Public License as published by *
11  * the Free Software Foundation; either version 2 of the License, or *
12  * (at your option) any later version. *
13  * *
14  ***************************************************************************/
15 
17 
18 #include "qgs3dutils.h"
19 #include "qgschunkedentity_p.h"
21 
22 #include "qgsvectorlayer.h"
23 #include "qgsxmlutils.h"
24 #include "qgsapplication.h"
25 #include "qgs3dsymbolregistry.h"
26 
27 
29  : Qgs3DRendererAbstractMetadata( QStringLiteral( "vector" ) )
30 {
31 }
32 
34 {
36  r->readXml( elem, context );
37  return r;
38 }
39 
40 
41 // ---------
42 
43 
45  : mSymbol( s )
46 {
47 }
48 
50 {
51  QgsVectorLayer3DRenderer *r = new QgsVectorLayer3DRenderer( mSymbol ? mSymbol->clone() : nullptr );
52  copyBaseProperties( r );
53  return r;
54 }
55 
57 {
58  mSymbol.reset( symbol );
59 }
60 
62 {
63  return mSymbol.get();
64 }
65 
66 Qt3DCore::QEntity *QgsVectorLayer3DRenderer::createEntity( const Qgs3DMapSettings &map ) const
67 {
68  QgsVectorLayer *vl = layer();
69 
70  if ( !mSymbol || !vl )
71  return nullptr;
72 
73  double zMin, zMax;
74  Qgs3DUtils::estimateVectorLayerZRange( vl, zMin, zMax );
75 
76  return new QgsVectorLayerChunkedEntity( vl, zMin + map.terrainElevationOffset(), zMax + + map.terrainElevationOffset(), tilingSettings(), mSymbol.get(), map );
77 }
78 
79 void QgsVectorLayer3DRenderer::writeXml( QDomElement &elem, const QgsReadWriteContext &context ) const
80 {
81  QDomDocument doc = elem.ownerDocument();
82 
83  writeXmlBaseProperties( elem, context );
84 
85  QDomElement elemSymbol = doc.createElement( QStringLiteral( "symbol" ) );
86  if ( mSymbol )
87  {
88  elemSymbol.setAttribute( QStringLiteral( "type" ), mSymbol->type() );
89  mSymbol->writeXml( elemSymbol, context );
90  }
91  elem.appendChild( elemSymbol );
92 }
93 
94 void QgsVectorLayer3DRenderer::readXml( const QDomElement &elem, const QgsReadWriteContext &context )
95 {
96  readXmlBaseProperties( elem, context );
97 
98  const QDomElement elemSymbol = elem.firstChildElement( QStringLiteral( "symbol" ) );
99  const QString symbolType = elemSymbol.attribute( QStringLiteral( "type" ) );
100  mSymbol.reset( QgsApplication::symbol3DRegistry()->createSymbol( symbolType ) );
101  if ( mSymbol )
102  mSymbol->readXml( elemSymbol, context );
103 }
float terrainElevationOffset() const
Returns the elevation offset of the terrain (used to move the terrain up or down)
Base metadata class for 3D renderers.
static void estimateVectorLayerZRange(QgsVectorLayer *layer, double &zMin, double &zMax)
Try to estimate range of Z values used in the given vector layer and store that in zMin and zMax.
Definition: qgs3dutils.cpp:569
Base class for all renderers that may to participate in 3D view.
QgsVectorLayer3DTilingSettings tilingSettings() const
Returns tiling settings of the renderer.
void writeXmlBaseProperties(QDomElement &elem, const QgsReadWriteContext &context) const
Writes common properties of this object to DOM element.
void readXmlBaseProperties(const QDomElement &elem, const QgsReadWriteContext &context)
Reads common properties of this object from DOM element.
void copyBaseProperties(QgsAbstractVectorLayer3DRenderer *r) const
Copies common properties of this object to another object.
QgsVectorLayer * layer() const
Returns vector layer associated with the renderer.
static Qgs3DSymbolRegistry * symbol3DRegistry()
Returns registry of available 3D symbols.
The class is used as a container of context for various read/write operations on other objects.
QgsAbstract3DRenderer * createRenderer(QDomElement &elem, const QgsReadWriteContext &context) override
Creates an instance of a 3D renderer based on a DOM element with renderer configuration.
3D renderer that renders all features of a vector layer with the same 3D symbol.
void setSymbol(QgsAbstract3DSymbol *symbol)
Sets 3D symbol associated with the renderer. Takes ownership of the symbol.
void readXml(const QDomElement &elem, const QgsReadWriteContext &context) override
Reads renderer's properties from given XML element.
QgsVectorLayer3DRenderer * clone() const override
Returns a cloned instance.
void writeXml(QDomElement &elem, const QgsReadWriteContext &context) const override
Writes renderer's properties to given XML element.
const QgsAbstract3DSymbol * symbol() const
Returns 3D symbol associated with the renderer.
Qt3DCore::QEntity * createEntity(const Qgs3DMapSettings &map) const override
Returns a 3D entity that will be used to show renderer's data in 3D scene.
QgsVectorLayer3DRenderer(QgsAbstract3DSymbol *s=nullptr)
Takes ownership of the symbol object.
Represents a vector layer which manages a vector based data sets.