QGIS API Documentation 4.0.0-Norrköping (1ddcee3d0e4)
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
34
36{
37 auto r = std::make_unique< QgsAnnotationLayer3DRenderer >();
38 r->readXml( elem, context );
39 return r.release();
40}
41
42
43//
44// QgsAnnotationLayer3DRenderer
45//
46
48{
50 mTextFormat.setNamedStyle( u"Bold"_s );
51 mTextFormat.setSize( 20 );
52}
53
58
60{
61 return qobject_cast<QgsAnnotationLayer *>( mLayerRef.layer );
62}
63
65{
66 mLayerRef.resolve( &project );
67}
68
70{
71 return mShowCalloutLines;
72}
73
75{
76 mShowCalloutLines = show;
77}
78
80{
81 mCalloutLineColor = color;
82}
83
85{
86 return mCalloutLineColor;
87}
88
90{
91 mCalloutLineWidth = width;
92}
93
95{
96 return mCalloutLineWidth;
97}
98
100{
101 return mTextFormat;
102}
103
105{
106 mTextFormat = format;
107}
108
110{
111 return "annotation";
112}
113
115{
116 auto r = std::make_unique< QgsAnnotationLayer3DRenderer >();
117 r->mLayerRef = mLayerRef;
118 r->mAltClamping = mAltClamping;
119 r->mZOffset = mZOffset;
120 r->mShowCalloutLines = mShowCalloutLines;
121 r->mCalloutLineColor = mCalloutLineColor;
122 r->mCalloutLineWidth = mCalloutLineWidth;
123 r->mTextFormat = mTextFormat;
124 return r.release();
125}
126
128{
130
131 if ( !l )
132 return nullptr;
133
134 // For some cases we start with a maximal z range because we can't know this upfront, as it potentially involves terrain heights.
135 // This range will be refined after populating the nodes to the actual z range of the generated chunks nodes.
136 // Assuming the vertical height is in meter, then it's extremely unlikely that a real vertical
137 // height will exceed this amount!
138 constexpr double MINIMUM_ANNOTATION_Z_ESTIMATE = -100000;
139 constexpr double MAXIMUM_ANNOTATION_Z_ESTIMATE = 100000;
140
141 double minimumZ = MINIMUM_ANNOTATION_Z_ESTIMATE;
142 double maximumZ = MAXIMUM_ANNOTATION_Z_ESTIMATE;
143 switch ( mAltClamping )
144 {
146 // special case where we DO know the exact z range upfront!
147 minimumZ = mZOffset;
148 maximumZ = mZOffset;
149 break;
150
153 break;
154 }
155
156 return new QgsAnnotationLayerChunkedEntity( map, l, mAltClamping, mZOffset, mShowCalloutLines, mCalloutLineColor, mCalloutLineWidth, mTextFormat, minimumZ, maximumZ );
157}
158
159void QgsAnnotationLayer3DRenderer::writeXml( QDomElement &elem, const QgsReadWriteContext &context ) const
160{
161 QDomDocument doc = elem.ownerDocument();
162
163 elem.setAttribute( u"layer"_s, mLayerRef.layerId );
164 elem.setAttribute( u"clamping"_s, qgsEnumValueToKey( mAltClamping ) );
165 elem.setAttribute( u"offset"_s, mZOffset );
166 if ( mShowCalloutLines )
167 elem.setAttribute( u"callouts"_s, u"1"_s );
168
169 if ( mTextFormat.isValid() )
170 {
171 elem.appendChild( mTextFormat.writeXml( doc, context ) );
172 }
173}
174
175void QgsAnnotationLayer3DRenderer::readXml( const QDomElement &elem, const QgsReadWriteContext &context )
176{
177 mLayerRef = QgsMapLayerRef( elem.attribute( u"layer"_s ) );
178 mAltClamping = qgsEnumKeyToValue( elem.attribute( u"clamping"_s ), Qgis::AltitudeClamping::Relative );
179 mZOffset = elem.attribute( u"offset"_s, QString::number( DEFAULT_Z_OFFSET ) ).toDouble();
180 mShowCalloutLines = elem.attribute( u"callouts"_s, u"0"_s ).toInt();
181 if ( !elem.firstChildElement( u"text-style"_s ).isNull() )
182 {
183 mTextFormat = QgsTextFormat();
184 mTextFormat.readXml( elem.firstChildElement( u"text-style"_s ), context );
185 }
186}
@ Relative
Elevation is relative to terrain height (final elevation = terrain elevation + feature elevation).
Definition qgis.h:4101
@ Terrain
Elevation is clamped to terrain (final elevation = terrain elevation).
Definition qgis.h:4102
@ Absolute
Elevation is taken directly from feature and is independent of terrain height (final elevation = feat...
Definition qgis.h:4100
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:113
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:148
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:7176
QString qgsEnumValueToKey(const T &value, bool *returnOk=nullptr)
Returns the value for the given key of an enum.
Definition qgis.h:7157
_LayerRef< QgsMapLayer > QgsMapLayerRef