QGIS API Documentation  3.16.0-Hannover (43b64b13f3)
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;
37 class QgsSymbolLayer;
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 
407  static QString ogrFeatureStyleBrush( const QColor &fillColr );
408 
409  static void createRotationElement( QDomDocument &doc, QDomElement &element, const QString &rotationFunc );
410  static bool rotationFromSldElement( QDomElement &element, QString &rotationFunc );
411 
412  static void createOpacityElement( QDomDocument &doc, QDomElement &element, const QString &alphaFunc );
413  static bool opacityFromSldElement( QDomElement &element, QString &alphaFunc );
414 
415  static void createDisplacementElement( QDomDocument &doc, QDomElement &element, QPointF offset );
416  static bool displacementFromSldElement( QDomElement &element, QPointF &offset );
417 
424  static void createAnchorPointElement( QDomDocument &doc, QDomElement &element, QPointF anchor );
425 
426  static void createOnlineResourceElement( QDomDocument &doc, QDomElement &element, const QString &path, const QString &format );
427  static bool onlineResourceFromSldElement( QDomElement &element, QString &path, QString &format );
428 
429  static void createGeometryElement( QDomDocument &doc, QDomElement &element, const QString &geomFunc );
430  static bool geometryFromSldElement( QDomElement &element, QString &geomFunc );
431 
438  static bool createExpressionElement( QDomDocument &doc, QDomElement &element, const QString &function );
439  static bool createFunctionElement( QDomDocument &doc, QDomElement &element, const QString &function );
440  static bool functionFromSldElement( QDomElement &element, QString &function );
441 
442  static QDomElement createSvgParameterElement( QDomDocument &doc, const QString &name, const QString &value );
443  static QgsStringMap getSvgParameterList( QDomElement &element );
444 
445  static QDomElement createVendorOptionElement( QDomDocument &doc, const QString &name, const QString &value );
446  static QgsStringMap getVendorOptionList( QDomElement &element );
447 
448  static QgsStringMap parseProperties( QDomElement &element );
449  static void saveProperties( QgsStringMap props, QDomDocument &doc, QDomElement &element );
450 
452  static QgsSymbolMap loadSymbols( QDomElement &element, const QgsReadWriteContext &context ) SIP_FACTORY;
454  static QDomElement saveSymbols( QgsSymbolMap &symbols, const QString &tagName, QDomDocument &doc, const QgsReadWriteContext &context );
455 
456  static void clearSymbolMap( QgsSymbolMap &symbols );
457 
465  static QMimeData *symbolToMimeData( const QgsSymbol *symbol ) SIP_FACTORY;
466 
473  static QgsSymbol *symbolFromMimeData( const QMimeData *data ) SIP_FACTORY;
474 
481  static QgsColorRamp *loadColorRamp( QDomElement &element ) SIP_FACTORY;
482 
491  static QDomElement saveColorRamp( const QString &name, QgsColorRamp *ramp, QDomDocument &doc );
492 
499  static QVariant colorRampToVariant( const QString &name, QgsColorRamp *ramp );
500 
507  static QgsColorRamp *loadColorRamp( const QVariant &value ) SIP_FACTORY;
508 
515  static QString colorToName( const QColor &color );
516 
524  static QList< QColor > parseColorList( const QString &colorStr );
525 
533  static QMimeData *colorToMimeData( const QColor &color ) SIP_FACTORY;
534 
543  static QColor colorFromMimeData( const QMimeData *data, bool &hasAlpha SIP_OUT );
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 
663  static QPolygonF polylineSubstring( const QPolygonF &polyline, double startOffset, double endOffset );
664 
672  static bool isSharpCorner( QPointF p1, QPointF p2, QPointF p3 );
673 
681  static void appendPolyline( QPolygonF &target, const QPolygonF &line );
682 
690  static QgsExpression *fieldOrExpressionToExpression( const QString &fieldOrExpression ) SIP_FACTORY;
691 
699  static QString fieldOrExpressionFromExpression( QgsExpression *expression );
700 
707  static QList<double> prettyBreaks( double minimum, double maximum, int classes );
708 
714  static double rescaleUom( double size, QgsUnitTypes::RenderUnit unit, const QgsStringMap &props );
715 
721  static QPointF rescaleUom( QPointF point, QgsUnitTypes::RenderUnit unit, const QgsStringMap &props ) SIP_PYNAME( rescalePointUom );
722 
728  static QVector<qreal> rescaleUom( const QVector<qreal> &array, QgsUnitTypes::RenderUnit unit, const QgsStringMap &props ) SIP_PYNAME( rescaleArrayUom );
729 
734  static void applyScaleDependency( QDomDocument &doc, QDomElement &ruleElem, QgsStringMap &props );
735 
740  static void mergeScaleDependencies( double mScaleMinDenom, double mScaleMaxDenom, QgsStringMap &props );
741 
748  static void parametricSvgToSld( QDomDocument &doc, QDomElement &graphicElem,
749  const QString &path,
750  const QColor &fillColor, double size, const QColor &strokeColor, double strokeWidth );
751 
756  static QString getSvgParametricPath( const QString &basePath, const QColor &fillColor, const QColor &strokeColor, double strokeWidth );
757 
762  static QSet<const QgsSymbolLayer *> toSymbolLayerPointers( QgsFeatureRenderer *renderer, const QSet<QgsSymbolLayerId> &symbolLayerIds );
763 
774  static QgsSymbol *restrictedSizeSymbol( const QgsSymbol *s, double minSize, double maxSize, QgsRenderContext *context, double &width, double &height );
775 };
776 
777 class QPolygonF;
778 
780 QList<QPolygonF> offsetLine( QPolygonF polyline, double dist, QgsWkbTypes::GeometryType geometryType ) SIP_SKIP;
781 
782 #endif
783 
784 
QgsExpressionContext
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
Definition: qgsexpressioncontext.h:370
QgsArrowSymbolLayer::ArrowType
ArrowType
Possible arrow types.
Definition: qgsarrowsymbollayer.h:129
SIP_PYNAME
#define SIP_PYNAME(name)
Definition: qgis_sip.h:81
QgsColorRamp
Abstract base class for color ramps.
Definition: qgscolorramp.h:32
QgsSymbolLayerUtils::Cross
@ Cross
Definition: qgssymbollayerutils.h:62
QgsUnitTypes::RenderUnit
RenderUnit
Rendering size units.
Definition: qgsunittypes.h:167
QgsReadWriteContext
The class is used as a container of context for various read/write operations on other objects.
Definition: qgsreadwritecontext.h:35
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
List of colors paired with a friendly display name identifying the color.
Definition: qgscolorscheme.h:34
qgis.h
QgsRenderContext
Contains information about the context of a rendering operation.
Definition: qgsrendercontext.h:58
QgsSymbol::ScaleMethod
ScaleMethod
Scale method.
Definition: qgssymbol.h:97
QgsSymbol
Abstract base class for all rendered symbols.
Definition: qgssymbol.h:64
QgsLegendPatchShape
Represents a patch shape for use in map legends.
Definition: qgslegendpatchshape.h:31
SIP_FACTORY
#define SIP_FACTORY
Definition: qgis_sip.h:76
QgsSymbolLayer
Definition: qgssymbollayer.h:53
SIP_SKIP
#define SIP_SKIP
Definition: qgis_sip.h:126
QgsArrowSymbolLayer::HeadType
HeadType
Possible head types.
Definition: qgsarrowsymbollayer.h:116
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:39
QgsSymbolLayerUtils::loadSymbol
static QgsSymbol * loadSymbol(const QDomElement &element, const QgsReadWriteContext &context)
Attempts to load a symbol from a DOM element.
Definition: qgssymbollayerutils.cpp:1043
QgsMapUnitScale
Struct for storing maximum and minimum scales for measurements in map units.
Definition: qgsmapunitscale.h:38
QgsStringMap
QMap< QString, QString > QgsStringMap
Definition: qgis.h:758
QgsFeatureRenderer
Definition: qgsrenderer.h:103
QgsWkbTypes::GeometryType
GeometryType
The geometry types are used to group QgsWkbTypes::Type in a coarse way.
Definition: qgswkbtypes.h:141
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:968
QgsSymbolLayerUtils::VertexMarkerType
VertexMarkerType
Editing vertex markers.
Definition: qgssymbollayerutils.h:60
qgsmapunitscale.h
QgsSymbolLayerId
We may need stable references to symbol layers, when pointers to symbol layers is not usable (when a ...
Definition: qgssymbollayerreference.h:54
QgsSymbolLayerUtils
Definition: qgssymbollayerutils.h:55
QgsWkbTypes
Handles storage of information regarding WKB types and their properties.
Definition: qgswkbtypes.h:42
QgsExpression
Class for parsing and evaluation of expressions (formerly called "search strings").
Definition: qgsexpression.h:105
QgsStringMap
QMap< QString, QString > QgsStringMap
Definition: qgssymbollayerutils.h:37
qgssymbol.h
QgsPathResolver
Resolves relative paths into absolute paths and vice versa.
Definition: qgspathresolver.h:32
QgsSymbolMap
QMap< QString, QgsSymbol * > QgsSymbolMap
Definition: qgssymbollayerutils.h:40