QGIS API Documentation  3.20.0-Odense (decaadbb31)
qgssinglesymbolrenderer.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgssinglesymbolrenderer.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 
17 
18 #include "qgssymbol.h"
19 #include "qgssymbollayerutils.h"
20 
22 #include "qgslogger.h"
23 #include "qgsfeature.h"
24 #include "qgsvectorlayer.h"
25 #include "qgssymbollayer.h"
26 #include "qgsogcutils.h"
29 #include "qgspainteffect.h"
30 #include "qgspainteffectregistry.h"
31 #include "qgsproperty.h"
32 #include "qgsstyleentityvisitor.h"
33 #include "qgslinesymbol.h"
34 #include "qgsmarkersymbol.h"
35 #include "qgsfillsymbol.h"
36 
37 #include <QDomDocument>
38 #include <QDomElement>
39 
41  : QgsFeatureRenderer( QStringLiteral( "singleSymbol" ) )
42  , mSymbol( symbol )
43 {
44  Q_ASSERT( symbol );
45 }
46 
48 
50 {
51  return mSymbol.get();
52 }
53 
55 {
56  Q_UNUSED( context )
57  Q_UNUSED( feature )
58  return mSymbol.get();
59 }
60 
62 {
63  QgsFeatureRenderer::startRender( context, fields );
64 
65  if ( !mSymbol )
66  return;
67 
68  mSymbol->startRender( context, fields );
69 }
70 
72 {
74 
75  if ( !mSymbol )
76  return;
77 
78  mSymbol->stopRender( context );
79 }
80 
81 QSet<QString> QgsSingleSymbolRenderer::usedAttributes( const QgsRenderContext &context ) const
82 {
83  QSet<QString> attributes;
84  if ( mSymbol )
85  attributes.unite( mSymbol->usedAttributes( context ) );
86  return attributes;
87 }
88 
90 {
91  if ( mSymbol )
92  {
93  QgsStyleSymbolEntity entity( mSymbol.get() );
94  return visitor->visit( QgsStyleEntityVisitorInterface::StyleLeaf( &entity ) );
95  }
96  return true;
97 }
98 
100 {
101  return mSymbol.get();
102 }
103 
105 {
106  Q_ASSERT( s );
107  mSymbol.reset( s );
108 }
109 
111 {
112  return mSymbol ? QStringLiteral( "SINGLE: %1" ).arg( mSymbol->dump() ) : QString();
113 }
114 
116 {
119  copyRendererData( r );
120  return r;
121 }
122 
123 void QgsSingleSymbolRenderer::toSld( QDomDocument &doc, QDomElement &element, const QVariantMap &props ) const
124 {
125  QVariantMap newProps = props;
126 
127  QDomElement ruleElem = doc.createElement( QStringLiteral( "se:Rule" ) );
128  element.appendChild( ruleElem );
129 
130  QDomElement nameElem = doc.createElement( QStringLiteral( "se:Name" ) );
131  nameElem.appendChild( doc.createTextNode( QStringLiteral( "Single symbol" ) ) );
132  ruleElem.appendChild( nameElem );
133 
134  QgsSymbolLayerUtils::applyScaleDependency( doc, ruleElem, newProps );
135 
136  if ( mSymbol ) mSymbol->toSld( doc, ruleElem, newProps );
137 }
138 
140 {
141  Q_UNUSED( context )
142  QgsSymbolList lst;
143  lst.append( mSymbol.get() );
144  return lst;
145 }
146 
147 
149 {
150  QDomElement symbolsElem = element.firstChildElement( QStringLiteral( "symbols" ) );
151  if ( symbolsElem.isNull() )
152  return nullptr;
153 
154  QgsSymbolMap symbolMap = QgsSymbolLayerUtils::loadSymbols( symbolsElem, context );
155 
156  if ( !symbolMap.contains( QStringLiteral( "0" ) ) )
157  return nullptr;
158 
159  QgsSingleSymbolRenderer *r = new QgsSingleSymbolRenderer( symbolMap.take( QStringLiteral( "0" ) ) );
160 
161  // delete symbols if there are any more
163 
164  QDomElement rotationElem = element.firstChildElement( QStringLiteral( "rotation" ) );
165  if ( !rotationElem.isNull() && !rotationElem.attribute( QStringLiteral( "field" ) ).isEmpty() )
166  {
167  convertSymbolRotation( r->mSymbol.get(), rotationElem.attribute( QStringLiteral( "field" ) ) );
168  }
169 
170  QDomElement sizeScaleElem = element.firstChildElement( QStringLiteral( "sizescale" ) );
171  if ( !sizeScaleElem.isNull() && !sizeScaleElem.attribute( QStringLiteral( "field" ) ).isEmpty() )
172  {
174  QgsSymbolLayerUtils::decodeScaleMethod( sizeScaleElem.attribute( QStringLiteral( "scalemethod" ) ) ),
175  sizeScaleElem.attribute( QStringLiteral( "field" ) ) );
176  }
177 
178  QDomElement ddsLegendSizeElem = element.firstChildElement( QStringLiteral( "data-defined-size-legend" ) );
179  if ( !ddsLegendSizeElem.isNull() )
180  {
181  r->mDataDefinedSizeLegend.reset( QgsDataDefinedSizeLegend::readXml( ddsLegendSizeElem, context ) );
182  }
183 
184  // TODO: symbol levels
185  return r;
186 }
187 
189 {
190  // XXX this renderer can handle only one Rule!
191 
192  // get the first Rule element
193  QDomElement ruleElem = element.firstChildElement( QStringLiteral( "Rule" ) );
194  if ( ruleElem.isNull() )
195  {
196  QgsDebugMsg( QStringLiteral( "no Rule elements found!" ) );
197  return nullptr;
198  }
199 
200  QString label, description;
201  QgsSymbolLayerList layers;
202 
203  // retrieve the Rule element child nodes
204  QDomElement childElem = ruleElem.firstChildElement();
205  while ( !childElem.isNull() )
206  {
207  if ( childElem.localName() == QLatin1String( "Name" ) )
208  {
209  // <se:Name> tag contains the rule identifier,
210  // so prefer title tag for the label property value
211  if ( label.isEmpty() )
212  label = childElem.firstChild().nodeValue();
213  }
214  else if ( childElem.localName() == QLatin1String( "Description" ) )
215  {
216  // <se:Description> can contains a title and an abstract
217  QDomElement titleElem = childElem.firstChildElement( QStringLiteral( "Title" ) );
218  if ( !titleElem.isNull() )
219  {
220  label = titleElem.firstChild().nodeValue();
221  }
222 
223  QDomElement abstractElem = childElem.firstChildElement( QStringLiteral( "Abstract" ) );
224  if ( !abstractElem.isNull() )
225  {
226  description = abstractElem.firstChild().nodeValue();
227  }
228  }
229  else if ( childElem.localName() == QLatin1String( "Abstract" ) )
230  {
231  // <sld:Abstract> (v1.0)
232  description = childElem.firstChild().nodeValue();
233  }
234  else if ( childElem.localName() == QLatin1String( "Title" ) )
235  {
236  // <sld:Title> (v1.0)
237  label = childElem.firstChild().nodeValue();
238  }
239  else if ( childElem.localName().endsWith( QLatin1String( "Symbolizer" ) ) )
240  {
241  // create symbol layers for this symbolizer
242  QgsSymbolLayerUtils::createSymbolLayerListFromSld( childElem, geomType, layers );
243  }
244 
245  childElem = childElem.nextSiblingElement();
246  }
247 
248  if ( layers.isEmpty() )
249  return nullptr;
250 
251  // now create the symbol
252  std::unique_ptr< QgsSymbol > symbol;
253  switch ( geomType )
254  {
256  symbol = std::make_unique< QgsLineSymbol >( layers );
257  break;
258 
260  symbol = std::make_unique< QgsFillSymbol >( layers );
261  break;
262 
264  symbol = std::make_unique< QgsMarkerSymbol >( layers );
265  break;
266 
267  default:
268  QgsDebugMsg( QStringLiteral( "invalid geometry type: found %1" ).arg( geomType ) );
269  return nullptr;
270  }
271 
272  // and finally return the new renderer
273  return new QgsSingleSymbolRenderer( symbol.release() );
274 }
275 
276 QDomElement QgsSingleSymbolRenderer::save( QDomDocument &doc, const QgsReadWriteContext &context )
277 {
278  QDomElement rendererElem = doc.createElement( RENDERER_TAG_NAME );
279  rendererElem.setAttribute( QStringLiteral( "type" ), QStringLiteral( "singleSymbol" ) );
280  rendererElem.setAttribute( QStringLiteral( "symbollevels" ), ( mUsingSymbolLevels ? QStringLiteral( "1" ) : QStringLiteral( "0" ) ) );
281  rendererElem.setAttribute( QStringLiteral( "forceraster" ), ( mForceRaster ? QStringLiteral( "1" ) : QStringLiteral( "0" ) ) );
282 
284  symbols[QStringLiteral( "0" )] = mSymbol.get();
285  QDomElement symbolsElem = QgsSymbolLayerUtils::saveSymbols( symbols, QStringLiteral( "symbols" ), doc, context );
286  rendererElem.appendChild( symbolsElem );
287 
288  QDomElement rotationElem = doc.createElement( QStringLiteral( "rotation" ) );
289  rendererElem.appendChild( rotationElem );
290 
291  QDomElement sizeScaleElem = doc.createElement( QStringLiteral( "sizescale" ) );
292  rendererElem.appendChild( sizeScaleElem );
293 
295  mPaintEffect->saveProperties( doc, rendererElem );
296 
297  if ( !mOrderBy.isEmpty() )
298  {
299  QDomElement orderBy = doc.createElement( QStringLiteral( "orderby" ) );
300  mOrderBy.save( orderBy );
301  rendererElem.appendChild( orderBy );
302  }
303  rendererElem.setAttribute( QStringLiteral( "enableorderby" ), ( mOrderByEnabled ? QStringLiteral( "1" ) : QStringLiteral( "0" ) ) );
304 
306  {
307  QDomElement ddsLegendElem = doc.createElement( QStringLiteral( "data-defined-size-legend" ) );
308  mDataDefinedSizeLegend->writeXml( ddsLegendElem, context );
309  rendererElem.appendChild( ddsLegendElem );
310  }
311 
312  return rendererElem;
313 }
314 
316 {
318  {
319  const QgsMarkerSymbol *symbol = static_cast<const QgsMarkerSymbol *>( mSymbol.get() );
320  QgsProperty sizeDD( symbol->dataDefinedSize() );
321  if ( sizeDD && sizeDD.isActive() )
322  {
324  ddSizeLegend.updateFromSymbolAndProperty( static_cast<const QgsMarkerSymbol *>( mSymbol.get() ), sizeDD );
325  return ddSizeLegend.legendSymbolList();
326  }
327  }
328 
330  lst << QgsLegendSymbolItem( mSymbol.get(), QString(), QStringLiteral( "0" ) );
331  return lst;
332 }
333 
334 QSet< QString > QgsSingleSymbolRenderer::legendKeysForFeature( const QgsFeature &feature, QgsRenderContext &context ) const
335 {
336  Q_UNUSED( feature )
337  Q_UNUSED( context )
338  return QSet< QString >() << QStringLiteral( "0" );
339 }
340 
341 void QgsSingleSymbolRenderer::setLegendSymbolItem( const QString &key, QgsSymbol *symbol )
342 {
343  Q_UNUSED( key )
344  setSymbol( symbol );
345 }
346 
348 {
349  QgsSingleSymbolRenderer *r = nullptr;
350  if ( renderer->type() == QLatin1String( "singleSymbol" ) )
351  {
352  r = dynamic_cast<QgsSingleSymbolRenderer *>( renderer->clone() );
353  }
354  else if ( renderer->type() == QLatin1String( "pointDisplacement" ) || renderer->type() == QLatin1String( "pointCluster" ) )
355  {
356  const QgsPointDistanceRenderer *pointDistanceRenderer = dynamic_cast<const QgsPointDistanceRenderer *>( renderer );
357  if ( pointDistanceRenderer )
358  r = convertFromRenderer( pointDistanceRenderer->embeddedRenderer() );
359  }
360  else if ( renderer->type() == QLatin1String( "invertedPolygonRenderer" ) )
361  {
362  const QgsInvertedPolygonRenderer *invertedPolygonRenderer = dynamic_cast<const QgsInvertedPolygonRenderer *>( renderer );
363  if ( invertedPolygonRenderer )
364  r = convertFromRenderer( invertedPolygonRenderer->embeddedRenderer() );
365  }
366 
367  if ( !r )
368  {
369  QgsRenderContext context;
370  QgsSymbolList symbols = const_cast<QgsFeatureRenderer *>( renderer )->symbols( context );
371  if ( !symbols.isEmpty() )
372  {
373  r = new QgsSingleSymbolRenderer( symbols.at( 0 )->clone() );
374  }
375  }
376 
377  if ( r )
378  {
379  r->setOrderBy( renderer->orderBy() );
380  r->setOrderByEnabled( renderer->orderByEnabled() );
381  }
382 
383  return r;
384 }
385 
387 {
388  mDataDefinedSizeLegend.reset( settings );
389 }
390 
392 {
393  return mDataDefinedSizeLegend.get();
394 }
@ Marker
Marker symbol.
Object that keeps configuration of appearance of marker symbol's data-defined size in legend.
static QgsDataDefinedSizeLegend * readXml(const QDomElement &elem, const QgsReadWriteContext &context) SIP_FACTORY
Creates instance from given element and returns it (caller takes ownership). Returns nullptr on error...
void updateFromSymbolAndProperty(const QgsMarkerSymbol *symbol, const QgsProperty &ddSize)
Updates the list of classes, source symbol and title label from given symbol and property.
QgsLegendSymbolList legendSymbolList() const
Generates legend symbol items according to the configuration.
void setOrderBy(const QgsFeatureRequest::OrderBy &orderBy)
Define the order in which features shall be processed by this renderer.
virtual QgsFeatureRenderer * clone() const =0
Create a deep copy of this renderer.
void setOrderByEnabled(bool enabled)
Sets whether custom ordering should be applied before features are processed by this renderer.
virtual void stopRender(QgsRenderContext &context)
Must be called when a render cycle has finished, to allow the renderer to clean up.
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
bool orderByEnabled() const
Returns whether custom ordering will be applied before features are processed by this renderer.
static void convertSymbolRotation(QgsSymbol *symbol, const QString &field)
QgsFeatureRequest::OrderBy mOrderBy
Definition: qgsrenderer.h:553
virtual void startRender(QgsRenderContext &context, const QgsFields &fields)
Must be called when a new render cycle is started.
Definition: qgsrenderer.cpp:94
static void convertSymbolSizeScale(QgsSymbol *symbol, Qgis::ScaleMethod method, const QString &field)
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.
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
Definition: qgsfeature.h:56
Container of fields for a vector layer.
Definition: qgsfields.h:45
QgsInvertedPolygonRenderer is a polygon-only feature renderer used to display features inverted,...
The class stores information about one class/rule of a vector layer renderer in a unified way that ca...
A marker symbol type, for rendering Point and MultiPoint geometries.
const QgsFeatureRenderer * embeddedRenderer() const override
Returns the current embedded renderer (subrenderer) for this feature renderer.
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.
An abstract base class for distance based point renderers (e.g., clusterer and displacement renderers...
const QgsFeatureRenderer * embeddedRenderer() const override
Returns the current embedded renderer (subrenderer) for this feature renderer.
A store for object properties.
Definition: qgsproperty.h:232
bool isActive() const
Returns whether the property is currently active.
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.
QSet< QString > usedAttributes(const QgsRenderContext &context) const override
Returns a list of attributes required by this renderer.
QDomElement save(QDomDocument &doc, const QgsReadWriteContext &context) override
store renderer info to XML element
QgsSymbol * symbol() const
Returns the symbol which will be rendered for every feature.
void setSymbol(QgsSymbol *s)
Sets the symbol which will be rendered for every feature.
std::unique_ptr< QgsSymbol > mSymbol
bool accept(QgsStyleEntityVisitorInterface *visitor) const override
Accepts the specified symbology visitor, causing it to visit all symbols associated with the renderer...
void setLegendSymbolItem(const QString &key, QgsSymbol *symbol) override
Sets the symbol to be used for a legend symbol item.
static QgsFeatureRenderer * create(QDomElement &element, const QgsReadWriteContext &context)
Creates a new single symbol renderer from an XML element, using the supplied read/write context.
~QgsSingleSymbolRenderer() override
QSet< QString > legendKeysForFeature(const QgsFeature &feature, QgsRenderContext &context) const override
Returns legend keys matching a specified feature.
void setDataDefinedSizeLegend(QgsDataDefinedSizeLegend *settings)
Configures appearance of legend when renderer is configured to use data-defined size for marker symbo...
static QgsFeatureRenderer * createFromSld(QDomElement &element, QgsWkbTypes::GeometryType geomType)
Creates a new single symbol renderer from an SLD element.
void stopRender(QgsRenderContext &context) override
Must be called when a render cycle has finished, to allow the renderer to clean up.
QgsLegendSymbolList legendSymbolItems() const override
Returns a list of symbology items for the legend.
void startRender(QgsRenderContext &context, const QgsFields &fields) override
Must be called when a new render cycle is started.
QgsDataDefinedSizeLegend * dataDefinedSizeLegend() const
Returns configuration of appearance of legend when using data-defined size for marker symbols.
std::unique_ptr< QgsDataDefinedSizeLegend > mDataDefinedSizeLegend
QgsSymbol * originalSymbolForFeature(const QgsFeature &feature, QgsRenderContext &context) const override
Returns symbol for feature.
static QgsSingleSymbolRenderer * convertFromRenderer(const QgsFeatureRenderer *renderer)
Creates a new single symbol renderer from an existing renderer.
QgsSymbol * symbolForFeature(const QgsFeature &feature, QgsRenderContext &context) const override
To be overridden.
void toSld(QDomDocument &doc, QDomElement &element, const QVariantMap &props=QVariantMap()) const override
used from subclasses to create SLD Rule elements following SLD v1.1 specs
QgsSingleSymbolRenderer * clone() const override
Create a deep copy of this renderer.
QgsSymbolList symbols(QgsRenderContext &context) const override
Returns list of symbols used by the renderer.
QString dump() const override
Returns debug information about this renderer.
QgsSingleSymbolRenderer(QgsSymbol *symbol)
Constructor for QgsSingleSymbolRenderer.
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 void applyScaleDependency(QDomDocument &doc, QDomElement &ruleElem, QVariantMap &props)
Checks if the properties contain scaleMinDenom and scaleMaxDenom, if available, they are added into t...
static Qgis::ScaleMethod decodeScaleMethod(const QString &str)
Decodes a symbol scale method from a string.
static bool createSymbolLayerListFromSld(QDomElement &element, QgsWkbTypes::GeometryType geomType, QList< QgsSymbolLayer * > &layers)
Creates a symbol layer list from a DOM element.
static void clearSymbolMap(QgsSymbolMap &symbols)
static QgsSymbolMap loadSymbols(QDomElement &element, const QgsReadWriteContext &context)
Reads a collection of symbols from XML and returns them in a map. Caller is responsible for deleting ...
static QDomElement saveSymbols(QgsSymbolMap &symbols, const QString &tagName, QDomDocument &doc, const QgsReadWriteContext &context)
Writes a collection of symbols to XML with specified tagName for the top-level element.
Abstract base class for all rendered symbols.
Definition: qgssymbol.h:38
GeometryType
The geometry types are used to group QgsWkbTypes::Type in a coarse way.
Definition: qgswkbtypes.h:141
QList< QgsLegendSymbolItem > QgsLegendSymbolList
#define QgsDebugMsg(str)
Definition: qgslogger.h:38
#define RENDERER_TAG_NAME
Definition: qgsrenderer.h:49
QMap< QString, QgsSymbol * > QgsSymbolMap
Definition: qgsrenderer.h:44
QList< QgsSymbol * > QgsSymbolList
Definition: qgsrenderer.h:43
QList< QgsSymbolLayer * > QgsSymbolLayerList
Definition: qgssymbol.h:27
Contains information relating to the style entity currently being visited.