QGIS API Documentation  3.4.15-Madeira (e83d02e274)
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 "qgsline3dsymbol.h"
19 #include "qgspoint3dsymbol.h"
20 #include "qgspolygon3dsymbol.h"
21 #include "qgsline3dsymbol_p.h"
22 #include "qgspoint3dsymbol_p.h"
23 #include "qgspolygon3dsymbol_p.h"
24 
25 #include "qgsvectorlayer.h"
26 #include "qgsxmlutils.h"
27 
28 
30  : Qgs3DRendererAbstractMetadata( QStringLiteral( "vector" ) )
31 {
32 }
33 
35 {
37  r->readXml( elem, context );
38  return r;
39 }
40 
41 
42 // ---------
43 
44 
46  : mSymbol( s )
47 {
48 }
49 
51 {
52  QgsVectorLayer3DRenderer *r = new QgsVectorLayer3DRenderer( mSymbol ? mSymbol->clone() : nullptr );
53  r->mLayerRef = mLayerRef;
54  return r;
55 }
56 
58 {
59  mLayerRef = QgsMapLayerRef( layer );
60 }
61 
63 {
64  return qobject_cast<QgsVectorLayer *>( mLayerRef.layer );
65 }
66 
68 {
69  mSymbol.reset( symbol );
70 }
71 
73 {
74  return mSymbol.get();
75 }
76 
77 Qt3DCore::QEntity *QgsVectorLayer3DRenderer::createEntity( const Qgs3DMapSettings &map ) const
78 {
79  QgsVectorLayer *vl = layer();
80 
81  if ( !mSymbol || !vl )
82  return nullptr;
83 
84  if ( mSymbol->type() == QLatin1String( "polygon" ) )
85  return new QgsPolygon3DSymbolEntity( map, vl, *static_cast<QgsPolygon3DSymbol *>( mSymbol.get() ) );
86  else if ( mSymbol->type() == QLatin1String( "point" ) )
87  return new QgsPoint3DSymbolEntity( map, vl, *static_cast<QgsPoint3DSymbol *>( mSymbol.get() ) );
88  else if ( mSymbol->type() == QLatin1String( "line" ) )
89  return new QgsLine3DSymbolEntity( map, vl, *static_cast<QgsLine3DSymbol *>( mSymbol.get() ) );
90  else
91  return nullptr;
92 }
93 
94 void QgsVectorLayer3DRenderer::writeXml( QDomElement &elem, const QgsReadWriteContext &context ) const
95 {
96  QDomDocument doc = elem.ownerDocument();
97 
98  elem.setAttribute( QStringLiteral( "layer" ), mLayerRef.layerId );
99 
100  QDomElement elemSymbol = doc.createElement( QStringLiteral( "symbol" ) );
101  if ( mSymbol )
102  {
103  elemSymbol.setAttribute( QStringLiteral( "type" ), mSymbol->type() );
104  mSymbol->writeXml( elemSymbol, context );
105  }
106  elem.appendChild( elemSymbol );
107 }
108 
109 void QgsVectorLayer3DRenderer::readXml( const QDomElement &elem, const QgsReadWriteContext &context )
110 {
111  mLayerRef = QgsMapLayerRef( elem.attribute( QStringLiteral( "layer" ) ) );
112 
113  QDomElement elemSymbol = elem.firstChildElement( QStringLiteral( "symbol" ) );
114  QString symbolType = elemSymbol.attribute( QStringLiteral( "type" ) );
115  QgsAbstract3DSymbol *symbol = nullptr;
116  if ( symbolType == QLatin1String( "polygon" ) )
117  symbol = new QgsPolygon3DSymbol;
118  else if ( symbolType == QLatin1String( "point" ) )
119  symbol = new QgsPoint3DSymbol;
120  else if ( symbolType == QLatin1String( "line" ) )
121  symbol = new QgsLine3DSymbol;
122 
123  if ( symbol )
124  symbol->readXml( elemSymbol, context );
125  mSymbol.reset( symbol );
126 }
127 
129 {
130  mLayerRef.setLayer( project.mapLayer( mLayerRef.layerId ) );
131 }
The class is used as a container of context for various read/write operations on other objects...
void readXml(const QDomElement &elem, const QgsReadWriteContext &context) override
Reads renderer&#39;s properties from given XML element.
_LayerRef< QgsMapLayer > QgsMapLayerRef
Base class for all renderers that may to participate in 3D view.
void resolveReferences(const QgsProject &project) override
Resolves references to other objects - second phase of loading - after readXml()
void writeXml(QDomElement &elem, const QgsReadWriteContext &context) const override
Writes renderer&#39;s properties to given XML element.
QgsAbstract3DRenderer * createRenderer(QDomElement &elem, const QgsReadWriteContext &context) override
Creates an instance of a 3D renderer based on a DOM element with renderer configuration.
3 3D symbol that draws polygon geometries as planar polygons, optionally extruded (with added walls)...
void setLayer(QgsVectorLayer *layer)
Sets vector layer associated with the renderer.
void setSymbol(QgsAbstract3DSymbol *symbol)
Sets 3D symbol associated with the renderer. Takes ownership of the symbol.
3 Definition of the world
3 Abstract base class for 3D symbols that are used by VectorLayer3DRenderer objects.
QgsVectorLayer3DRenderer * clone() const override
Returns a cloned instance.
QString layerId
Original layer ID.
QPointer< TYPE > layer
Weak pointer to map layer.
3 3D symbol that draws point geometries as 3D objects using one of the predefined shapes...
Reads and writes project states.
Definition: qgsproject.h:89
3 3D symbol that draws linestring geometries as planar polygons (created from lines using a buffer wi...
void setLayer(TYPE *l)
Sets the reference to point to a specified layer.
3D renderer that renders all features of a vector layer with the same 3D symbol.
QgsMapLayer * mapLayer(const QString &layerId) const
Retrieve a pointer to a registered layer by layer ID.
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&#39;s data in 3D scene.
virtual void readXml(const QDomElement &elem, const QgsReadWriteContext &context)=0
Reads symbol configuration from the given DOM element.
QgsVectorLayer * layer() const
Returns vector layer associated with the renderer.
Base metadata class for 3D renderers.
Represents a vector layer which manages a vector based data sets.
QgsVectorLayer3DRenderer(QgsAbstract3DSymbol *s=nullptr)
Takes ownership of the symbol object.