QGIS API Documentation  2.17.0-Master (6f7b933)
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 
48 class CORE_EXPORT QgsSymbolLayerV2Utils
49 {
50  public:
51 
52  static QString encodeColor( const QColor& color );
53  static QColor decodeColor( const QString& str );
54 
55  static QString encodeSldAlpha( int alpha );
56  static int decodeSldAlpha( const QString& str );
57 
58  static QString encodeSldFontStyle( QFont::Style style );
59  static QFont::Style decodeSldFontStyle( const QString& str );
60 
61  static QString encodeSldFontWeight( int weight );
62  static int decodeSldFontWeight( const QString& str );
63 
64  static QString encodePenStyle( Qt::PenStyle style );
65  static Qt::PenStyle decodePenStyle( const QString& str );
66 
67  static QString encodePenJoinStyle( Qt::PenJoinStyle style );
68  static Qt::PenJoinStyle decodePenJoinStyle( const QString& str );
69 
70  static QString encodePenCapStyle( Qt::PenCapStyle style );
71  static Qt::PenCapStyle decodePenCapStyle( const QString& str );
72 
73  static QString encodeSldLineJoinStyle( Qt::PenJoinStyle style );
74  static Qt::PenJoinStyle decodeSldLineJoinStyle( const QString& str );
75 
76  static QString encodeSldLineCapStyle( Qt::PenCapStyle style );
77  static Qt::PenCapStyle decodeSldLineCapStyle( const QString& str );
78 
79  static QString encodeBrushStyle( Qt::BrushStyle style );
80  static Qt::BrushStyle decodeBrushStyle( const QString& str );
81 
82  static QString encodeSldBrushStyle( Qt::BrushStyle style );
83  static Qt::BrushStyle decodeSldBrushStyle( const QString& str );
84 
85  static QString encodePoint( QPointF point );
86  static QPointF decodePoint( const QString& str );
87 
88  static QString encodeMapUnitScale( const QgsMapUnitScale& mapUnitScale );
89  static QgsMapUnitScale decodeMapUnitScale( const QString& str );
90 
91  static QString encodeRealVector( const QVector<qreal>& v );
92  static QVector<qreal> decodeRealVector( const QString& s );
93 
94  static QString encodeSldRealVector( const QVector<qreal>& v );
95  static QVector<qreal> decodeSldRealVector( const QString& s );
96 
97  static QString encodeOutputUnit( QgsSymbolV2::OutputUnit unit );
98  static QgsSymbolV2::OutputUnit decodeOutputUnit( const QString& str );
99 
100  static QString encodeSldUom( QgsSymbolV2::OutputUnit unit, double *scaleFactor );
101  static QgsSymbolV2::OutputUnit decodeSldUom( const QString& str, double *scaleFactor );
102 
103  static QString encodeScaleMethod( QgsSymbolV2::ScaleMethod scaleMethod );
104  static QgsSymbolV2::ScaleMethod decodeScaleMethod( const QString& str );
105 
106  static QPainter::CompositionMode decodeBlendMode( const QString& s );
107 
108  static QIcon symbolPreviewIcon( QgsSymbolV2* symbol, QSize size );
109 
118  static QPicture symbolLayerPreviewPicture( QgsSymbolLayerV2* layer, QgsSymbolV2::OutputUnit units, QSize size, const QgsMapUnitScale& scale = QgsMapUnitScale() );
119  static QIcon symbolLayerPreviewIcon( QgsSymbolLayerV2* layer, QgsSymbolV2::OutputUnit u, QSize size, const QgsMapUnitScale& scale = QgsMapUnitScale() );
120  static QIcon colorRampPreviewIcon( QgsVectorColorRampV2* ramp, QSize size );
121 
122  static void drawStippledBackground( QPainter* painter, QRect rect );
123 
125  static QPixmap symbolPreviewPixmap( QgsSymbolV2* symbol, QSize size, QgsRenderContext* customContext = nullptr );
126  static QPixmap colorRampPreviewPixmap( QgsVectorColorRampV2* ramp, QSize size );
127 
129  static double estimateMaxSymbolBleed( QgsSymbolV2* symbol );
130 
135  static QgsSymbolV2* loadSymbol( const QDomElement& element );
136 
143  template <class SymbolType> static SymbolType* loadSymbol( const QDomElement& element )
144  {
145  QgsSymbolV2* tmpSymbol = QgsSymbolLayerV2Utils::loadSymbol( element );
146  SymbolType* symbolCastToType = dynamic_cast<SymbolType*>( tmpSymbol );
147 
148  if ( symbolCastToType )
149  {
150  return symbolCastToType;
151  }
152  else
153  {
154  //could not cast
155  delete tmpSymbol;
156  return nullptr;
157  }
158  }
159 
160  static QgsSymbolLayerV2* loadSymbolLayer( QDomElement& element );
161  static QDomElement saveSymbol( const QString& symbolName, QgsSymbolV2* symbol, QDomDocument& doc );
162 
167  static QString symbolProperties( QgsSymbolV2* symbol );
168 
169  static bool createSymbolLayerV2ListFromSld( QDomElement& element, QGis::GeometryType geomType, QgsSymbolLayerV2List &layers );
170 
171  static QgsSymbolLayerV2* createFillLayerFromSld( QDomElement &element );
172  static QgsSymbolLayerV2* createLineLayerFromSld( QDomElement &element );
173  static QgsSymbolLayerV2* createMarkerLayerFromSld( QDomElement &element );
174 
175  static bool convertPolygonSymbolizerToPointMarker( QDomElement &element, QgsSymbolLayerV2List &layerList );
176  static bool hasExternalGraphic( QDomElement &element );
177  static bool hasWellKnownMark( QDomElement &element );
178 
179  static bool needFontMarker( QDomElement &element );
180  static bool needSvgMarker( QDomElement &element );
181  static bool needEllipseMarker( QDomElement &element );
182  static bool needMarkerLine( QDomElement &element );
183  static bool needLinePatternFill( QDomElement &element );
184  static bool needPointPatternFill( QDomElement &element );
185  static bool needSvgFill( QDomElement &element );
186 
187  static void fillToSld( QDomDocument &doc, QDomElement &element,
188  Qt::BrushStyle brushStyle, const QColor& color = QColor() );
189  static bool fillFromSld( QDomElement &element,
190  Qt::BrushStyle &brushStyle, QColor &color );
191 
193  static void lineToSld( QDomDocument &doc, QDomElement &element,
194  Qt::PenStyle penStyle, const QColor& color, double width = -1,
195  const Qt::PenJoinStyle *penJoinStyle = nullptr, const Qt::PenCapStyle *penCapStyle = nullptr,
196  const QVector<qreal> *customDashPattern = nullptr, double dashOffset = 0.0 );
197  static bool lineFromSld( QDomElement &element,
198  Qt::PenStyle &penStyle, QColor &color, double &width,
199  Qt::PenJoinStyle *penJoinStyle = nullptr, Qt::PenCapStyle *penCapStyle = nullptr,
200  QVector<qreal> *customDashPattern = nullptr, double *dashOffset = nullptr );
201 
202  static void externalGraphicToSld( QDomDocument &doc, QDomElement &element,
203  const QString& path, const QString& mime,
204  const QColor& color, double size = -1 );
205  static bool externalGraphicFromSld( QDomElement &element,
206  QString &path, QString &mime,
207  QColor &color, double &size );
208 
210  Q_DECL_DEPRECATED static void wellKnownMarkerToSld( QDomDocument &doc, QDomElement &element,
211  const QString& name, const QColor& color, const QColor& borderColor = QColor(),
212  double borderWidth = -1, double size = -1 );
213  static void wellKnownMarkerToSld( QDomDocument &doc, QDomElement &element,
214  const QString& name, const QColor& color, const QColor& borderColor, Qt::PenStyle borderStyle,
215  double borderWidth = -1, double size = -1 );
217  Q_DECL_DEPRECATED static bool wellKnownMarkerFromSld( QDomElement &element,
218  QString &name, QColor &color, QColor &borderColor,
219  double &borderWidth, double &size );
220 
222  static bool wellKnownMarkerFromSld( QDomElement &element,
223  QString &name, QColor &color, QColor &borderColor, Qt::PenStyle &borderStyle,
224  double &borderWidth, double &size );
225 
226  static void externalMarkerToSld( QDomDocument &doc, QDomElement &element,
227  const QString& path, const QString& format, int *markIndex = nullptr,
228  const QColor& color = QColor(), double size = -1 );
229  static bool externalMarkerFromSld( QDomElement &element,
230  QString &path, QString &format, int &markIndex,
231  QColor &color, double &size );
232 
233 
234  static void labelTextToSld( QDomDocument &doc, QDomElement &element, const QString& label,
235  const QFont &font, const QColor& color = QColor(), double size = -1 );
236 
238  static QString ogrFeatureStylePen( double width, double mmScaleFactor, double mapUnitsScaleFactor, const QColor& c,
239  Qt::PenJoinStyle joinStyle = Qt::MiterJoin,
240  Qt::PenCapStyle capStyle = Qt::FlatCap,
241  double offset = 0.0,
242  const QVector<qreal>* dashPattern = nullptr );
245  static QString ogrFeatureStyleBrush( const QColor& fillColr );
246 
247  static void createRotationElement( QDomDocument &doc, QDomElement &element, const QString& rotationFunc );
248  static bool rotationFromSldElement( QDomElement &element, QString &rotationFunc );
249 
250  static void createOpacityElement( QDomDocument &doc, QDomElement &element, const QString& alphaFunc );
251  static bool opacityFromSldElement( QDomElement &element, QString &alphaFunc );
252 
253  static void createDisplacementElement( QDomDocument &doc, QDomElement &element, QPointF offset );
254  static bool displacementFromSldElement( QDomElement &element, QPointF &offset );
255 
256  static void createOnlineResourceElement( QDomDocument &doc, QDomElement &element, const QString& path, const QString& format );
257  static bool onlineResourceFromSldElement( QDomElement &element, QString &path, QString &format );
258 
259  static void createGeometryElement( QDomDocument &doc, QDomElement &element, const QString& geomFunc );
260  static bool geometryFromSldElement( QDomElement &element, QString &geomFunc );
261 
262  static bool createFunctionElement( QDomDocument &doc, QDomElement &element, const QString& function );
263  static bool functionFromSldElement( QDomElement &element, QString &function );
264 
265  static QDomElement createSvgParameterElement( QDomDocument &doc, const QString& name, const QString& value );
266  static QgsStringMap getSvgParameterList( QDomElement &element );
267 
268  static QDomElement createVendorOptionElement( QDomDocument &doc, const QString& name, const QString& value );
269  static QgsStringMap getVendorOptionList( QDomElement &element );
270 
271  static QgsStringMap parseProperties( QDomElement& element );
272  static void saveProperties( QgsStringMap props, QDomDocument& doc, QDomElement& element );
273 
274  static QgsSymbolV2Map loadSymbols( QDomElement& element );
275  static QDomElement saveSymbols( QgsSymbolV2Map& symbols, const QString& tagName, QDomDocument& doc );
276 
277  static void clearSymbolMap( QgsSymbolV2Map& symbols );
278 
279  static QgsVectorColorRampV2* loadColorRamp( QDomElement& element );
280  static QDomElement saveColorRamp( const QString& name, QgsVectorColorRampV2* ramp, QDomDocument& doc );
281 
288  static QString colorToName( const QColor& color );
289 
297  static QList< QColor > parseColorList( const QString& colorStr );
298 
306  static QMimeData *colorToMimeData( const QColor &color );
307 
317  static QColor colorFromMimeData( const QMimeData *data, bool& hasAlpha );
318 
325  static QgsNamedColorList colorListFromMimeData( const QMimeData *data );
326 
334  static QMimeData* colorListToMimeData( const QgsNamedColorList& colorList, const bool allFormats = true );
335 
344  static bool saveColorsToGpl( QFile &file, const QString& paletteName, const QgsNamedColorList& colors );
345 
354  static QgsNamedColorList importColorsFromGpl( QFile &file, bool &ok, QString& name );
355 
364  static QColor parseColor( const QString& colorStr, bool strictEval = false );
365 
375  static QColor parseColorWithAlpha( const QString& colorStr, bool &containsAlpha, bool strictEval = false );
376 
385  static double lineWidthScaleFactor( const QgsRenderContext& c, QgsSymbolV2::OutputUnit u, const QgsMapUnitScale& scale = QgsMapUnitScale() );
386 
397  static double convertToPainterUnits( const QgsRenderContext&c, double size, QgsSymbolV2::OutputUnit unit, const QgsMapUnitScale& scale = QgsMapUnitScale() );
398 
408  static double convertToMapUnits( const QgsRenderContext&c, double size, QgsSymbolV2::OutputUnit unit, const QgsMapUnitScale& scale = QgsMapUnitScale() );
409 
411  static double pixelSizeScaleFactor( const QgsRenderContext& c, QgsSymbolV2::OutputUnit u, const QgsMapUnitScale& scale = QgsMapUnitScale() );
412 
414  static double mapUnitScaleFactor( const QgsRenderContext& c, QgsSymbolV2::OutputUnit u, const QgsMapUnitScale& scale = QgsMapUnitScale() );
415 
417  static QgsRenderContext createRenderContext( QPainter* p );
418 
420  static void multiplyImageOpacity( QImage* image, qreal alpha );
421 
423  static void blurImageInPlace( QImage& image, QRect rect, int radius, bool alphaOnly );
424 
428  static void premultiplyColor( QColor& rgb, int alpha );
429 
431  static void sortVariantList( QList<QVariant>& list, Qt::SortOrder order );
433  static QPointF pointOnLineWithDistance( QPointF startPoint, QPointF directionPoint, double distance );
434 
436  static QStringList listSvgFiles();
437 
439  static QStringList listSvgFilesAt( const QString& directory );
440 
445  static QString symbolNameToPath( QString name );
446 
448  static QString symbolPathToName( QString path );
449 
451  static QPointF polygonCentroid( const QPolygonF& points );
452 
454  static QPointF polygonPointOnSurface( const QPolygonF& points );
455 
457  static bool pointInPolygon( const QPolygonF &points, QPointF point );
458 
465  static QgsExpression* fieldOrExpressionToExpression( const QString& fieldOrExpression );
466 
473  static QString fieldOrExpressionFromExpression( QgsExpression* expression );
474 
480  static QList<double> prettyBreaks( double minimum, double maximum, int classes );
481 
482 };
483 
484 class QPolygonF;
485 
487 QList<QPolygonF> offsetLine( const QPolygonF& polyline, double dist );
489 QList<QPolygonF> offsetLine( QPolygonF polyline, double dist, QGis::GeometryType geometryType );
490 
491 #endif
492 
493 
Class for parsing and evaluation of expressions (formerly called "search strings").
OutputUnit
The unit of the output.
Definition: qgssymbolv2.h:65
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:90
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.