QGIS API Documentation  2.13.0-Master
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 
95  Q_DECL_DEPRECATED static QString encodeOutputUnit( QgsSymbolV2::OutputUnit unit );
97  Q_DECL_DEPRECATED static QgsSymbolV2::OutputUnit decodeOutputUnit( const QString& str );
98 
99  static QString encodeSldUom( QgsSymbolV2::OutputUnit unit, double *scaleFactor );
100  static QgsSymbolV2::OutputUnit decodeSldUom( const QString& str, double *scaleFactor );
101 
102  static QString encodeScaleMethod( QgsSymbolV2::ScaleMethod scaleMethod );
103  static QgsSymbolV2::ScaleMethod decodeScaleMethod( const QString& str );
104 
105  static QPainter::CompositionMode decodeBlendMode( const QString& s );
106 
107  static QIcon symbolPreviewIcon( QgsSymbolV2* symbol, QSize size );
108 
117  static QPicture symbolLayerPreviewPicture( QgsSymbolLayerV2* layer, QgsSymbolV2::OutputUnit units, QSize size, const QgsMapUnitScale& scale = QgsMapUnitScale() );
118  static QIcon symbolLayerPreviewIcon( QgsSymbolLayerV2* layer, QgsSymbolV2::OutputUnit u, QSize size, const QgsMapUnitScale& scale = QgsMapUnitScale() );
119  static QIcon colorRampPreviewIcon( QgsVectorColorRampV2* ramp, QSize size );
120 
121  static void drawStippledBackground( QPainter* painter, QRect rect );
122 
124  static QPixmap symbolPreviewPixmap( QgsSymbolV2* symbol, QSize size, QgsRenderContext* customContext = nullptr );
125  static QPixmap colorRampPreviewPixmap( QgsVectorColorRampV2* ramp, QSize size );
126 
128  static double estimateMaxSymbolBleed( QgsSymbolV2* symbol );
129 
134  static QgsSymbolV2* loadSymbol( const QDomElement& element );
135 
142  template <class SymbolType> static SymbolType* loadSymbol( const QDomElement& element )
143  {
144  QgsSymbolV2* tmpSymbol = QgsSymbolLayerV2Utils::loadSymbol( element );
145  SymbolType* symbolCastToType = dynamic_cast<SymbolType*>( tmpSymbol );
146 
147  if ( symbolCastToType )
148  {
149  return symbolCastToType;
150  }
151  else
152  {
153  //could not cast
154  delete tmpSymbol;
155  return nullptr;
156  }
157  }
158 
159  static QgsSymbolLayerV2* loadSymbolLayer( QDomElement& element );
160  static QDomElement saveSymbol( const QString& symbolName, QgsSymbolV2* symbol, QDomDocument& doc );
161 
166  static QString symbolProperties( QgsSymbolV2* symbol );
167 
168  static bool createSymbolLayerV2ListFromSld( QDomElement& element, QGis::GeometryType geomType, QgsSymbolLayerV2List &layers );
169 
170  static QgsSymbolLayerV2* createFillLayerFromSld( QDomElement &element );
171  static QgsSymbolLayerV2* createLineLayerFromSld( QDomElement &element );
172  static QgsSymbolLayerV2* createMarkerLayerFromSld( QDomElement &element );
173 
174  static bool convertPolygonSymbolizerToPointMarker( QDomElement &element, QgsSymbolLayerV2List &layerList );
175  static bool hasExternalGraphic( QDomElement &element );
176  static bool hasWellKnownMark( QDomElement &element );
177 
178  static bool needFontMarker( QDomElement &element );
179  static bool needSvgMarker( QDomElement &element );
180  static bool needEllipseMarker( QDomElement &element );
181  static bool needMarkerLine( QDomElement &element );
182  static bool needLinePatternFill( QDomElement &element );
183  static bool needPointPatternFill( QDomElement &element );
184  static bool needSvgFill( QDomElement &element );
185 
186  static void fillToSld( QDomDocument &doc, QDomElement &element,
187  Qt::BrushStyle brushStyle, const QColor& color = QColor() );
188  static bool fillFromSld( QDomElement &element,
189  Qt::BrushStyle &brushStyle, QColor &color );
190 
192  static void lineToSld( QDomDocument &doc, QDomElement &element,
193  Qt::PenStyle penStyle, const QColor& color, double width = -1,
194  const Qt::PenJoinStyle *penJoinStyle = nullptr, const Qt::PenCapStyle *penCapStyle = nullptr,
195  const QVector<qreal> *customDashPattern = nullptr, double dashOffset = 0.0 );
196  static bool lineFromSld( QDomElement &element,
197  Qt::PenStyle &penStyle, QColor &color, double &width,
198  Qt::PenJoinStyle *penJoinStyle = nullptr, Qt::PenCapStyle *penCapStyle = nullptr,
199  QVector<qreal> *customDashPattern = nullptr, double *dashOffset = nullptr );
200 
201  static void externalGraphicToSld( QDomDocument &doc, QDomElement &element,
202  const QString& path, const QString& mime,
203  const QColor& color, double size = -1 );
204  static bool externalGraphicFromSld( QDomElement &element,
205  QString &path, QString &mime,
206  QColor &color, double &size );
207 
209  Q_DECL_DEPRECATED static void wellKnownMarkerToSld( QDomDocument &doc, QDomElement &element,
210  const QString& name, const QColor& color, const QColor& borderColor = QColor(),
211  double borderWidth = -1, double size = -1 );
212  static void wellKnownMarkerToSld( QDomDocument &doc, QDomElement &element,
213  const QString& name, const QColor& color, const QColor& borderColor, Qt::PenStyle borderStyle,
214  double borderWidth = -1, double size = -1 );
216  Q_DECL_DEPRECATED static bool wellKnownMarkerFromSld( QDomElement &element,
217  QString &name, QColor &color, QColor &borderColor,
218  double &borderWidth, double &size );
219 
221  static bool wellKnownMarkerFromSld( QDomElement &element,
222  QString &name, QColor &color, QColor &borderColor, Qt::PenStyle &borderStyle,
223  double &borderWidth, double &size );
224 
225  static void externalMarkerToSld( QDomDocument &doc, QDomElement &element,
226  const QString& path, const QString& format, int *markIndex = nullptr,
227  const QColor& color = QColor(), double size = -1 );
228  static bool externalMarkerFromSld( QDomElement &element,
229  QString &path, QString &format, int &markIndex,
230  QColor &color, double &size );
231 
232 
233  static void labelTextToSld( QDomDocument &doc, QDomElement &element, const QString& label,
234  const QFont &font, const QColor& color = QColor(), double size = -1 );
235 
237  static QString ogrFeatureStylePen( double width, double mmScaleFactor, double mapUnitsScaleFactor, const QColor& c,
238  Qt::PenJoinStyle joinStyle = Qt::MiterJoin,
239  Qt::PenCapStyle capStyle = Qt::FlatCap,
240  double offset = 0.0,
241  const QVector<qreal>* dashPattern = nullptr );
244  static QString ogrFeatureStyleBrush( const QColor& fillColr );
245 
246  static void createRotationElement( QDomDocument &doc, QDomElement &element, const QString& rotationFunc );
247  static bool rotationFromSldElement( QDomElement &element, QString &rotationFunc );
248 
249  static void createOpacityElement( QDomDocument &doc, QDomElement &element, const QString& alphaFunc );
250  static bool opacityFromSldElement( QDomElement &element, QString &alphaFunc );
251 
252  static void createDisplacementElement( QDomDocument &doc, QDomElement &element, QPointF offset );
253  static bool displacementFromSldElement( QDomElement &element, QPointF &offset );
254 
255  static void createOnlineResourceElement( QDomDocument &doc, QDomElement &element, const QString& path, const QString& format );
256  static bool onlineResourceFromSldElement( QDomElement &element, QString &path, QString &format );
257 
258  static void createGeometryElement( QDomDocument &doc, QDomElement &element, const QString& geomFunc );
259  static bool geometryFromSldElement( QDomElement &element, QString &geomFunc );
260 
261  static bool createFunctionElement( QDomDocument &doc, QDomElement &element, const QString& function );
262  static bool functionFromSldElement( QDomElement &element, QString &function );
263 
264  static QDomElement createSvgParameterElement( QDomDocument &doc, const QString& name, const QString& value );
265  static QgsStringMap getSvgParameterList( QDomElement &element );
266 
267  static QDomElement createVendorOptionElement( QDomDocument &doc, const QString& name, const QString& value );
268  static QgsStringMap getVendorOptionList( QDomElement &element );
269 
270  static QgsStringMap parseProperties( QDomElement& element );
271  static void saveProperties( QgsStringMap props, QDomDocument& doc, QDomElement& element );
272 
273  static QgsSymbolV2Map loadSymbols( QDomElement& element );
274  static QDomElement saveSymbols( QgsSymbolV2Map& symbols, const QString& tagName, QDomDocument& doc );
275 
276  static void clearSymbolMap( QgsSymbolV2Map& symbols );
277 
278  static QgsVectorColorRampV2* loadColorRamp( QDomElement& element );
279  static QDomElement saveColorRamp( const QString& name, QgsVectorColorRampV2* ramp, QDomDocument& doc );
280 
287  static QString colorToName( const QColor& color );
288 
296  static QList< QColor > parseColorList( const QString& colorStr );
297 
305  static QMimeData *colorToMimeData( const QColor &color );
306 
316  static QColor colorFromMimeData( const QMimeData *data, bool& hasAlpha );
317 
324  static QgsNamedColorList colorListFromMimeData( const QMimeData *data );
325 
333  static QMimeData* colorListToMimeData( const QgsNamedColorList& colorList, const bool allFormats = true );
334 
343  static bool saveColorsToGpl( QFile &file, const QString& paletteName, const QgsNamedColorList& colors );
344 
353  static QgsNamedColorList importColorsFromGpl( QFile &file, bool &ok, QString& name );
354 
363  static QColor parseColor( const QString& colorStr, bool strictEval = false );
364 
374  static QColor parseColorWithAlpha( const QString& colorStr, bool &containsAlpha, bool strictEval = false );
375 
384  static double lineWidthScaleFactor( const QgsRenderContext& c, QgsSymbolV2::OutputUnit u, const QgsMapUnitScale& scale = QgsMapUnitScale() );
385 
395  static double convertToPainterUnits( const QgsRenderContext&c, double size, QgsSymbolV2::OutputUnit unit, const QgsMapUnitScale& scale = QgsMapUnitScale() );
396 
398  static double pixelSizeScaleFactor( const QgsRenderContext& c, QgsSymbolV2::OutputUnit u, const QgsMapUnitScale& scale = QgsMapUnitScale() );
399 
401  static double mapUnitScaleFactor( const QgsRenderContext& c, QgsSymbolV2::OutputUnit u, const QgsMapUnitScale& scale = QgsMapUnitScale() );
402 
404  static QgsRenderContext createRenderContext( QPainter* p );
405 
407  static void multiplyImageOpacity( QImage* image, qreal alpha );
408 
410  static void blurImageInPlace( QImage& image, QRect rect, int radius, bool alphaOnly );
411 
415  static void premultiplyColor( QColor& rgb, int alpha );
416 
418  static void sortVariantList( QList<QVariant>& list, Qt::SortOrder order );
420  static QPointF pointOnLineWithDistance( QPointF startPoint, QPointF directionPoint, double distance );
421 
423  static QStringList listSvgFiles();
424 
426  static QStringList listSvgFilesAt( const QString& directory );
427 
432  static QString symbolNameToPath( QString name );
433 
435  static QString symbolPathToName( QString path );
436 
438  static QPointF polygonCentroid( const QPolygonF& points );
439 
441  static QPointF polygonPointOnSurface( const QPolygonF& points );
442 
444  static bool pointInPolygon( const QPolygonF &points, QPointF point );
445 
452  static QgsExpression* fieldOrExpressionToExpression( const QString& fieldOrExpression );
453 
460  static QString fieldOrExpressionFromExpression( QgsExpression* expression );
461 
467  static QList<double> prettyBreaks( double minimum, double maximum, int classes );
468 
469 };
470 
471 class QPolygonF;
472 
474 QList<QPolygonF> offsetLine( const QPolygonF& polyline, double dist );
476 QList<QPolygonF> offsetLine( QPolygonF polyline, double dist, QGis::GeometryType geometryType );
477 
478 #endif
479 
480 
Class for parsing and evaluation of expressions (formerly called "search strings").
OutputUnit
The unit of the output.
Definition: qgssymbolv2.h:59
QMap< QString, QgsSymbolV2 * > QgsSymbolV2Map
GeometryType
Definition: qgis.h:111
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:84
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.