QGIS API Documentation  2.11.0-Master
qgssinglesymbolrendererv2.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgssinglesymbolrendererv2.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 "qgssymbolv2.h"
19 #include "qgssymbollayerv2utils.h"
20 
21 #include "qgslogger.h"
22 #include "qgsfeature.h"
23 #include "qgsvectorlayer.h"
24 #include "qgssymbollayerv2.h"
25 #include "qgsogcutils.h"
28 #include "qgspainteffect.h"
29 #include "qgsscaleexpression.h"
30 #include "qgsdatadefined.h"
31 
32 #include <QDomDocument>
33 #include <QDomElement>
34 
36  : QgsFeatureRendererV2( "singleSymbol" )
37  , mSymbol( symbol )
38  , mScaleMethod( DEFAULT_SCALE_METHOD )
39  , mOrigSize( 0.0 )
40 {
41  Q_ASSERT( symbol );
42 }
43 
45 {
46 }
47 
49 {
50  if ( !mRotation.data() && !mSizeScale.data() ) return mSymbol.data();
51 
52  const double rotation = mRotation.data() ? mRotation->evaluate( feature ).toDouble() : 0;
53  const double sizeScale = mSizeScale.data() ? mSizeScale->evaluate( feature ).toDouble() : 1.;
54 
55  if ( mTempSymbol->type() == QgsSymbolV2::Marker )
56  {
57  QgsMarkerSymbolV2* markerSymbol = static_cast<QgsMarkerSymbolV2*>( mTempSymbol.data() );
58  if ( mRotation.data() ) markerSymbol->setAngle( rotation );
59  markerSymbol->setSize( sizeScale * mOrigSize );
60  markerSymbol->setScaleMethod( mScaleMethod );
61  }
62  else if ( mTempSymbol->type() == QgsSymbolV2::Line )
63  {
64  QgsLineSymbolV2* lineSymbol = static_cast<QgsLineSymbolV2*>( mTempSymbol.data() );
65  lineSymbol->setWidth( sizeScale * mOrigSize );
66  }
67  else if ( mTempSymbol->type() == QgsSymbolV2::Fill )
68  {
69  QgsFillSymbolV2* fillSymbol = static_cast<QgsFillSymbolV2*>( mTempSymbol.data() );
70  if ( mRotation.data() ) fillSymbol->setAngle( rotation );
71  }
72 
73  return mTempSymbol.data();
74 }
75 
77 {
78  Q_UNUSED( feature );
79  return mSymbol.data();
80 }
81 
83 {
84  if ( !mSymbol.data() ) return;
85 
86  mSymbol->startRender( context, &fields );
87 
88  if ( mRotation.data() || mSizeScale.data() )
89  {
90  // we are going to need a temporary symbol
91  mTempSymbol.reset( mSymbol->clone() );
92 
93  int hints = 0;
94  if ( mRotation.data() )
96  if ( mSizeScale.data() )
98  mTempSymbol->setRenderHints( hints );
99 
100  mTempSymbol->startRender( context, &fields );
101 
102  if ( mSymbol->type() == QgsSymbolV2::Marker )
103  {
104  mOrigSize = static_cast<QgsMarkerSymbolV2*>( mSymbol.data() )->size();
105  }
106  else if ( mSymbol->type() == QgsSymbolV2::Line )
107  {
108  mOrigSize = static_cast<QgsLineSymbolV2*>( mSymbol.data() )->width();
109  }
110  else
111  {
112  mOrigSize = 0;
113  }
114  }
115 }
116 
118 {
119  if ( !mSymbol.data() ) return;
120 
121  mSymbol->stopRender( context );
122 
123  if ( mRotation.data() || mSizeScale.data() )
124  {
125  // we are going to need a temporary symbol
126  mTempSymbol->stopRender( context );
127  mTempSymbol.reset();
128  }
129 }
130 
132 {
133  QSet<QString> attributes;
134  if ( mSymbol.data() ) attributes.unite( mSymbol->usedAttributes() );
135  if ( mRotation.data() ) attributes.unite( mRotation->referencedColumns().toSet() );
136  if ( mSizeScale.data() ) attributes.unite( mSizeScale->referencedColumns().toSet() );
137  return attributes.toList();
138 }
139 
141 {
142  return mSymbol.data();
143 }
144 
146 {
147  Q_ASSERT( s );
148  mSymbol.reset( s );
149 }
150 
152 {
154 }
155 
157 {
159 }
160 
162 {
164 }
165 
167 {
169 }
170 
172 {
175 }
176 
178 {
179  return mSymbol.data() ? QString( "SINGLE: %1" ).arg( mSymbol->dump() ) : "";
180 }
181 
183 {
188  //r->setScaleMethod( scaleMethod() );
189  copyPaintEffect( r );
190  return r;
191 }
192 
194 {
195  QgsStringMap props;
196  QString errorMsg;
197  if ( mRotation.data() )
198  props[ "angle" ] = mRotation->expression();
199  if ( mSizeScale.data() )
200  props[ "scale" ] = mSizeScale->expression();
201 
202  QDomElement ruleElem = doc.createElement( "se:Rule" );
203  element.appendChild( ruleElem );
204 
205  QDomElement nameElem = doc.createElement( "se:Name" );
206  nameElem.appendChild( doc.createTextNode( "Single symbol" ) );
207  ruleElem.appendChild( nameElem );
208 
209  if ( mSymbol.data() ) mSymbol->toSld( doc, ruleElem, props );
210 }
211 
213 {
214  QgsSymbolV2List lst;
215  lst.append( mSymbol.data() );
216  return lst;
217 }
218 
219 
221 {
222  QDomElement symbolsElem = element.firstChildElement( "symbols" );
223  if ( symbolsElem.isNull() )
224  return NULL;
225 
226  QgsSymbolV2Map symbolMap = QgsSymbolLayerV2Utils::loadSymbols( symbolsElem );
227 
228  if ( !symbolMap.contains( "0" ) )
229  return NULL;
230 
231  QgsSingleSymbolRendererV2* r = new QgsSingleSymbolRendererV2( symbolMap.take( "0" ) );
232 
233  // delete symbols if there are any more
235 
236  QDomElement rotationElem = element.firstChildElement( "rotation" );
237  if ( !rotationElem.isNull() && !rotationElem.attribute( "field" ).isEmpty() )
238  {
239  convertSymbolRotation( r->mSymbol.data(), rotationElem.attribute( "field" ) );
240  }
241 
242  QDomElement sizeScaleElem = element.firstChildElement( "sizescale" );
243  if ( !sizeScaleElem.isNull() && !sizeScaleElem.attribute( "field" ).isEmpty() )
244  {
246  QgsSymbolLayerV2Utils::decodeScaleMethod( sizeScaleElem.attribute( "scalemethod" ) ),
247  sizeScaleElem.attribute( "field" ) );
248  }
249 
250  // TODO: symbol levels
251  return r;
252 }
253 
255 {
256  // XXX this renderer can handle only one Rule!
257 
258  // get the first Rule element
259  QDomElement ruleElem = element.firstChildElement( "Rule" );
260  if ( ruleElem.isNull() )
261  {
262  QgsDebugMsg( "no Rule elements found!" );
263  return NULL;
264  }
265 
266  QString label, description;
267  QgsSymbolLayerV2List layers;
268 
269  // retrieve the Rule element child nodes
270  QDomElement childElem = ruleElem.firstChildElement();
271  while ( !childElem.isNull() )
272  {
273  if ( childElem.localName() == "Name" )
274  {
275  // <se:Name> tag contains the rule identifier,
276  // so prefer title tag for the label property value
277  if ( label.isEmpty() )
278  label = childElem.firstChild().nodeValue();
279  }
280  else if ( childElem.localName() == "Description" )
281  {
282  // <se:Description> can contains a title and an abstract
283  QDomElement titleElem = childElem.firstChildElement( "Title" );
284  if ( !titleElem.isNull() )
285  {
286  label = titleElem.firstChild().nodeValue();
287  }
288 
289  QDomElement abstractElem = childElem.firstChildElement( "Abstract" );
290  if ( !abstractElem.isNull() )
291  {
292  description = abstractElem.firstChild().nodeValue();
293  }
294  }
295  else if ( childElem.localName() == "Abstract" )
296  {
297  // <sld:Abstract> (v1.0)
298  description = childElem.firstChild().nodeValue();
299  }
300  else if ( childElem.localName() == "Title" )
301  {
302  // <sld:Title> (v1.0)
303  label = childElem.firstChild().nodeValue();
304  }
305  else if ( childElem.localName().endsWith( "Symbolizer" ) )
306  {
307  // create symbol layers for this symbolizer
308  QgsSymbolLayerV2Utils::createSymbolLayerV2ListFromSld( childElem, geomType, layers );
309  }
310 
311  childElem = childElem.nextSiblingElement();
312  }
313 
314  if ( layers.size() == 0 )
315  return NULL;
316 
317  // now create the symbol
319  switch ( geomType )
320  {
321  case QGis::Line:
322  symbol = new QgsLineSymbolV2( layers );
323  break;
324 
325  case QGis::Polygon:
326  symbol = new QgsFillSymbolV2( layers );
327  break;
328 
329  case QGis::Point:
330  symbol = new QgsMarkerSymbolV2( layers );
331  break;
332 
333  default:
334  QgsDebugMsg( QString( "invalid geometry type: found %1" ).arg( geomType ) );
335  return NULL;
336  }
337 
338  // and finally return the new renderer
339  return new QgsSingleSymbolRendererV2( symbol );
340 }
341 
343 {
344  QDomElement rendererElem = doc.createElement( RENDERER_TAG_NAME );
345  rendererElem.setAttribute( "type", "singleSymbol" );
346  rendererElem.setAttribute( "symbollevels", ( mUsingSymbolLevels ? "1" : "0" ) );
347 
349  symbols["0"] = mSymbol.data();
350  QDomElement symbolsElem = QgsSymbolLayerV2Utils::saveSymbols( symbols, "symbols", doc );
351  rendererElem.appendChild( symbolsElem );
352 
353  QDomElement rotationElem = doc.createElement( "rotation" );
354  if ( mRotation.data() )
356  rendererElem.appendChild( rotationElem );
357 
358  QDomElement sizeScaleElem = doc.createElement( "sizescale" );
359  if ( mSizeScale.data() )
361  sizeScaleElem.setAttribute( "scalemethod", QgsSymbolLayerV2Utils::encodeScaleMethod( mScaleMethod ) );
362  rendererElem.appendChild( sizeScaleElem );
363 
364  if ( mPaintEffect )
365  mPaintEffect->saveProperties( doc, rendererElem );
366 
367  return rendererElem;
368 }
369 
371 {
373  if ( mSymbol.data() )
374  {
376  lst << qMakePair( QString(), pix );
377  }
378  return lst;
379 }
380 
382 {
383  Q_UNUSED( scaleDenominator );
384  Q_UNUSED( rule );
386  lst << qMakePair( QString(), mSymbol.data() );
387  return lst;
388 }
389 
391 {
393  if ( mSymbol->type() == QgsSymbolV2::Marker )
394  {
395  const QgsMarkerSymbolV2 * symbol = static_cast<const QgsMarkerSymbolV2 *>( mSymbol.data() );
396  QgsDataDefined sizeDD = symbol->dataDefinedSize();
397  if ( sizeDD.isActive() && sizeDD.useExpression() )
398  {
399  QgsScaleExpression scaleExp( sizeDD.expressionString() );
400  if ( scaleExp.type() != QgsScaleExpression::Unknown )
401  {
402  QgsLegendSymbolItemV2 title( NULL, scaleExp.baseExpression(), 0 );
403  lst << title;
404  foreach ( double v, QgsSymbolLayerV2Utils::prettyBreaks( scaleExp.minValue(), scaleExp.maxValue(), 4 ) )
405  {
407  QgsMarkerSymbolV2 * s = static_cast<QgsMarkerSymbolV2 *>( si.symbol() );
408  s->setDataDefinedSize( 0 );
409  s->setSize( scaleExp.size( v ) );
410  lst << si;
411  }
412  return lst;
413  }
414  }
415  }
416 
417  lst << QgsLegendSymbolItemV2( mSymbol.data(), QString(), 0 );
418  return lst;
419 }
420 
422 {
423  if ( renderer->type() == "singleSymbol" )
424  {
425  return dynamic_cast<QgsSingleSymbolRendererV2*>( renderer->clone() );
426  }
427  if ( renderer->type() == "pointDisplacement" )
428  {
429  const QgsPointDisplacementRenderer* pointDisplacementRenderer = dynamic_cast<const QgsPointDisplacementRenderer*>( renderer );
430  if ( pointDisplacementRenderer )
431  return convertFromRenderer( pointDisplacementRenderer->embeddedRenderer() );
432  }
433  if ( renderer->type() == "invertedPolygonRenderer" )
434  {
435  const QgsInvertedPolygonRenderer* invertedPolygonRenderer = dynamic_cast<const QgsInvertedPolygonRenderer*>( renderer );
436  if ( invertedPolygonRenderer )
437  return convertFromRenderer( invertedPolygonRenderer->embeddedRenderer() );
438  }
439 
440  QgsSymbolV2List symbols = const_cast<QgsFeatureRendererV2 *>( renderer )->symbols();
441  if ( symbols.size() > 0 )
442  {
443  return new QgsSingleSymbolRendererV2( symbols.at( 0 )->clone() );
444  }
445  return 0;
446 }
static QgsSymbolV2Map loadSymbols(QDomElement &element)
#define RENDERER_TAG_NAME
Definition: qgsrendererv2.h:48
A container class for data source field mapping or expression.
static QList< double > prettyBreaks(double minimum, double maximum, int classes)
Computes a sequence of about 'classes' equally spaced round values which cover the range of values fr...
bool contains(const Key &key) const
QDomNode appendChild(const QDomNode &newChild)
virtual QDomElement save(QDomDocument &doc) override
store renderer info to XML element
virtual void stopRender(QgsRenderContext &context) override
QString attribute(const QString &name, const QString &defValue) const
QString nodeValue() const
#define QgsDebugMsg(str)
Definition: qgslogger.h:33
void setDataDefinedSize(const QgsDataDefined &dd)
Set data defined size for whole symbol (including all symbol layers).
virtual QgsSymbolV2List symbols() override
for symbol levels
virtual QgsSymbolV2 * symbolForFeature(QgsFeature &feature) override
to be overridden
Class storing parameters of a scale expression, which is a subclass of QgsExpression for expressions ...
virtual QgsSymbolV2 * originalSymbolForFeature(QgsFeature &feature) override
Return symbol for feature.
QgsDataDefined dataDefinedSize() const
Returns data defined size for whole symbol (including all symbol layers).
void setSizeScaleField(QString fieldOrExpression)
static QgsFeatureRendererV2 * createFromSld(QDomElement &element, QGis::GeometryType geomType)
const T & at(int i) const
QgsSymbolV2::ScaleMethod scaleMethod() const
QDomElement nextSiblingElement(const QString &tagName) const
Container of fields for a vector layer.
Definition: qgsfield.h:173
static QgsSingleSymbolRendererV2 * convertFromRenderer(const QgsFeatureRendererV2 *renderer)
creates a QgsSingleSymbolRendererV2 from an existing renderer.
QString expressionString() const
GeometryType
Definition: qgis.h:155
QScopedPointer< QgsSymbolV2 > mSymbol
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
Definition: qgsfeature.h:119
virtual QgsFeatureRendererV2 * clone() const override
QgsPaintEffect * mPaintEffect
QgsSymbolV2::ScaleMethod mScaleMethod
void setWidth(double width)
int size() const
virtual void toSld(QDomDocument &doc, QDomElement &element) const override
used from subclasses to create SLD Rule elements following SLD v1.1 specs
void setAngle(double angle)
void reset(T *other)
QString type() const
Definition: qgsrendererv2.h:82
static bool createSymbolLayerV2ListFromSld(QDomElement &element, QGis::GeometryType geomType, QgsSymbolLayerV2List &layers)
virtual QgsFeatureRendererV2 * clone() const =0
QString number(int n, int base)
void append(const T &value)
QString localName() const
QgsInvertedPolygonRenderer is a polygon-only feature renderer used to display features inverted...
void setAttribute(const QString &name, const QString &value)
#define DEFAULT_SCALE_METHOD
static QDomElement saveSymbols(QgsSymbolV2Map &symbols, QString tagName, QDomDocument &doc)
bool isEmpty() const
QgsSingleSymbolRendererV2(QgsSymbolV2 *symbol)
void setAngle(double angle)
virtual QList< QString > usedAttributes() override
void setSize(double size)
bool endsWith(const QString &s, Qt::CaseSensitivity cs) const
QScopedPointer< QgsExpression > mSizeScale
static void convertSymbolSizeScale(QgsSymbolV2 *symbol, QgsSymbolV2::ScaleMethod method, const QString &field)
QgsFeatureRendererV2 * embeddedRenderer() const
bool useExpression() const
QDomText createTextNode(const QString &value)
T * data() const
virtual QgsLegendSymbologyList legendSymbologyItems(QSize iconSize) override
return a list of symbology items for the legend
virtual QString dump() const override
for debugging
A renderer that automatically displaces points with the same position.
bool isNull() const
void setUsingSymbolLevels(bool usingSymbolLevels)
virtual bool saveProperties(QDomDocument &doc, QDomElement &element) const
Saves the current state of the effect to a DOM element.
QString rotationField() const override
return rotation field name (or empty string if not set or not supported by renderer) ...
void setScaleMethod(QgsSymbolV2::ScaleMethod scaleMethod)
Contains information about the context of a rendering operation.
const QgsFeatureRendererV2 * embeddedRenderer() const
QDomNode firstChild() const
void copyPaintEffect(QgsFeatureRendererV2 *destRenderer) const
Copies paint effect of this renderer to another renderer.
QSet< T > & unite(const QSet< T > &other)
static QgsExpression * fieldOrExpressionToExpression(const QString &fieldOrExpression)
Return a new valid expression instance for given field or expression string.
virtual QgsLegendSymbolList legendSymbolItems(double scaleDenominator=-1, QString rule=QString()) override
return a list of item text / symbol
static QString encodeScaleMethod(QgsSymbolV2::ScaleMethod scaleMethod)
QDomElement firstChildElement(const QString &tagName) const
static QString fieldOrExpressionFromExpression(QgsExpression *expression)
Return a field name if the whole expression is just a name of the field .
bool usingSymbolLevels() const
void setScaleMethodToSymbol(QgsSymbolV2 *symbol, int scaleMethod)
QList< T > toList() const
static void clearSymbolMap(QgsSymbolV2Map &symbols)
static QgsSymbolV2::ScaleMethod decodeScaleMethod(QString str)
static QgsFeatureRendererV2 * create(QDomElement &element)
create renderer from XML element
QDomElement createElement(const QString &tagName)
static QPixmap symbolPreviewPixmap(QgsSymbolV2 *symbol, QSize size, QgsRenderContext *customContext=0)
virtual void startRender(QgsRenderContext &context, const QgsFields &fields) override
The class stores information about one class/rule of a vector layer renderer in a unified way that ca...
QString arg(qlonglong a, int fieldWidth, int base, const QChar &fillChar) const
QScopedPointer< QgsSymbolV2 > mTempSymbol
bool isActive() const
T take(const Key &key)
void setScaleMethod(QgsSymbolV2::ScaleMethod scaleMethod)
void setRotationField(QString fieldOrExpression) override
sets rotation field of renderer (if supported by the renderer)
QScopedPointer< QgsExpression > mRotation
static void convertSymbolRotation(QgsSymbolV2 *symbol, const QString &field)
virtual QgsLegendSymbolListV2 legendSymbolItemsV2() const override
Return a list of symbology items for the legend.