QGIS API Documentation  2.15.0-Master (972fc9f)
qgssymbollayerv2utils.h
Go to the documentation of this file.
1 /***************************************************************************
2  qgssymbollayerv2utils.h
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 
16 
17 #ifndef QGSSYMBOLLAYERV2UTILS_H
18 #define QGSSYMBOLLAYERV2UTILS_H
19 
20 #include <QMap>
21 #include <Qt>
22 #include <QtCore>
23 #include <QFont>
24 #include <QColor>
25 #include <QPainter>
26 #include "qgssymbolv2.h"
27 #include "qgis.h"
28 #include "qgsmapunitscale.h"
29 
30 class QgsExpression;
31 class QgsSymbolLayerV2;
33 
37 
38 class QDomDocument;
39 class QDomElement;
40 class QIcon;
41 class QPixmap;
42 class QPointF;
43 class QSize;
44 
45 class CORE_EXPORT QgsSymbolLayerV2Utils
46 {
47  public:
48 
49  static QString encodeColor( const QColor& color );
50  static QColor decodeColor( const QString& str );
51 
52  static QString encodeSldAlpha( int alpha );
53  static int decodeSldAlpha( const QString& str );
54 
55  static QString encodeSldFontStyle( QFont::Style style );
56  static QFont::Style decodeSldFontStyle( const QString& str );
57 
58  static QString encodeSldFontWeight( int weight );
59  static int decodeSldFontWeight( const QString& str );
60 
61  static QString encodePenStyle( Qt::PenStyle style );
62  static Qt::PenStyle decodePenStyle( const QString& str );
63 
64  static QString encodePenJoinStyle( Qt::PenJoinStyle style );
65  static Qt::PenJoinStyle decodePenJoinStyle( const QString& str );
66 
67  static QString encodePenCapStyle( Qt::PenCapStyle style );
68  static Qt::PenCapStyle decodePenCapStyle( const QString& str );
69 
70  static QString encodeSldLineJoinStyle( Qt::PenJoinStyle style );
71  static Qt::PenJoinStyle decodeSldLineJoinStyle( const QString& str );
72 
73  static QString encodeSldLineCapStyle( Qt::PenCapStyle style );
74  static Qt::PenCapStyle decodeSldLineCapStyle( const QString& str );
75 
76  static QString encodeBrushStyle( Qt::BrushStyle style );
77  static Qt::BrushStyle decodeBrushStyle( const QString& str );
78 
79  static QString encodeSldBrushStyle( Qt::BrushStyle style );
80  static Qt::BrushStyle decodeSldBrushStyle( const QString& str );
81 
82  static QString encodePoint( QPointF point );
83  static QPointF decodePoint( const QString& str );
84 
85  static QString encodeMapUnitScale( const QgsMapUnitScale& mapUnitScale );
86  static QgsMapUnitScale decodeMapUnitScale( const QString& str );
87 
88  static QString encodeRealVector( const QVector<qreal>& v );
89  static QVector<qreal> decodeRealVector( const QString& s );
90 
91  static QString encodeSldRealVector( const QVector<qreal>& v );
92  static QVector<qreal> decodeSldRealVector( const QString& s );
93 
94  static QString encodeOutputUnit( QgsSymbolV2::OutputUnit unit );
95  static QgsSymbolV2::OutputUnit decodeOutputUnit( const QString& str );
96 
97  static QString encodeSldUom( QgsSymbolV2::OutputUnit unit, double *scaleFactor );
98  static QgsSymbolV2::OutputUnit decodeSldUom( const QString& str, double *scaleFactor );
99 
100  static QString encodeScaleMethod( QgsSymbolV2::ScaleMethod scaleMethod );
101  static QgsSymbolV2::ScaleMethod decodeScaleMethod( const QString& str );
102 
103  static QPainter::CompositionMode decodeBlendMode( const QString& s );
104 
105  static QIcon symbolPreviewIcon( QgsSymbolV2* symbol, QSize size );
106 
115  static QPicture symbolLayerPreviewPicture( QgsSymbolLayerV2* layer, QgsSymbolV2::OutputUnit units, QSize size, const QgsMapUnitScale& scale = QgsMapUnitScale() );
116  static QIcon symbolLayerPreviewIcon( QgsSymbolLayerV2* layer, QgsSymbolV2::OutputUnit u, QSize size, const QgsMapUnitScale& scale = QgsMapUnitScale() );
117  static QIcon colorRampPreviewIcon( QgsVectorColorRampV2* ramp, QSize size );
118 
119  static void drawStippledBackground( QPainter* painter, QRect rect );
120 
122  static QPixmap symbolPreviewPixmap( QgsSymbolV2* symbol, QSize size, QgsRenderContext* customContext = nullptr );
123  static QPixmap colorRampPreviewPixmap( QgsVectorColorRampV2* ramp, QSize size );
124 
126  static double estimateMaxSymbolBleed( QgsSymbolV2* symbol );
127 
132  static QgsSymbolV2* loadSymbol( const QDomElement& element );
133 
140  template <class SymbolType> static SymbolType* loadSymbol( const QDomElement& element )
141  {
142  QgsSymbolV2* tmpSymbol = QgsSymbolLayerV2Utils::loadSymbol( element );
143  SymbolType* symbolCastToType = dynamic_cast<SymbolType*>( tmpSymbol );
144 
145  if ( symbolCastToType )
146  {
147  return symbolCastToType;
148  }
149  else
150  {
151  //could not cast
152  delete tmpSymbol;
153  return nullptr;
154  }
155  }
156 
157  static QgsSymbolLayerV2* loadSymbolLayer( QDomElement& element );
158  static QDomElement saveSymbol( const QString& symbolName, QgsSymbolV2* symbol, QDomDocument& doc );
159 
164  static QString symbolProperties( QgsSymbolV2* symbol );
165 
166  static bool createSymbolLayerV2ListFromSld( QDomElement& element, QGis::GeometryType geomType, QgsSymbolLayerV2List &layers );
167 
168  static QgsSymbolLayerV2* createFillLayerFromSld( QDomElement &element );
169  static QgsSymbolLayerV2* createLineLayerFromSld( QDomElement &element );
170  static QgsSymbolLayerV2* createMarkerLayerFromSld( QDomElement &element );
171 
172  static bool convertPolygonSymbolizerToPointMarker( QDomElement &element, QgsSymbolLayerV2List &layerList );
173  static bool hasExternalGraphic( QDomElement &element );
174  static bool hasWellKnownMark( QDomElement &element );
175 
176  static bool needFontMarker( QDomElement &element );
177  static bool needSvgMarker( QDomElement &element );
178  static bool needEllipseMarker( QDomElement &element );
179  static bool needMarkerLine( QDomElement &element );
180  static bool needLinePatternFill( QDomElement &element );
181  static bool needPointPatternFill( QDomElement &element );
182  static bool needSvgFill( QDomElement &element );
183 
184  static void fillToSld( QDomDocument &doc, QDomElement &element,
185  Qt::BrushStyle brushStyle, const QColor& color = QColor() );
186  static bool fillFromSld( QDomElement &element,
187  Qt::BrushStyle &brushStyle, QColor &color );
188 
190  static void lineToSld( QDomDocument &doc, QDomElement &element,
191  Qt::PenStyle penStyle, const QColor& color, double width = -1,
192  const Qt::PenJoinStyle *penJoinStyle = nullptr, const Qt::PenCapStyle *penCapStyle = nullptr,
193  const QVector<qreal> *customDashPattern = nullptr, double dashOffset = 0.0 );
194  static bool lineFromSld( QDomElement &element,
195  Qt::PenStyle &penStyle, QColor &color, double &width,
196  Qt::PenJoinStyle *penJoinStyle = nullptr, Qt::PenCapStyle *penCapStyle = nullptr,
197  QVector<qreal> *customDashPattern = nullptr, double *dashOffset = nullptr );
198 
199  static void externalGraphicToSld( QDomDocument &doc, QDomElement &element,
200  const QString& path, const QString& mime,
201  const QColor& color, double size = -1 );
202  static bool externalGraphicFromSld( QDomElement &element,
203  QString &path, QString &mime,
204  QColor &color, double &size );
205 
207  Q_DECL_DEPRECATED static void wellKnownMarkerToSld( QDomDocument &doc, QDomElement &element,
208  const QString& name, const QColor& color, const QColor& borderColor = QColor(),
209  double borderWidth = -1, double size = -1 );
210  static void wellKnownMarkerToSld( QDomDocument &doc, QDomElement &element,
211  const QString& name, const QColor& color, const QColor& borderColor, Qt::PenStyle borderStyle,
212  double borderWidth = -1, double size = -1 );
214  Q_DECL_DEPRECATED static bool wellKnownMarkerFromSld( QDomElement &element,
215  QString &name, QColor &color, QColor &borderColor,
216  double &borderWidth, double &size );
217 
219  static bool wellKnownMarkerFromSld( QDomElement &element,
220  QString &name, QColor &color, QColor &borderColor, Qt::PenStyle &borderStyle,
221  double &borderWidth, double &size );
222 
223  static void externalMarkerToSld( QDomDocument &doc, QDomElement &element,
224  const QString& path, const QString& format, int *markIndex = nullptr,
225  const QColor& color = QColor(), double size = -1 );
226  static bool externalMarkerFromSld( QDomElement &element,
227  QString &path, QString &format, int &markIndex,
228  QColor &color, double &size );
229 
230 
231  static void labelTextToSld( QDomDocument &doc, QDomElement &element, const QString& label,
232  const QFont &font, const QColor& color = QColor(), double size = -1 );
233 
235  static QString ogrFeatureStylePen( double width, double mmScaleFactor, double mapUnitsScaleFactor, const QColor& c,
236  Qt::PenJoinStyle joinStyle = Qt::MiterJoin,
237  Qt::PenCapStyle capStyle = Qt::FlatCap,
238  double offset = 0.0,
239  const QVector<qreal>* dashPattern = nullptr );
242  static QString ogrFeatureStyleBrush( const QColor& fillColr );
243 
244  static void createRotationElement( QDomDocument &doc, QDomElement &element, const QString& rotationFunc );
245  static bool rotationFromSldElement( QDomElement &element, QString &rotationFunc );
246 
247  static void createOpacityElement( QDomDocument &doc, QDomElement &element, const QString& alphaFunc );
248  static bool opacityFromSldElement( QDomElement &element, QString &alphaFunc );
249 
250  static void createDisplacementElement( QDomDocument &doc, QDomElement &element, QPointF offset );
251  static bool displacementFromSldElement( QDomElement &element, QPointF &offset );
252 
253  static void createOnlineResourceElement( QDomDocument &doc, QDomElement &element, const QString& path, const QString& format );
254  static bool onlineResourceFromSldElement( QDomElement &element, QString &path, QString &format );
255 
256  static void createGeometryElement( QDomDocument &doc, QDomElement &element, const QString& geomFunc );
257  static bool geometryFromSldElement( QDomElement &element, QString &geomFunc );
258 
259  static bool createFunctionElement( QDomDocument &doc, QDomElement &element, const QString& function );
260  static bool functionFromSldElement( QDomElement &element, QString &function );
261 
262  static QDomElement createSvgParameterElement( QDomDocument &doc, const QString& name, const QString& value );
263  static QgsStringMap getSvgParameterList( QDomElement &element );
264 
265  static QDomElement createVendorOptionElement( QDomDocument &doc, const QString& name, const QString& value );
266  static QgsStringMap getVendorOptionList( QDomElement &element );
267 
268  static QgsStringMap parseProperties( QDomElement& element );
269  static void saveProperties( QgsStringMap props, QDomDocument& doc, QDomElement& element );
270 
271  static QgsSymbolV2Map loadSymbols( QDomElement& element );
272  static QDomElement saveSymbols( QgsSymbolV2Map& symbols, const QString& tagName, QDomDocument& doc );
273 
274  static void clearSymbolMap( QgsSymbolV2Map& symbols );
275 
276  static QgsVectorColorRampV2* loadColorRamp( QDomElement& element );
277  static QDomElement saveColorRamp( const QString& name, QgsVectorColorRampV2* ramp, QDomDocument& doc );
278 
285  static QString colorToName( const QColor& color );
286 
294  static QList< QColor > parseColorList( const QString& colorStr );
295 
303  static QMimeData *colorToMimeData( const QColor &color );
304 
314  static QColor colorFromMimeData( const QMimeData *data, bool& hasAlpha );
315 
322  static QgsNamedColorList colorListFromMimeData( const QMimeData *data );
323 
331  static QMimeData* colorListToMimeData( const QgsNamedColorList& colorList, const bool allFormats = true );
332 
341  static bool saveColorsToGpl( QFile &file, const QString& paletteName, const QgsNamedColorList& colors );
342 
351  static QgsNamedColorList importColorsFromGpl( QFile &file, bool &ok, QString& name );
352 
361  static QColor parseColor( const QString& colorStr, bool strictEval = false );
362 
372  static QColor parseColorWithAlpha( const QString& colorStr, bool &containsAlpha, bool strictEval = false );
373 
382  static double lineWidthScaleFactor( const QgsRenderContext& c, QgsSymbolV2::OutputUnit u, const QgsMapUnitScale& scale = QgsMapUnitScale() );
383 
394  static double convertToPainterUnits( const QgsRenderContext&c, double size, QgsSymbolV2::OutputUnit unit, const QgsMapUnitScale& scale = QgsMapUnitScale() );
395 
405  static double convertToMapUnits( const QgsRenderContext&c, double size, QgsSymbolV2::OutputUnit unit, const QgsMapUnitScale& scale = QgsMapUnitScale() );
406 
408  static double pixelSizeScaleFactor( const QgsRenderContext& c, QgsSymbolV2::OutputUnit u, const QgsMapUnitScale& scale = QgsMapUnitScale() );
409 
411  static double mapUnitScaleFactor( const QgsRenderContext& c, QgsSymbolV2::OutputUnit u, const QgsMapUnitScale& scale = QgsMapUnitScale() );
412 
414  static QgsRenderContext createRenderContext( QPainter* p );
415 
417  static void multiplyImageOpacity( QImage* image, qreal alpha );
418 
420  static void blurImageInPlace( QImage& image, QRect rect, int radius, bool alphaOnly );
421 
425  static void premultiplyColor( QColor& rgb, int alpha );
426 
428  static void sortVariantList( QList<QVariant>& list, Qt::SortOrder order );
430  static QPointF pointOnLineWithDistance( QPointF startPoint, QPointF directionPoint, double distance );
431 
433  static QStringList listSvgFiles();
434 
436  static QStringList listSvgFilesAt( const QString& directory );
437 
442  static QString symbolNameToPath( QString name );
443 
445  static QString symbolPathToName( QString path );
446 
448  static QPointF polygonCentroid( const QPolygonF& points );
449 
451  static QPointF polygonPointOnSurface( const QPolygonF& points );
452 
454  static bool pointInPolygon( const QPolygonF &points, QPointF point );
455 
462  static QgsExpression* fieldOrExpressionToExpression( const QString& fieldOrExpression );
463 
470  static QString fieldOrExpressionFromExpression( QgsExpression* expression );
471 
477  static QList<double> prettyBreaks( double minimum, double maximum, int classes );
478 
479 };
480 
481 class QPolygonF;
482 
484 QList<QPolygonF> offsetLine( const QPolygonF& polyline, double dist );
486 QList<QPolygonF> offsetLine( QPolygonF polyline, double dist, QGis::GeometryType geometryType );
487 
488 #endif
489 
490 
Class for parsing and evaluation of expressions (formerly called "search strings").
OutputUnit
The unit of the output.
Definition: qgssymbolv2.h:62
QMap< QString, QgsSymbolV2 * > QgsSymbolV2Map
GeometryType
Definition: qgis.h:115
QList< QPolygonF > offsetLine(const QPolygonF &polyline, double dist)
static QgsSymbolV2 * loadSymbol(const QDomElement &element)
Attempts to load a symbol from a DOM element.
QMap< QString, QString > QgsStringMap
Contains information about the context of a rendering operation.
ScaleMethod
Scale method.
Definition: qgssymbolv2.h:87
Struct for storing maximum and minimum scales for measurements in map units.
QList< QPair< QColor, QString > > QgsNamedColorList
static SymbolType * loadSymbol(const QDomElement &element)
Attempts to load a symbol from a DOM element and cast it to a particular symbol type.
Abstract base class for color ramps.