QGIS API Documentation 4.0.0-Norrköping (1ddcee3d0e4)
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"
17
18#include <memory>
19
20#include "qgseffectstack.h"
21#include "qgsfillsymbol.h"
22#include "qgsfillsymbollayer.h"
24#include "qgsgloweffect.h"
25#include "qgspainteffect.h"
26#include "qgsproperty.h"
28#include "qgssymbollayerutils.h"
29
30#include <QString>
31
32using namespace Qt::StringLiterals;
33
34#define ROOF_EXPRESSION \
35 "translate(" \
36 " @geometry," \
37 " cos( radians( eval( @qgis_25d_angle ) ) ) * eval( @qgis_25d_height )," \
38 " sin( radians( eval( @qgis_25d_angle ) ) ) * eval( @qgis_25d_height )" \
39 ")"
40
41#define WALL_EXPRESSION \
42 "order_parts( " \
43 " extrude(" \
44 " segments_to_lines( @geometry )," \
45 " cos( radians( eval( @qgis_25d_angle ) ) ) * eval( @qgis_25d_height )," \
46 " sin( radians( eval( @qgis_25d_angle ) ) ) * eval( @qgis_25d_height )" \
47 " )," \
48 " '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 + " \
49 "180 ) ) ))'," \
50 " False" \
51 ")"
52
53#define ORDER_BY_EXPRESSION \
54 "distance(" \
55 " @geometry," \
56 " translate(" \
57 " @map_extent_center," \
58 " 1000 * @map_extent_width * cos( radians( @qgis_25d_angle + 180 ) )," \
59 " 1000 * @map_extent_width * sin( radians( @qgis_25d_angle + 180 ) )" \
60 " )" \
61 ")"
62
63#define WALL_SHADING_EXPRESSION \
64 "set_color_part( " \
65 " @symbol_color," \
66 " 'value'," \
67 " 40 + 19 * abs( $pi - azimuth( " \
68 " point_n( geometry_n(@geometry, @geometry_part_num) , 1 ), " \
69 " point_n( geometry_n(@geometry, @geometry_part_num) , 2 )" \
70 " ) ) " \
71 ")"
72
74 : QgsFeatureRenderer( u"25dRenderer"_s )
75{
76 mSymbol = std::make_unique<QgsFillSymbol>();
77
78 mSymbol->deleteSymbolLayer( 0 ); // We never asked for the default layer
79
81
82 QVariantMap wallProperties;
83 wallProperties.insert( u"geometryModifier"_s, WALL_EXPRESSION );
84 wallProperties.insert( u"symbolType"_s, u"Fill"_s );
86
87 QVariantMap roofProperties;
88 roofProperties.insert( u"geometryModifier"_s, ROOF_EXPRESSION );
89 roofProperties.insert( u"symbolType"_s, u"Fill"_s );
91
92 floor->setLocked( true );
93
94 mSymbol->appendSymbolLayer( floor );
95 mSymbol->appendSymbolLayer( walls );
96 mSymbol->appendSymbolLayer( roof );
97
98 QgsEffectStack *effectStack = new QgsEffectStack();
99 QgsOuterGlowEffect *glowEffect = new QgsOuterGlowEffect();
100 glowEffect->setBlurLevel( 5 );
101 glowEffect->setSpreadUnit( Qgis::RenderUnit::MapUnits );
102 effectStack->appendEffect( glowEffect );
103 floor->setPaintEffect( effectStack );
104
105 // These methods must only be used after the above initialization!
106
107 setRoofColor( QColor( 177, 169, 124 ) );
108 setWallColor( QColor( 119, 119, 119 ) );
109
110 wallLayer()->setDataDefinedProperty( QgsSymbolLayer::Property::FillColor, QgsProperty::fromExpression( QString( WALL_SHADING_EXPRESSION ) ) );
111
112 setShadowSpread( 4 );
113 setShadowColor( QColor( 17, 17, 17 ) );
114
117
119 setOrderByEnabled( true );
120}
121
122QDomElement Qgs25DRenderer::save( QDomDocument &doc, const QgsReadWriteContext &context )
123{
124 QDomElement rendererElem = doc.createElement( RENDERER_TAG_NAME );
125
126 rendererElem.setAttribute( u"type"_s, u"25dRenderer"_s );
127
128 const QDomElement symbolElem = QgsSymbolLayerUtils::saveSymbol( u"symbol"_s, mSymbol.get(), doc, context );
129
130 saveRendererData( doc, rendererElem, context );
131
132 rendererElem.appendChild( symbolElem );
133
134 return rendererElem;
135}
136
138{
140 if ( mSymbol && mSymbol->flags().testFlag( Qgis::SymbolFlag::AffectsLabeling ) )
142
143 return res;
144}
145
146QgsFeatureRenderer *Qgs25DRenderer::create( QDomElement &element, const QgsReadWriteContext &context )
147{
148 Qgs25DRenderer *renderer = new Qgs25DRenderer();
149
150 const QDomNodeList symbols = element.elementsByTagName( u"symbol"_s );
151 if ( symbols.size() )
152 {
153 renderer->mSymbol = QgsSymbolLayerUtils::loadSymbol( symbols.at( 0 ).toElement(), context );
154 }
155
156 return renderer;
157}
158
160{
161 QgsFeatureRenderer::startRender( context, fields );
162
163 mSymbol->startRender( context, fields );
164}
165
167{
169
170 mSymbol->stopRender( context );
171}
172
173QSet<QString> Qgs25DRenderer::usedAttributes( const QgsRenderContext &context ) const
174{
175 return mSymbol->usedAttributes( context );
176}
177
179{
181 c->mSymbol.reset( mSymbol->clone() );
182 return c;
183}
184
186{
187 Q_UNUSED( feature )
188 Q_UNUSED( context )
189 return mSymbol.get();
190}
191
193{
194 Q_UNUSED( context )
195 QgsSymbolList lst;
196 lst.append( mSymbol.get() );
197 return lst;
198}
199
201{
202 if ( mSymbol )
203 {
204 QgsStyleSymbolEntity entity( mSymbol.get() );
205 return visitor->visit( QgsStyleEntityVisitorInterface::StyleLeaf( &entity ) );
206 }
207 return true;
208}
209
210QgsFillSymbolLayer *Qgs25DRenderer::roofLayer() const
211{
212 return static_cast<QgsFillSymbolLayer *>( mSymbol->symbolLayer( 2 )->subSymbol()->symbolLayer( 0 ) );
213}
214
215QgsFillSymbolLayer *Qgs25DRenderer::wallLayer() const
216{
217 return static_cast<QgsFillSymbolLayer *>( mSymbol->symbolLayer( 1 )->subSymbol()->symbolLayer( 0 ) );
218}
219
220QgsOuterGlowEffect *Qgs25DRenderer::glowEffect() const
221{
222 QgsEffectStack *stack = static_cast<QgsEffectStack *>( mSymbol->symbolLayer( 0 )->paintEffect() );
223 return static_cast<QgsOuterGlowEffect *>( stack->effect( 0 ) );
224}
225
227{
228 return glowEffect()->enabled();
229}
230
231void Qgs25DRenderer::setShadowEnabled( bool value ) const
232{
233 glowEffect()->setEnabled( value );
234}
235
237{
238 return glowEffect()->color();
239}
240
242{
243 glowEffect()->setColor( shadowColor );
244}
245
247{
248 return glowEffect()->spread();
249}
250
251void Qgs25DRenderer::setShadowSpread( double spread ) const
252{
253 glowEffect()->setSpread( spread );
254}
255
257{
258 return wallLayer()->fillColor();
259}
260
261void Qgs25DRenderer::setWallColor( const QColor &wallColor ) const
262{
263 wallLayer()->setFillColor( wallColor );
264 wallLayer()->setStrokeColor( wallColor );
265}
266
268{
269 wallLayer()->dataDefinedProperties().property( QgsSymbolLayer::Property::FillColor ).setActive( enabled );
270}
271
273{
274 return wallLayer()->dataDefinedProperties().property( QgsSymbolLayer::Property::FillColor ).isActive();
275}
276
278{
279 return roofLayer()->fillColor();
280}
281
282void Qgs25DRenderer::setRoofColor( const QColor &roofColor ) const
283{
284 roofLayer()->setFillColor( roofColor );
285 roofLayer()->setStrokeColor( roofColor );
286}
287
289{
290 if ( renderer->type() == "25dRenderer"_L1 )
291 {
292 return static_cast<Qgs25DRenderer *>( renderer->clone() );
293 }
294 else
295 {
296 auto res = std::make_unique< Qgs25DRenderer >();
297 renderer->copyRendererData( res.get() );
298 return res.release();
299 }
300}
QFlags< FeatureRendererFlag > FeatureRendererFlags
Flags controlling behavior of vector feature renderers.
Definition qgis.h:864
@ AffectsLabeling
If present, indicates that the renderer will participate in the map labeling problem.
Definition qgis.h:855
@ MapUnits
Map units.
Definition qgis.h:5342
@ AffectsLabeling
If present, indicates that the symbol will participate in the map labeling problem.
Definition qgis.h:875
Qgis::FeatureRendererFlags flags() const override
Returns flags associated with the renderer.
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.
QgsPaintEffect * paintEffect() const
Returns the current paint effect for the renderer.
QgsFeatureRenderer(const QString &type)
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:60
Container of fields for a vector layer.
Definition qgsfields.h:46
Abstract base class for fill symbol layers.
static QgsSymbolLayer * create(const QVariantMap &properties)
Creates the symbol layer.
A paint effect which draws a glow outside of a picture.
bool enabled() const
Returns whether the effect is enabled.
static QgsProperty fromExpression(const QString &expression, bool isActive=true)
Returns a new ExpressionBasedProperty created from the specified expression.
A container for the context for various read/write operations on 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:1393
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.
Abstract base class for symbol layers.
void setPaintEffect(QgsPaintEffect *effect)
Sets the current paint effect for the layer.
void setLocked(bool locked)
Sets whether the layer's colors are locked.
Abstract base class for all rendered symbols.
Definition qgssymbol.h:227
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:57
QList< QgsSymbol * > QgsSymbolList
Definition qgsrenderer.h:51
Contains information relating to the style entity currently being visited.