QGIS API Documentation  3.20.0-Odense (decaadbb31)
qgspointclusterrenderer.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgspointclusterrenderer.cpp
3  ---------------------------
4  begin : February 2016
5  copyright : (C) 2016 by Nyall Dawson
6  email : nyall dot dawson at gmail dot com
7  ***************************************************************************/
8 
9 /***************************************************************************
10  * *
11  * This program is free software; you can redistribute it and/or modify *
12  * it under the terms of the GNU General Public License as published by *
13  * the Free Software Foundation; either version 2 of the License, or *
14  * (at your option) any later version. *
15  * *
16  ***************************************************************************/
17 
20 #include "qgssymbollayerutils.h"
21 #include "qgspainteffectregistry.h"
22 #include "qgspainteffect.h"
23 #include "qgsmarkersymbollayer.h"
24 #include "qgsproperty.h"
25 #include "qgsstyleentityvisitor.h"
26 #include "qgsmarkersymbol.h"
27 
28 #include <cmath>
29 
31  : QgsPointDistanceRenderer( QStringLiteral( "pointCluster" ) )
32 {
33  mClusterSymbol.reset( new QgsMarkerSymbol() );
34  mClusterSymbol->setSize( 4 );
35  mClusterSymbol->setColor( QColor( 245, 75, 80 ) );
36 
38  fm->setFontFamily( QFont().defaultFamily() );
39  fm->setColor( QColor( 255, 255, 255 ) );
40  fm->setSize( 3.2 );
41  fm->setOffset( QPointF( 0, -0.4 ) );
43  mClusterSymbol->insertSymbolLayer( 1, fm );
44 }
45 
47 {
49  if ( mRenderer )
50  r->setEmbeddedRenderer( mRenderer->clone() );
57  if ( mClusterSymbol )
58  {
59  r->setClusterSymbol( mClusterSymbol->clone() );
60  }
61  copyRendererData( r );
62  return r;
63 }
64 
65 void QgsPointClusterRenderer::drawGroup( QPointF centerPoint, QgsRenderContext &context, const ClusteredGroup &group )
66 {
67  if ( group.size() > 1 )
68  {
69  mClusterSymbol->renderPoint( centerPoint, &( group.at( 0 ).feature ), context, -1, false );
70  }
71  else
72  {
73  //single isolated symbol, draw it untouched
74  QgsMarkerSymbol *symbol = group.at( 0 ).symbol();
75  symbol->startRender( context );
76  symbol->renderPoint( centerPoint, &( group.at( 0 ).feature ), context, -1, group.at( 0 ).isSelected );
77  symbol->stopRender( context );
78  }
79 }
80 
82 {
83  if ( mClusterSymbol )
84  {
85  mClusterSymbol->startRender( context, fields );
86  }
87  QgsPointDistanceRenderer::startRender( context, fields );
88 }
89 
91 {
93  if ( mClusterSymbol )
94  {
95  mClusterSymbol->stopRender( context );
96  }
97 }
98 
99 QgsFeatureRenderer *QgsPointClusterRenderer::create( QDomElement &symbologyElem, const QgsReadWriteContext &context )
100 {
102  r->setTolerance( symbologyElem.attribute( QStringLiteral( "tolerance" ), QStringLiteral( "0.00001" ) ).toDouble() );
103  r->setToleranceUnit( QgsUnitTypes::decodeRenderUnit( symbologyElem.attribute( QStringLiteral( "toleranceUnit" ), QStringLiteral( "MapUnit" ) ) ) );
104  r->setToleranceMapUnitScale( QgsSymbolLayerUtils::decodeMapUnitScale( symbologyElem.attribute( QStringLiteral( "toleranceUnitScale" ) ) ) );
105 
106  //look for an embedded renderer <renderer-v2>
107  QDomElement embeddedRendererElem = symbologyElem.firstChildElement( QStringLiteral( "renderer-v2" ) );
108  if ( !embeddedRendererElem.isNull() )
109  {
110  r->setEmbeddedRenderer( QgsFeatureRenderer::load( embeddedRendererElem, context ) );
111  }
112 
113  //center symbol
114  QDomElement centerSymbolElem = symbologyElem.firstChildElement( QStringLiteral( "symbol" ) );
115  if ( !centerSymbolElem.isNull() )
116  {
117  r->setClusterSymbol( QgsSymbolLayerUtils::loadSymbol<QgsMarkerSymbol>( centerSymbolElem, context ) );
118  }
119  return r;
120 }
121 
123 {
124  return mClusterSymbol.get();
125 }
126 
127 QDomElement QgsPointClusterRenderer::save( QDomDocument &doc, const QgsReadWriteContext &context )
128 {
129  QDomElement rendererElement = doc.createElement( RENDERER_TAG_NAME );
130  rendererElement.setAttribute( QStringLiteral( "forceraster" ), ( mForceRaster ? QStringLiteral( "1" ) : QStringLiteral( "0" ) ) );
131  rendererElement.setAttribute( QStringLiteral( "type" ), QStringLiteral( "pointCluster" ) );
132  rendererElement.setAttribute( QStringLiteral( "tolerance" ), QString::number( mTolerance ) );
133  rendererElement.setAttribute( QStringLiteral( "toleranceUnit" ), QgsUnitTypes::encodeUnit( mToleranceUnit ) );
134  rendererElement.setAttribute( QStringLiteral( "toleranceUnitScale" ), QgsSymbolLayerUtils::encodeMapUnitScale( mToleranceMapUnitScale ) );
135 
136  if ( mRenderer )
137  {
138  QDomElement embeddedRendererElem = mRenderer->save( doc, context );
139  rendererElement.appendChild( embeddedRendererElem );
140  }
141  if ( mClusterSymbol )
142  {
143  QDomElement centerSymbolElem = QgsSymbolLayerUtils::saveSymbol( QStringLiteral( "centerSymbol" ), mClusterSymbol.get(), doc, context );
144  rendererElement.appendChild( centerSymbolElem );
145  }
146 
148  mPaintEffect->saveProperties( doc, rendererElement );
149 
150  if ( !mOrderBy.isEmpty() )
151  {
152  QDomElement orderBy = doc.createElement( QStringLiteral( "orderby" ) );
153  mOrderBy.save( orderBy );
154  rendererElement.appendChild( orderBy );
155  }
156  rendererElement.setAttribute( QStringLiteral( "enableorderby" ), ( mOrderByEnabled ? QStringLiteral( "1" ) : QStringLiteral( "0" ) ) );
157 
158  return rendererElement;
159 }
160 
161 QSet<QString> QgsPointClusterRenderer::usedAttributes( const QgsRenderContext &context ) const
162 {
163  QSet<QString> attr = QgsPointDistanceRenderer::usedAttributes( context );
164  if ( mClusterSymbol )
165  attr.unite( mClusterSymbol->usedAttributes( context ) );
166  return attr;
167 }
168 
170 {
171  if ( !QgsPointDistanceRenderer::accept( visitor ) )
172  return false;
173 
174  if ( mClusterSymbol )
175  {
176  QgsStyleSymbolEntity entity( mClusterSymbol.get() );
177  if ( !visitor->visit( QgsStyleEntityVisitorInterface::StyleLeaf( &entity, QStringLiteral( "cluster" ), QObject::tr( "Cluster Symbol" ) ) ) )
178  return false;
179  }
180 
181  return true;
182 }
183 
185 {
186  mClusterSymbol.reset( symbol );
187 }
188 
190 {
191  if ( renderer->type() == QLatin1String( "pointCluster" ) )
192  {
193  return dynamic_cast<QgsPointClusterRenderer *>( renderer->clone() );
194  }
195  else if ( renderer->type() == QLatin1String( "singleSymbol" ) ||
196  renderer->type() == QLatin1String( "categorizedSymbol" ) ||
197  renderer->type() == QLatin1String( "graduatedSymbol" ) ||
198  renderer->type() == QLatin1String( "RuleRenderer" ) )
199  {
200  QgsPointClusterRenderer *pointRenderer = new QgsPointClusterRenderer();
201  pointRenderer->setEmbeddedRenderer( renderer->clone() );
202  return pointRenderer;
203  }
204  else if ( renderer->type() == QLatin1String( "pointDisplacement" ) )
205  {
206  QgsPointClusterRenderer *pointRenderer = new QgsPointClusterRenderer();
207  const QgsPointDisplacementRenderer *displacementRenderer = static_cast< const QgsPointDisplacementRenderer * >( renderer );
208  if ( displacementRenderer->embeddedRenderer() )
209  pointRenderer->setEmbeddedRenderer( displacementRenderer->embeddedRenderer()->clone() );
210  pointRenderer->setTolerance( displacementRenderer->tolerance() );
211  pointRenderer->setToleranceUnit( displacementRenderer->toleranceUnit() );
212  pointRenderer->setToleranceMapUnitScale( displacementRenderer->toleranceMapUnitScale() );
213  if ( const_cast< QgsPointDisplacementRenderer * >( displacementRenderer )->centerSymbol() )
214  pointRenderer->setClusterSymbol( const_cast< QgsPointDisplacementRenderer * >( displacementRenderer )->centerSymbol()->clone() );
215  return pointRenderer;
216  }
217  else
218  {
219  return nullptr;
220  }
221 }
virtual QgsFeatureRenderer * clone() const =0
Create a deep copy of this renderer.
QgsPaintEffect * mPaintEffect
Definition: qgsrenderer.h:537
QString type() const
Definition: qgsrenderer.h:141
void copyRendererData(QgsFeatureRenderer *destRenderer) const
Clones generic renderer data to another renderer.
Definition: qgsrenderer.cpp:52
static QgsFeatureRenderer * load(QDomElement &symbologyElem, const QgsReadWriteContext &context)
create a renderer from XML element
QgsFeatureRequest::OrderBy mOrderBy
Definition: qgsrenderer.h:553
QgsFeatureRequest::OrderBy orderBy() const
Gets the order in which features shall be processed by this renderer.
void CORE_EXPORT save(QDomElement &elem) const
Serialize to XML.
Container of fields for a vector layer.
Definition: qgsfields.h:45
void setFontFamily(const QString &family)
Sets the font family for the font which will be used to render the point.
virtual void setSize(double size)
Sets the symbol size.
void setOffset(QPointF offset)
Sets the marker's offset, which is the horizontal and vertical displacement which the rendered marker...
A marker symbol type, for rendering Point and MultiPoint geometries.
QgsMarkerSymbol * clone() const override
Returns a deep copy of this symbol.
void renderPoint(QPointF point, const QgsFeature *f, QgsRenderContext &context, int layer=-1, bool selected=false)
Renders the symbol at the specified point, using the given render context.
static bool isDefaultStack(QgsPaintEffect *effect)
Tests whether a paint effect matches the default effects stack.
virtual bool saveProperties(QDomDocument &doc, QDomElement &element) const
Saves the current state of the effect to a DOM element.
A renderer that automatically clusters points with the same geographic position.
void startRender(QgsRenderContext &context, const QgsFields &fields) override
Must be called when a new render cycle is started.
QgsPointClusterRenderer * clone() const override
Create a deep copy of this renderer.
void setClusterSymbol(QgsMarkerSymbol *symbol)
Sets the symbol for rendering clustered groups.
QDomElement save(QDomDocument &doc, const QgsReadWriteContext &context) override
store renderer info to XML element
QSet< QString > usedAttributes(const QgsRenderContext &context) const override
Returns a list of attributes required by this renderer.
static QgsFeatureRenderer * create(QDomElement &symbologyElem, const QgsReadWriteContext &context)
Creates a renderer from XML element.
bool accept(QgsStyleEntityVisitorInterface *visitor) const override
Accepts the specified symbology visitor, causing it to visit all symbols associated with the renderer...
QgsMarkerSymbol * clusterSymbol()
Returns the symbol used for rendering clustered groups (but not ownership of the symbol).
void stopRender(QgsRenderContext &context) override
Must be called when a render cycle has finished, to allow the renderer to clean up.
static QgsPointClusterRenderer * convertFromRenderer(const QgsFeatureRenderer *renderer)
Creates a QgsPointClusterRenderer from an existing renderer.
A renderer that automatically displaces points with the same geographic location.
QgsMarkerSymbol * centerSymbol()
Returns the symbol for the center of a displacement group (but not ownership of the symbol).
An abstract base class for distance based point renderers (e.g., clusterer and displacement renderers...
void setLabelColor(const QColor &color)
Sets the color to use for for labeling points.
double mMinLabelScale
Maximum scale denominator for label display. A zero value indicates no scale limitation.
QColor mLabelColor
Label text color.
QgsMapUnitScale mToleranceMapUnitScale
Map unit scale for distance tolerance.
QgsUnitTypes::RenderUnit toleranceUnit() const
Returns the units for the tolerance distance.
void setLabelFont(const QFont &font)
Sets the font used for labeling points.
void setMinimumLabelScale(double scale)
Sets the minimum map scale (i.e.
void stopRender(QgsRenderContext &context) override
Must be called when a render cycle has finished, to allow the renderer to clean up.
double tolerance() const
Returns the tolerance distance for grouping points.
const QgsMapUnitScale & toleranceMapUnitScale() const
Returns the map unit scale object for the distance tolerance.
void startRender(QgsRenderContext &context, const QgsFields &fields) override
Must be called when a new render cycle is started.
void setEmbeddedRenderer(QgsFeatureRenderer *r) override
Sets an embedded renderer (subrenderer) for this feature renderer.
void setToleranceUnit(QgsUnitTypes::RenderUnit unit)
Sets the units for the tolerance distance.
const QgsFeatureRenderer * embeddedRenderer() const override
Returns the current embedded renderer (subrenderer) for this feature renderer.
double mTolerance
Distance tolerance. Points that are closer together than this distance are considered clustered.
QSet< QString > usedAttributes(const QgsRenderContext &context) const override
Returns a list of attributes required by this renderer.
std::unique_ptr< QgsFeatureRenderer > mRenderer
Embedded base renderer. This can be used for rendering individual, isolated points.
QgsUnitTypes::RenderUnit mToleranceUnit
Unit for distance tolerance.
void setToleranceMapUnitScale(const QgsMapUnitScale &scale)
Sets the map unit scale object for the distance tolerance.
void setTolerance(double distance)
Sets the tolerance distance for grouping points.
bool accept(QgsStyleEntityVisitorInterface *visitor) const override
Accepts the specified symbology visitor, causing it to visit all symbols associated with the renderer...
static QgsProperty fromExpression(const QString &expression, bool isActive=true)
Returns a new ExpressionBasedProperty created from the specified expression.
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.
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:1219
static QString encodeMapUnitScale(const QgsMapUnitScale &mapUnitScale)
static QgsMapUnitScale decodeMapUnitScale(const QString &str)
static QDomElement saveSymbol(const QString &symbolName, const QgsSymbol *symbol, QDomDocument &doc, const QgsReadWriteContext &context)
Writes a symbol definition to XML.
@ PropertyCharacter
Character, eg for font marker symbol layers.
virtual void setDataDefinedProperty(Property key, const QgsProperty &property)
Sets a data defined property for the layer.
virtual void setColor(const QColor &color)
The fill color.
void stopRender(QgsRenderContext &context)
Ends the rendering process.
Definition: qgssymbol.cpp:516
void startRender(QgsRenderContext &context, const QgsFields &fields=QgsFields())
Begins the rendering process for the symbol.
Definition: qgssymbol.cpp:489
static Q_INVOKABLE QString encodeUnit(QgsUnitTypes::DistanceUnit unit)
Encodes a distance unit to a string.
static Q_INVOKABLE QgsUnitTypes::RenderUnit decodeRenderUnit(const QString &string, bool *ok=nullptr)
Decodes a render unit from a string.
#define RENDERER_TAG_NAME
Definition: qgsrenderer.h:49
Contains information relating to the style entity currently being visited.