QGIS API Documentation  2.11.0-Master
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
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() )
85  return;
86 
87  mSymbol->startRender( context, &fields );
88 
89  if ( mRotation.data() || mSizeScale.data() )
90  {
91  // we are going to need a temporary symbol
92  mTempSymbol.reset( mSymbol->clone() );
93 
94  int hints = 0;
95  if ( mRotation.data() )
97  if ( mSizeScale.data() )
99  mTempSymbol->setRenderHints( hints );
100 
101  mTempSymbol->startRender( context, &fields );
102 
103  if ( mSymbol->type() == QgsSymbolV2::Marker )
104  {
105  mOrigSize = static_cast<QgsMarkerSymbolV2*>( mSymbol.data() )->size();
106  }
107  else if ( mSymbol->type() == QgsSymbolV2::Line )
108  {
109  mOrigSize = static_cast<QgsLineSymbolV2*>( mSymbol.data() )->width();
110  }
111  else
112  {
113  mOrigSize = 0;
114  }
115  }
116 
117  return;
118 }
119 
121 {
122  if ( !mSymbol.data() ) return;
123 
124  mSymbol->stopRender( context );
125 
126  if ( mRotation.data() || mSizeScale.data() )
127  {
128  // we are going to need a temporary symbol
129  mTempSymbol->stopRender( context );
130  mTempSymbol.reset();
131  }
132 }
133 
135 {
136  QSet<QString> attributes;
137  if ( mSymbol.data() ) attributes.unite( mSymbol->usedAttributes() );
138  if ( mRotation.data() ) attributes.unite( mRotation->referencedColumns().toSet() );
139  if ( mSizeScale.data() ) attributes.unite( mSizeScale->referencedColumns().toSet() );
140  return attributes.toList();
141 }
142 
144 {
145  return mSymbol.data();
146 }
147 
149 {
150  Q_ASSERT( s );
151  mSymbol.reset( s );
152 }
153 
155 {
157 }
158 
160 {
162 }
163 
165 {
167 }
168 
170 {
172 }
173 
175 {
178 }
179 
181 {
182  return mSymbol.data() ? QString( "SINGLE: %1" ).arg( mSymbol->dump() ) : "";
183 }
184 
186 {
191  //r->setScaleMethod( scaleMethod() );
192  copyPaintEffect( r );
193  return r;
194 }
195 
197 {
198  QgsStringMap props;
199  QString errorMsg;
200  if ( mRotation.data() )
201  props[ "angle" ] = mRotation->expression();
202  if ( mSizeScale.data() )
203  props[ "scale" ] = mSizeScale->expression();
204 
205  QDomElement ruleElem = doc.createElement( "se:Rule" );
206  element.appendChild( ruleElem );
207 
208  QDomElement nameElem = doc.createElement( "se:Name" );
209  nameElem.appendChild( doc.createTextNode( "Single symbol" ) );
210  ruleElem.appendChild( nameElem );
211 
212  if ( mSymbol.data() ) mSymbol->toSld( doc, ruleElem, props );
213 }
214 
216 {
217  QgsSymbolV2List lst;
218  lst.append( mSymbol.data() );
219  return lst;
220 }
221 
222 
224 {
225  QDomElement symbolsElem = element.firstChildElement( "symbols" );
226  if ( symbolsElem.isNull() )
227  return NULL;
228 
229  QgsSymbolV2Map symbolMap = QgsSymbolLayerV2Utils::loadSymbols( symbolsElem );
230 
231  if ( !symbolMap.contains( "0" ) )
232  return NULL;
233 
234  QgsSingleSymbolRendererV2* r = new QgsSingleSymbolRendererV2( symbolMap.take( "0" ) );
235 
236  // delete symbols if there are any more
238 
239  QDomElement rotationElem = element.firstChildElement( "rotation" );
240  if ( !rotationElem.isNull() && !rotationElem.attribute( "field" ).isEmpty() )
241  {
242  convertSymbolRotation( r->mSymbol.data(), rotationElem.attribute( "field" ) );
243  }
244 
245  QDomElement sizeScaleElem = element.firstChildElement( "sizescale" );
246  if ( !sizeScaleElem.isNull() && !sizeScaleElem.attribute( "field" ).isEmpty() )
247  {
249  QgsSymbolLayerV2Utils::decodeScaleMethod( sizeScaleElem.attribute( "scalemethod" ) ),
250  sizeScaleElem.attribute( "field" ) );
251  }
252 
253  // TODO: symbol levels
254  return r;
255 }
256 
258 {
259  // XXX this renderer can handle only one Rule!
260 
261  // get the first Rule element
262  QDomElement ruleElem = element.firstChildElement( "Rule" );
263  if ( ruleElem.isNull() )
264  {
265  QgsDebugMsg( "no Rule elements found!" );
266  return NULL;
267  }
268 
269  QString label, description;
270  QgsSymbolLayerV2List layers;
271 
272  // retrieve the Rule element child nodes
273  QDomElement childElem = ruleElem.firstChildElement();
274  while ( !childElem.isNull() )
275  {
276  if ( childElem.localName() == "Name" )
277  {
278  // <se:Name> tag contains the rule identifier,
279  // so prefer title tag for the label property value
280  if ( label.isEmpty() )
281  label = childElem.firstChild().nodeValue();
282  }
283  else if ( childElem.localName() == "Description" )
284  {
285  // <se:Description> can contains a title and an abstract
286  QDomElement titleElem = childElem.firstChildElement( "Title" );
287  if ( !titleElem.isNull() )
288  {
289  label = titleElem.firstChild().nodeValue();
290  }
291 
292  QDomElement abstractElem = childElem.firstChildElement( "Abstract" );
293  if ( !abstractElem.isNull() )
294  {
295  description = abstractElem.firstChild().nodeValue();
296  }
297  }
298  else if ( childElem.localName() == "Abstract" )
299  {
300  // <sld:Abstract> (v1.0)
301  description = childElem.firstChild().nodeValue();
302  }
303  else if ( childElem.localName() == "Title" )
304  {
305  // <sld:Title> (v1.0)
306  label = childElem.firstChild().nodeValue();
307  }
308  else if ( childElem.localName().endsWith( "Symbolizer" ) )
309  {
310  // create symbol layers for this symbolizer
311  QgsSymbolLayerV2Utils::createSymbolLayerV2ListFromSld( childElem, geomType, layers );
312  }
313 
314  childElem = childElem.nextSiblingElement();
315  }
316 
317  if ( layers.size() == 0 )
318  return NULL;
319 
320  // now create the symbol
322  switch ( geomType )
323  {
324  case QGis::Line:
325  symbol = new QgsLineSymbolV2( layers );
326  break;
327 
328  case QGis::Polygon:
329  symbol = new QgsFillSymbolV2( layers );
330  break;
331 
332  case QGis::Point:
333  symbol = new QgsMarkerSymbolV2( layers );
334  break;
335 
336  default:
337  QgsDebugMsg( QString( "invalid geometry type: found %1" ).arg( geomType ) );
338  return NULL;
339  }
340 
341  // and finally return the new renderer
342  return new QgsSingleSymbolRendererV2( symbol );
343 }
344 
346 {
347  QDomElement rendererElem = doc.createElement( RENDERER_TAG_NAME );
348  rendererElem.setAttribute( "type", "singleSymbol" );
349  rendererElem.setAttribute( "symbollevels", ( mUsingSymbolLevels ? "1" : "0" ) );
350  rendererElem.setAttribute( "forceraster", ( mForceRaster ? "1" : "0" ) );
351 
353  symbols["0"] = mSymbol.data();
354  QDomElement symbolsElem = QgsSymbolLayerV2Utils::saveSymbols( symbols, "symbols", doc );
355  rendererElem.appendChild( symbolsElem );
356 
357  QDomElement rotationElem = doc.createElement( "rotation" );
358  if ( mRotation.data() )
360  rendererElem.appendChild( rotationElem );
361 
362  QDomElement sizeScaleElem = doc.createElement( "sizescale" );
363  if ( mSizeScale.data() )
365  sizeScaleElem.setAttribute( "scalemethod", QgsSymbolLayerV2Utils::encodeScaleMethod( mScaleMethod ) );
366  rendererElem.appendChild( sizeScaleElem );
367 
368  if ( mPaintEffect )
369  mPaintEffect->saveProperties( doc, rendererElem );
370 
371  return rendererElem;
372 }
373 
375 {
377  if ( mSymbol.data() )
378  {
380  lst << qMakePair( QString(), pix );
381  }
382  return lst;
383 }
384 
386 {
387  Q_UNUSED( scaleDenominator );
388  Q_UNUSED( rule );
390  lst << qMakePair( QString(), mSymbol.data() );
391  return lst;
392 }
393 
395 {
397  if ( mSymbol->type() == QgsSymbolV2::Marker )
398  {
399  const QgsMarkerSymbolV2 * symbol = static_cast<const QgsMarkerSymbolV2 *>( mSymbol.data() );
400  QgsDataDefined sizeDD = symbol->dataDefinedSize();
401  if ( sizeDD.isActive() && sizeDD.useExpression() )
402  {
403  QgsScaleExpression scaleExp( sizeDD.expressionString() );
404  if ( scaleExp.type() != QgsScaleExpression::Unknown )
405  {
406  QgsLegendSymbolItemV2 title( NULL, scaleExp.baseExpression(), 0 );
407  lst << title;
408  foreach ( double v, QgsSymbolLayerV2Utils::prettyBreaks( scaleExp.minValue(), scaleExp.maxValue(), 4 ) )
409  {
411  QgsMarkerSymbolV2 * s = static_cast<QgsMarkerSymbolV2 *>( si.symbol() );
412  s->setDataDefinedSize( 0 );
413  s->setSize( scaleExp.size( v ) );
414  lst << si;
415  }
416  return lst;
417  }
418  }
419  }
420 
421  lst << QgsLegendSymbolItemV2( mSymbol.data(), QString(), 0 );
422  return lst;
423 }
424 
426 {
427  if ( renderer->type() == "singleSymbol" )
428  {
429  return dynamic_cast<QgsSingleSymbolRendererV2*>( renderer->clone() );
430  }
431  if ( renderer->type() == "pointDisplacement" )
432  {
433  const QgsPointDisplacementRenderer* pointDisplacementRenderer = dynamic_cast<const QgsPointDisplacementRenderer*>( renderer );
434  if ( pointDisplacementRenderer )
435  return convertFromRenderer( pointDisplacementRenderer->embeddedRenderer() );
436  }
437  if ( renderer->type() == "invertedPolygonRenderer" )
438  {
439  const QgsInvertedPolygonRenderer* invertedPolygonRenderer = dynamic_cast<const QgsInvertedPolygonRenderer*>( renderer );
440  if ( invertedPolygonRenderer )
441  return convertFromRenderer( invertedPolygonRenderer->embeddedRenderer() );
442  }
443 
444  QgsSymbolV2List symbols = const_cast<QgsFeatureRendererV2 *>( renderer )->symbols();
445  if ( symbols.size() > 0 )
446  {
447  return new QgsSingleSymbolRendererV2( symbols.at( 0 )->clone() );
448  }
449  return 0;
450 }
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:162
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
Needs to be called when a new render cycle is started.
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.