QGIS API Documentation  3.8.0-Zanzibar (11aff65)
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 
131  static QString encodeSize( QSizeF size );
132 
139  static QSizeF decodeSize( const QString &string );
140 
141  static QString encodeMapUnitScale( const QgsMapUnitScale &mapUnitScale );
142  static QgsMapUnitScale decodeMapUnitScale( const QString &str );
143 
144  static QString encodeRealVector( const QVector<qreal> &v );
145  static QVector<qreal> decodeRealVector( const QString &s );
146 
147  static QString encodeSldRealVector( const QVector<qreal> &v );
148  static QVector<qreal> decodeSldRealVector( const QString &s );
149 
157  static QString encodeSldUom( QgsUnitTypes::RenderUnit unit, double *scaleFactor );
158 
166  static QgsUnitTypes::RenderUnit decodeSldUom( const QString &str, double *scaleFactor );
167 
175  static double sizeInPixelsFromSldUom( const QString &uom, double size );
176 
177  static QString encodeScaleMethod( QgsSymbol::ScaleMethod scaleMethod );
178  static QgsSymbol::ScaleMethod decodeScaleMethod( const QString &str );
179 
180  static QPainter::CompositionMode decodeBlendMode( const QString &s );
181 
189  static QIcon symbolPreviewIcon( const QgsSymbol *symbol, QSize size, int padding = 0 );
190 
200  static QPixmap symbolPreviewPixmap( const QgsSymbol *symbol, QSize size, int padding = 0, QgsRenderContext *customContext = nullptr );
201 
212  static QPicture symbolLayerPreviewPicture( const QgsSymbolLayer *layer, QgsUnitTypes::RenderUnit units, QSize size, const QgsMapUnitScale &scale = QgsMapUnitScale() );
213 
223  static QIcon symbolLayerPreviewIcon( const QgsSymbolLayer *layer, QgsUnitTypes::RenderUnit u, QSize size, const QgsMapUnitScale &scale = QgsMapUnitScale() );
224 
232  static QIcon colorRampPreviewIcon( QgsColorRamp *ramp, QSize size, int padding = 0 );
233 
241  static QPixmap colorRampPreviewPixmap( QgsColorRamp *ramp, QSize size, int padding = 0 );
242 
243  static void drawStippledBackground( QPainter *painter, QRect rect );
244 
249  static void drawVertexMarker( double x, double y, QPainter &p, QgsSymbolLayerUtils::VertexMarkerType type, int markerSize );
250 
252  static double estimateMaxSymbolBleed( QgsSymbol *symbol, const QgsRenderContext &context );
253 
260  static QgsSymbol *loadSymbol( const QDomElement &element, const QgsReadWriteContext &context ) SIP_FACTORY;
261 
270  template <class SymbolType> static SymbolType *loadSymbol( const QDomElement &element, const QgsReadWriteContext &context ) SIP_SKIP
271  {
272  QgsSymbol *tmpSymbol = QgsSymbolLayerUtils::loadSymbol( element, context );
273  SymbolType *symbolCastToType = dynamic_cast<SymbolType *>( tmpSymbol );
274 
275  if ( symbolCastToType )
276  {
277  return symbolCastToType;
278  }
279  else
280  {
281  //could not cast
282  delete tmpSymbol;
283  return nullptr;
284  }
285  }
286 
288  static QgsSymbolLayer *loadSymbolLayer( QDomElement &element, const QgsReadWriteContext &context ) SIP_FACTORY;
290  static QDomElement saveSymbol( const QString &symbolName, QgsSymbol *symbol, QDomDocument &doc, const QgsReadWriteContext &context );
291 
297  static QString symbolProperties( QgsSymbol *symbol );
298 
299  static bool createSymbolLayerListFromSld( QDomElement &element, QgsWkbTypes::GeometryType geomType, QgsSymbolLayerList &layers );
300 
301  static QgsSymbolLayer *createFillLayerFromSld( QDomElement &element ) SIP_FACTORY;
302  static QgsSymbolLayer *createLineLayerFromSld( QDomElement &element ) SIP_FACTORY;
303  static QgsSymbolLayer *createMarkerLayerFromSld( QDomElement &element ) SIP_FACTORY;
304 
305  static bool convertPolygonSymbolizerToPointMarker( QDomElement &element, QgsSymbolLayerList &layerList );
306  static bool hasExternalGraphic( QDomElement &element );
307  static bool hasWellKnownMark( QDomElement &element );
308 
309  static bool needFontMarker( QDomElement &element );
310  static bool needSvgMarker( QDomElement &element );
311  static bool needEllipseMarker( QDomElement &element );
312  static bool needMarkerLine( QDomElement &element );
313  static bool needLinePatternFill( QDomElement &element );
314  static bool needPointPatternFill( QDomElement &element );
315  static bool needSvgFill( QDomElement &element );
316 
317  static void fillToSld( QDomDocument &doc, QDomElement &element,
318  Qt::BrushStyle brushStyle, const QColor &color = QColor() );
319  static bool fillFromSld( QDomElement &element,
320  Qt::BrushStyle &brushStyle, QColor &color );
321 
323  static void lineToSld( QDomDocument &doc, QDomElement &element,
324  Qt::PenStyle penStyle, const QColor &color, double width = -1,
325  const Qt::PenJoinStyle *penJoinStyle = nullptr, const Qt::PenCapStyle *penCapStyle = nullptr,
326  const QVector<qreal> *customDashPattern = nullptr, double dashOffset = 0.0 ) SIP_SKIP;
327  static bool lineFromSld( QDomElement &element,
328  Qt::PenStyle &penStyle, QColor &color, double &width,
329  Qt::PenJoinStyle *penJoinStyle = nullptr, Qt::PenCapStyle *penCapStyle = nullptr,
330  QVector<qreal> *customDashPattern = nullptr, double *dashOffset = nullptr );
331 
332  static void externalGraphicToSld( QDomDocument &doc, QDomElement &element,
333  const QString &path, const QString &mime,
334  const QColor &color, double size = -1 );
335  static bool externalGraphicFromSld( QDomElement &element,
336  QString &path, QString &mime,
337  QColor &color, double &size );
338 
339  static void wellKnownMarkerToSld( QDomDocument &doc, QDomElement &element,
340  const QString &name, const QColor &color, const QColor &strokeColor, Qt::PenStyle strokeStyle,
341  double strokeWidth = -1, double size = -1 );
342 
344  static bool wellKnownMarkerFromSld( QDomElement &element,
345  QString &name, QColor &color, QColor &strokeColor, Qt::PenStyle &strokeStyle,
346  double &strokeWidth, double &size ) SIP_PYNAME( wellKnownMarkerFromSld2 );
347 
348  static void externalMarkerToSld( QDomDocument &doc, QDomElement &element,
349  const QString &path, const QString &format, int *markIndex = nullptr,
350  const QColor &color = QColor(), double size = -1 );
351  static bool externalMarkerFromSld( QDomElement &element,
352  QString &path, QString &format, int &markIndex,
353  QColor &color, double &size );
354 
355 
356  static void labelTextToSld( QDomDocument &doc, QDomElement &element, const QString &label,
357  const QFont &font, const QColor &color = QColor(), double size = -1 );
358 
360  static QString ogrFeatureStylePen( double width, double mmScaleFactor, double mapUnitsScaleFactor, const QColor &c,
361  Qt::PenJoinStyle joinStyle = Qt::MiterJoin,
362  Qt::PenCapStyle capStyle = Qt::FlatCap,
363  double offset = 0.0,
364  const QVector<qreal> *dashPattern = nullptr );
365 
369  static QString ogrFeatureStyleBrush( const QColor &fillColr );
370 
371  static void createRotationElement( QDomDocument &doc, QDomElement &element, const QString &rotationFunc );
372  static bool rotationFromSldElement( QDomElement &element, QString &rotationFunc );
373 
374  static void createOpacityElement( QDomDocument &doc, QDomElement &element, const QString &alphaFunc );
375  static bool opacityFromSldElement( QDomElement &element, QString &alphaFunc );
376 
377  static void createDisplacementElement( QDomDocument &doc, QDomElement &element, QPointF offset );
378  static bool displacementFromSldElement( QDomElement &element, QPointF &offset );
379 
386  static void createAnchorPointElement( QDomDocument &doc, QDomElement &element, QPointF anchor );
387 
388  static void createOnlineResourceElement( QDomDocument &doc, QDomElement &element, const QString &path, const QString &format );
389  static bool onlineResourceFromSldElement( QDomElement &element, QString &path, QString &format );
390 
391  static void createGeometryElement( QDomDocument &doc, QDomElement &element, const QString &geomFunc );
392  static bool geometryFromSldElement( QDomElement &element, QString &geomFunc );
393 
400  static bool createExpressionElement( QDomDocument &doc, QDomElement &element, const QString &function );
401  static bool createFunctionElement( QDomDocument &doc, QDomElement &element, const QString &function );
402  static bool functionFromSldElement( QDomElement &element, QString &function );
403 
404  static QDomElement createSvgParameterElement( QDomDocument &doc, const QString &name, const QString &value );
405  static QgsStringMap getSvgParameterList( QDomElement &element );
406 
407  static QDomElement createVendorOptionElement( QDomDocument &doc, const QString &name, const QString &value );
408  static QgsStringMap getVendorOptionList( QDomElement &element );
409 
410  static QgsStringMap parseProperties( QDomElement &element );
411  static void saveProperties( QgsStringMap props, QDomDocument &doc, QDomElement &element );
412 
414  static QgsSymbolMap loadSymbols( QDomElement &element, const QgsReadWriteContext &context ) SIP_FACTORY;
416  static QDomElement saveSymbols( QgsSymbolMap &symbols, const QString &tagName, QDomDocument &doc, const QgsReadWriteContext &context );
417 
418  static void clearSymbolMap( QgsSymbolMap &symbols );
419 
427  static QMimeData *symbolToMimeData( QgsSymbol *symbol ) SIP_FACTORY;
428 
435  static QgsSymbol *symbolFromMimeData( const QMimeData *data ) SIP_FACTORY;
436 
443  static QgsColorRamp *loadColorRamp( QDomElement &element ) SIP_FACTORY;
444 
453  static QDomElement saveColorRamp( const QString &name, QgsColorRamp *ramp, QDomDocument &doc );
454 
461  static QVariant colorRampToVariant( const QString &name, QgsColorRamp *ramp );
462 
469  static QgsColorRamp *loadColorRamp( const QVariant &value ) SIP_FACTORY;
470 
477  static QString colorToName( const QColor &color );
478 
486  static QList< QColor > parseColorList( const QString &colorStr );
487 
495  static QMimeData *colorToMimeData( const QColor &color ) SIP_FACTORY;
496 
506  static QColor colorFromMimeData( const QMimeData *data, bool &hasAlpha );
507 
514  static QgsNamedColorList colorListFromMimeData( const QMimeData *data );
515 
523  static QMimeData *colorListToMimeData( const QgsNamedColorList &colorList, bool allFormats = true ) SIP_FACTORY;
524 
533  static bool saveColorsToGpl( QFile &file, const QString &paletteName, const QgsNamedColorList &colors );
534 
543  static QgsNamedColorList importColorsFromGpl( QFile &file, bool &ok, QString &name );
544 
553  static QColor parseColor( const QString &colorStr, bool strictEval = false );
554 
564  static QColor parseColorWithAlpha( const QString &colorStr, bool &containsAlpha, bool strictEval = false );
565 
569  static void multiplyImageOpacity( QImage *image, qreal opacity );
570 
572  static void blurImageInPlace( QImage &image, QRect rect, int radius, bool alphaOnly );
573 
578  static void premultiplyColor( QColor &rgb, int alpha );
579 
581  static void sortVariantList( QList<QVariant> &list, Qt::SortOrder order );
583  static QPointF pointOnLineWithDistance( QPointF startPoint, QPointF directionPoint, double distance );
584 
586  static QStringList listSvgFiles();
587 
589  static QStringList listSvgFilesAt( const QString &directory );
590 
597  static QString svgSymbolNameToPath( const QString &name, const QgsPathResolver &pathResolver );
598 
603  static QString svgSymbolPathToName( const QString &path, const QgsPathResolver &pathResolver );
604 
606  static QPointF polygonCentroid( const QPolygonF &points );
607 
609  static QPointF polygonPointOnSurface( const QPolygonF &points );
610 
612  static bool pointInPolygon( const QPolygonF &points, QPointF point );
613 
621  static QgsExpression *fieldOrExpressionToExpression( const QString &fieldOrExpression ) SIP_FACTORY;
622 
630  static QString fieldOrExpressionFromExpression( QgsExpression *expression );
631 
638  static QList<double> prettyBreaks( double minimum, double maximum, int classes );
639 
645  static double rescaleUom( double size, QgsUnitTypes::RenderUnit unit, const QgsStringMap &props );
646 
652  static QPointF rescaleUom( QPointF point, QgsUnitTypes::RenderUnit unit, const QgsStringMap &props ) SIP_PYNAME( rescalePointUom );
653 
659  static QVector<qreal> rescaleUom( const QVector<qreal> &array, QgsUnitTypes::RenderUnit unit, const QgsStringMap &props ) SIP_PYNAME( rescaleArrayUom );
660 
665  static void applyScaleDependency( QDomDocument &doc, QDomElement &ruleElem, QgsStringMap &props );
666 
671  static void mergeScaleDependencies( double mScaleMinDenom, double mScaleMaxDenom, QgsStringMap &props );
672 
679  static void parametricSvgToSld( QDomDocument &doc, QDomElement &graphicElem,
680  const QString &path,
681  const QColor &fillColor, double size, const QColor &strokeColor, double strokeWidth );
682 
687  static QString getSvgParametricPath( const QString &basePath, const QColor &fillColor, const QColor &strokeColor, double strokeWidth );
688 
689 };
690 
691 class QPolygonF;
692 
694 QList<QPolygonF> offsetLine( QPolygonF polyline, double dist, QgsWkbTypes::GeometryType geometryType ) SIP_SKIP;
695 
696 #endif
697 
698 
Class for parsing and evaluation of expressions (formerly called "search strings").
The class is used as a container of context for various read/write operations on other objects...
Abstract base class for all rendered symbols.
Definition: qgssymbol.h:61
Abstract base class for color ramps.
Definition: qgscolorramp.h:31
QList< QPolygonF > offsetLine(QPolygonF polyline, double dist, QgsWkbTypes::GeometryType geometryType)
calculate geometry shifted by a specified distance
static QgsSymbol * loadSymbol(const QDomElement &element, const QgsReadWriteContext &context)
Attempts to load a symbol from a DOM element.
QMap< QString, QString > QgsStringMap
Definition: qgis.h:587
QMap< QString, QString > QgsStringMap
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
#define SIP_SKIP
Definition: qgis_sip.h:119
QList< QPair< QColor, QString > > QgsNamedColorList
List of colors paired with a friendly display name identifying the color.
ArrowType
Possible arrow types.
QList< QgsSymbolLayer * > QgsSymbolLayerList
Definition: qgssymbol.h:51
#define SIP_FACTORY
Definition: qgis_sip.h:69
ScaleMethod
Scale method.
Definition: qgssymbol.h:94
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.
GeometryType
The geometry types are used to group QgsWkbTypes::Type in a coarse way.
Definition: qgswkbtypes.h:139
VertexMarkerType
Editing vertex markers.
Contains information about the context of a rendering operation.
Struct for storing maximum and minimum scales for measurements in map units.
#define SIP_OUT
Definition: qgis_sip.h:51
QMap< QString, QgsSymbol *> QgsSymbolMap
Definition: qgsrenderer.h:44
QMap< QString, QgsSymbol *> QgsSymbolMap
Resolves relative paths into absolute paths and vice versa.
HeadType
Possible head types.
RenderUnit
Rendering size units.
Definition: qgsunittypes.h:111
#define SIP_PYNAME(name)
Definition: qgis_sip.h:74