QGIS API Documentation  2.18.21-Las Palmas (9fba24a)
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 
112  static double sizeInPixelsFromSldUom( const QString &uom, double size );
113 
114  static QPainter::CompositionMode decodeBlendMode( const QString& s );
115 
116  static QIcon symbolPreviewIcon( QgsSymbolV2* symbol, QSize size );
117 
126  static QPicture symbolLayerPreviewPicture( QgsSymbolLayerV2* layer, QgsSymbolV2::OutputUnit units, QSize size, const QgsMapUnitScale& scale = QgsMapUnitScale() );
127  static QIcon symbolLayerPreviewIcon( QgsSymbolLayerV2* layer, QgsSymbolV2::OutputUnit u, QSize size, const QgsMapUnitScale& scale = QgsMapUnitScale() );
128  static QIcon colorRampPreviewIcon( QgsVectorColorRampV2* ramp, QSize size );
129 
130  static void drawStippledBackground( QPainter* painter, QRect rect );
131 
133  static QPixmap symbolPreviewPixmap( QgsSymbolV2* symbol, QSize size, QgsRenderContext* customContext = nullptr );
134  static QPixmap colorRampPreviewPixmap( QgsVectorColorRampV2* ramp, QSize size );
135 
137  static double estimateMaxSymbolBleed( QgsSymbolV2* symbol );
138 
143  static QgsSymbolV2* loadSymbol( const QDomElement& element );
144 
151  template <class SymbolType> static SymbolType* loadSymbol( const QDomElement& element )
152  {
153  QgsSymbolV2* tmpSymbol = QgsSymbolLayerV2Utils::loadSymbol( element );
154  SymbolType* symbolCastToType = dynamic_cast<SymbolType*>( tmpSymbol );
155 
156  if ( symbolCastToType )
157  {
158  return symbolCastToType;
159  }
160  else
161  {
162  //could not cast
163  delete tmpSymbol;
164  return nullptr;
165  }
166  }
167 
168  static QgsSymbolLayerV2* loadSymbolLayer( QDomElement& element );
169  static QDomElement saveSymbol( const QString& symbolName, QgsSymbolV2* symbol, QDomDocument& doc );
170 
175  static QString symbolProperties( QgsSymbolV2* symbol );
176 
177  static bool createSymbolLayerV2ListFromSld( QDomElement& element, QGis::GeometryType geomType, QgsSymbolLayerV2List &layers );
178 
179  static QgsSymbolLayerV2* createFillLayerFromSld( QDomElement &element );
180  static QgsSymbolLayerV2* createLineLayerFromSld( QDomElement &element );
181  static QgsSymbolLayerV2* createMarkerLayerFromSld( QDomElement &element );
182 
183  static bool convertPolygonSymbolizerToPointMarker( QDomElement &element, QgsSymbolLayerV2List &layerList );
184  static bool hasExternalGraphic( QDomElement &element );
185  static bool hasWellKnownMark( QDomElement &element );
186 
187  static bool needFontMarker( QDomElement &element );
188  static bool needSvgMarker( QDomElement &element );
189  static bool needEllipseMarker( QDomElement &element );
190  static bool needMarkerLine( QDomElement &element );
191  static bool needLinePatternFill( QDomElement &element );
192  static bool needPointPatternFill( QDomElement &element );
193  static bool needSvgFill( QDomElement &element );
194 
195  static void fillToSld( QDomDocument &doc, QDomElement &element,
196  Qt::BrushStyle brushStyle, const QColor& color = QColor() );
197  static bool fillFromSld( QDomElement &element,
198  Qt::BrushStyle &brushStyle, QColor &color );
199 
201  static void lineToSld( QDomDocument &doc, QDomElement &element,
202  Qt::PenStyle penStyle, const QColor& color, double width = -1,
203  const Qt::PenJoinStyle *penJoinStyle = nullptr, const Qt::PenCapStyle *penCapStyle = nullptr,
204  const QVector<qreal> *customDashPattern = nullptr, double dashOffset = 0.0 );
205  static bool lineFromSld( QDomElement &element,
206  Qt::PenStyle &penStyle, QColor &color, double &width,
207  Qt::PenJoinStyle *penJoinStyle = nullptr, Qt::PenCapStyle *penCapStyle = nullptr,
208  QVector<qreal> *customDashPattern = nullptr, double *dashOffset = nullptr );
209 
210  static void externalGraphicToSld( QDomDocument &doc, QDomElement &element,
211  const QString& path, const QString& mime,
212  const QColor& color, double size = -1 );
213  static bool externalGraphicFromSld( QDomElement &element,
214  QString &path, QString &mime,
215  QColor &color, double &size );
216 
218  Q_DECL_DEPRECATED static void wellKnownMarkerToSld( QDomDocument &doc, QDomElement &element,
219  const QString& name, const QColor& color, const QColor& borderColor = QColor(),
220  double borderWidth = -1, double size = -1 );
221  static void wellKnownMarkerToSld( QDomDocument &doc, QDomElement &element,
222  const QString& name, const QColor& color, const QColor& borderColor, Qt::PenStyle borderStyle,
223  double borderWidth = -1, double size = -1 );
225  Q_DECL_DEPRECATED static bool wellKnownMarkerFromSld( QDomElement &element,
226  QString &name, QColor &color, QColor &borderColor,
227  double &borderWidth, double &size );
228 
230  static bool wellKnownMarkerFromSld( QDomElement &element,
231  QString &name, QColor &color, QColor &borderColor, Qt::PenStyle &borderStyle,
232  double &borderWidth, double &size );
233 
234  static void externalMarkerToSld( QDomDocument &doc, QDomElement &element,
235  const QString& path, const QString& format, int *markIndex = nullptr,
236  const QColor& color = QColor(), double size = -1 );
237  static bool externalMarkerFromSld( QDomElement &element,
238  QString &path, QString &format, int &markIndex,
239  QColor &color, double &size );
240 
241 
242  static void labelTextToSld( QDomDocument &doc, QDomElement &element, const QString& label,
243  const QFont &font, const QColor& color = QColor(), double size = -1 );
244 
246  static QString ogrFeatureStylePen( double width, double mmScaleFactor, double mapUnitsScaleFactor, const QColor& c,
247  Qt::PenJoinStyle joinStyle = Qt::MiterJoin,
248  Qt::PenCapStyle capStyle = Qt::FlatCap,
249  double offset = 0.0,
250  const QVector<qreal>* dashPattern = nullptr );
253  static QString ogrFeatureStyleBrush( const QColor& fillColr );
254 
255  static void createRotationElement( QDomDocument &doc, QDomElement &element, const QString& rotationFunc );
256  static bool rotationFromSldElement( QDomElement &element, QString &rotationFunc );
257 
258  static void createOpacityElement( QDomDocument &doc, QDomElement &element, const QString& alphaFunc );
259  static bool opacityFromSldElement( QDomElement &element, QString &alphaFunc );
260 
261  static void createDisplacementElement( QDomDocument &doc, QDomElement &element, QPointF offset );
262  static bool displacementFromSldElement( QDomElement &element, QPointF &offset );
263 
264  static void createOnlineResourceElement( QDomDocument &doc, QDomElement &element, const QString& path, const QString& format );
265  static bool onlineResourceFromSldElement( QDomElement &element, QString &path, QString &format );
266 
267  static void createGeometryElement( QDomDocument &doc, QDomElement &element, const QString& geomFunc );
268  static bool geometryFromSldElement( QDomElement &element, QString &geomFunc );
269 
277  static bool createExpressionElement( QDomDocument &doc, QDomElement &element, const QString& function );
278  static bool createFunctionElement( QDomDocument &doc, QDomElement &element, const QString& function );
279  static bool functionFromSldElement( QDomElement &element, QString &function );
280 
281  static QDomElement createSvgParameterElement( QDomDocument &doc, const QString& name, const QString& value );
282  static QgsStringMap getSvgParameterList( QDomElement &element );
283 
284  static QDomElement createVendorOptionElement( QDomDocument &doc, const QString& name, const QString& value );
285  static QgsStringMap getVendorOptionList( QDomElement &element );
286 
287  static QgsStringMap parseProperties( QDomElement& element );
288  static void saveProperties( QgsStringMap props, QDomDocument& doc, QDomElement& element );
289 
290  static QgsSymbolV2Map loadSymbols( QDomElement& element );
291  static QDomElement saveSymbols( QgsSymbolV2Map& symbols, const QString& tagName, QDomDocument& doc );
292 
293  static void clearSymbolMap( QgsSymbolV2Map& symbols );
294 
295  static QgsVectorColorRampV2* loadColorRamp( QDomElement& element );
296  static QDomElement saveColorRamp( const QString& name, QgsVectorColorRampV2* ramp, QDomDocument& doc );
297 
304  static QString colorToName( const QColor& color );
305 
313  static QList< QColor > parseColorList( const QString& colorStr );
314 
322  static QMimeData *colorToMimeData( const QColor &color );
323 
333  static QColor colorFromMimeData( const QMimeData *data, bool& hasAlpha );
334 
341  static QgsNamedColorList colorListFromMimeData( const QMimeData *data );
342 
350  static QMimeData* colorListToMimeData( const QgsNamedColorList& colorList, const bool allFormats = true );
351 
360  static bool saveColorsToGpl( QFile &file, const QString& paletteName, const QgsNamedColorList& colors );
361 
370  static QgsNamedColorList importColorsFromGpl( QFile &file, bool &ok, QString& name );
371 
380  static QColor parseColor( const QString& colorStr, bool strictEval = false );
381 
391  static QColor parseColorWithAlpha( const QString& colorStr, bool &containsAlpha, bool strictEval = false );
392 
401  static double lineWidthScaleFactor( const QgsRenderContext& c, QgsSymbolV2::OutputUnit u, const QgsMapUnitScale& scale = QgsMapUnitScale() );
402 
413  static double convertToPainterUnits( const QgsRenderContext&c, double size, QgsSymbolV2::OutputUnit unit, const QgsMapUnitScale& scale = QgsMapUnitScale() );
414 
424  static double convertToMapUnits( const QgsRenderContext&c, double size, QgsSymbolV2::OutputUnit unit, const QgsMapUnitScale& scale = QgsMapUnitScale() );
425 
427  static double pixelSizeScaleFactor( const QgsRenderContext& c, QgsSymbolV2::OutputUnit u, const QgsMapUnitScale& scale = QgsMapUnitScale() );
428 
430  static double mapUnitScaleFactor( const QgsRenderContext& c, QgsSymbolV2::OutputUnit u, const QgsMapUnitScale& scale = QgsMapUnitScale() );
431 
433  static QgsRenderContext createRenderContext( QPainter* p );
434 
436  static void multiplyImageOpacity( QImage* image, qreal alpha );
437 
439  static void blurImageInPlace( QImage& image, QRect rect, int radius, bool alphaOnly );
440 
444  static void premultiplyColor( QColor& rgb, int alpha );
445 
447  static void sortVariantList( QList<QVariant>& list, Qt::SortOrder order );
449  static QPointF pointOnLineWithDistance( QPointF startPoint, QPointF directionPoint, double distance );
450 
452  static QStringList listSvgFiles();
453 
455  static QStringList listSvgFilesAt( const QString& directory );
456 
461  static QString symbolNameToPath( QString name );
462 
464  static QString symbolPathToName( QString path );
465 
467  static QPointF polygonCentroid( const QPolygonF& points );
468 
470  static QPointF polygonPointOnSurface( const QPolygonF& points );
471 
473  static bool pointInPolygon( const QPolygonF &points, QPointF point );
474 
481  static QgsExpression* fieldOrExpressionToExpression( const QString& fieldOrExpression );
482 
489  static QString fieldOrExpressionFromExpression( QgsExpression* expression );
490 
496  static QList<double> prettyBreaks( double minimum, double maximum, int classes );
497 
502  static double rescaleUom( double size, QgsSymbolV2::OutputUnit unit, const QgsStringMap& props );
503 
508  static QPointF rescaleUom( const QPointF& point, QgsSymbolV2::OutputUnit unit, const QgsStringMap& props );
509 
514  static QVector<qreal> rescaleUom( const QVector<qreal>& array, QgsSymbolV2::OutputUnit unit, const QgsStringMap& props );
515 
520  static void applyScaleDependency( QDomDocument& doc, QDomElement& ruleElem, const QgsStringMap& props );
521 
526  static void mergeScaleDependencies( int mScaleMinDenom, int mScaleMaxDenom, QgsStringMap& props );
527 
528 };
529 
530 class QPolygonF;
531 
533 QList<QPolygonF> offsetLine( const QPolygonF& polyline, double dist );
535 QList<QPolygonF> offsetLine( QPolygonF polyline, double dist, QGis::GeometryType geometryType );
536 
537 #endif
538 
539 
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.