QGIS API Documentation  3.0.2-Girona (307d082)
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 
24 #define ROOF_EXPRESSION \
25  "translate(" \
26  " $geometry," \
27  " cos( radians( eval( @qgis_25d_angle ) ) ) * eval( @qgis_25d_height )," \
28  " sin( radians( eval( @qgis_25d_angle ) ) ) * eval( @qgis_25d_height )" \
29  ")"
30 
31 #define WALL_EXPRESSION \
32  "order_parts( "\
33  " extrude(" \
34  " segments_to_lines( $geometry )," \
35  " cos( radians( eval( @qgis_25d_angle ) ) ) * eval( @qgis_25d_height )," \
36  " sin( radians( eval( @qgis_25d_angle ) ) ) * eval( @qgis_25d_height )" \
37  " )," \
38  " '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 ) ) ))'," \
39  " False" \
40  ")"
41 
42 #define ORDER_BY_EXPRESSION \
43  "distance(" \
44  " $geometry," \
45  " translate(" \
46  " @map_extent_center," \
47  " 1000 * @map_extent_width * cos( radians( @qgis_25d_angle + 180 ) )," \
48  " 1000 * @map_extent_width * sin( radians( @qgis_25d_angle + 180 ) )" \
49  " )" \
50  ")"
51 
52 #define WALL_SHADING_EXPRESSION \
53  "set_color_part( " \
54  " @symbol_color," \
55  " 'value'," \
56  " 40 + 19 * abs( $pi - azimuth( " \
57  " point_n( geometry_n($geometry, @geometry_part_num) , 1 ), " \
58  " point_n( geometry_n($geometry, @geometry_part_num) , 2 )" \
59  " ) ) " \
60  ")"
61 
63  : QgsFeatureRenderer( QStringLiteral( "25dRenderer" ) )
64 {
65  mSymbol.reset( new QgsFillSymbol() );
66 
67  mSymbol->deleteSymbolLayer( 0 ); // We never asked for the default layer
68 
70 
71  QgsStringMap wallProperties;
72  wallProperties.insert( QStringLiteral( "geometryModifier" ), WALL_EXPRESSION );
73  wallProperties.insert( QStringLiteral( "symbolType" ), QStringLiteral( "Fill" ) );
75 
76  QgsStringMap roofProperties;
77  roofProperties.insert( QStringLiteral( "geometryModifier" ), ROOF_EXPRESSION );
78  roofProperties.insert( QStringLiteral( "symbolType" ), QStringLiteral( "Fill" ) );
80 
81  floor->setLocked( true );
82 
83  mSymbol->appendSymbolLayer( floor );
84  mSymbol->appendSymbolLayer( walls );
85  mSymbol->appendSymbolLayer( roof );
86 
87  QgsEffectStack *effectStack = new QgsEffectStack();
88  QgsOuterGlowEffect *glowEffect = new QgsOuterGlowEffect();
89  glowEffect->setBlurLevel( 5 );
91  effectStack->appendEffect( glowEffect );
92  floor->setPaintEffect( effectStack );
93 
94  // These methods must only be used after the above initialization!
95 
96  setRoofColor( QColor( 177, 169, 124 ) );
97  setWallColor( QColor( 119, 119, 119 ) );
98 
100 
101  setShadowSpread( 4 );
102  setShadowColor( QColor( 17, 17, 17 ) );
103 
107  false );
108 
109  setOrderBy( orderBy );
110  setOrderByEnabled( true );
111 }
112 
113 QDomElement Qgs25DRenderer::save( QDomDocument &doc, const QgsReadWriteContext &context )
114 {
115  QDomElement rendererElem = doc.createElement( RENDERER_TAG_NAME );
116 
117  rendererElem.setAttribute( QStringLiteral( "type" ), QStringLiteral( "25dRenderer" ) );
118 
119  QDomElement symbolElem = QgsSymbolLayerUtils::saveSymbol( QStringLiteral( "symbol" ), mSymbol.get(), doc, context );
120 
121  rendererElem.appendChild( symbolElem );
122 
123  return rendererElem;
124 }
125 
126 QgsFeatureRenderer *Qgs25DRenderer::create( QDomElement &element, const QgsReadWriteContext &context )
127 {
128  Qgs25DRenderer *renderer = new Qgs25DRenderer();
129 
130  QDomNodeList symbols = element.elementsByTagName( QStringLiteral( "symbol" ) );
131  if ( symbols.size() )
132  {
133  renderer->mSymbol.reset( QgsSymbolLayerUtils::loadSymbol( symbols.at( 0 ).toElement(), context ) );
134  }
135 
136  return renderer;
137 }
138 
140 {
141  QgsFeatureRenderer::startRender( context, fields );
142 
143  mSymbol->startRender( context, fields );
144 }
145 
147 {
149 
150  mSymbol->stopRender( context );
151 }
152 
153 QSet<QString> Qgs25DRenderer::usedAttributes( const QgsRenderContext &context ) const
154 {
155  return mSymbol->usedAttributes( context );
156 }
157 
159 {
160  Qgs25DRenderer *c = new Qgs25DRenderer();
161  c->mSymbol.reset( mSymbol->clone() );
162  return c;
163 }
164 
166 {
167  Q_UNUSED( feature )
168  Q_UNUSED( context )
169  return mSymbol.get();
170 }
171 
173 {
174  Q_UNUSED( context );
175  QgsSymbolList lst;
176  lst.append( mSymbol.get() );
177  return lst;
178 }
179 
180 QgsFillSymbolLayer *Qgs25DRenderer::roofLayer() const
181 {
182  return static_cast<QgsFillSymbolLayer *>( mSymbol->symbolLayer( 2 )->subSymbol()->symbolLayer( 0 ) );
183 }
184 
185 QgsFillSymbolLayer *Qgs25DRenderer::wallLayer() const
186 {
187  return static_cast<QgsFillSymbolLayer *>( mSymbol->symbolLayer( 1 )->subSymbol()->symbolLayer( 0 ) );
188 }
189 
190 QgsOuterGlowEffect *Qgs25DRenderer::glowEffect() const
191 {
192  QgsEffectStack *stack = static_cast<QgsEffectStack *>( mSymbol->symbolLayer( 0 )->paintEffect() );
193  return static_cast<QgsOuterGlowEffect *>( stack->effect( 0 ) );
194 }
195 
197 {
198  return glowEffect()->enabled();
199 }
200 
202 {
203  glowEffect()->setEnabled( value );
204 }
205 
207 {
208  return glowEffect()->color();
209 }
210 
212 {
213  glowEffect()->setColor( shadowColor );
214 }
215 
217 {
218  return glowEffect()->spread();
219 }
220 
221 void Qgs25DRenderer::setShadowSpread( double spread )
222 {
223  glowEffect()->setSpread( spread );
224 }
225 
227 {
228  return wallLayer()->fillColor();
229 }
230 
232 {
233  wallLayer()->setFillColor( wallColor );
234  wallLayer()->setStrokeColor( wallColor );
235 }
236 
238 {
240 }
241 
243 {
245 }
246 
248 {
249  return roofLayer()->fillColor();
250 }
251 
253 {
254  roofLayer()->setFillColor( roofColor );
255  roofLayer()->setStrokeColor( roofColor );
256 }
257 
259 {
260  if ( renderer->type() == QLatin1String( "25dRenderer" ) )
261  {
262  return static_cast<Qgs25DRenderer *>( renderer->clone() );
263  }
264  else
265  {
266  return new Qgs25DRenderer();
267  }
268 }
269 
The class is used as a container of context for various read/write operations on other objects...
void stopRender(QgsRenderContext &context) override
Must be called when a render cycle has finished, to allow the renderer to clean up.
void setEnabled(const bool enabled)
Sets whether the effect is enabled.
QDomElement save(QDomDocument &doc, const QgsReadWriteContext &context) override
store renderer info to XML element
void setLocked(bool locked)
void startRender(QgsRenderContext &context, const QgsFields &fields) override
Must be called when a new render cycle is started.
QColor wallColor() const
Get the wall color.
QgsSymbolList symbols(QgsRenderContext &context) override
Returns list of symbols used by the renderer.
QgsFeatureRequest::OrderBy orderBy() const
Get the order in which features shall be processed by this renderer.
QgsFeatureRenderer * clone() const override
Create a deep copy of this renderer.
static QgsProperty fromExpression(const QString &expression, bool isActive=true)
Returns a new ExpressionBasedProperty created from the specified expression.
Container of fields for a vector layer.
Definition: qgsfields.h:42
#define RENDERER_TAG_NAME
Definition: qgsrenderer.h:49
#define WALL_SHADING_EXPRESSION
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
Definition: qgsfeature.h:62
static QgsSymbol * loadSymbol(const QDomElement &element, const QgsReadWriteContext &context)
Attempts to load a symbol from a DOM element.
QMap< QString, QString > QgsStringMap
Definition: qgis.h:479
void setBlurLevel(const int level)
Sets blur level (strength) for the glow.
bool shadowEnabled() const
Is the shadow enabled.
QList< QgsSymbol * > QgsSymbolList
Definition: qgsrenderer.h:43
QString type() const
Definition: qgsrenderer.h:126
void setShadowSpread(double shadowSpread)
Set the shadow&#39;s spread distance in map units.
QColor shadowColor() const
Get the shadow&#39;s color.
QgsPaintEffect * effect(int index) const
Returns a pointer to the effect at a specified index within the stack.
void setSpreadUnit(const QgsUnitTypes::RenderUnit unit)
Sets the units used for the glow spread distance.
Definition: qgsgloweffect.h:82
void setActive(bool active)
Sets whether the property is currently active.
static Qgs25DRenderer * convertFromRenderer(QgsFeatureRenderer *renderer)
Try to convert from an existing renderer.
#define ORDER_BY_EXPRESSION
QgsPropertyCollection & dataDefinedProperties()
Returns a reference to the symbol layer&#39;s property collection, used for data defined overrides...
A paint effect which consists of a stack of other chained paint effects.
void setPaintEffect(QgsPaintEffect *effect)
Sets the current paint effect for the layer.
bool enabled() const
Returns whether the effect is enabled.
void setSpread(const double spread)
Sets the spread distance for drawing the glow effect.
Definition: qgsgloweffect.h:64
void appendEffect(QgsPaintEffect *effect)
Appends an effect to the end of the stack.
#define ROOF_EXPRESSION
virtual void setStrokeColor(const QColor &color)
Set stroke color.
void setOrderBy(const QgsFeatureRequest::OrderBy &orderBy)
Define the order in which features shall be processed by this renderer.
A paint effect which draws a glow outside of a picture.
The OrderByClause class represents an order by clause for a QgsFeatureRequest.
QgsSymbol * symbolForFeature(QgsFeature &feature, QgsRenderContext &context) override
To be overridden.
QSet< QString > usedAttributes(const QgsRenderContext &context) const override
Return a list of attributes required by this renderer.
Contains information about the context of a rendering operation.
virtual void setFillColor(const QColor &color)
Set fill color.
double spread() const
Returns the spread distance used for drawing the glow effect.
Definition: qgsgloweffect.h:73
static QgsFeatureRenderer * create(QDomElement &element, const QgsReadWriteContext &context)
Create a new 2.5D renderer from XML.
static QgsSymbolLayer * create(const QgsStringMap &properties=QgsStringMap())
static QgsSymbolLayer * create(const QgsStringMap &properties)
virtual void startRender(QgsRenderContext &context, const QgsFields &fields)
Must be called when a new render cycle is started.
Definition: qgsrenderer.cpp:92
#define WALL_EXPRESSION
void setRoofColor(const QColor &roofColor)
Set the roof color.
bool wallShadingEnabled() const
Get wall shading enabled.
virtual void stopRender(QgsRenderContext &context)
Must be called when a render cycle has finished, to allow the renderer to clean up.
void setShadowEnabled(bool value)
Enable or disable the shadow.
void setWallShadingEnabled(bool enabled)
Set wall shading enabled.
static QDomElement saveSymbol(const QString &symbolName, QgsSymbol *symbol, QDomDocument &doc, const QgsReadWriteContext &context)
Writes a symbol definition to XML.
QColor roofColor() const
Get the roof color.
double shadowSpread() const
Get the shadow&#39;s spread distance in map units.
virtual QColor fillColor() const
Get fill color.
void setShadowColor(const QColor &shadowColor)
Set the shadow&#39;s color.
void setColor(const QColor &color)
Sets the color for the glow.
void setWallColor(const QColor &wallColor)
Set the wall color.
void setOrderByEnabled(bool enabled)
Sets whether custom ordering should be applied before features are processed by this renderer...
virtual QgsFeatureRenderer * clone() const =0
Create a deep copy of this renderer.
bool isActive() const
Returns whether the property is currently active.
QgsProperty property(int key) const override
Returns a matching property from the collection, if one exists.
virtual void setDataDefinedProperty(Property key, const QgsProperty &property)
Sets a data defined property for the layer.
Represents a list of OrderByClauses, with the most important first and the least important last...
QColor color() const
Returns the color for the glow.