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