QGIS API Documentation 3.41.0-Master (fda2aa46e9a)
Loading...
Searching...
No Matches
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"
22#include "qgsproperty.h"
24#include "qgsmarkersymbol.h"
25#include "qgsunittypes.h"
26
27#include <cmath>
28
30 : QgsPointDistanceRenderer( QStringLiteral( "pointCluster" ) )
31{
32 mClusterSymbol.reset( new QgsMarkerSymbol() );
33 mClusterSymbol->setSize( 4 );
34 mClusterSymbol->setColor( QColor( 245, 75, 80 ) );
35
37 fm->setFontFamily( QFont().defaultFamily() );
38 fm->setColor( QColor( 255, 255, 255 ) );
39 fm->setSize( 3.2 );
40 fm->setOffset( QPointF( 0, -0.4 ) );
42 mClusterSymbol->insertSymbolLayer( 1, fm );
43}
44
46{
48 if ( mClusterSymbol && mClusterSymbol->flags().testFlag( Qgis::SymbolFlag::AffectsLabeling ) )
52 return res;
53}
54
56{
58 if ( mRenderer )
59 r->setEmbeddedRenderer( mRenderer->clone() );
66 if ( mClusterSymbol )
67 {
68 r->setClusterSymbol( mClusterSymbol->clone() );
69 }
71 return r;
72}
73
74void QgsPointClusterRenderer::drawGroup( QPointF centerPoint, QgsRenderContext &context, const ClusteredGroup &group ) const
75{
76 if ( group.size() > 1 )
77 {
78 mClusterSymbol->renderPoint( centerPoint, &( group.at( 0 ).feature ), context, -1, false );
79 }
80 else
81 {
82 //single isolated symbol, draw it untouched
83 QgsMarkerSymbol *symbol = group.at( 0 ).symbol();
84 symbol->startRender( context );
85 symbol->renderPoint( centerPoint, &( group.at( 0 ).feature ), context, -1, group.at( 0 ).isSelected );
86 symbol->stopRender( context );
87 }
88}
89
91{
92 if ( mClusterSymbol )
93 {
94 mClusterSymbol->startRender( context, fields );
95 }
97}
98
100{
102 if ( mClusterSymbol )
103 {
104 mClusterSymbol->stopRender( context );
105 }
106}
107
108QgsFeatureRenderer *QgsPointClusterRenderer::create( QDomElement &symbologyElem, const QgsReadWriteContext &context )
109{
111 r->setTolerance( symbologyElem.attribute( QStringLiteral( "tolerance" ), QStringLiteral( "0.00001" ) ).toDouble() );
112 r->setToleranceUnit( QgsUnitTypes::decodeRenderUnit( symbologyElem.attribute( QStringLiteral( "toleranceUnit" ), QStringLiteral( "MapUnit" ) ) ) );
113 r->setToleranceMapUnitScale( QgsSymbolLayerUtils::decodeMapUnitScale( symbologyElem.attribute( QStringLiteral( "toleranceUnitScale" ) ) ) );
114
115 //look for an embedded renderer <renderer-v2>
116 QDomElement embeddedRendererElem = symbologyElem.firstChildElement( QStringLiteral( "renderer-v2" ) );
117 if ( !embeddedRendererElem.isNull() )
118 {
119 r->setEmbeddedRenderer( QgsFeatureRenderer::load( embeddedRendererElem, context ) );
120 }
121
122 //center symbol
123 const QDomElement centerSymbolElem = symbologyElem.firstChildElement( QStringLiteral( "symbol" ) );
124 if ( !centerSymbolElem.isNull() )
125 {
126 r->setClusterSymbol( QgsSymbolLayerUtils::loadSymbol<QgsMarkerSymbol>( centerSymbolElem, context ) );
127 }
128 return r;
129}
130
132{
133 return mClusterSymbol.get();
134}
135
136QDomElement QgsPointClusterRenderer::save( QDomDocument &doc, const QgsReadWriteContext &context )
137{
138 QDomElement rendererElement = doc.createElement( RENDERER_TAG_NAME );
139 rendererElement.setAttribute( QStringLiteral( "type" ), QStringLiteral( "pointCluster" ) );
140 rendererElement.setAttribute( QStringLiteral( "tolerance" ), QString::number( mTolerance ) );
141 rendererElement.setAttribute( QStringLiteral( "toleranceUnit" ), QgsUnitTypes::encodeUnit( mToleranceUnit ) );
142 rendererElement.setAttribute( QStringLiteral( "toleranceUnitScale" ), QgsSymbolLayerUtils::encodeMapUnitScale( mToleranceMapUnitScale ) );
143
144 if ( mRenderer )
145 {
146 const QDomElement embeddedRendererElem = mRenderer->save( doc, context );
147 rendererElement.appendChild( embeddedRendererElem );
148 }
149 if ( mClusterSymbol )
150 {
151 const QDomElement centerSymbolElem = QgsSymbolLayerUtils::saveSymbol( QStringLiteral( "centerSymbol" ), mClusterSymbol.get(), doc, context );
152 rendererElement.appendChild( centerSymbolElem );
153 }
154
155 saveRendererData( doc, rendererElement, context );
156
157 return rendererElement;
158}
159
161{
162 QSet<QString> attr = QgsPointDistanceRenderer::usedAttributes( context );
163 if ( mClusterSymbol )
164 attr.unite( mClusterSymbol->usedAttributes( context ) );
165 return attr;
166}
167
169{
170 if ( !QgsPointDistanceRenderer::accept( visitor ) )
171 return false;
172
173 if ( mClusterSymbol )
174 {
175 QgsStyleSymbolEntity entity( mClusterSymbol.get() );
176 if ( !visitor->visit( QgsStyleEntityVisitorInterface::StyleLeaf( &entity, QStringLiteral( "cluster" ), QObject::tr( "Cluster Symbol" ) ) ) )
177 return false;
178 }
179
180 return true;
181}
182
184{
185 mClusterSymbol.reset( symbol );
186}
187
189{
190 if ( renderer->type() == QLatin1String( "pointCluster" ) )
191 {
192 return dynamic_cast<QgsPointClusterRenderer *>( renderer->clone() );
193 }
194 else if ( renderer->type() == QLatin1String( "singleSymbol" ) ||
195 renderer->type() == QLatin1String( "categorizedSymbol" ) ||
196 renderer->type() == QLatin1String( "graduatedSymbol" ) ||
197 renderer->type() == QLatin1String( "RuleRenderer" ) )
198 {
200 pointRenderer->setEmbeddedRenderer( renderer->clone() );
201 renderer->copyRendererData( pointRenderer );
202 return pointRenderer;
203 }
204 else if ( renderer->type() == QLatin1String( "pointDisplacement" ) )
205 {
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 renderer->copyRendererData( pointRenderer );
214 return pointRenderer;
215 }
216 else
217 {
218 return nullptr;
219 }
220}
QFlags< FeatureRendererFlag > FeatureRendererFlags
Flags controlling behavior of vector feature renderers.
Definition qgis.h:772
@ AffectsLabeling
If present, indicates that the renderer will participate in the map labeling problem.
@ AffectsLabeling
If present, indicates that the symbol will participate in the map labeling problem.
Abstract base class for all 2D vector feature renderers.
QString type() const
void copyRendererData(QgsFeatureRenderer *destRenderer) const
Clones generic renderer data to another renderer.
static QgsFeatureRenderer * load(QDomElement &symbologyElem, const QgsReadWriteContext &context)
create a renderer from XML element
void saveRendererData(QDomDocument &doc, QDomElement &element, const QgsReadWriteContext &context)
Saves generic renderer data into the specified element.
virtual QgsFeatureRenderer * clone() const =0
Create a deep copy of this renderer.
Container of fields for a vector layer.
Definition qgsfields.h:46
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.
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.
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
Stores renderer properties to an 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.
Qgis::FeatureRendererFlags flags() const override
Returns flags associated with the renderer.
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.
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.
void setLabelFont(const QFont &font)
Sets the font used for labeling points.
void setToleranceUnit(Qgis::RenderUnit unit)
Sets the units for the tolerance distance.
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.
void startRender(QgsRenderContext &context, const QgsFields &fields) override
Must be called when a new render cycle is started.
const QgsMapUnitScale & toleranceMapUnitScale() const
Returns the map unit scale object for the distance tolerance.
void setEmbeddedRenderer(QgsFeatureRenderer *r) override
Sets an embedded renderer (subrenderer) for this feature renderer.
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.
Qgis::RenderUnit toleranceUnit() const
Returns the units for the tolerance distance.
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.
Qgis::RenderUnit mToleranceUnit
Unit for distance tolerance.
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:1396
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.
@ Character
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)
Sets the "representative" color for the symbol layer.
void stopRender(QgsRenderContext &context)
Ends the rendering process.
void startRender(QgsRenderContext &context, const QgsFields &fields=QgsFields())
Begins the rendering process for the symbol.
static Q_INVOKABLE Qgis::RenderUnit decodeRenderUnit(const QString &string, bool *ok=nullptr)
Decodes a render unit from a string.
static Q_INVOKABLE QString encodeUnit(Qgis::DistanceUnit unit)
Encodes a distance unit to a string.
#define RENDERER_TAG_NAME
Definition qgsrenderer.h:53
Contains information relating to the style entity currently being visited.