QGIS API Documentation 3.34.0-Prizren (ffbdd678812)
Loading...
Searching...
No Matches
qgs25drenderer.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgs25drenderer.cpp - qgs25drenderer
3 -----------------------------------
4
5 begin : 14.1.2016
6 Copyright : (C) 2016 Matthias Kuhn
7 Email : matthias at opengis dot ch
8 ***************************************************************************
9 * *
10 * This program is free software; you can redistribute it and/or modify *
11 * it under the terms of the GNU General Public License as published by *
12 * the Free Software Foundation; either version 2 of the License, or *
13 * (at your option) any later version. *
14 * *
15 ***************************************************************************/
16#include "qgs25drenderer.h"
18#include "qgsfillsymbollayer.h"
19#include "qgspainteffect.h"
20#include "qgseffectstack.h"
21#include "qgsgloweffect.h"
22#include "qgsproperty.h"
23#include "qgssymbollayerutils.h"
25#include "qgsfillsymbol.h"
26
27#define ROOF_EXPRESSION \
28 "translate(" \
29 " $geometry," \
30 " cos( radians( eval( @qgis_25d_angle ) ) ) * eval( @qgis_25d_height )," \
31 " sin( radians( eval( @qgis_25d_angle ) ) ) * eval( @qgis_25d_height )" \
32 ")"
33
34#define WALL_EXPRESSION \
35 "order_parts( "\
36 " extrude(" \
37 " segments_to_lines( $geometry )," \
38 " cos( radians( eval( @qgis_25d_angle ) ) ) * eval( @qgis_25d_height )," \
39 " sin( radians( eval( @qgis_25d_angle ) ) ) * eval( @qgis_25d_height )" \
40 " )," \
41 " 'distance( $geometry, translate( @map_extent_center, 1000 * @map_extent_width * cos( radians( @qgis_25d_angle + 180 ) ), 1000 * @map_extent_width * sin( radians( @qgis_25d_angle + 180 ) ) ))'," \
42 " False" \
43 ")"
44
45#define ORDER_BY_EXPRESSION \
46 "distance(" \
47 " $geometry," \
48 " translate(" \
49 " @map_extent_center," \
50 " 1000 * @map_extent_width * cos( radians( @qgis_25d_angle + 180 ) )," \
51 " 1000 * @map_extent_width * sin( radians( @qgis_25d_angle + 180 ) )" \
52 " )" \
53 ")"
54
55#define WALL_SHADING_EXPRESSION \
56 "set_color_part( " \
57 " @symbol_color," \
58 " 'value'," \
59 " 40 + 19 * abs( $pi - azimuth( " \
60 " point_n( geometry_n($geometry, @geometry_part_num) , 1 ), " \
61 " point_n( geometry_n($geometry, @geometry_part_num) , 2 )" \
62 " ) ) " \
63 ")"
64
66 : QgsFeatureRenderer( QStringLiteral( "25dRenderer" ) )
67{
68 mSymbol.reset( new QgsFillSymbol() );
69
70 mSymbol->deleteSymbolLayer( 0 ); // We never asked for the default layer
71
73
74 QVariantMap wallProperties;
75 wallProperties.insert( QStringLiteral( "geometryModifier" ), WALL_EXPRESSION );
76 wallProperties.insert( QStringLiteral( "symbolType" ), QStringLiteral( "Fill" ) );
78
79 QVariantMap roofProperties;
80 roofProperties.insert( QStringLiteral( "geometryModifier" ), ROOF_EXPRESSION );
81 roofProperties.insert( QStringLiteral( "symbolType" ), QStringLiteral( "Fill" ) );
83
84 floor->setLocked( true );
85
86 mSymbol->appendSymbolLayer( floor );
87 mSymbol->appendSymbolLayer( walls );
88 mSymbol->appendSymbolLayer( roof );
89
90 QgsEffectStack *effectStack = new QgsEffectStack();
91 QgsOuterGlowEffect *glowEffect = new QgsOuterGlowEffect();
92 glowEffect->setBlurLevel( 5 );
94 effectStack->appendEffect( glowEffect );
95 floor->setPaintEffect( effectStack );
96
97 // These methods must only be used after the above initialization!
98
99 setRoofColor( QColor( 177, 169, 124 ) );
100 setWallColor( QColor( 119, 119, 119 ) );
101
103
104 setShadowSpread( 4 );
105 setShadowColor( QColor( 17, 17, 17 ) );
106
110 false );
111
113 setOrderByEnabled( true );
114}
115
116QDomElement Qgs25DRenderer::save( QDomDocument &doc, const QgsReadWriteContext &context )
117{
118 QDomElement rendererElem = doc.createElement( RENDERER_TAG_NAME );
119
120 rendererElem.setAttribute( QStringLiteral( "type" ), QStringLiteral( "25dRenderer" ) );
121
122 const QDomElement symbolElem = QgsSymbolLayerUtils::saveSymbol( QStringLiteral( "symbol" ), mSymbol.get(), doc, context );
123
124 saveRendererData( doc, rendererElem, context );
125
126 rendererElem.appendChild( symbolElem );
127
128 return rendererElem;
129}
130
131QgsFeatureRenderer *Qgs25DRenderer::create( QDomElement &element, const QgsReadWriteContext &context )
132{
133 Qgs25DRenderer *renderer = new Qgs25DRenderer();
134
135 const QDomNodeList symbols = element.elementsByTagName( QStringLiteral( "symbol" ) );
136 if ( symbols.size() )
137 {
138 renderer->mSymbol.reset( QgsSymbolLayerUtils::loadSymbol( symbols.at( 0 ).toElement(), context ) );
139 }
140
141 return renderer;
142}
143
145{
146 QgsFeatureRenderer::startRender( context, fields );
147
148 mSymbol->startRender( context, fields );
149}
150
152{
154
155 mSymbol->stopRender( context );
156}
157
158QSet<QString> Qgs25DRenderer::usedAttributes( const QgsRenderContext &context ) const
159{
160 return mSymbol->usedAttributes( context );
161}
162
164{
166 c->mSymbol.reset( mSymbol->clone() );
167 return c;
168}
169
171{
172 Q_UNUSED( feature )
173 Q_UNUSED( context )
174 return mSymbol.get();
175}
176
178{
179 Q_UNUSED( context )
180 QgsSymbolList lst;
181 lst.append( mSymbol.get() );
182 return lst;
183}
184
186{
187 if ( mSymbol )
188 {
189 QgsStyleSymbolEntity entity( mSymbol.get() );
190 return visitor->visit( QgsStyleEntityVisitorInterface::StyleLeaf( &entity ) );
191 }
192 return true;
193}
194
195QgsFillSymbolLayer *Qgs25DRenderer::roofLayer() const
196{
197 return static_cast<QgsFillSymbolLayer *>( mSymbol->symbolLayer( 2 )->subSymbol()->symbolLayer( 0 ) );
198}
199
200QgsFillSymbolLayer *Qgs25DRenderer::wallLayer() const
201{
202 return static_cast<QgsFillSymbolLayer *>( mSymbol->symbolLayer( 1 )->subSymbol()->symbolLayer( 0 ) );
203}
204
205QgsOuterGlowEffect *Qgs25DRenderer::glowEffect() const
206{
207 QgsEffectStack *stack = static_cast<QgsEffectStack *>( mSymbol->symbolLayer( 0 )->paintEffect() );
208 return static_cast<QgsOuterGlowEffect *>( stack->effect( 0 ) );
209}
210
212{
213 return glowEffect()->enabled();
214}
215
216void Qgs25DRenderer::setShadowEnabled( bool value ) const
217{
218 glowEffect()->setEnabled( value );
219}
220
222{
223 return glowEffect()->color();
224}
225
226void Qgs25DRenderer::setShadowColor( const QColor &shadowColor ) const
227{
228 glowEffect()->setColor( shadowColor );
229}
230
232{
233 return glowEffect()->spread();
234}
235
236void Qgs25DRenderer::setShadowSpread( double spread ) const
237{
238 glowEffect()->setSpread( spread );
239}
240
242{
243 return wallLayer()->fillColor();
244}
245
246void Qgs25DRenderer::setWallColor( const QColor &wallColor ) const
247{
248 wallLayer()->setFillColor( wallColor );
249 wallLayer()->setStrokeColor( wallColor );
250}
251
253{
255}
256
261
263{
264 return roofLayer()->fillColor();
265}
266
267void Qgs25DRenderer::setRoofColor( const QColor &roofColor ) const
268{
269 roofLayer()->setFillColor( roofColor );
270 roofLayer()->setStrokeColor( roofColor );
271}
272
274{
275 if ( renderer->type() == QLatin1String( "25dRenderer" ) )
276 {
277 return static_cast<Qgs25DRenderer *>( renderer->clone() );
278 }
279 else
280 {
281 std::unique_ptr< Qgs25DRenderer > res = std::make_unique< Qgs25DRenderer >();
282 renderer->copyRendererData( res.get() );
283 return res.release();
284 }
285}
286
@ MapUnits
Map units.
QgsSymbolList symbols(QgsRenderContext &context) const override
Returns list of symbols used by the renderer.
QgsSymbol * symbolForFeature(const QgsFeature &feature, QgsRenderContext &context) const override
To be overridden.
void stopRender(QgsRenderContext &context) override
Must be called when a render cycle has finished, to allow the renderer to clean up.
void setShadowSpread(double shadowSpread) const
Set the shadow's spread distance in map units.
QColor roofColor() const
Gets the roof color.
void setWallShadingEnabled(bool enabled) const
Set wall shading enabled.
QColor shadowColor() const
Gets the shadow's color.
QgsFeatureRenderer * clone() const override
Create a deep copy of this renderer.
static Qgs25DRenderer * convertFromRenderer(QgsFeatureRenderer *renderer)
Try to convert from an existing renderer.
double shadowSpread() const
Gets the shadow's spread distance in map units.
void startRender(QgsRenderContext &context, const QgsFields &fields) override
Must be called when a new render cycle is started.
bool accept(QgsStyleEntityVisitorInterface *visitor) const override
Accepts the specified symbology visitor, causing it to visit all symbols associated with the renderer...
static QgsFeatureRenderer * create(QDomElement &element, const QgsReadWriteContext &context)
Create a new 2.5D renderer from XML.
void setShadowColor(const QColor &shadowColor) const
Set the shadow's color.
QSet< QString > usedAttributes(const QgsRenderContext &context) const override
Returns a list of attributes required by this renderer.
bool wallShadingEnabled() const
Gets wall shading enabled.
bool shadowEnabled() const
Is the shadow enabled.
void setRoofColor(const QColor &roofColor) const
Set the roof color.
QDomElement save(QDomDocument &doc, const QgsReadWriteContext &context) override
Stores renderer properties to an XML element.
void setWallColor(const QColor &wallColor) const
Set the wall color.
QColor wallColor() const
Gets the wall color.
void setShadowEnabled(bool value) const
Enable or disable the shadow.
A paint effect which consists of a stack of other chained paint effects.
void appendEffect(QgsPaintEffect *effect)
Appends an effect to the end of the stack.
QgsPaintEffect * effect(int index) const
Returns a pointer to the effect at a specified index within the stack.
void setOrderBy(const QgsFeatureRequest::OrderBy &orderBy)
Define the order in which features shall be processed by this renderer.
void setOrderByEnabled(bool enabled)
Sets whether custom ordering should be applied before features are processed by this renderer.
virtual void stopRender(QgsRenderContext &context)
Must be called when a render cycle has finished, to allow the renderer to clean up.
QString type() const
void copyRendererData(QgsFeatureRenderer *destRenderer) const
Clones generic renderer data to another renderer.
void saveRendererData(QDomDocument &doc, QDomElement &element, const QgsReadWriteContext &context)
Saves generic renderer data into the specified element.
virtual void startRender(QgsRenderContext &context, const QgsFields &fields)
Must be called when a new render cycle is started.
QgsFeatureRequest::OrderBy orderBy() const
Gets the order in which features shall be processed by this renderer.
virtual QgsFeatureRenderer * clone() const =0
Create a deep copy of this renderer.
The OrderByClause class represents an order by clause for a QgsFeatureRequest.
Represents a list of OrderByClauses, with the most important first and the least important last.
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
Definition qgsfeature.h:56
Container of fields for a vector layer.
Definition qgsfields.h:45
A fill symbol type, for rendering Polygon and MultiPolygon geometries.
static QgsSymbolLayer * create(const QVariantMap &properties)
Creates the symbol layer.
void setSpread(const double spread)
Sets the spread distance for drawing the glow effect.
double spread() const
Returns the spread distance used for drawing the glow effect.
QColor color() const
Returns the color for the glow.
void setBlurLevel(const double level)
Sets blur level (radius) for the glow.
void setSpreadUnit(const Qgis::RenderUnit unit)
Sets the units used for the glow spread distance.
void setColor(const QColor &color)
Sets the color for the glow.
A paint effect which draws a glow outside of a picture.
void setEnabled(bool enabled)
Sets whether the effect is enabled.
bool enabled() const
Returns whether the effect is enabled.
QgsProperty property(int key) const override
Returns a matching property from the collection, if one exists.
static QgsProperty fromExpression(const QString &expression, bool isActive=true)
Returns a new ExpressionBasedProperty created from the specified expression.
bool isActive() const
Returns whether the property is currently active.
void setActive(bool active)
Sets whether the property is currently active.
The class is used as a container of context for various read/write operations on other objects.
Contains information about the context of a rendering operation.
static QgsSymbolLayer * create(const QVariantMap &properties=QVariantMap())
Creates a new QgsSimpleFillSymbolLayer using the specified properties map containing symbol propertie...
An interface for classes which can visit style entity (e.g.
virtual bool visit(const QgsStyleEntityVisitorInterface::StyleLeaf &entity)
Called when the visitor will visit a style entity.
A symbol entity for QgsStyle databases.
Definition qgsstyle.h:1341
static 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.
@ PropertyFillColor
Fill color.
virtual QColor fillColor() const
Returns the fill color for the symbol layer.
virtual void setStrokeColor(const QColor &color)
Sets the stroke color for the symbol layer.
void setPaintEffect(QgsPaintEffect *effect)
Sets the current paint effect for the layer.
virtual void setFillColor(const QColor &color)
Sets the fill color for the symbol layer.
virtual void setDataDefinedProperty(Property key, const QgsProperty &property)
Sets a data defined property for the layer.
void setLocked(bool locked)
Sets whether the layer's colors are locked.
QgsPropertyCollection & dataDefinedProperties()
Returns a reference to the symbol layer's property collection, used for data defined overrides.
Abstract base class for all rendered symbols.
Definition qgssymbol.h:94
As part of the API refactoring and improvements which landed in the Processing API was substantially reworked from the x version This was done in order to allow much of the underlying Processing framework to be ported into c
#define WALL_SHADING_EXPRESSION
#define ORDER_BY_EXPRESSION
#define WALL_EXPRESSION
#define ROOF_EXPRESSION
#define RENDERER_TAG_NAME
Definition qgsrenderer.h:50
QList< QgsSymbol * > QgsSymbolList
Definition qgsrenderer.h:44
Contains information relating to the style entity currently being visited.