QGIS API Documentation  3.18.1-Zürich (202f1bf7e5)
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 <QFont>
24 #include <QColor>
25 #include <QPainter>
26 #include "qgssymbol.h"
27 #include "qgis.h"
28 #include "qgsmapunitscale.h"
29 #include "qgscolorramp.h"
30 #include "qgsarrowsymbollayer.h"
31 
32 #include <QFile>
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 class QMimeData;
50 
55 class CORE_EXPORT QgsSymbolLayerUtils
56 {
57  public:
58 
61  {
64  NoMarker
65  };
66 
67  static QString encodeColor( const QColor &color );
68  static QColor decodeColor( const QString &str );
69 
70  static QString encodeSldAlpha( int alpha );
71  static int decodeSldAlpha( const QString &str );
72 
73  static QString encodeSldFontStyle( QFont::Style style );
74  static QFont::Style decodeSldFontStyle( const QString &str );
75 
76  static QString encodeSldFontWeight( int weight );
77  static int decodeSldFontWeight( const QString &str );
78 
79  static QString encodePenStyle( Qt::PenStyle style );
80  static Qt::PenStyle decodePenStyle( const QString &str );
81 
82  static QString encodePenJoinStyle( Qt::PenJoinStyle style );
83  static Qt::PenJoinStyle decodePenJoinStyle( const QString &str );
84 
85  static QString encodePenCapStyle( Qt::PenCapStyle style );
86  static Qt::PenCapStyle decodePenCapStyle( const QString &str );
87 
88  static QString encodeSldLineJoinStyle( Qt::PenJoinStyle style );
89  static Qt::PenJoinStyle decodeSldLineJoinStyle( const QString &str );
90 
91  static QString encodeSldLineCapStyle( Qt::PenCapStyle style );
92  static Qt::PenCapStyle decodeSldLineCapStyle( const QString &str );
93 
94  static QString encodeBrushStyle( Qt::BrushStyle style );
95  static Qt::BrushStyle decodeBrushStyle( const QString &str );
96 
97  static QString encodeSldBrushStyle( Qt::BrushStyle style );
98  static Qt::BrushStyle decodeSldBrushStyle( const QString &str );
99 
104  static QgsArrowSymbolLayer::HeadType decodeArrowHeadType( const QVariant &value, bool *ok SIP_OUT = nullptr );
105 
110  static QgsArrowSymbolLayer::ArrowType decodeArrowType( const QVariant &value, bool *ok SIP_OUT = nullptr );
111 
117  static QString encodePoint( QPointF point );
118 
124  static QPointF decodePoint( const QString &string );
125 
138  static QPointF toPoint( const QVariant &value, bool *ok SIP_OUT = nullptr );
139 
146  static QString encodeSize( QSizeF size );
147 
154  static QSizeF decodeSize( const QString &string );
155 
168  static QSizeF toSize( const QVariant &value, bool *ok SIP_OUT = nullptr );
169 
170  static QString encodeMapUnitScale( const QgsMapUnitScale &mapUnitScale );
171  static QgsMapUnitScale decodeMapUnitScale( const QString &str );
172 
173  static QString encodeRealVector( const QVector<qreal> &v );
174  static QVector<qreal> decodeRealVector( const QString &s );
175 
176  static QString encodeSldRealVector( const QVector<qreal> &v );
177  static QVector<qreal> decodeSldRealVector( const QString &s );
178 
186  static QString encodeSldUom( QgsUnitTypes::RenderUnit unit, double *scaleFactor );
187 
195  static QgsUnitTypes::RenderUnit decodeSldUom( const QString &str, double *scaleFactor = nullptr );
196 
204  static double sizeInPixelsFromSldUom( const QString &uom, double size );
205 
206  static QString encodeScaleMethod( QgsSymbol::ScaleMethod scaleMethod );
207  static QgsSymbol::ScaleMethod decodeScaleMethod( const QString &str );
208 
209  static QPainter::CompositionMode decodeBlendMode( const QString &s );
210 
219  static QIcon symbolPreviewIcon( const QgsSymbol *symbol, QSize size, int padding = 0, QgsLegendPatchShape *shape = nullptr );
220 
236  static QPixmap symbolPreviewPixmap( const QgsSymbol *symbol, QSize size, int padding = 0, QgsRenderContext *customContext = nullptr, bool selected = false,
237  const QgsExpressionContext *expressionContext = nullptr,
238  const QgsLegendPatchShape *shape = nullptr );
239 
250  static QPicture symbolLayerPreviewPicture( const QgsSymbolLayer *layer, QgsUnitTypes::RenderUnit units, QSize size, const QgsMapUnitScale &scale = QgsMapUnitScale() );
251 
261  static QIcon symbolLayerPreviewIcon( const QgsSymbolLayer *layer, QgsUnitTypes::RenderUnit u, QSize size, const QgsMapUnitScale &scale = QgsMapUnitScale() );
262 
270  static QIcon colorRampPreviewIcon( QgsColorRamp *ramp, QSize size, int padding = 0 );
271 
284  static QPixmap colorRampPreviewPixmap( QgsColorRamp *ramp, QSize size, int padding = 0, Qt::Orientation direction = Qt::Horizontal,
285  bool flipDirection = false, bool drawTransparentBackground = true );
286 
287  static void drawStippledBackground( QPainter *painter, QRect rect );
288 
293  static void drawVertexMarker( double x, double y, QPainter &p, QgsSymbolLayerUtils::VertexMarkerType type, int markerSize );
294 
296  static double estimateMaxSymbolBleed( QgsSymbol *symbol, const QgsRenderContext &context );
297 
304  static QgsSymbol *loadSymbol( const QDomElement &element, const QgsReadWriteContext &context ) SIP_FACTORY;
305 
314  template <class SymbolType> static SymbolType *loadSymbol( const QDomElement &element, const QgsReadWriteContext &context ) SIP_SKIP
315  {
316  QgsSymbol *tmpSymbol = QgsSymbolLayerUtils::loadSymbol( element, context );
317  SymbolType *symbolCastToType = dynamic_cast<SymbolType *>( tmpSymbol );
318 
319  if ( symbolCastToType )
320  {
321  return symbolCastToType;
322  }
323  else
324  {
325  //could not cast
326  delete tmpSymbol;
327  return nullptr;
328  }
329  }
330 
332  static QgsSymbolLayer *loadSymbolLayer( QDomElement &element, const QgsReadWriteContext &context ) SIP_FACTORY;
334  static QDomElement saveSymbol( const QString &symbolName, const QgsSymbol *symbol, QDomDocument &doc, const QgsReadWriteContext &context );
335 
341  static QString symbolProperties( QgsSymbol *symbol );
342 
343  static bool createSymbolLayerListFromSld( QDomElement &element, QgsWkbTypes::GeometryType geomType, QgsSymbolLayerList &layers );
344 
345  static QgsSymbolLayer *createFillLayerFromSld( QDomElement &element ) SIP_FACTORY;
346  static QgsSymbolLayer *createLineLayerFromSld( QDomElement &element ) SIP_FACTORY;
347  static QgsSymbolLayer *createMarkerLayerFromSld( QDomElement &element ) SIP_FACTORY;
348 
349  static bool convertPolygonSymbolizerToPointMarker( QDomElement &element, QgsSymbolLayerList &layerList );
350  static bool hasExternalGraphic( QDomElement &element );
351  static bool hasWellKnownMark( QDomElement &element );
352 
353  static bool needFontMarker( QDomElement &element );
354  static bool needSvgMarker( QDomElement &element );
355  static bool needEllipseMarker( QDomElement &element );
356  static bool needMarkerLine( QDomElement &element );
357  static bool needLinePatternFill( QDomElement &element );
358  static bool needPointPatternFill( QDomElement &element );
359  static bool needSvgFill( QDomElement &element );
360 
361  static void fillToSld( QDomDocument &doc, QDomElement &element,
362  Qt::BrushStyle brushStyle, const QColor &color = QColor() );
363  static bool fillFromSld( QDomElement &element,
364  Qt::BrushStyle &brushStyle, QColor &color );
365 
367  static void lineToSld( QDomDocument &doc, QDomElement &element,
368  Qt::PenStyle penStyle, const QColor &color, double width = -1,
369  const Qt::PenJoinStyle *penJoinStyle = nullptr, const Qt::PenCapStyle *penCapStyle = nullptr,
370  const QVector<qreal> *customDashPattern = nullptr, double dashOffset = 0.0 ) SIP_SKIP;
371  static bool lineFromSld( QDomElement &element,
372  Qt::PenStyle &penStyle, QColor &color, double &width,
373  Qt::PenJoinStyle *penJoinStyle = nullptr, Qt::PenCapStyle *penCapStyle = nullptr,
374  QVector<qreal> *customDashPattern = nullptr, double *dashOffset = nullptr );
375 
376  static void externalGraphicToSld( QDomDocument &doc, QDomElement &element,
377  const QString &path, const QString &mime,
378  const QColor &color, double size = -1 );
379  static bool externalGraphicFromSld( QDomElement &element,
380  QString &path, QString &mime,
381  QColor &color, double &size );
382 
383  static void wellKnownMarkerToSld( QDomDocument &doc, QDomElement &element,
384  const QString &name, const QColor &color, const QColor &strokeColor, Qt::PenStyle strokeStyle,
385  double strokeWidth = -1, double size = -1 );
386 
388  static bool wellKnownMarkerFromSld( QDomElement &element,
389  QString &name, QColor &color, QColor &strokeColor, Qt::PenStyle &strokeStyle,
390  double &strokeWidth, double &size ) SIP_PYNAME( wellKnownMarkerFromSld2 );
391 
392  static void externalMarkerToSld( QDomDocument &doc, QDomElement &element,
393  const QString &path, const QString &format, int *markIndex = nullptr,
394  const QColor &color = QColor(), double size = -1 );
395  static bool externalMarkerFromSld( QDomElement &element,
396  QString &path, QString &format, int &markIndex,
397  QColor &color, double &size );
398 
399 
400  static void labelTextToSld( QDomDocument &doc, QDomElement &element, const QString &label,
401  const QFont &font, const QColor &color = QColor(), double size = -1 );
402 
404  static QString ogrFeatureStylePen( double width, double mmScaleFactor, double mapUnitsScaleFactor, const QColor &c,
405  Qt::PenJoinStyle joinStyle = Qt::MiterJoin,
406  Qt::PenCapStyle capStyle = Qt::FlatCap,
407  double offset = 0.0,
408  const QVector<qreal> *dashPattern = nullptr );
409 
414  static QString ogrFeatureStyleBrush( const QColor &fillColr );
415 
416  static void createRotationElement( QDomDocument &doc, QDomElement &element, const QString &rotationFunc );
417  static bool rotationFromSldElement( QDomElement &element, QString &rotationFunc );
418 
419  static void createOpacityElement( QDomDocument &doc, QDomElement &element, const QString &alphaFunc );
420  static bool opacityFromSldElement( QDomElement &element, QString &alphaFunc );
421 
422  static void createDisplacementElement( QDomDocument &doc, QDomElement &element, QPointF offset );
423  static bool displacementFromSldElement( QDomElement &element, QPointF &offset );
424 
431  static void createAnchorPointElement( QDomDocument &doc, QDomElement &element, QPointF anchor );
432 
433  static void createOnlineResourceElement( QDomDocument &doc, QDomElement &element, const QString &path, const QString &format );
434  static bool onlineResourceFromSldElement( QDomElement &element, QString &path, QString &format );
435 
436  static void createGeometryElement( QDomDocument &doc, QDomElement &element, const QString &geomFunc );
437  static bool geometryFromSldElement( QDomElement &element, QString &geomFunc );
438 
445  static bool createExpressionElement( QDomDocument &doc, QDomElement &element, const QString &function );
446  static bool createFunctionElement( QDomDocument &doc, QDomElement &element, const QString &function );
447  static bool functionFromSldElement( QDomElement &element, QString &function );
448 
449  static QDomElement createSvgParameterElement( QDomDocument &doc, const QString &name, const QString &value );
450  static QgsStringMap getSvgParameterList( QDomElement &element );
451 
452  static QDomElement createVendorOptionElement( QDomDocument &doc, const QString &name, const QString &value );
453  static QgsStringMap getVendorOptionList( QDomElement &element );
454 
456  static QVariantMap parseProperties( const QDomElement &element );
458  static void saveProperties( QVariantMap props, QDomDocument &doc, QDomElement &element );
459 
461  static QgsSymbolMap loadSymbols( QDomElement &element, const QgsReadWriteContext &context ) SIP_FACTORY;
463  static QDomElement saveSymbols( QgsSymbolMap &symbols, const QString &tagName, QDomDocument &doc, const QgsReadWriteContext &context );
464 
465  static void clearSymbolMap( QgsSymbolMap &symbols );
466 
474  static QMimeData *symbolToMimeData( const QgsSymbol *symbol ) SIP_FACTORY;
475 
482  static QgsSymbol *symbolFromMimeData( const QMimeData *data ) SIP_FACTORY;
483 
490  static QgsColorRamp *loadColorRamp( QDomElement &element ) SIP_FACTORY;
491 
500  static QDomElement saveColorRamp( const QString &name, QgsColorRamp *ramp, QDomDocument &doc );
501 
508  static QVariant colorRampToVariant( const QString &name, QgsColorRamp *ramp );
509 
516  static QgsColorRamp *loadColorRamp( const QVariant &value ) SIP_FACTORY;
517 
524  static QString colorToName( const QColor &color );
525 
533  static QList< QColor > parseColorList( const QString &colorStr );
534 
542  static QMimeData *colorToMimeData( const QColor &color ) SIP_FACTORY;
543 
552  static QColor colorFromMimeData( const QMimeData *data, bool &hasAlpha SIP_OUT );
553 
560  static QgsNamedColorList colorListFromMimeData( const QMimeData *data );
561 
569  static QMimeData *colorListToMimeData( const QgsNamedColorList &colorList, bool allFormats = true ) SIP_FACTORY;
570 
579  static bool saveColorsToGpl( QFile &file, const QString &paletteName, const QgsNamedColorList &colors );
580 
589  static QgsNamedColorList importColorsFromGpl( QFile &file, bool &ok, QString &name );
590 
599  static QColor parseColor( const QString &colorStr, bool strictEval = false );
600 
610  static QColor parseColorWithAlpha( const QString &colorStr, bool &containsAlpha, bool strictEval = false );
611 
615  static void multiplyImageOpacity( QImage *image, qreal opacity );
616 
618  static void blurImageInPlace( QImage &image, QRect rect, int radius, bool alphaOnly );
619 
624  static void premultiplyColor( QColor &rgb, int alpha );
625 
627  static void sortVariantList( QList<QVariant> &list, Qt::SortOrder order );
629  static QPointF pointOnLineWithDistance( QPointF startPoint, QPointF directionPoint, double distance );
630 
632  static QStringList listSvgFiles();
633 
635  static QStringList listSvgFilesAt( const QString &directory );
636 
643  static QString svgSymbolNameToPath( const QString &name, const QgsPathResolver &pathResolver );
644 
649  static QString svgSymbolPathToName( const QString &path, const QgsPathResolver &pathResolver );
650 
652  static QPointF polygonCentroid( const QPolygonF &points );
653 
655  static QPointF polygonPointOnSurface( const QPolygonF &points, const QVector<QPolygonF> *rings = nullptr );
656 
658  static bool pointInPolygon( const QPolygonF &points, QPointF point );
659 
672  static QPolygonF polylineSubstring( const QPolygonF &polyline, double startOffset, double endOffset );
673 
681  static bool isSharpCorner( QPointF p1, QPointF p2, QPointF p3 );
682 
690  static void appendPolyline( QPolygonF &target, const QPolygonF &line );
691 
699  static QgsExpression *fieldOrExpressionToExpression( const QString &fieldOrExpression ) SIP_FACTORY;
700 
708  static QString fieldOrExpressionFromExpression( QgsExpression *expression );
709 
716  static QList<double> prettyBreaks( double minimum, double maximum, int classes );
717 
723  static double rescaleUom( double size, QgsUnitTypes::RenderUnit unit, const QVariantMap &props );
724 
730  static QPointF rescaleUom( QPointF point, QgsUnitTypes::RenderUnit unit, const QVariantMap &props ) SIP_PYNAME( rescalePointUom );
731 
737  static QVector<qreal> rescaleUom( const QVector<qreal> &array, QgsUnitTypes::RenderUnit unit, const QVariantMap &props ) SIP_PYNAME( rescaleArrayUom );
738 
743  static void applyScaleDependency( QDomDocument &doc, QDomElement &ruleElem, QVariantMap &props );
744 
749  static void mergeScaleDependencies( double mScaleMinDenom, double mScaleMaxDenom, QVariantMap &props );
750 
757  static void parametricSvgToSld( QDomDocument &doc, QDomElement &graphicElem,
758  const QString &path,
759  const QColor &fillColor, double size, const QColor &strokeColor, double strokeWidth );
760 
765  static QString getSvgParametricPath( const QString &basePath, const QColor &fillColor, const QColor &strokeColor, double strokeWidth );
766 
771  static QSet<const QgsSymbolLayer *> toSymbolLayerPointers( QgsFeatureRenderer *renderer, const QSet<QgsSymbolLayerId> &symbolLayerIds );
772 
783  static QgsSymbol *restrictedSizeSymbol( const QgsSymbol *s, double minSize, double maxSize, QgsRenderContext *context, double &width, double &height );
784 
789  static QgsStringMap evaluatePropertiesMap( const QMap<QString, QgsProperty> &propertiesMap, const QgsExpressionContext &context );
790 };
791 
792 class QPolygonF;
793 
795 QList<QPolygonF> offsetLine( QPolygonF polyline, double dist, QgsWkbTypes::GeometryType geometryType ) SIP_SKIP;
796 
797 #endif
798 
799 
HeadType
Possible head types.
ArrowType
Possible arrow types.
Abstract base class for color ramps.
Definition: qgscolorramp.h:32
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
Class for parsing and evaluation of expressions (formerly called "search strings").
Represents a patch shape for use in map legends.
Struct for storing maximum and minimum scales for measurements in map units.
Resolves relative paths into absolute paths and vice versa.
A store for object properties.
Definition: qgsproperty.h:232
The class is used as a container of context for various read/write operations on other objects.
Contains information about the context of a rendering operation.
We may need stable references to symbol layers, when pointers to symbol layers is not usable (when a ...
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.
static QgsSymbol * loadSymbol(const QDomElement &element, const QgsReadWriteContext &context)
Attempts to load a symbol from a DOM element.
VertexMarkerType
Editing vertex markers.
Abstract base class for all rendered symbols.
Definition: qgssymbol.h:65
ScaleMethod
Scale method.
Definition: qgssymbol.h:98
Helper functions for various unit types.
Definition: qgsunittypes.h:39
RenderUnit
Rendering size units.
Definition: qgsunittypes.h:167
Handles storage of information regarding WKB types and their properties.
Definition: qgswkbtypes.h:42
GeometryType
The geometry types are used to group QgsWkbTypes::Type in a coarse way.
Definition: qgswkbtypes.h:141
QList< QPair< QColor, QString > > QgsNamedColorList
List of colors paired with a friendly display name identifying the color.
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
QMap< QString, QString > QgsStringMap
Definition: qgis.h:759
#define SIP_SKIP
Definition: qgis_sip.h:126
#define SIP_PYNAME(name)
Definition: qgis_sip.h:81
#define SIP_OUT
Definition: qgis_sip.h:58
#define SIP_FACTORY
Definition: qgis_sip.h:76
QMap< QString, QgsSymbol * > QgsSymbolMap
Definition: qgsrenderer.h:46
QList< QgsSymbolLayer * > QgsSymbolLayerList
Definition: qgssymbol.h:54
QList< QPolygonF > offsetLine(QPolygonF polyline, double dist, QgsWkbTypes::GeometryType geometryType)
calculate geometry shifted by a specified distance
QMap< QString, QgsSymbol * > QgsSymbolMap
QMap< QString, QString > QgsStringMap