QGIS API Documentation  3.14.0-Pi (9f7028fd23)
qgssymbollayerutils.h
Go to the documentation of this file.
1 /***************************************************************************
2  qgssymbollayerutils.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 QGSSYMBOLLAYERUTILS_H
18 #define QGSSYMBOLLAYERUTILS_H
19 
20 #include "qgis_core.h"
21 #include "qgis_sip.h"
22 #include <QMap>
23 #include <Qt>
24 #include <QtCore>
25 #include <QFont>
26 #include <QColor>
27 #include <QPainter>
28 #include "qgssymbol.h"
29 #include "qgis.h"
30 #include "qgsmapunitscale.h"
31 #include "qgscolorramp.h"
32 #include "qgsarrowsymbollayer.h"
33 
34 class QgsExpression;
35 class QgsPathResolver;
38 
39 typedef QMap<QString, QString> QgsStringMap;
40 typedef QMap<QString, QgsSymbol * > QgsSymbolMap;
41 typedef QList< QPair< QColor, QString > > QgsNamedColorList SIP_SKIP;
42 
43 class QDomDocument;
44 class QDomElement;
45 class QIcon;
46 class QPixmap;
47 class QPointF;
48 class QSize;
49 
54 class CORE_EXPORT QgsSymbolLayerUtils
55 {
56  public:
57 
60  {
63  NoMarker
64  };
65 
66  static QString encodeColor( const QColor &color );
67  static QColor decodeColor( const QString &str );
68 
69  static QString encodeSldAlpha( int alpha );
70  static int decodeSldAlpha( const QString &str );
71 
72  static QString encodeSldFontStyle( QFont::Style style );
73  static QFont::Style decodeSldFontStyle( const QString &str );
74 
75  static QString encodeSldFontWeight( int weight );
76  static int decodeSldFontWeight( const QString &str );
77 
78  static QString encodePenStyle( Qt::PenStyle style );
79  static Qt::PenStyle decodePenStyle( const QString &str );
80 
81  static QString encodePenJoinStyle( Qt::PenJoinStyle style );
82  static Qt::PenJoinStyle decodePenJoinStyle( const QString &str );
83 
84  static QString encodePenCapStyle( Qt::PenCapStyle style );
85  static Qt::PenCapStyle decodePenCapStyle( const QString &str );
86 
87  static QString encodeSldLineJoinStyle( Qt::PenJoinStyle style );
88  static Qt::PenJoinStyle decodeSldLineJoinStyle( const QString &str );
89 
90  static QString encodeSldLineCapStyle( Qt::PenCapStyle style );
91  static Qt::PenCapStyle decodeSldLineCapStyle( const QString &str );
92 
93  static QString encodeBrushStyle( Qt::BrushStyle style );
94  static Qt::BrushStyle decodeBrushStyle( const QString &str );
95 
96  static QString encodeSldBrushStyle( Qt::BrushStyle style );
97  static Qt::BrushStyle decodeSldBrushStyle( const QString &str );
98 
103  static QgsArrowSymbolLayer::HeadType decodeArrowHeadType( const QVariant &value, bool *ok SIP_OUT = nullptr );
104 
109  static QgsArrowSymbolLayer::ArrowType decodeArrowType( const QVariant &value, bool *ok SIP_OUT = nullptr );
110 
116  static QString encodePoint( QPointF point );
117 
123  static QPointF decodePoint( const QString &string );
124 
137  static QPointF toPoint( const QVariant &value, bool *ok SIP_OUT = nullptr );
138 
145  static QString encodeSize( QSizeF size );
146 
153  static QSizeF decodeSize( const QString &string );
154 
167  static QSizeF toSize( const QVariant &value, bool *ok SIP_OUT = nullptr );
168 
169  static QString encodeMapUnitScale( const QgsMapUnitScale &mapUnitScale );
170  static QgsMapUnitScale decodeMapUnitScale( const QString &str );
171 
172  static QString encodeRealVector( const QVector<qreal> &v );
173  static QVector<qreal> decodeRealVector( const QString &s );
174 
175  static QString encodeSldRealVector( const QVector<qreal> &v );
176  static QVector<qreal> decodeSldRealVector( const QString &s );
177 
185  static QString encodeSldUom( QgsUnitTypes::RenderUnit unit, double *scaleFactor );
186 
194  static QgsUnitTypes::RenderUnit decodeSldUom( const QString &str, double *scaleFactor = nullptr );
195 
203  static double sizeInPixelsFromSldUom( const QString &uom, double size );
204 
205  static QString encodeScaleMethod( QgsSymbol::ScaleMethod scaleMethod );
206  static QgsSymbol::ScaleMethod decodeScaleMethod( const QString &str );
207 
208  static QPainter::CompositionMode decodeBlendMode( const QString &s );
209 
218  static QIcon symbolPreviewIcon( const QgsSymbol *symbol, QSize size, int padding = 0, QgsLegendPatchShape *shape = nullptr );
219 
235  static QPixmap symbolPreviewPixmap( const QgsSymbol *symbol, QSize size, int padding = 0, QgsRenderContext *customContext = nullptr, bool selected = false,
236  const QgsExpressionContext *expressionContext = nullptr,
237  const QgsLegendPatchShape *shape = nullptr );
238 
249  static QPicture symbolLayerPreviewPicture( const QgsSymbolLayer *layer, QgsUnitTypes::RenderUnit units, QSize size, const QgsMapUnitScale &scale = QgsMapUnitScale() );
250 
260  static QIcon symbolLayerPreviewIcon( const QgsSymbolLayer *layer, QgsUnitTypes::RenderUnit u, QSize size, const QgsMapUnitScale &scale = QgsMapUnitScale() );
261 
269  static QIcon colorRampPreviewIcon( QgsColorRamp *ramp, QSize size, int padding = 0 );
270 
278  static QPixmap colorRampPreviewPixmap( QgsColorRamp *ramp, QSize size, int padding = 0 );
279 
280  static void drawStippledBackground( QPainter *painter, QRect rect );
281 
286  static void drawVertexMarker( double x, double y, QPainter &p, QgsSymbolLayerUtils::VertexMarkerType type, int markerSize );
287 
289  static double estimateMaxSymbolBleed( QgsSymbol *symbol, const QgsRenderContext &context );
290 
297  static QgsSymbol *loadSymbol( const QDomElement &element, const QgsReadWriteContext &context ) SIP_FACTORY;
298 
307  template <class SymbolType> static SymbolType *loadSymbol( const QDomElement &element, const QgsReadWriteContext &context ) SIP_SKIP
308  {
309  QgsSymbol *tmpSymbol = QgsSymbolLayerUtils::loadSymbol( element, context );
310  SymbolType *symbolCastToType = dynamic_cast<SymbolType *>( tmpSymbol );
311 
312  if ( symbolCastToType )
313  {
314  return symbolCastToType;
315  }
316  else
317  {
318  //could not cast
319  delete tmpSymbol;
320  return nullptr;
321  }
322  }
323 
325  static QgsSymbolLayer *loadSymbolLayer( QDomElement &element, const QgsReadWriteContext &context ) SIP_FACTORY;
327  static QDomElement saveSymbol( const QString &symbolName, const QgsSymbol *symbol, QDomDocument &doc, const QgsReadWriteContext &context );
328 
334  static QString symbolProperties( QgsSymbol *symbol );
335 
336  static bool createSymbolLayerListFromSld( QDomElement &element, QgsWkbTypes::GeometryType geomType, QgsSymbolLayerList &layers );
337 
338  static QgsSymbolLayer *createFillLayerFromSld( QDomElement &element ) SIP_FACTORY;
339  static QgsSymbolLayer *createLineLayerFromSld( QDomElement &element ) SIP_FACTORY;
340  static QgsSymbolLayer *createMarkerLayerFromSld( QDomElement &element ) SIP_FACTORY;
341 
342  static bool convertPolygonSymbolizerToPointMarker( QDomElement &element, QgsSymbolLayerList &layerList );
343  static bool hasExternalGraphic( QDomElement &element );
344  static bool hasWellKnownMark( QDomElement &element );
345 
346  static bool needFontMarker( QDomElement &element );
347  static bool needSvgMarker( QDomElement &element );
348  static bool needEllipseMarker( QDomElement &element );
349  static bool needMarkerLine( QDomElement &element );
350  static bool needLinePatternFill( QDomElement &element );
351  static bool needPointPatternFill( QDomElement &element );
352  static bool needSvgFill( QDomElement &element );
353 
354  static void fillToSld( QDomDocument &doc, QDomElement &element,
355  Qt::BrushStyle brushStyle, const QColor &color = QColor() );
356  static bool fillFromSld( QDomElement &element,
357  Qt::BrushStyle &brushStyle, QColor &color );
358 
360  static void lineToSld( QDomDocument &doc, QDomElement &element,
361  Qt::PenStyle penStyle, const QColor &color, double width = -1,
362  const Qt::PenJoinStyle *penJoinStyle = nullptr, const Qt::PenCapStyle *penCapStyle = nullptr,
363  const QVector<qreal> *customDashPattern = nullptr, double dashOffset = 0.0 ) SIP_SKIP;
364  static bool lineFromSld( QDomElement &element,
365  Qt::PenStyle &penStyle, QColor &color, double &width,
366  Qt::PenJoinStyle *penJoinStyle = nullptr, Qt::PenCapStyle *penCapStyle = nullptr,
367  QVector<qreal> *customDashPattern = nullptr, double *dashOffset = nullptr );
368 
369  static void externalGraphicToSld( QDomDocument &doc, QDomElement &element,
370  const QString &path, const QString &mime,
371  const QColor &color, double size = -1 );
372  static bool externalGraphicFromSld( QDomElement &element,
373  QString &path, QString &mime,
374  QColor &color, double &size );
375 
376  static void wellKnownMarkerToSld( QDomDocument &doc, QDomElement &element,
377  const QString &name, const QColor &color, const QColor &strokeColor, Qt::PenStyle strokeStyle,
378  double strokeWidth = -1, double size = -1 );
379 
381  static bool wellKnownMarkerFromSld( QDomElement &element,
382  QString &name, QColor &color, QColor &strokeColor, Qt::PenStyle &strokeStyle,
383  double &strokeWidth, double &size ) SIP_PYNAME( wellKnownMarkerFromSld2 );
384 
385  static void externalMarkerToSld( QDomDocument &doc, QDomElement &element,
386  const QString &path, const QString &format, int *markIndex = nullptr,
387  const QColor &color = QColor(), double size = -1 );
388  static bool externalMarkerFromSld( QDomElement &element,
389  QString &path, QString &format, int &markIndex,
390  QColor &color, double &size );
391 
392 
393  static void labelTextToSld( QDomDocument &doc, QDomElement &element, const QString &label,
394  const QFont &font, const QColor &color = QColor(), double size = -1 );
395 
397  static QString ogrFeatureStylePen( double width, double mmScaleFactor, double mapUnitsScaleFactor, const QColor &c,
398  Qt::PenJoinStyle joinStyle = Qt::MiterJoin,
399  Qt::PenCapStyle capStyle = Qt::FlatCap,
400  double offset = 0.0,
401  const QVector<qreal> *dashPattern = nullptr );
402 
406  static QString ogrFeatureStyleBrush( const QColor &fillColr );
407 
408  static void createRotationElement( QDomDocument &doc, QDomElement &element, const QString &rotationFunc );
409  static bool rotationFromSldElement( QDomElement &element, QString &rotationFunc );
410 
411  static void createOpacityElement( QDomDocument &doc, QDomElement &element, const QString &alphaFunc );
412  static bool opacityFromSldElement( QDomElement &element, QString &alphaFunc );
413 
414  static void createDisplacementElement( QDomDocument &doc, QDomElement &element, QPointF offset );
415  static bool displacementFromSldElement( QDomElement &element, QPointF &offset );
416 
423  static void createAnchorPointElement( QDomDocument &doc, QDomElement &element, QPointF anchor );
424 
425  static void createOnlineResourceElement( QDomDocument &doc, QDomElement &element, const QString &path, const QString &format );
426  static bool onlineResourceFromSldElement( QDomElement &element, QString &path, QString &format );
427 
428  static void createGeometryElement( QDomDocument &doc, QDomElement &element, const QString &geomFunc );
429  static bool geometryFromSldElement( QDomElement &element, QString &geomFunc );
430 
437  static bool createExpressionElement( QDomDocument &doc, QDomElement &element, const QString &function );
438  static bool createFunctionElement( QDomDocument &doc, QDomElement &element, const QString &function );
439  static bool functionFromSldElement( QDomElement &element, QString &function );
440 
441  static QDomElement createSvgParameterElement( QDomDocument &doc, const QString &name, const QString &value );
442  static QgsStringMap getSvgParameterList( QDomElement &element );
443 
444  static QDomElement createVendorOptionElement( QDomDocument &doc, const QString &name, const QString &value );
445  static QgsStringMap getVendorOptionList( QDomElement &element );
446 
447  static QgsStringMap parseProperties( QDomElement &element );
448  static void saveProperties( QgsStringMap props, QDomDocument &doc, QDomElement &element );
449 
451  static QgsSymbolMap loadSymbols( QDomElement &element, const QgsReadWriteContext &context ) SIP_FACTORY;
453  static QDomElement saveSymbols( QgsSymbolMap &symbols, const QString &tagName, QDomDocument &doc, const QgsReadWriteContext &context );
454 
455  static void clearSymbolMap( QgsSymbolMap &symbols );
456 
464  static QMimeData *symbolToMimeData( const QgsSymbol *symbol ) SIP_FACTORY;
465 
472  static QgsSymbol *symbolFromMimeData( const QMimeData *data ) SIP_FACTORY;
473 
480  static QgsColorRamp *loadColorRamp( QDomElement &element ) SIP_FACTORY;
481 
490  static QDomElement saveColorRamp( const QString &name, QgsColorRamp *ramp, QDomDocument &doc );
491 
498  static QVariant colorRampToVariant( const QString &name, QgsColorRamp *ramp );
499 
506  static QgsColorRamp *loadColorRamp( const QVariant &value ) SIP_FACTORY;
507 
514  static QString colorToName( const QColor &color );
515 
523  static QList< QColor > parseColorList( const QString &colorStr );
524 
532  static QMimeData *colorToMimeData( const QColor &color ) SIP_FACTORY;
533 
543  static QColor colorFromMimeData( const QMimeData *data, bool &hasAlpha );
544 
551  static QgsNamedColorList colorListFromMimeData( const QMimeData *data );
552 
560  static QMimeData *colorListToMimeData( const QgsNamedColorList &colorList, bool allFormats = true ) SIP_FACTORY;
561 
570  static bool saveColorsToGpl( QFile &file, const QString &paletteName, const QgsNamedColorList &colors );
571 
580  static QgsNamedColorList importColorsFromGpl( QFile &file, bool &ok, QString &name );
581 
590  static QColor parseColor( const QString &colorStr, bool strictEval = false );
591 
601  static QColor parseColorWithAlpha( const QString &colorStr, bool &containsAlpha, bool strictEval = false );
602 
606  static void multiplyImageOpacity( QImage *image, qreal opacity );
607 
609  static void blurImageInPlace( QImage &image, QRect rect, int radius, bool alphaOnly );
610 
615  static void premultiplyColor( QColor &rgb, int alpha );
616 
618  static void sortVariantList( QList<QVariant> &list, Qt::SortOrder order );
620  static QPointF pointOnLineWithDistance( QPointF startPoint, QPointF directionPoint, double distance );
621 
623  static QStringList listSvgFiles();
624 
626  static QStringList listSvgFilesAt( const QString &directory );
627 
634  static QString svgSymbolNameToPath( const QString &name, const QgsPathResolver &pathResolver );
635 
640  static QString svgSymbolPathToName( const QString &path, const QgsPathResolver &pathResolver );
641 
643  static QPointF polygonCentroid( const QPolygonF &points );
644 
646  static QPointF polygonPointOnSurface( const QPolygonF &points, const QVector<QPolygonF> *rings = nullptr );
647 
649  static bool pointInPolygon( const QPolygonF &points, QPointF point );
650 
658  static QgsExpression *fieldOrExpressionToExpression( const QString &fieldOrExpression ) SIP_FACTORY;
659 
667  static QString fieldOrExpressionFromExpression( QgsExpression *expression );
668 
675  static QList<double> prettyBreaks( double minimum, double maximum, int classes );
676 
682  static double rescaleUom( double size, QgsUnitTypes::RenderUnit unit, const QgsStringMap &props );
683 
689  static QPointF rescaleUom( QPointF point, QgsUnitTypes::RenderUnit unit, const QgsStringMap &props ) SIP_PYNAME( rescalePointUom );
690 
696  static QVector<qreal> rescaleUom( const QVector<qreal> &array, QgsUnitTypes::RenderUnit unit, const QgsStringMap &props ) SIP_PYNAME( rescaleArrayUom );
697 
702  static void applyScaleDependency( QDomDocument &doc, QDomElement &ruleElem, QgsStringMap &props );
703 
708  static void mergeScaleDependencies( double mScaleMinDenom, double mScaleMaxDenom, QgsStringMap &props );
709 
716  static void parametricSvgToSld( QDomDocument &doc, QDomElement &graphicElem,
717  const QString &path,
718  const QColor &fillColor, double size, const QColor &strokeColor, double strokeWidth );
719 
724  static QString getSvgParametricPath( const QString &basePath, const QColor &fillColor, const QColor &strokeColor, double strokeWidth );
725 
730  static QSet<const QgsSymbolLayer *> toSymbolLayerPointers( QgsFeatureRenderer *renderer, const QSet<QgsSymbolLayerId> &symbolLayerIds );
731 };
732 
733 class QPolygonF;
734 
736 QList<QPolygonF> offsetLine( QPolygonF polyline, double dist, QgsWkbTypes::GeometryType geometryType ) SIP_SKIP;
737 
738 #endif
739 
740 
QgsExpressionContext
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
Definition: qgsexpressioncontext.h:369
QgsArrowSymbolLayer::ArrowType
ArrowType
Possible arrow types.
Definition: qgsarrowsymbollayer.h:128
SIP_PYNAME
#define SIP_PYNAME(name)
Definition: qgis_sip.h:81
QgsColorRamp
Abstract base class for color ramps.
Definition: qgscolorramp.h:31
QgsSymbolLayerUtils::Cross
@ Cross
Definition: qgssymbollayerutils.h:62
QgsUnitTypes::RenderUnit
RenderUnit
Rendering size units.
Definition: qgsunittypes.h:166
QgsReadWriteContext
Definition: qgsreadwritecontext.h:34
SIP_OUT
#define SIP_OUT
Definition: qgis_sip.h:58
QgsSymbolMap
QMap< QString, QgsSymbol * > QgsSymbolMap
Definition: qgsrenderer.h:46
QgsNamedColorList
QList< QPair< QColor, QString > > QgsNamedColorList
Definition: qgscolorscheme.h:34
qgis.h
QgsRenderContext
Definition: qgsrendercontext.h:57
QgsSymbol::ScaleMethod
ScaleMethod
Scale method.
Definition: qgssymbol.h:96
QgsSymbol
Definition: qgssymbol.h:63
QgsLegendPatchShape
Definition: qgslegendpatchshape.h:30
SIP_FACTORY
#define SIP_FACTORY
Definition: qgis_sip.h:76
QgsSymbolLayer
Definition: qgssymbollayer.h:52
SIP_SKIP
#define SIP_SKIP
Definition: qgis_sip.h:126
QgsArrowSymbolLayer::HeadType
HeadType
Possible head types.
Definition: qgsarrowsymbollayer.h:115
qgsarrowsymbollayer.h
qgscolorramp.h
QgsSymbolLayerUtils::SemiTransparentCircle
@ SemiTransparentCircle
Definition: qgssymbollayerutils.h:61
qgis_sip.h
QgsSymbolLayerUtils::loadSymbol
static SymbolType * loadSymbol(const QDomElement &element, const QgsReadWriteContext &context)
Attempts to load a symbol from a DOM element and cast it to a particular symbol type.
Definition: qgssymbollayerutils.h:307
QgsUnitTypes
Helper functions for various unit types.
Definition: qgsunittypes.h:38
QgsSymbolLayerUtils::loadSymbol
static QgsSymbol * loadSymbol(const QDomElement &element, const QgsReadWriteContext &context)
Attempts to load a symbol from a DOM element.
Definition: qgssymbollayerutils.cpp:1041
QgsMapUnitScale
Struct for storing maximum and minimum scales for measurements in map units.
Definition: qgsmapunitscale.h:37
QgsStringMap
QMap< QString, QString > QgsStringMap
Definition: qgis.h:714
QgsFeatureRenderer
Definition: qgsrenderer.h:102
QgsWkbTypes::GeometryType
GeometryType
The geometry types are used to group QgsWkbTypes::Type in a coarse way.
Definition: qgswkbtypes.h:139
c
As part of the API refactoring and improvements which landed in the Processing API was substantially reworked from the x version This was done in order to allow much of the underlying Processing framework to be ported into c
Definition: porting_processing.dox:1
QgsSymbolLayerList
QList< QgsSymbolLayer * > QgsSymbolLayerList
Definition: qgssymbol.h:53
offsetLine
QList< QPolygonF > offsetLine(QPolygonF polyline, double dist, QgsWkbTypes::GeometryType geometryType)
calculate geometry shifted by a specified distance
Definition: qgssymbollayerutils.cpp:966
QgsSymbolLayerUtils::VertexMarkerType
VertexMarkerType
Editing vertex markers.
Definition: qgssymbollayerutils.h:59
qgsmapunitscale.h
QgsSymbolLayerId
We may need stable references to symbol layers, when pointers to symbol layers is not usable (when a ...
Definition: qgssymbollayerreference.h:53
QgsSymbolLayerUtils
Definition: qgssymbollayerutils.h:54
QgsWkbTypes
Handles storage of information regarding WKB types and their properties.
Definition: qgswkbtypes.h:40
QgsExpression
Definition: qgsexpression.h:113
QgsStringMap
QMap< QString, QString > QgsStringMap
Definition: qgssymbollayerutils.h:37
qgssymbol.h
QgsPathResolver
Definition: qgspathresolver.h:31
QgsSymbolMap
QMap< QString, QgsSymbol * > QgsSymbolMap
Definition: qgssymbollayerutils.h:40