QGIS API Documentation  3.14.0-Pi (9f7028fd23)
qgsmasksymbollayer.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsmasksymbollayer.cpp
3  ---------------------
4  begin : July 2019
5  copyright : (C) 2019 by Hugo Mercier
6  email : hugo dot mercier at oslandia 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 "qgsmasksymbollayer.h"
17 #include "qgssymbollayerutils.h"
18 #include "qgsproject.h"
19 #include "qgsvectorlayer.h"
20 #include "qgspainteffect.h"
21 #include "qgspainterswapper.h"
22 
24 {
25  mSymbol.reset( static_cast<QgsMarkerSymbol *>( QgsMarkerSymbol::createSimple( QgsStringMap() ) ) );
26 }
27 
29 {
30  if ( symbol && symbol->type() == QgsSymbol::Marker )
31  {
32  mSymbol.reset( static_cast<QgsMarkerSymbol *>( symbol ) );
33  return true;
34  }
35  delete symbol;
36  return false;
37 }
38 
40 {
42 
44 
45  if ( props.contains( QStringLiteral( "mask_symbollayers" ) ) )
46  {
47  l->setMasks( stringToSymbolLayerReferenceList( props[QStringLiteral( "mask_symbollayers" )] ) );
48  }
49  return l;
50 }
51 
53 {
55  l->setSubSymbol( mSymbol->clone() );
56  l->setMasks( mMaskedSymbolLayers );
58  copyPaintEffect( l );
59  return l;
60 }
61 
63 {
64  return QStringLiteral( "MaskMarker" );
65 }
66 
68 {
69  QgsStringMap props;
70  props[QStringLiteral( "mask_symbollayers" )] = symbolLayerReferenceListToString( masks() );
71  return props;
72 }
73 
74 QSet<QString> QgsMaskMarkerSymbolLayer::usedAttributes( const QgsRenderContext &context ) const
75 {
76  QSet<QString> attributes = QgsMarkerSymbolLayer::usedAttributes( context );
77 
78  attributes.unite( mSymbol->usedAttributes( context ) );
79 
80  return attributes;
81 }
82 
84 {
86  return true;
87  if ( mSymbol && mSymbol->hasDataDefinedProperties() )
88  return true;
89  return false;
90 }
91 
93 {
94  // since we need to swap the regular painter with the mask painter during rendering,
95  // effects won't work. So we cheat by handling effects ourselves in renderPoint
96  if ( paintEffect() )
97  {
98  mEffect.reset( paintEffect()->clone() );
99  setPaintEffect( nullptr );
100  }
101  mSymbol->startRender( context.renderContext() );
102 }
103 
105 {
106  mSymbol->stopRender( context.renderContext() );
107  if ( mEffect )
108  {
109  setPaintEffect( mEffect.release() );
110  }
111 }
112 
114 {
116 }
117 
119 {
120  return mSymbol->bounds( point, context.renderContext() );
121 }
122 
124 {
125  if ( !context.renderContext().painter() )
126  return;
127 
128  if ( context.renderContext().isGuiPreview() )
129  {
130  mSymbol->renderPoint( point, context.feature(), context.renderContext(), /* layer = */ -1, /* selected = */ false );
131  return;
132  }
133 
134  if ( ! context.renderContext().maskPainter() )
135  return;
136 
137  if ( mMaskedSymbolLayers.isEmpty() )
138  return;
139 
140  {
141  // Otherwise switch to the mask painter before rendering
142  QgsPainterSwapper swapper( context.renderContext(), context.renderContext().maskPainter() );
143 
144  // Special case when an effect is defined on this mask symbol layer
145  // (effects defined on sub symbol's layers do not need special handling)
146  if ( mEffect && mEffect->enabled() )
147  {
148  QgsEffectPainter p( context.renderContext() );
149  // translate operates on the mask painter, which is what we want
150  p->translate( point );
151  p.setEffect( mEffect.get() );
152  mSymbol->renderPoint( QPointF( 0, 0 ), context.feature(), context.renderContext(), /* layer = */ -1, /* selected = */ false );
153  // the translation will be canceled at the end of scope here
154  }
155  else
156  {
157  mSymbol->renderPoint( point, context.feature(), context.renderContext(), /* layer = */ -1, /* selected = */ false );
158  }
159  }
160 }
161 
162 
QgsMaskMarkerSymbolLayer::properties
QgsStringMap properties() const override
Should be reimplemented by subclasses to return a string map that contains the configuration informat...
Definition: qgsmasksymbollayer.cpp:67
QgsRenderContext::maskPainter
QPainter * maskPainter(int id=0)
Returns a mask QPainter for the render operation.
Definition: qgsrendercontext.h:195
QgsEffectPainter::setEffect
void setEffect(QgsPaintEffect *effect)
Sets the effect to be painted.
Definition: qgspainteffect.cpp:325
QgsMarkerSymbolLayer::size
double size() const
Returns the symbol size.
Definition: qgssymbollayer.h:655
QgsEffectPainter
A class to manager painter saving and restoring required for effect drawing.
Definition: qgspainteffect.h:396
QgsSymbolRenderContext::feature
const QgsFeature * feature() const
Returns the current feature being rendered.
Definition: qgssymbol.h:789
QgsMaskMarkerSymbolLayer::create
static QgsSymbolLayer * create(const QgsStringMap &properties=QgsStringMap())
Create a new QgsMaskMarkerSymbolLayer.
Definition: qgsmasksymbollayer.cpp:39
QgsMarkerSymbol::createSimple
static QgsMarkerSymbol * createSimple(const QgsStringMap &properties)
Create a marker symbol with one symbol layer: SimpleMarker with specified properties.
Definition: qgssymbol.cpp:1418
qgssymbollayerutils.h
QgsSymbolLayer::hasDataDefinedProperties
virtual bool hasDataDefinedProperties() const
Returns true if the symbol layer (or any of its sub-symbols) contains data defined properties.
Definition: qgssymbollayer.cpp:215
QgsRenderContext
Definition: qgsrendercontext.h:57
QgsMaskMarkerSymbolLayer::layerType
QString layerType() const override
Returns a string that represents this layer type.
Definition: qgsmasksymbollayer.cpp:62
QgsSymbol
Definition: qgssymbol.h:63
qgspainteffect.h
QgsMarkerSymbolLayer::drawPreviewIcon
void drawPreviewIcon(QgsSymbolRenderContext &context, QSize size) override
Definition: qgssymbollayer.cpp:453
QgsSymbolRenderContext
Definition: qgssymbol.h:681
QgsMaskMarkerSymbolLayer::QgsMaskMarkerSymbolLayer
QgsMaskMarkerSymbolLayer()
Simple constructor.
Definition: qgsmasksymbollayer.cpp:23
QgsSymbolLayer
Definition: qgssymbollayer.h:52
QgsMaskMarkerSymbolLayer::renderPoint
void renderPoint(QPointF point, QgsSymbolRenderContext &context) override
Renders a marker at the specified point.
Definition: qgsmasksymbollayer.cpp:123
QgsMaskMarkerSymbolLayer::setSubSymbol
bool setSubSymbol(QgsSymbol *symbol) override
Sets layer's subsymbol. takes ownership of the passed symbol.
Definition: qgsmasksymbollayer.cpp:28
QgsMaskMarkerSymbolLayer::hasDataDefinedProperties
bool hasDataDefinedProperties() const override
Returns true if the symbol layer (or any of its sub-symbols) contains data defined properties.
Definition: qgsmasksymbollayer.cpp:83
QgsMaskMarkerSymbolLayer::startRender
void startRender(QgsSymbolRenderContext &context) override
Called before a set of rendering operations commences on the supplied render context.
Definition: qgsmasksymbollayer.cpp:92
QgsMarkerSymbol
Definition: qgssymbol.h:917
QgsSymbolLayer::copyPaintEffect
void copyPaintEffect(QgsSymbolLayer *destLayer) const
Copies paint effect of this layer to another symbol layer.
Definition: qgssymbollayer.cpp:406
QgsMaskMarkerSymbolLayer::bounds
QRectF bounds(QPointF point, QgsSymbolRenderContext &context) override
Returns the approximate bounding box of the marker symbol layer, taking into account any data defined...
Definition: qgsmasksymbollayer.cpp:118
qgsmasksymbollayer.h
QgsSymbolLayer::setPaintEffect
void setPaintEffect(QgsPaintEffect *effect)
Sets the current paint effect for the layer.
Definition: qgssymbollayer.cpp:190
stringToSymbolLayerReferenceList
QgsSymbolLayerReferenceList stringToSymbolLayerReferenceList(const QString &str)
Utilitary function to parse a string originated from symbolLayerReferenceListToString into a QgsSymbo...
Definition: qgssymbollayerreference.cpp:34
QgsSymbolLayer::paintEffect
QgsPaintEffect * paintEffect() const
Returns the current paint effect for the layer.
Definition: qgssymbollayer.cpp:185
QgsSymbolLayer::usedAttributes
virtual QSet< QString > usedAttributes(const QgsRenderContext &context) const
Returns the set of attributes referenced by the layer.
Definition: qgssymbollayer.cpp:246
QgsMaskMarkerSymbolLayer::stopRender
void stopRender(QgsSymbolRenderContext &context) override
Called after a set of rendering operations has finished on the supplied render context.
Definition: qgsmasksymbollayer.cpp:104
symbolLayerReferenceListToString
QString symbolLayerReferenceListToString(const QgsSymbolLayerReferenceList &lst)
Utilitary function to turn a QgsSymbolLayerReferenceList into a string.
Definition: qgssymbollayerreference.cpp:19
qgsvectorlayer.h
QgsMaskMarkerSymbolLayer::masks
QgsSymbolLayerReferenceList masks() const override
Returns a list of references to symbol layers that are masked by the sub symbol's shape.
Definition: qgsmasksymbollayer.h:72
QgsStringMap
QMap< QString, QString > QgsStringMap
Definition: qgis.h:714
QgsMaskMarkerSymbolLayer::usedAttributes
QSet< QString > usedAttributes(const QgsRenderContext &context) const override
Returns the set of attributes referenced by the layer.
Definition: qgsmasksymbollayer.cpp:74
QgsMaskMarkerSymbolLayer::setMasks
void setMasks(QgsSymbolLayerReferenceList maskedLayers)
Sets the symbol layers that will be masked by the sub symbol's shape.
Definition: qgsmasksymbollayer.h:79
QgsRenderContext::isGuiPreview
bool isGuiPreview() const
Returns the Gui preview mode.
Definition: qgsrendercontext.h:762
QgsSymbol::type
SymbolType type() const
Returns the symbol's type.
Definition: qgssymbol.h:122
QgsSymbol::Marker
@ Marker
Marker symbol.
Definition: qgssymbol.h:87
QgsMaskMarkerSymbolLayer::drawPreviewIcon
virtual void drawPreviewIcon(QgsSymbolRenderContext &context, QSize size) override
Definition: qgsmasksymbollayer.cpp:113
qgspainterswapper.h
QgsMaskMarkerSymbolLayer::clone
QgsMaskMarkerSymbolLayer * clone() const override
Shall be reimplemented by subclasses to create a deep copy of the instance.
Definition: qgsmasksymbollayer.cpp:52
QgsSymbolRenderContext::renderContext
QgsRenderContext & renderContext()
Returns a reference to the context's render context.
Definition: qgssymbol.h:708
QgsRenderContext::painter
QPainter * painter()
Returns the destination QPainter for the render operation.
Definition: qgsrendercontext.h:174
QgsMaskMarkerSymbolLayer
Special symbol layer that uses its sub symbol as a selective mask.
Definition: qgsmasksymbollayer.h:33
QgsSymbolLayer::copyDataDefinedProperties
void copyDataDefinedProperties(QgsSymbolLayer *destLayer) const
Copies all data defined properties of this layer to another symbol layer.
Definition: qgssymbollayer.cpp:398
QgsPainterSwapper
A class to manage painter saving and restoring required for drawing on a different painter (mask pain...
Definition: qgspainterswapper.h:34
qgsproject.h