QGIS API Documentation 3.99.0-Master (d270888f95f)
Loading...
Searching...
No Matches
qgsannotationlayer3drenderer.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsannotationlayer3drenderer.cpp
3 --------------------------------------
4 Date : January 2020
5 Copyright : (C) 2020 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 "qgis.h"
19#include "qgsannotationlayer.h"
21#include "qgsstyle.h"
22
23#include <QString>
24
25using namespace Qt::StringLiterals;
26
27//
28// QgsAnnotationLayer3DRendererMetadata
29//
30
35
37{
38 auto r = std::make_unique< QgsAnnotationLayer3DRenderer >();
39 r->readXml( elem, context );
40 return r.release();
41}
42
43
44//
45// QgsAnnotationLayer3DRenderer
46//
47
49{
51 mTextFormat.setNamedStyle( u"Bold"_s );
52 mTextFormat.setSize( 20 );
53}
54
59
61{
62 return qobject_cast<QgsAnnotationLayer *>( mLayerRef.layer );
63}
64
66{
67 mLayerRef.resolve( &project );
68}
69
71{
72 return mShowCalloutLines;
73}
74
76{
77 mShowCalloutLines = show;
78}
79
81{
82 mCalloutLineColor = color;
83}
84
86{
87 return mCalloutLineColor;
88}
89
91{
92 mCalloutLineWidth = width;
93}
94
96{
97 return mCalloutLineWidth;
98}
99
101{
102 return mTextFormat;
103}
104
106{
107 mTextFormat = format;
108}
109
111{
112 return "annotation";
113}
114
116{
117 auto r = std::make_unique< QgsAnnotationLayer3DRenderer >();
118 r->mLayerRef = mLayerRef;
119 r->mAltClamping = mAltClamping;
120 r->mZOffset = mZOffset;
121 r->mShowCalloutLines = mShowCalloutLines;
122 r->mCalloutLineColor = mCalloutLineColor;
123 r->mCalloutLineWidth = mCalloutLineWidth;
124 r->mTextFormat = mTextFormat;
125 return r.release();
126}
127
129{
131
132 if ( !l )
133 return nullptr;
134
135 // For some cases we start with a maximal z range because we can't know this upfront, as it potentially involves terrain heights.
136 // This range will be refined after populating the nodes to the actual z range of the generated chunks nodes.
137 // Assuming the vertical height is in meter, then it's extremely unlikely that a real vertical
138 // height will exceed this amount!
139 constexpr double MINIMUM_ANNOTATION_Z_ESTIMATE = -100000;
140 constexpr double MAXIMUM_ANNOTATION_Z_ESTIMATE = 100000;
141
142 double minimumZ = MINIMUM_ANNOTATION_Z_ESTIMATE;
143 double maximumZ = MAXIMUM_ANNOTATION_Z_ESTIMATE;
144 switch ( mAltClamping )
145 {
147 // special case where we DO know the exact z range upfront!
148 minimumZ = mZOffset;
149 maximumZ = mZOffset;
150 break;
151
154 break;
155 }
156
157 return new QgsAnnotationLayerChunkedEntity( map, l, mAltClamping, mZOffset, mShowCalloutLines, mCalloutLineColor, mCalloutLineWidth, mTextFormat, minimumZ, maximumZ );
158}
159
160void QgsAnnotationLayer3DRenderer::writeXml( QDomElement &elem, const QgsReadWriteContext &context ) const
161{
162 QDomDocument doc = elem.ownerDocument();
163
164 elem.setAttribute( u"layer"_s, mLayerRef.layerId );
165 elem.setAttribute( u"clamping"_s, qgsEnumValueToKey( mAltClamping ) );
166 elem.setAttribute( u"offset"_s, mZOffset );
167 if ( mShowCalloutLines )
168 elem.setAttribute( u"callouts"_s, u"1"_s );
169
170 if ( mTextFormat.isValid() )
171 {
172 elem.appendChild( mTextFormat.writeXml( doc, context ) );
173 }
174}
175
176void QgsAnnotationLayer3DRenderer::readXml( const QDomElement &elem, const QgsReadWriteContext &context )
177{
178 mLayerRef = QgsMapLayerRef( elem.attribute( u"layer"_s ) );
179 mAltClamping = qgsEnumKeyToValue( elem.attribute( u"clamping"_s ), Qgis::AltitudeClamping::Relative );
180 mZOffset = elem.attribute( u"offset"_s, QString::number( DEFAULT_Z_OFFSET ) ).toDouble();
181 mShowCalloutLines = elem.attribute( u"callouts"_s, u"0"_s ).toInt();
182 if ( !elem.firstChildElement( u"text-style"_s ).isNull() )
183 {
184 mTextFormat = QgsTextFormat();
185 mTextFormat.readXml( elem.firstChildElement( u"text-style"_s ), context );
186 }
187}
@ Relative
Elevation is relative to terrain height (final elevation = terrain elevation + feature elevation).
Definition qgis.h:4043
@ Terrain
Elevation is clamped to terrain (final elevation = terrain elevation).
Definition qgis.h:4044
@ Absolute
Elevation is taken directly from feature and is independent of terrain height (final elevation = feat...
Definition qgis.h:4042
Definition of the world.
Qgs3DRendererAbstractMetadata(const QString &type)
Constructor of the base class.
Base class for all renderers that participate in 3D views.
QgsAbstract3DRenderer * createRenderer(QDomElement &elem, const QgsReadWriteContext &context) override
Creates an instance of a 3D renderer based on a DOM element with renderer configuration.
QString type() const override
Returns unique identifier of the renderer class (used to identify subclass).
Qt3DCore::QEntity * createEntity(Qgs3DMapSettings *map) const override
Returns a 3D entity that will be used to show renderer's data in 3D scene.
void writeXml(QDomElement &elem, const QgsReadWriteContext &context) const override
Writes renderer's properties to given XML element.
void readXml(const QDomElement &elem, const QgsReadWriteContext &context) override
Reads renderer's properties from given XML element.
QColor calloutLineColor() const
Returns the callout line color.
void setCalloutLineColor(const QColor &color)
Sets the callout line color.
void setShowCalloutLines(bool show)
Sets whether callout lines are shown, vertically joining the annotations to the terrain.
QgsTextFormat textFormat() const
Returns the text format to use for rendering text annotations in 3D.
void resolveReferences(const QgsProject &project) override
Resolves references to other objects - second phase of loading - after readXml().
double calloutLineWidth() const
Returns the callout line width.
QgsAnnotationLayer * layer() const
Returns the annotation layer associated with the renderer.
bool showCalloutLines() const
Returns true if callout lines are shown, vertically joining the annotations to the terrain.
void setCalloutLineWidth(double width)
Sets the callout line width.
void setLayer(QgsAnnotationLayer *layer)
Sets the annotation layer associated with the renderer.
void setTextFormat(const QgsTextFormat &format)
Sets the text format to use for rendering text annotations in 3D.
QgsAnnotationLayer3DRenderer * clone() const override
Returns a cloned instance.
Represents a map layer containing a set of georeferenced annotations, e.g.
Encapsulates a QGIS project, including sets of map layers and their styles, layouts,...
Definition qgsproject.h:112
A container for the context for various read/write operations on objects.
QgsTextFormat defaultTextFormat(QgsStyle::TextFormatContext context=QgsStyle::TextFormatContext::Labeling) const
Returns the default text format to use for new text based objects in the specified context.
static QgsStyle * defaultStyle(bool initialize=true)
Returns the default application-wide style.
Definition qgsstyle.cpp:150
Container for all settings relating to text rendering.
void setNamedStyle(const QString &style)
Sets the named style for the font used for rendering text.
T qgsEnumKeyToValue(const QString &key, const T &defaultValue, bool tryValueAsKey=true, bool *returnOk=nullptr)
Returns the value corresponding to the given key of an enum.
Definition qgis.h:7110
QString qgsEnumValueToKey(const T &value, bool *returnOk=nullptr)
Returns the value for the given key of an enum.
Definition qgis.h:7091
_LayerRef< QgsMapLayer > QgsMapLayerRef