QGIS API Documentation  3.26.3-Buenos Aires (65e4edfdad)
qgsembeddedsymbolrenderer.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsembeddedsymbolrenderer.cpp
3  ---------------------
4  begin : March 2021
5  copyright : (C) 2021 by Nyall Dawson
6  email : nyall dot dawson 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 #include "qgspainteffectregistry.h"
18 #include "qgssymbollayerutils.h"
20 #include "qgssymbol.h"
21 #include "qgspainteffect.h"
22 
24  : QgsFeatureRenderer( QStringLiteral( "embeddedSymbol" ) )
25  , mDefaultSymbol( defaultSymbol )
26 {
27  Q_ASSERT( mDefaultSymbol );
28 }
29 
31 
33 {
34  if ( feature.embeddedSymbol() )
35  return const_cast< QgsSymbol * >( feature.embeddedSymbol() );
36  else
37  return mDefaultSymbol.get();
38 }
39 
41 {
42  Q_UNUSED( context )
43  if ( feature.embeddedSymbol() )
44  return const_cast< QgsSymbol * >( feature.embeddedSymbol() );
45  else
46  return mDefaultSymbol.get();
47 }
48 
50 {
51  QgsFeatureRenderer::startRender( context, fields );
52 
53  mDefaultSymbol->startRender( context, fields );
54 }
55 
56 bool QgsEmbeddedSymbolRenderer::renderFeature( const QgsFeature &feature, QgsRenderContext &context, int layer, bool selected, bool drawVertexMarker )
57 {
58  if ( const QgsSymbol *symbol = feature.embeddedSymbol() )
59  {
60  std::unique_ptr< QgsSymbol > clone( symbol->clone() );
61 
62  clone->startRender( context );
63  renderFeatureWithSymbol( feature, clone.get(), context, layer, selected, drawVertexMarker );
64  clone->stopRender( context );
65  }
66  else
67  {
68  renderFeatureWithSymbol( feature, mDefaultSymbol.get(), context, layer, selected, drawVertexMarker );
69  }
70  return true;
71 }
72 
74 {
76  mDefaultSymbol->stopRender( context );
77 }
78 
79 QSet<QString> QgsEmbeddedSymbolRenderer::usedAttributes( const QgsRenderContext &context ) const
80 {
81  QSet<QString> attributes;
82  if ( mDefaultSymbol )
83  attributes.unite( mDefaultSymbol->usedAttributes( context ) );
84  return attributes;
85 }
86 
88 {
89  return true;
90 }
91 
93 {
94  QgsEmbeddedSymbolRenderer *r = new QgsEmbeddedSymbolRenderer( mDefaultSymbol->clone() );
95  copyRendererData( r );
96  return r;
97 }
98 
99 QgsFeatureRenderer::Capabilities QgsEmbeddedSymbolRenderer::capabilities()
100 {
101  return SymbolLevels;
102 }
103 
105 {
106  QDomElement symbolsElem = element.firstChildElement( QStringLiteral( "symbols" ) );
107  if ( symbolsElem.isNull() )
108  return nullptr;
109 
110  QgsSymbolMap symbolMap = QgsSymbolLayerUtils::loadSymbols( symbolsElem, context );
111 
112  if ( !symbolMap.contains( QStringLiteral( "0" ) ) )
113  return nullptr;
114 
115  QgsEmbeddedSymbolRenderer *r = new QgsEmbeddedSymbolRenderer( symbolMap.take( QStringLiteral( "0" ) ) );
116  return r;
117 }
118 
120 {
121  if ( renderer->type() == QLatin1String( "embeddedSymbol" ) )
122  {
123  return dynamic_cast<QgsEmbeddedSymbolRenderer *>( renderer->clone() );
124  }
125  else if ( renderer->type() == QLatin1String( "singleSymbol" ) )
126  {
127  std::unique_ptr< QgsEmbeddedSymbolRenderer > symbolRenderer = std::make_unique< QgsEmbeddedSymbolRenderer >( static_cast< const QgsSingleSymbolRenderer * >( renderer )->symbol()->clone() );
128  renderer->copyRendererData( symbolRenderer.get() );
129  return symbolRenderer.release();
130  }
131  else
132  {
133  return nullptr;
134  }
135 }
136 
137 QDomElement QgsEmbeddedSymbolRenderer::save( QDomDocument &doc, const QgsReadWriteContext &context )
138 {
139  QDomElement rendererElem = doc.createElement( RENDERER_TAG_NAME );
140  rendererElem.setAttribute( QStringLiteral( "type" ), QStringLiteral( "embeddedSymbol" ) );
141 
143  symbols[QStringLiteral( "0" )] = mDefaultSymbol.get();
144  const QDomElement symbolsElem = QgsSymbolLayerUtils::saveSymbols( symbols, QStringLiteral( "symbols" ), doc, context );
145  rendererElem.appendChild( symbolsElem );
146 
147  saveRendererData( doc, rendererElem, context );
148 
149  return rendererElem;
150 }
151 
153 {
154  Q_UNUSED( context )
155  QgsSymbolList lst;
156  lst.append( mDefaultSymbol.get() );
157  return lst;
158 }
159 
161 {
162  return mDefaultSymbol.get();
163 }
164 
166 {
167  Q_ASSERT( symbol );
168  mDefaultSymbol.reset( symbol );
169 }
QgsFeatureRenderer::copyRendererData
void copyRendererData(QgsFeatureRenderer *destRenderer) const
Clones generic renderer data to another renderer.
Definition: qgsrenderer.cpp:52
QgsEmbeddedSymbolRenderer::clone
QgsEmbeddedSymbolRenderer * clone() const override
Create a deep copy of this renderer.
Definition: qgsembeddedsymbolrenderer.cpp:92
QgsEmbeddedSymbolRenderer::capabilities
QgsFeatureRenderer::Capabilities capabilities() override
Returns details about internals of this renderer.
Definition: qgsembeddedsymbolrenderer.cpp:99
QgsSymbolLayerUtils::loadSymbols
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 ...
Definition: qgssymbollayerutils.cpp:3189
QgsReadWriteContext
The class is used as a container of context for various read/write operations on other objects.
Definition: qgsreadwritecontext.h:34
qgssinglesymbolrenderer.h
QgsSymbolMap
QMap< QString, QgsSymbol * > QgsSymbolMap
Definition: qgsrenderer.h:45
qgssymbollayerutils.h
QgsFields
Container of fields for a vector layer.
Definition: qgsfields.h:44
QgsFeatureRenderer::renderFeatureWithSymbol
void renderFeatureWithSymbol(const QgsFeature &feature, QgsSymbol *symbol, QgsRenderContext &context, int layer, bool selected, bool drawVertexMarker) SIP_THROW(QgsCsException)
Render the feature with the symbol using context.
Definition: qgsrenderer.cpp:141
QgsFeatureRenderer::type
QString type() const
Definition: qgsrenderer.h:142
QgsRenderContext
Contains information about the context of a rendering operation.
Definition: qgsrendercontext.h:59
QgsEmbeddedSymbolRenderer::renderFeature
bool renderFeature(const QgsFeature &feature, QgsRenderContext &context, int layer=-1, bool selected=false, bool drawVertexMarker=false) SIP_THROW(QgsCsException) override
Render a feature using this renderer in the given context.
Definition: qgsembeddedsymbolrenderer.cpp:56
QgsSingleSymbolRenderer
Definition: qgssinglesymbolrenderer.h:29
QgsEmbeddedSymbolRenderer::symbols
QgsSymbolList symbols(QgsRenderContext &context) const override
Returns list of symbols used by the renderer.
Definition: qgsembeddedsymbolrenderer.cpp:152
qgspainteffectregistry.h
QgsEmbeddedSymbolRenderer::stopRender
void stopRender(QgsRenderContext &context) override
Must be called when a render cycle has finished, to allow the renderer to clean up.
Definition: qgsembeddedsymbolrenderer.cpp:73
QgsSymbol
Abstract base class for all rendered symbols.
Definition: qgssymbol.h:92
QgsEmbeddedSymbolRenderer::~QgsEmbeddedSymbolRenderer
~QgsEmbeddedSymbolRenderer() override
qgspainteffect.h
QgsFeatureRenderer::stopRender
virtual void stopRender(QgsRenderContext &context)
Must be called when a render cycle has finished, to allow the renderer to clean up.
Definition: qgsrenderer.cpp:110
QgsFeatureRenderer::SymbolLevels
@ SymbolLevels
Rendering with symbol levels (i.e. implements symbols(), symbolForFeature())
Definition: qgsrenderer.h:263
QgsFeature::embeddedSymbol
const QgsSymbol * embeddedSymbol() const
Returns the feature's embedded symbology, or nullptr if the feature has no embedded symbol.
Definition: qgsfeature.cpp:313
QgsEmbeddedSymbolRenderer::create
static QgsFeatureRenderer * create(QDomElement &element, const QgsReadWriteContext &context)
Creates a new embedded symbol renderer from an XML element, using the supplied read/write context.
Definition: qgsembeddedsymbolrenderer.cpp:104
QgsEmbeddedSymbolRenderer::usedAttributes
QSet< QString > usedAttributes(const QgsRenderContext &context) const override
Returns a list of attributes required by this renderer.
Definition: qgsembeddedsymbolrenderer.cpp:79
QgsEmbeddedSymbolRenderer::usesEmbeddedSymbols
bool usesEmbeddedSymbols() const override
Returns true if the renderer uses embedded symbols for features.
Definition: qgsembeddedsymbolrenderer.cpp:87
QgsSymbolList
QList< QgsSymbol * > QgsSymbolList
Definition: qgsrenderer.h:44
QgsFeatureRenderer::saveRendererData
void saveRendererData(QDomDocument &doc, QDomElement &element, const QgsReadWriteContext &context)
Saves generic renderer data into the specified element.
Definition: qgsrenderer.cpp:204
QgsFeatureRenderer::clone
virtual QgsFeatureRenderer * clone() const =0
Create a deep copy of this renderer.
QgsEmbeddedSymbolRenderer::convertFromRenderer
static QgsEmbeddedSymbolRenderer * convertFromRenderer(const QgsFeatureRenderer *renderer)
Creates a QgsEmbeddedSymbolRenderer from an existing renderer.
Definition: qgsembeddedsymbolrenderer.cpp:119
QgsEmbeddedSymbolRenderer::save
QDomElement save(QDomDocument &doc, const QgsReadWriteContext &context) override
Stores renderer properties to an XML element.
Definition: qgsembeddedsymbolrenderer.cpp:137
RENDERER_TAG_NAME
#define RENDERER_TAG_NAME
Definition: qgsrenderer.h:50
QgsEmbeddedSymbolRenderer::QgsEmbeddedSymbolRenderer
QgsEmbeddedSymbolRenderer(QgsSymbol *defaultSymbol)
Constructor for QgsEmbeddedSymbolRenderer.
Definition: qgsembeddedsymbolrenderer.cpp:23
QgsFeatureRenderer
Definition: qgsrenderer.h:101
QgsEmbeddedSymbolRenderer
A vector feature renderer which uses embedded feature symbology to render per-feature symbols.
Definition: qgsembeddedsymbolrenderer.h:35
QgsSymbolLayerUtils::saveSymbols
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.
Definition: qgssymbollayerutils.cpp:3267
QgsEmbeddedSymbolRenderer::setDefaultSymbol
void setDefaultSymbol(QgsSymbol *symbol)
Sets the default symbol which will be rendered for any feature which does not have embedded symbology...
Definition: qgsembeddedsymbolrenderer.cpp:165
qgsembeddedsymbolrenderer.h
QgsFeature
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
Definition: qgsfeature.h:55
QgsEmbeddedSymbolRenderer::originalSymbolForFeature
QgsSymbol * originalSymbolForFeature(const QgsFeature &feature, QgsRenderContext &context) const override
Returns symbol for feature.
Definition: qgsembeddedsymbolrenderer.cpp:40
QgsEmbeddedSymbolRenderer::startRender
void startRender(QgsRenderContext &context, const QgsFields &fields) override
Must be called when a new render cycle is started.
Definition: qgsembeddedsymbolrenderer.cpp:49
QgsFeatureRenderer::startRender
virtual void startRender(QgsRenderContext &context, const QgsFields &fields)
Must be called when a new render cycle is started.
Definition: qgsrenderer.cpp:96
QgsEmbeddedSymbolRenderer::defaultSymbol
QgsSymbol * defaultSymbol() const
Returns the default symbol which will be rendered for any feature which does not have embedded symbol...
Definition: qgsembeddedsymbolrenderer.cpp:160
qgssymbol.h
QgsEmbeddedSymbolRenderer::symbolForFeature
QgsSymbol * symbolForFeature(const QgsFeature &feature, QgsRenderContext &context) const override
To be overridden.
Definition: qgsembeddedsymbolrenderer.cpp:32