QGIS API Documentation 3.99.0-Master (e9821da5c6b)
Loading...
Searching...
No Matches
qgstiledscenewireframerenderer.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgstiledscenewireframerenderer.h
3 --------------------
4 begin : August 2023
5 copyright : (C) 2023 by Nyall Dawson
6 email : nyall dot dawson at gmail dot com
7 ***************************************************************************/
8
9/***************************************************************************
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
15 * *
16 ***************************************************************************/
17
19
20#include "qgsfillsymbol.h"
21#include "qgslinesymbol.h"
22#include "qgssymbollayerutils.h"
23
24#include <QString>
25
26using namespace Qt::StringLiterals;
27
33
35
37{
38 return u"wireframe"_s;
39}
40
42{
43 auto res = std::make_unique< QgsTiledSceneWireframeRenderer >();
44
45 res->setFillSymbol( mFillSymbol->clone() );
46 res->setLineSymbol( mLineSymbol->clone() );
47 res->setUseTextureColors( mUseTextureColors );
48
49 copyCommonProperties( res.get() );
50
51 return res.release();
52}
53
55{
56 auto r = std::make_unique< QgsTiledSceneWireframeRenderer >();
57 {
58 const QDomElement fillSymbolElem = element.firstChildElement( u"fillSymbol"_s );
59 if ( !fillSymbolElem.isNull() )
60 {
61 const QDomElement symbolElem = fillSymbolElem.firstChildElement( u"symbol"_s );
62 std::unique_ptr< QgsFillSymbol > fillSymbol( QgsSymbolLayerUtils::loadSymbol<QgsFillSymbol>( symbolElem, context ) );
63 if ( fillSymbol )
64 r->mFillSymbol = std::move( fillSymbol );
65 }
66 }
67 {
68 const QDomElement lineSymbolElem = element.firstChildElement( u"lineSymbol"_s );
69 if ( !lineSymbolElem.isNull() )
70 {
71 const QDomElement symbolElem = lineSymbolElem.firstChildElement( u"symbol"_s );
72 std::unique_ptr< QgsLineSymbol > lineSymbol( QgsSymbolLayerUtils::loadSymbol<QgsLineSymbol>( symbolElem, context ) );
73 if ( lineSymbol )
74 r->mLineSymbol = std::move( lineSymbol );
75 }
76 }
77
78 r->setUseTextureColors( element.attribute( u"useTextureColors"_s, u"0"_s ).toInt() );
79
80 r->restoreCommonProperties( element, context );
81 return r.release();
82}
83
85{
86 QVariantMap properties;
87 properties.insert( u"color"_s, u"white"_s );
88 properties.insert( u"style"_s, u"solid"_s );
89 properties.insert( u"style_border"_s, u"solid"_s );
90 properties.insert( u"color_border"_s, u"black"_s );
91 properties.insert( u"width_border"_s, u"0.3"_s );
92 properties.insert( u"joinstyle"_s, u"miter"_s );
93
94 return QgsFillSymbol::createSimple( properties );
95}
96
98{
99 return mFillSymbol.get();
100}
101
103{
104 mFillSymbol.reset( symbol );
105}
106
108{
109 QVariantMap properties;
110 properties.insert( u"color"_s, u"red"_s );
111
112 return QgsLineSymbol::createSimple( properties );
113}
114
116{
117 return mLineSymbol.get();
118}
119
121{
122 mLineSymbol.reset( symbol );
123}
124
126{
127 return mUseTextureColors;
128}
129
131{
132 mUseTextureColors = newUseTextureColors;
133}
134
135QDomElement QgsTiledSceneWireframeRenderer::save( QDomDocument &doc, const QgsReadWriteContext &context ) const
136{
137 QDomElement rendererElem = doc.createElement( u"renderer"_s );
138
139 rendererElem.setAttribute( u"type"_s, u"wireframe"_s );
140 rendererElem.setAttribute( u"useTextureColors"_s, mUseTextureColors ? u"1"_s : u"0"_s );
141
142 {
143 QDomElement fillSymbolElem = doc.createElement( u"fillSymbol"_s );
144 const QDomElement symbolElement = QgsSymbolLayerUtils::saveSymbol( QString(),
145 mFillSymbol.get(),
146 doc,
147 context );
148 fillSymbolElem.appendChild( symbolElement );
149 rendererElem.appendChild( fillSymbolElem );
150 }
151 {
152 QDomElement lineSymbolElem = doc.createElement( u"lineSymbol"_s );
153 const QDomElement symbolElement = QgsSymbolLayerUtils::saveSymbol( QString(),
154 mLineSymbol.get(),
155 doc,
156 context );
157 lineSymbolElem.appendChild( symbolElement );
158 rendererElem.appendChild( lineSymbolElem );
159 }
160 saveCommonProperties( rendererElem, context );
161
162 return rendererElem;
163}
164
166{
167 if ( mUseTextureColors )
168 {
169 std::unique_ptr< QgsFillSymbol > s( mFillSymbol->clone() );
170 const QImage textureImage = context.textureImage();
171 if ( !textureImage.isNull() )
172 {
173 float textureX1;
174 float textureY1;
175 float textureX2;
176 float textureY2;
177 float textureX3;
178 float textureY3;
179 context.textureCoordinates( textureX1, textureY1, textureX2, textureY2, textureX3, textureY3 );
180
181 const QColor centerColor( textureImage.pixelColor(
182 static_cast<int>( ( ( textureX1 + textureX2 + textureX3 ) / 3 ) * ( textureImage.width() - 1 ) ),
183 static_cast< int >( ( ( textureY1 + textureY2 + textureY3 ) / 3 ) * ( textureImage.height() - 1 ) ) )
184 );
185 s->setColor( centerColor );
186 }
187 s->startRender( context.renderContext() );
188 s->renderPolygon( triangle, nullptr, nullptr, context.renderContext() );
189 s->stopRender( context.renderContext() );
190 }
191 else
192 {
193 mFillSymbol->renderPolygon( triangle, nullptr, nullptr, context.renderContext() );
194 }
195}
196
198{
199 mLineSymbol->renderPolyline( line, nullptr, context.renderContext() );
200}
201
203{
205
206 if ( !mUseTextureColors )
207 mFillSymbol->startRender( context.renderContext() );
208
209 mLineSymbol->startRender( context.renderContext() );
210}
211
213{
214 if ( !mUseTextureColors )
215 mFillSymbol->stopRender( context.renderContext() );
216
217 mLineSymbol->stopRender( context.renderContext() );
218
220}
221
@ RendersLines
Renderer can render line primitives.
Definition qgis.h:5994
@ RequiresTextures
Renderer requires textures.
Definition qgis.h:5991
@ RendersTriangles
Renderer can render triangle primitives.
Definition qgis.h:5993
QFlags< TiledSceneRendererFlag > TiledSceneRendererFlags
Flags which control how tiled scene 2D renderers behave.
Definition qgis.h:6003
A fill symbol type, for rendering Polygon and MultiPolygon geometries.
static std::unique_ptr< QgsFillSymbol > createSimple(const QVariantMap &properties)
Create a fill symbol with one symbol layer: SimpleFill with specified properties.
A line symbol type, for rendering LineString and MultiLineString geometries.
static std::unique_ptr< QgsLineSymbol > createSimple(const QVariantMap &properties)
Create a line symbol with one symbol layer: SimpleLine with specified properties.
A container for the context for various read/write operations on objects.
static std::unique_ptr< QgsSymbol > loadSymbol(const QDomElement &element, const QgsReadWriteContext &context)
Attempts to load a symbol from a DOM element.
static QDomElement saveSymbol(const QString &symbolName, const QgsSymbol *symbol, QDomDocument &doc, const QgsReadWriteContext &context)
Writes a symbol definition to XML.
Encapsulates the render context for a 2D tiled scene rendering operation.
void textureCoordinates(float &textureX1, float &textureY1, float &textureX2, float &textureY2, float &textureX3, float &textureY3) const
Returns the current texture coordinates.
QgsRenderContext & renderContext()
Returns a reference to the context's render context.
QImage textureImage() const
Returns the current texture image.
void saveCommonProperties(QDomElement &element, const QgsReadWriteContext &context) const
Saves common renderer properties (such as point size and screen error) to the specified DOM element.
virtual void stopRender(QgsTiledSceneRenderContext &context)
Must be called when a render cycle has finished, to allow the renderer to clean up.
QgsTiledSceneRenderer()=default
virtual void startRender(QgsTiledSceneRenderContext &context)
Must be called when a new render cycle is started.
void copyCommonProperties(QgsTiledSceneRenderer *destination) const
Copies common tiled scene renderer properties (such as screen error) to the destination renderer.
Qgis::TiledSceneRendererFlags flags() const override
Returns flags which control how the renderer behaves.
QDomElement save(QDomDocument &doc, const QgsReadWriteContext &context) const override
Saves the renderer configuration to an XML element.
void startRender(QgsTiledSceneRenderContext &context) override
Must be called when a new render cycle is started.
~QgsTiledSceneWireframeRenderer() override
QString type() const override
Returns the identifier of the renderer type.
void renderTriangle(QgsTiledSceneRenderContext &context, const QPolygonF &triangle) override
Renders a triangle.
void setFillSymbol(QgsFillSymbol *symbol)
Sets the fill symbol used to render triangles in the wireframe.
void renderLine(QgsTiledSceneRenderContext &context, const QPolygonF &line) override
Renders a line.
static std::unique_ptr< QgsLineSymbol > createDefaultLineSymbol()
Returns a copy of the default line symbol used to render lines in the wireframe.
void stopRender(QgsTiledSceneRenderContext &context) override
Must be called when a render cycle has finished, to allow the renderer to clean up.
static QgsTiledSceneRenderer * create(QDomElement &element, const QgsReadWriteContext &context)
Creates a textured renderer from an XML element.
void setLineSymbol(QgsLineSymbol *symbol)
Sets the line symbol used to render lines in the wireframe.
static std::unique_ptr< QgsFillSymbol > createDefaultFillSymbol()
Returns a copy of the default fill symbol used to render triangles in the wireframe.
QgsTiledSceneWireframeRenderer()
Constructor for QgsTiledSceneWireframeRenderer.
QgsFillSymbol * fillSymbol() const
Returns the fill symbol used to render triangles in the wireframe.
QgsLineSymbol * lineSymbol() const
Returns the line symbol used to render lines in the wireframe.
void setUseTextureColors(bool enabled)
Sets whether representative colors from the textures should be used to recolor the symbols used to re...
bool useTextureColors() const
Returns true if representative colors from the textures will be used to recolor the symbols used to r...
QgsTiledSceneRenderer * clone() const override
Create a deep copy of this renderer.