QGIS API Documentation  2.14.0-Essen
qgssymbologyv2conversion.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgssymbologyv2conversion.cpp
3  ---------------------
4  begin : December 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  ***************************************************************************/
16 
17 #include "qgslogger.h"
18 
19 #include "qgsmarkersymbollayerv2.h"
20 #include "qgslinesymbollayerv2.h"
21 #include "qgsfillsymbollayerv2.h"
25 
26 
27 
29 {
33 };
34 
35 
36 static QgsOldSymbolMeta readSymbolMeta( const QDomNode& synode )
37 {
38  QgsOldSymbolMeta meta;
39 
40  QDomNode lvalnode = synode.namedItem( "lowervalue" );
41  if ( ! lvalnode.isNull() )
42  {
43  QDomElement lvalelement = lvalnode.toElement();
44  if ( lvalelement.attribute( "null" ).toInt() == 1 )
45  {
46  meta.lowerValue = QString::null;
47  }
48  else
49  {
50  meta.lowerValue = lvalelement.text();
51  }
52  }
53 
54  QDomNode uvalnode = synode.namedItem( "uppervalue" );
55  if ( ! uvalnode.isNull() )
56  {
57  QDomElement uvalelement = uvalnode.toElement();
58  meta.upperValue = uvalelement.text();
59  }
60 
61  QDomNode labelnode = synode.namedItem( "label" );
62  if ( ! labelnode.isNull() )
63  {
64  QDomElement labelelement = labelnode.toElement();
65  meta.label = labelelement.text();
66  }
67 
68  return meta;
69 }
70 
71 
72 static QColor readSymbolColor( const QDomNode& synode, bool fillColor )
73 {
74  QDomNode cnode = synode.namedItem( fillColor ? "fillcolor" : "outlinecolor" );
75  QDomElement celement = cnode.toElement();
76  int red = celement.attribute( "red" ).toInt();
77  int green = celement.attribute( "green" ).toInt();
78  int blue = celement.attribute( "blue" ).toInt();
79  return QColor( red, green, blue );
80 }
81 
82 static double readOutlineWidth( const QDomNode& synode )
83 {
84  QDomNode outlwnode = synode.namedItem( "outlinewidth" );
85  QDomElement outlwelement = outlwnode.toElement();
86  return outlwelement.text().toDouble();
87 }
88 
89 
90 static Qt::PenStyle readOutlineStyle( const QDomNode& synode )
91 {
92  QDomNode outlstnode = synode.namedItem( "outlinestyle" );
93  QDomElement outlstelement = outlstnode.toElement();
94  return QgsSymbologyV2Conversion::qString2PenStyle( outlstelement.text() );
95 }
96 
97 static Qt::BrushStyle readBrushStyle( const QDomNode& synode )
98 {
99  QDomNode fillpnode = synode.namedItem( "fillpattern" );
100  QDomElement fillpelement = fillpnode.toElement();
101  return QgsSymbologyV2Conversion::qString2BrushStyle( fillpelement.text() );
102 }
103 
104 static QString readMarkerSymbolName( const QDomNode& synode )
105 {
106  QDomNode psymbnode = synode.namedItem( "pointsymbol" );
107  if ( ! psymbnode.isNull() )
108  {
109  QDomElement psymbelement = psymbnode.toElement();
110  return psymbelement.text();
111  }
112  return QString( "hard:circle" );
113 }
114 
115 static float readMarkerSymbolSize( const QDomNode& synode )
116 {
117  QDomNode psizenode = synode.namedItem( "pointsize" );
118  if ( ! psizenode.isNull() )
119  {
120  QDomElement psizeelement = psizenode.toElement();
121  return psizeelement.text().toFloat();
122  }
123  return DEFAULT_POINT_SIZE;
124 }
125 
126 
127 
128 static QgsSymbolV2* readOldSymbol( const QDomNode& synode, QGis::GeometryType geomType )
129 {
130  switch ( geomType )
131  {
132  case QGis::Point:
133  {
134  QgsMarkerSymbolLayerV2* sl = nullptr;
135  double size = readMarkerSymbolSize( synode );
136  double angle = 0; // rotation only from classification field
137  QString symbolName = readMarkerSymbolName( synode );
138  if ( symbolName.startsWith( "hard:" ) )
139  {
140  // simple symbol marker
141  QColor color = readSymbolColor( synode, true );
142  QColor borderColor = readSymbolColor( synode, false );
143  QString name = symbolName.mid( 5 );
144  sl = new QgsSimpleMarkerSymbolLayerV2( name, color, borderColor, size, angle );
145  }
146  else
147  {
148  // svg symbol marker
149  QString name = symbolName.mid( 4 );
150  sl = new QgsSvgMarkerSymbolLayerV2( name, size, angle );
151  }
152  QgsSymbolLayerV2List layers;
153  layers.append( sl );
154  return new QgsMarkerSymbolV2( layers );
155  }
156 
157  case QGis::Line:
158  {
159  QColor color = readSymbolColor( synode, false );
160  double width = readOutlineWidth( synode );
161  Qt::PenStyle penStyle = readOutlineStyle( synode );
162  QgsLineSymbolLayerV2* sl = new QgsSimpleLineSymbolLayerV2( color, width, penStyle );
163 
164  QgsSymbolLayerV2List layers;
165  layers.append( sl );
166  return new QgsLineSymbolV2( layers );
167  }
168 
169  case QGis::Polygon:
170  {
171  QColor color = readSymbolColor( synode, true );
172  QColor borderColor = readSymbolColor( synode, false );
173  Qt::BrushStyle brushStyle = readBrushStyle( synode );
174  Qt::PenStyle borderStyle = readOutlineStyle( synode );
175  double borderWidth = readOutlineWidth( synode );
176  QgsFillSymbolLayerV2* sl = new QgsSimpleFillSymbolLayerV2( color, brushStyle, borderColor, borderStyle, borderWidth );
177 
178  QgsSymbolLayerV2List layers;
179  layers.append( sl );
180  return new QgsFillSymbolV2( layers );
181  }
182 
183  default:
184  return nullptr;
185  }
186 }
187 
188 
189 
191 {
192  QDomNode synode = rnode.namedItem( "symbol" );
193  if ( synode.isNull() )
194  return nullptr;
195 
196  QgsSymbolV2* sy2 = readOldSymbol( synode, geomType );
198  return r;
199 }
200 
201 
203 {
204  QDomNode modeNode = rnode.namedItem( "mode" );
205  QString modeValue = modeNode.toElement().text();
206  QDomNode classnode = rnode.namedItem( "classificationfield" );
207  QString classificationField = classnode.toElement().text();
208 
210  if ( modeValue == "Empty" )
211  {
213  }
214  else if ( modeValue == "Quantile" )
215  {
217  }
218  else //default
219  {
221  }
222 
223  // load ranges and symbols
224  QgsRangeList ranges;
225  QDomNode symbolnode = rnode.namedItem( "symbol" );
226  while ( !symbolnode.isNull() )
227  {
228  QgsSymbolV2* symbolv2 = readOldSymbol( symbolnode, geomType );
229  if ( symbolv2 )
230  {
231  QgsOldSymbolMeta meta = readSymbolMeta( symbolnode );
232  double lowerValue = meta.lowerValue.toDouble();
233  double upperValue = meta.upperValue.toDouble();
234  QString label = meta.label;
235  if ( label.isEmpty() )
236  label = QString( "%1 - %2" ).arg( lowerValue, -1, 'f', 3 ).arg( upperValue, -1, 'f', 3 );
237  ranges.append( QgsRendererRangeV2( lowerValue, upperValue, symbolv2, label ) );
238  }
239 
240  symbolnode = symbolnode.nextSibling();
241  }
242 
243  // create renderer
244  QgsGraduatedSymbolRendererV2* r = new QgsGraduatedSymbolRendererV2( classificationField, ranges );
245  r->setMode( m );
246  return r;
247 }
248 
249 
250 
252 {
253  QDomNode classnode = rnode.namedItem( "classificationfield" );
254  QString classificationField = classnode.toElement().text();
255 
256  // read categories and symbols
257  QgsCategoryList cats;
258  QDomNode symbolnode = rnode.namedItem( "symbol" );
259  while ( !symbolnode.isNull() )
260  {
261  QgsSymbolV2* symbolv2 = readOldSymbol( symbolnode, geomType );
262  if ( symbolv2 )
263  {
264  QgsOldSymbolMeta meta = readSymbolMeta( symbolnode );
265  QVariant value = QVariant( meta.lowerValue );
266  QString label = meta.label;
267  if ( label.isEmpty() )
268  label = value.toString();
269  cats.append( QgsRendererCategoryV2( value, symbolv2, label, true ) );
270  }
271 
272  symbolnode = symbolnode.nextSibling();
273  }
274 
275  QgsCategorizedSymbolRendererV2* r = new QgsCategorizedSymbolRendererV2( classificationField, cats );
276  // source symbol and color ramp are not set (unknown)
277  return r;
278 }
279 
280 
281 
282 
284 {
285  QDomNode singlenode = layerNode.namedItem( "singlesymbol" );
286  QDomNode graduatednode = layerNode.namedItem( "graduatedsymbol" );
287  QDomNode continuousnode = layerNode.namedItem( "continuoussymbol" );
288  QDomNode uniquevaluenode = layerNode.namedItem( "uniquevalue" );
289 
290  if ( !singlenode.isNull() )
291  {
292  return readOldSingleSymbolRenderer( singlenode, geomType );
293  }
294  else if ( !graduatednode.isNull() )
295  {
296  return readOldGraduatedSymbolRenderer( graduatednode, geomType );
297  }
298  else if ( !continuousnode.isNull() )
299  {
300  return nullptr;
301  }
302  else if ( !uniquevaluenode.isNull() )
303  {
304  return readOldUniqueValueRenderer( uniquevaluenode, geomType );
305  }
306 
307  return nullptr;
308 }
309 
310 
311 /*
312 UNSUPPORTED RENDERER: continuous color
313 
314  QDomNode classnode = rnode.namedItem( "classificationfield" );
315  QDomNode polyoutlinenode = rnode.namedItem( "polygonoutline" );
316  QString polyoutline = polyoutlinenode.toElement().text();
317  if ( polyoutline == "0" )
318  drawPolygonOutline = false;
319  else if ( polyoutline == "1" )
320  drawPolygonOutline = true;
321  QDomNode lowernode = rnode.namedItem( "lowestsymbol" );
322  lowSymbol = readOldSymbol( lowernode.namedItem( "symbol" ), geomType );
323  QDomNode uppernode = rnode.namedItem( "highestsymbol" );
324  highSymbol = readOldSymbol( uppernode.namedItem( "symbol" ), geomType );
325 
326 UNSUPPORTED SYMBOL PROPERTY: point size units
327 
328  QDomNode psizeunitnodes = synode.namedItem( "pointsizeunits" );
329  if ( ! psizeunitnodes.isNull() )
330  {
331  QDomElement psizeunitelement = psizeunitnodes.toElement();
332  QgsDebugMsg( QString( "psizeunitelement:%1" ).arg( psizeunitelement.text() ) );
333  setPointSizeUnits( psizeunitelement.text().compare( "mapunits", Qt::CaseInsensitive ) == 0 );
334  }
335 
336 UNSUPPORTED SYMBOL PROPERTY: data-defined rotation / scale / symbol name
337 
338  rotationClassificationFieldName = synode.namedItem( "rotationclassificationfieldname" ).toElement().text();
339  scaleClassificationFieldName = synode.namedItem( "scaleclassificationfield" ).toElement().text();
340  symbolFieldName = synode.namedItem( "symbolfieldname" ).toElement().text();
341 
342 UNSUPPORTED SYMBOL PROPERTY: texture
343 
344  QDomNode texturepathnode = synode.namedItem( "texturepath" );
345  QDomElement texturepathelement = texturepathnode.toElement();
346  setCustomTexture( QgsProject::instance()->readPath( texturepathelement.text() ) );
347 */
348 
349 
350 
351 
352 
354 {
355  if ( penstyle == Qt::NoPen )
356  {
357  return "NoPen";
358  }
359  else if ( penstyle == Qt::SolidLine )
360  {
361  return "SolidLine";
362  }
363  else if ( penstyle == Qt::DashLine )
364  {
365  return "DashLine";
366  }
367  else if ( penstyle == Qt::DotLine )
368  {
369  return "DotLine";
370  }
371  else if ( penstyle == Qt::DashDotLine )
372  {
373  return "DashDotLine";
374  }
375  else if ( penstyle == Qt::DashDotDotLine )
376  {
377  return "DashDotDotLine";
378  }
379  else if ( penstyle == Qt::MPenStyle )
380  {
381  return "MPenStyle";
382  }
383  else //return a null string
384  {
385  return QString();
386  }
387 }
388 
389 Qt::PenStyle QgsSymbologyV2Conversion::qString2PenStyle( const QString& penString )
390 {
391  if ( penString == "NoPen" )
392  {
393  return Qt::NoPen;
394  }
395  else if ( penString == "SolidLine" )
396  {
397  return Qt::SolidLine;
398  }
399  else if ( penString == "DashLine" )
400  {
401  return Qt::DashLine;
402  }
403  else if ( penString == "DotLine" )
404  {
405  return Qt::DotLine;
406  }
407  else if ( penString == "DashDotLine" )
408  {
409  return Qt::DashDotLine;
410  }
411  else if ( penString == "DashDotDotLine" )
412  {
413  return Qt::DashDotDotLine;
414  }
415  else if ( penString == "MPenStyle" )
416  {
417  return Qt::MPenStyle;
418  }
419  else
420  {
421  return Qt::NoPen;
422  }
423 }
424 
426 {
427  if ( brushstyle == Qt::NoBrush )
428  {
429  return "NoBrush";
430  }
431  else if ( brushstyle == Qt::SolidPattern )
432  {
433  return "SolidPattern";
434  }
435  else if ( brushstyle == Qt::Dense1Pattern )
436  {
437  return "Dense1Pattern";
438  }
439  else if ( brushstyle == Qt::Dense2Pattern )
440  {
441  return "Dense2Pattern";
442  }
443  else if ( brushstyle == Qt::Dense3Pattern )
444  {
445  return "Dense3Pattern";
446  }
447  else if ( brushstyle == Qt::Dense4Pattern )
448  {
449  return "Dense4Pattern";
450  }
451  else if ( brushstyle == Qt::Dense5Pattern )
452  {
453  return "Dense5Pattern";
454  }
455  else if ( brushstyle == Qt::Dense6Pattern )
456  {
457  return "Dense6Pattern";
458  }
459  else if ( brushstyle == Qt::Dense7Pattern )
460  {
461  return "Dense7Pattern";
462  }
463  else if ( brushstyle == Qt::HorPattern )
464  {
465  return "HorPattern";
466  }
467  else if ( brushstyle == Qt::VerPattern )
468  {
469  return "VerPattern";
470  }
471  else if ( brushstyle == Qt::CrossPattern )
472  {
473  return "CrossPattern";
474  }
475  else if ( brushstyle == Qt::BDiagPattern )
476  {
477  return "BDiagPattern";
478  }
479  else if ( brushstyle == Qt::FDiagPattern )
480  {
481  return "FDiagPattern";
482  }
483  else if ( brushstyle == Qt::DiagCrossPattern )
484  {
485  return "DiagCrossPattern";
486  }
487  else if ( brushstyle == Qt::TexturePattern )
488  {
489  return "TexturePattern";
490  }
491  else //return a null string
492  {
493  QgsDebugMsg( "no matching pattern found" );
494  return " ";
495  }
496 }
497 
498 Qt::BrushStyle QgsSymbologyV2Conversion::qString2BrushStyle( const QString& brushString )
499 {
500  if ( brushString == "NoBrush" )
501  {
502  return Qt::NoBrush;
503  }
504  else if ( brushString == "SolidPattern" )
505  {
506  return Qt::SolidPattern;
507  }
508  else if ( brushString == "Dense1Pattern" )
509  {
510  return Qt::Dense1Pattern;
511  }
512  else if ( brushString == "Dense2Pattern" )
513  {
514  return Qt::Dense2Pattern;
515  }
516  else if ( brushString == "Dense3Pattern" )
517  {
518  return Qt::Dense3Pattern;
519  }
520  else if ( brushString == "Dense4Pattern" )
521  {
522  return Qt::Dense4Pattern;
523  }
524  else if ( brushString == "Dense5Pattern" )
525  {
526  return Qt::Dense5Pattern;
527  }
528  else if ( brushString == "Dense6Pattern" )
529  {
530  return Qt::Dense6Pattern;
531  }
532  else if ( brushString == "Dense7Pattern" )
533  {
534  return Qt::Dense7Pattern;
535  }
536  else if ( brushString == "HorPattern" )
537  {
538  return Qt::HorPattern;
539  }
540  else if ( brushString == "VerPattern" )
541  {
542  return Qt::VerPattern;
543  }
544  else if ( brushString == "CrossPattern" )
545  {
546  return Qt::CrossPattern;
547  }
548  else if ( brushString == "BDiagPattern" )
549  {
550  return Qt::BDiagPattern;
551  }
552  else if ( brushString == "FDiagPattern" )
553  {
554  return Qt::FDiagPattern;
555  }
556  else if ( brushString == "DiagCrossPattern" )
557  {
558  return Qt::DiagCrossPattern;
559  }
560  else if ( brushString == "TexturePattern" )
561  {
562  return Qt::TexturePattern;
563  }
564  else //return a null string
565  {
566  QgsDebugMsg( QString( "Brush style \"%1\" not found" ).arg( brushString ) );
567  return Qt::NoBrush;
568  }
569 }
static QString penStyle2QString(Qt::PenStyle penstyle)
static Qt::BrushStyle readBrushStyle(const QDomNode &synode)
GeometryType
Definition: qgis.h:111
static QgsOldSymbolMeta readSymbolMeta(const QDomNode &synode)
QString attribute(const QString &name, const QString &defValue) const
#define QgsDebugMsg(str)
Definition: qgslogger.h:33
static double readOutlineWidth(const QDomNode &synode)
static QgsFeatureRendererV2 * readOldUniqueValueRenderer(const QDomNode &rnode, QGis::GeometryType geomType)
static QgsSymbolV2 * readOldSymbol(const QDomNode &synode, QGis::GeometryType geomType)
double toDouble(bool *ok) const
static float readMarkerSymbolSize(const QDomNode &synode)
static QString readMarkerSymbolName(const QDomNode &synode)
QDomNode nextSibling() const
QDomElement toElement() const
void append(const T &value)
QString text() const
static Qt::PenStyle qString2PenStyle(const QString &string)
static QgsFeatureRendererV2 * readOldSingleSymbolRenderer(const QDomNode &rnode, QGis::GeometryType geomType)
int toInt(bool *ok, int base) const
bool isEmpty() const
bool startsWith(const QString &s, Qt::CaseSensitivity cs) const
static QgsFeatureRendererV2 * readOldGraduatedSymbolRenderer(const QDomNode &rnode, QGis::GeometryType geomType)
double ANALYSIS_EXPORT angle(Point3D *p1, Point3D *p2, Point3D *p3, Point3D *p4)
Calculates the angle between two segments (in 2 dimension, z-values are ignored)
QDomNode namedItem(const QString &name) const
static Qt::BrushStyle qString2BrushStyle(const QString &string)
const double DEFAULT_POINT_SIZE
Magic number that determines the default point size for point symbols.
Definition: qgis.h:378
bool isNull() const
static QColor readSymbolColor(const QDomNode &synode, bool fillColor)
QString mid(int position, int n) const
float toFloat(bool *ok) const
static Qt::PenStyle readOutlineStyle(const QDomNode &synode)
static QString brushStyle2QString(Qt::BrushStyle brushstyle)
static QgsFeatureRendererV2 * readOldRenderer(const QDomNode &layerNode, QGis::GeometryType geomType)
Read old renderer definition from XML and create matching new renderer.
QString arg(qlonglong a, int fieldWidth, int base, const QChar &fillChar) const
QString toString() const