QGIS API Documentation  2.18.21-Las Palmas (9fba24a)
qgsgeometrygeneratorsymbollayerv2.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsgeometrygeneratorsymbollayerv2.cpp
3  ---------------------
4  begin : November 2015
5  copyright : (C) 2015 by Matthias Kuhn
6  email : matthias at opengis dot ch
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 "qgsgeometry.h"
18 
20 {
21  delete mMarkerSymbol;
22  delete mLineSymbol;
23  delete mFillSymbol;
24 }
25 
27 {
28  QString expression = properties.value( "geometryModifier" );
29  if ( expression.isEmpty() )
30  {
31  expression = "$geometry";
32  }
34 
35  if ( properties.value( "SymbolType" ) == "Marker" )
36  {
37  symbolLayer->setSubSymbol( QgsMarkerSymbolV2::createSimple( properties ) );
38  }
39  else if ( properties.value( "SymbolType" ) == "Line" )
40  {
41  symbolLayer->setSubSymbol( QgsLineSymbolV2::createSimple( properties ) );
42  }
43  else
44  {
45  symbolLayer->setSubSymbol( QgsFillSymbolV2::createSimple( properties ) );
46  }
47 
48  return symbolLayer;
49 }
50 
51 QgsGeometryGeneratorSymbolLayerV2::QgsGeometryGeneratorSymbolLayerV2( const QString& expression )
53  , mExpression( new QgsExpression( expression ) )
54  , mFillSymbol( nullptr )
55  , mLineSymbol( nullptr )
56  , mMarkerSymbol( nullptr )
57  , mSymbol( nullptr )
58  , mSymbolType( QgsSymbolV2::Marker )
59 {
60 
61 }
62 
64 {
65  return "GeometryGenerator";
66 }
67 
69 {
70  if ( symbolType == QgsSymbolV2::Fill )
71  {
72  if ( !mFillSymbol )
74  mSymbol = mFillSymbol;
75  }
76  else if ( symbolType == QgsSymbolV2::Line )
77  {
78  if ( !mLineSymbol )
80  mSymbol = mLineSymbol;
81  }
82  else if ( symbolType == QgsSymbolV2::Marker )
83  {
84  if ( !mMarkerSymbol )
85  mMarkerSymbol = QgsMarkerSymbolV2::createSimple( QgsStringMap() );
86  mSymbol = mMarkerSymbol;
87  }
88  else
89  Q_ASSERT( false );
90 
91  mSymbolType = symbolType;
92 }
93 
95 {
96  mExpression->prepare( &context.renderContext().expressionContext() );
97 
98  subSymbol()->startRender( context.renderContext() );
99 }
100 
102 {
103  if ( mSymbol )
104  mSymbol->stopRender( context.renderContext() );
105 }
106 
108 {
110 
111  if ( mFillSymbol )
112  clone->mFillSymbol = mFillSymbol->clone();
113  if ( mLineSymbol )
114  clone->mLineSymbol = mLineSymbol->clone();
115  if ( mMarkerSymbol )
116  clone->mMarkerSymbol = mMarkerSymbol->clone();
117 
118  clone->setSymbolType( mSymbolType );
119 
120  copyPaintEffect( clone );
121 
122  return clone;
123 }
124 
126 {
127  QgsStringMap props;
128  props.insert( "geometryModifier" , mExpression->expression() );
129  switch ( mSymbolType )
130  {
131  case QgsSymbolV2::Marker:
132  props.insert( "SymbolType", "Marker" );
133  break;
134  case QgsSymbolV2::Line:
135  props.insert( "SymbolType", "Line" );
136  break;
137  default:
138  props.insert( "SymbolType", "Fill" );
139  break;
140  }
141 
142  return props;
143 }
144 
146 {
147  if ( mSymbol )
148  mSymbol->drawPreviewIcon( context.renderContext().painter(), size );
149 }
150 
152 {
153  mExpression.reset( new QgsExpression( exp ) );
154 }
155 
157 {
158  switch ( symbol->type() )
159  {
160  case QgsSymbolV2::Marker:
161  mMarkerSymbol = static_cast<QgsMarkerSymbolV2*>( symbol );
162  break;
163 
164  case QgsSymbolV2::Line:
165  mLineSymbol = static_cast<QgsLineSymbolV2*>( symbol );
166  break;
167 
168  case QgsSymbolV2::Fill:
169  mFillSymbol = static_cast<QgsFillSymbolV2*>( symbol );
170  break;
171 
172  default:
173  break;
174  }
175 
176  setSymbolType( symbol->type() );
177 
178  return true;
179 }
180 
182 {
183  return mSymbol->usedAttributes() + mExpression->referencedColumns().toSet();
184 }
185 
187 {
188  Q_UNUSED( symbol )
189  return true;
190 }
192 {
193  if ( context.feature() )
194  {
195  QgsExpressionContext& expressionContext = context.renderContext().expressionContext();
196 
197  QgsFeature f = expressionContext.feature();
198  QgsGeometry geom = mExpression->evaluate( &expressionContext ).value<QgsGeometry>();
199  f.setGeometry( geom );
200 
201  QgsExpressionContextScope* subSymbolExpressionContextScope = mSymbol->symbolRenderContext()->expressionContextScope();
202 
203  subSymbolExpressionContextScope->setFeature( f );
204 
205  mSymbol->renderFeature( f, context.renderContext(), -1, context.selected() );
206  }
207 }
208 
210 {
211  mSymbol->setColor( color );
212 }
Class for parsing and evaluation of expressions (formerly called "search strings").
QgsSymbolV2::SymbolType symbolType() const
Access the symbol type.
bool isCompatibleWithSymbol(QgsSymbolV2 *symbol) const override
Will always return true.
static QgsMarkerSymbolV2 * createSimple(const QgsStringMap &properties)
Create a marker symbol with one symbol layer: SimpleMarker with specified properties.
QgsSymbolLayerV2 * clone() const override
Shall be reimplemented by subclasses to create a deep copy of the instance.
static QgsSymbolLayerV2 * create(const QgsStringMap &properties)
void setSymbolType(QgsSymbolV2::SymbolType symbolType)
Set the type of symbol which should be created.
virtual QgsLineSymbolV2 * clone() const override
static QgsFillSymbolV2 * createSimple(const QgsStringMap &properties)
Create a fill symbol with one symbol layer: SimpleFill with specified properties. ...
virtual bool setSubSymbol(QgsSymbolV2 *symbol) override
set layer&#39;s subsymbol. takes ownership of the passed symbol
void renderFeature(const QgsFeature &feature, QgsRenderContext &context, int layer=-1, bool selected=false, bool drawVertexMarker=false, int currentVertexMarkerType=0, int currentVertexMarkerSize=0)
Render a feature.
A geometry is the spatial representation of a feature.
Definition: qgsgeometry.h:76
SymbolType type() const
Definition: qgssymbolv2.h:107
Line symbol.
Definition: qgssymbolv2.h:82
QgsStringMap properties() const override
Should be reimplemented by subclasses to return a string map that contains the configuration informat...
QgsSymbolLayerV2(QgsSymbolV2::SymbolType type, bool locked=false)
const QgsFeature * feature() const
Current feature being rendered - may be null.
Definition: qgssymbolv2.h:388
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
Definition: qgsfeature.h:187
QMap< QString, QString > QgsStringMap
Definition: qgis.h:492
void drawPreviewIcon(QPainter *painter, QSize size, QgsRenderContext *customContext=nullptr)
Draw icon of the symbol that occupyies area given by size using the painter.
Marker symbol.
Definition: qgssymbolv2.h:81
void reset(T *other)
void drawPreviewIcon(QgsSymbolV2RenderContext &context, QSize size) override
virtual void render(QgsSymbolV2RenderContext &context)
Will render this symbol layer using the context.
void setColor(const QColor &color)
void setGeometry(const QgsGeometry &geom)
Set this feature&#39;s geometry from another QgsGeometry object.
Definition: qgsfeature.cpp:124
void startRender(QgsSymbolV2RenderContext &context) override
void startRender(QgsRenderContext &context, const QgsFields *fields=nullptr)
static QgsLineSymbolV2 * createSimple(const QgsStringMap &properties)
Create a line symbol with one symbol layer: SimpleLine with specified properties. ...
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
bool isEmpty() const
void setGeometryExpression(const QString &exp)
Set the expression to generate this geometry.
void stopRender(QgsSymbolV2RenderContext &context) override
Single scope for storing variables and functions for use within a QgsExpressionContext.
void setFeature(const QgsFeature &feature)
Convenience function for setting a feature for the scope.
virtual QColor color() const
The fill color.
virtual QSet< QString > usedAttributes() const override
Returns the set of attributes referenced by the layer.
virtual QgsFillSymbolV2 * clone() const override
QgsExpressionContext & expressionContext()
Gets the expression context.
void setColor(const QColor &color) override
The fill color.
SymbolType
Type of the symbol.
Definition: qgssymbolv2.h:79
void copyPaintEffect(QgsSymbolLayerV2 *destLayer) const
Copies paint effect of this layer to another symbol layer.
Hybrid symbol.
Definition: qgssymbolv2.h:84
QgsExpressionContextScope * expressionContextScope()
This scope is always available when a symbol of this type is being rendered.
QPainter * painter()
void stopRender(QgsRenderContext &context)
QgsRenderContext & renderContext()
Definition: qgssymbolv2.h:359
Fill symbol.
Definition: qgssymbolv2.h:83
QgsFeature feature() const
Convenience function for retrieving the feature for the context, if set.
virtual Q_DECL_DEPRECATED QgsExpression * expression(const QString &property) const
Returns the data defined expression associated with a property.
iterator insert(const Key &key, const T &value)
QSet< QString > usedAttributes() const
Return a list of attributes required to render this feature.
QgsSymbolV2RenderContext * symbolRenderContext()
Returns the symbol render context.
virtual QgsMarkerSymbolV2 * clone() const override
QString layerType() const override
Returns a string that represents this layer type.
const T value(const Key &key) const