QGIS API Documentation  3.20.0-Odense (decaadbb31)
qgsfillsymbol.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsfillsymbol.cpp
3  ---------------------
4  begin : November 2009
5  copyright : (C) 2009 by Martin Dobias
6  email : wonder dot sk at gmail dot com
7  ***************************************************************************
8  * *
9  * This program is free software; you can redistribute it and/or modify *
10  * it under the terms of the GNU General Public License as published by *
11  * the Free Software Foundation; either version 2 of the License, or *
12  * (at your option) any later version. *
13  * *
14  ***************************************************************************/
15 
16 #include "qgsfillsymbol.h"
17 #include "qgsfillsymbollayer.h"
18 #include "qgspainteffect.h"
19 
20 QgsFillSymbol *QgsFillSymbol::createSimple( const QVariantMap &properties )
21 {
23  if ( !sl )
24  return nullptr;
25 
26  QgsSymbolLayerList layers;
27  layers.append( sl );
28  return new QgsFillSymbol( layers );
29 }
30 
31 
33  : QgsSymbol( Qgis::SymbolType::Fill, layers )
34 {
35  if ( mLayers.isEmpty() )
36  mLayers.append( new QgsSimpleFillSymbolLayer() );
37 }
38 
39 void QgsFillSymbol::renderPolygon( const QPolygonF &points, const QVector<QPolygonF> *rings, const QgsFeature *f, QgsRenderContext &context, int layerIdx, bool selected )
40 {
42 
43  QgsSymbolRenderContext symbolContext( context, QgsUnitTypes::RenderUnknownUnit, opacity, selected, mRenderHints, f );
45  symbolContext.setGeometryPartCount( symbolRenderContext()->geometryPartCount() );
46  symbolContext.setGeometryPartNum( symbolRenderContext()->geometryPartNum() );
47 
48  if ( layerIdx != -1 )
49  {
50  QgsSymbolLayer *symbolLayer = mLayers.value( layerIdx );
52  {
54  renderPolygonUsingLayer( symbolLayer, points, rings, symbolContext );
55  else
56  renderUsingLayer( symbolLayer, symbolContext );
57  }
58  return;
59  }
60 
61  const auto constMLayers = mLayers;
62  for ( QgsSymbolLayer *symbolLayer : constMLayers )
63  {
64  if ( context.renderingStopped() )
65  break;
66 
67  if ( !symbolLayer->enabled() || !context.isSymbolLayerEnabled( symbolLayer ) )
68  continue;
69 
71  renderPolygonUsingLayer( symbolLayer, points, rings, symbolContext );
72  else
73  renderUsingLayer( symbolLayer, symbolContext );
74  }
75 }
76 
77 void QgsFillSymbol::renderPolygonUsingLayer( QgsSymbolLayer *layer, const QPolygonF &points, const QVector<QPolygonF> *rings, QgsSymbolRenderContext &context )
78 {
79  if ( layer->dataDefinedProperties().hasActiveProperties() && !layer->dataDefinedProperties().valueAsBool( QgsSymbolLayer::PropertyLayerEnabled, context.renderContext().expressionContext(), true ) )
80  return;
81 
82  Qgis::SymbolType layertype = layer->type();
83 
84  QgsPaintEffect *effect = layer->paintEffect();
85  if ( effect && effect->enabled() )
86  {
87  QRectF bounds = polygonBounds( points, rings );
88  QVector<QPolygonF> *translatedRings = translateRings( rings, -bounds.left(), -bounds.top() );
89 
90  QgsEffectPainter p( context.renderContext() );
91  p->translate( bounds.topLeft() );
92  p.setEffect( effect );
93  if ( layertype == Qgis::SymbolType::Fill )
94  {
95  ( static_cast<QgsFillSymbolLayer *>( layer ) )->renderPolygon( points.translated( -bounds.topLeft() ), translatedRings, context );
96  }
97  else if ( layertype == Qgis::SymbolType::Line )
98  {
99  ( static_cast<QgsLineSymbolLayer *>( layer ) )->renderPolygonStroke( points.translated( -bounds.topLeft() ), translatedRings, context );
100  }
101  delete translatedRings;
102  }
103  else
104  {
105  if ( layertype == Qgis::SymbolType::Fill )
106  {
107  ( static_cast<QgsFillSymbolLayer *>( layer ) )->renderPolygon( points, rings, context );
108  }
109  else if ( layertype == Qgis::SymbolType::Line )
110  {
111  ( static_cast<QgsLineSymbolLayer *>( layer ) )->renderPolygonStroke( points, rings, context );
112  }
113  }
114 }
115 
116 QRectF QgsFillSymbol::polygonBounds( const QPolygonF &points, const QVector<QPolygonF> *rings ) const
117 {
118  QRectF bounds = points.boundingRect();
119  if ( rings )
120  {
121  for ( auto it = rings->constBegin(); it != rings->constEnd(); ++it )
122  {
123  bounds = bounds.united( ( *it ).boundingRect() );
124  }
125  }
126  return bounds;
127 }
128 
129 QVector<QPolygonF> *QgsFillSymbol::translateRings( const QVector<QPolygonF> *rings, double dx, double dy ) const
130 {
131  if ( !rings )
132  return nullptr;
133 
134  QVector<QPolygonF> *translatedRings = new QVector<QPolygonF>;
135  translatedRings->reserve( rings->size() );
136  for ( auto it = rings->constBegin(); it != rings->constEnd(); ++it )
137  {
138  translatedRings->append( ( *it ).translated( dx, dy ) );
139  }
140  return translatedRings;
141 }
142 
144 {
145  QgsFillSymbol *cloneSymbol = new QgsFillSymbol( cloneLayers() );
146  cloneSymbol->setOpacity( mOpacity );
148  cloneSymbol->setLayer( mLayer );
151  cloneSymbol->setForceRHR( mForceRHR );
153  cloneSymbol->setFlags( mSymbolFlags );
154  return cloneSymbol;
155 }
156 
158 {
159  const auto constMLayers = mLayers;
160  for ( QgsSymbolLayer *layer : constMLayers )
161  {
162  if ( layer->type() != Qgis::SymbolType::Fill )
163  continue;
164 
165  QgsFillSymbolLayer *fillLayer = static_cast<QgsFillSymbolLayer *>( layer );
166 
167  if ( fillLayer )
168  fillLayer->setAngle( angle );
169  }
170 }
171 
172 
The Qgis class provides global constants for use throughout the application.
Definition: qgis.h:62
SymbolType
Symbol types.
Definition: qgis.h:168
@ Line
Line symbol.
@ Fill
Fill symbol.
double valueAsDouble(int key, const QgsExpressionContext &context, double defaultValue=0.0, bool *ok=nullptr) const
Calculates the current value of the property with the specified key and interprets it as a double.
A class to manager painter saving and restoring required for effect drawing.
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
Definition: qgsfeature.h:56
void setAngle(double angle)
A fill symbol type, for rendering Polygon and MultiPolygon geometries.
Definition: qgsfillsymbol.h:30
QgsFillSymbol(const QgsSymbolLayerList &layers=QgsSymbolLayerList())
Constructor for QgsFillSymbol, with the specified list of initial symbol layers.
static QgsFillSymbol * createSimple(const QVariantMap &properties)
Create a fill symbol with one symbol layer: SimpleFill with specified properties.
void setAngle(double angle)
void renderPolygon(const QPolygonF &points, const QVector< QPolygonF > *rings, const QgsFeature *f, QgsRenderContext &context, int layer=-1, bool selected=false)
Renders the symbol using the given render context.
QgsFillSymbol * clone() const override
Returns a deep copy of this symbol.
QgsMapLayerType type
Definition: qgsmaplayer.h:77
Base class for visual effects which can be applied to QPicture drawings.
bool enabled() const
Returns whether the effect is enabled.
Contains information about the context of a rendering operation.
QgsExpressionContext & expressionContext()
Gets the expression context.
bool isSymbolLayerEnabled(const QgsSymbolLayer *layer) const
When rendering a map layer in a second pass (for selective masking), some symbol layers may be disabl...
bool renderingStopped() const
Returns true if the rendering operation has been stopped and any ongoing rendering should be canceled...
static QgsSymbolLayer * create(const QVariantMap &properties=QVariantMap())
Creates a new QgsSimpleFillSymbolLayer using the specified properties map containing symbol propertie...
@ PropertyLayerEnabled
Whether symbol layer is enabled.
Qgis::SymbolType type() const
bool enabled() const
Returns true if symbol layer is enabled and will be drawn.
QgsRenderContext & renderContext()
Returns a reference to the context's render context.
void setGeometryPartCount(int count)
Sets the part count of current geometry.
void setGeometryPartNum(int num)
Sets the part number of current geometry.
void setOriginalGeometryType(QgsWkbTypes::GeometryType type)
Sets the geometry type for the original feature geometry being rendered.
Abstract base class for all rendered symbols.
Definition: qgssymbol.h:38
void setDataDefinedProperties(const QgsPropertyCollection &collection)
Sets the symbol's property collection, used for data defined overrides.
Definition: qgssymbol.h:558
Qgis::SymbolFlags mSymbolFlags
Symbol flags.
Definition: qgssymbol.h:716
QgsSymbolLayerList cloneLayers() const
Retrieve a cloned list of all layers that make up this symbol.
Definition: qgssymbol.cpp:767
QgsSymbolRenderContext * symbolRenderContext()
Returns the symbol render context.
Definition: qgssymbol.cpp:1433
QgsSymbolLayer * symbolLayer(int layer)
Returns the symbol layer at the specified index.
Definition: qgssymbol.cpp:420
@ PropertyOpacity
Opacity.
Definition: qgssymbol.h:76
qreal mOpacity
Symbol opacity (in the range 0 - 1)
Definition: qgssymbol.h:707
Q_DECL_DEPRECATED const QgsVectorLayer * mLayer
Definition: qgssymbol.h:721
bool mClipFeaturesToExtent
Definition: qgssymbol.h:718
qreal opacity() const
Returns the opacity for the symbol.
Definition: qgssymbol.h:440
void setFlags(Qgis::SymbolFlags flags)
Sets flags for the symbol.
Definition: qgssymbol.h:467
QgsPropertyCollection & dataDefinedProperties()
Returns a reference to the symbol's property collection, used for data defined overrides.
Definition: qgssymbol.h:543
void setOpacity(qreal opacity)
Sets the opacity for the symbol.
Definition: qgssymbol.h:447
Qgis::SymbolRenderHints mRenderHints
Definition: qgssymbol.h:709
bool mForceRHR
Definition: qgssymbol.h:719
QgsSymbolLayerList mLayers
Definition: qgssymbol.h:704
Q_DECL_DEPRECATED const QgsVectorLayer * layer() const
Definition: qgssymbol.cpp:854
void renderUsingLayer(QgsSymbolLayer *layer, QgsSymbolRenderContext &context)
Renders a context using a particular symbol layer without passing in a geometry.
Definition: qgssymbol.cpp:781
Q_DECL_DEPRECATED void setLayer(const QgsVectorLayer *layer)
Definition: qgssymbol.cpp:847
void setClipFeaturesToExtent(bool clipFeaturesToExtent)
Sets whether features drawn by the symbol should be clipped to the render context's extent.
Definition: qgssymbol.h:486
void setForceRHR(bool force)
Sets whether polygon features drawn by the symbol should be reoriented to follow the standard right-h...
Definition: qgssymbol.h:508
@ RenderUnknownUnit
Mixed or unknown units.
Definition: qgsunittypes.h:175
double ANALYSIS_EXPORT angle(QgsPoint *p1, QgsPoint *p2, QgsPoint *p3, QgsPoint *p4)
Calculates the angle between two segments (in 2 dimension, z-values are ignored)
Definition: MathUtils.cpp:786
#define Q_NOWARN_DEPRECATED_POP
Definition: qgis.h:1080
#define Q_NOWARN_DEPRECATED_PUSH
Definition: qgis.h:1079
QList< QgsSymbolLayer * > QgsSymbolLayerList
Definition: qgssymbol.h:27