QGIS API Documentation  3.10.0-A Coruña (6c816b4204)
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 );
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 
217  static QIcon symbolPreviewIcon( const QgsSymbol *symbol, QSize size, int padding = 0 );
218 
232  static QPixmap symbolPreviewPixmap( const QgsSymbol *symbol, QSize size, int padding = 0, QgsRenderContext *customContext = nullptr, bool selected = false,
233  const QgsExpressionContext *expressionContext = nullptr );
234 
245  static QPicture symbolLayerPreviewPicture( const QgsSymbolLayer *layer, QgsUnitTypes::RenderUnit units, QSize size, const QgsMapUnitScale &scale = QgsMapUnitScale() );
246 
256  static QIcon symbolLayerPreviewIcon( const QgsSymbolLayer *layer, QgsUnitTypes::RenderUnit u, QSize size, const QgsMapUnitScale &scale = QgsMapUnitScale() );
257 
265  static QIcon colorRampPreviewIcon( QgsColorRamp *ramp, QSize size, int padding = 0 );
266 
274  static QPixmap colorRampPreviewPixmap( QgsColorRamp *ramp, QSize size, int padding = 0 );
275 
276  static void drawStippledBackground( QPainter *painter, QRect rect );
277 
282  static void drawVertexMarker( double x, double y, QPainter &p, QgsSymbolLayerUtils::VertexMarkerType type, int markerSize );
283 
285  static double estimateMaxSymbolBleed( QgsSymbol *symbol, const QgsRenderContext &context );
286 
293  static QgsSymbol *loadSymbol( const QDomElement &element, const QgsReadWriteContext &context ) SIP_FACTORY;
294 
303  template <class SymbolType> static SymbolType *loadSymbol( const QDomElement &element, const QgsReadWriteContext &context ) SIP_SKIP
304  {
305  QgsSymbol *tmpSymbol = QgsSymbolLayerUtils::loadSymbol( element, context );
306  SymbolType *symbolCastToType = dynamic_cast<SymbolType *>( tmpSymbol );
307 
308  if ( symbolCastToType )
309  {
310  return symbolCastToType;
311  }
312  else
313  {
314  //could not cast
315  delete tmpSymbol;
316  return nullptr;
317  }
318  }
319 
321  static QgsSymbolLayer *loadSymbolLayer( QDomElement &element, const QgsReadWriteContext &context ) SIP_FACTORY;
323  static QDomElement saveSymbol( const QString &symbolName, const QgsSymbol *symbol, QDomDocument &doc, const QgsReadWriteContext &context );
324 
330  static QString symbolProperties( QgsSymbol *symbol );
331 
332  static bool createSymbolLayerListFromSld( QDomElement &element, QgsWkbTypes::GeometryType geomType, QgsSymbolLayerList &layers );
333 
334  static QgsSymbolLayer *createFillLayerFromSld( QDomElement &element ) SIP_FACTORY;
335  static QgsSymbolLayer *createLineLayerFromSld( QDomElement &element ) SIP_FACTORY;
336  static QgsSymbolLayer *createMarkerLayerFromSld( QDomElement &element ) SIP_FACTORY;
337 
338  static bool convertPolygonSymbolizerToPointMarker( QDomElement &element, QgsSymbolLayerList &layerList );
339  static bool hasExternalGraphic( QDomElement &element );
340  static bool hasWellKnownMark( QDomElement &element );
341 
342  static bool needFontMarker( QDomElement &element );
343  static bool needSvgMarker( QDomElement &element );
344  static bool needEllipseMarker( QDomElement &element );
345  static bool needMarkerLine( QDomElement &element );
346  static bool needLinePatternFill( QDomElement &element );
347  static bool needPointPatternFill( QDomElement &element );
348  static bool needSvgFill( QDomElement &element );
349 
350  static void fillToSld( QDomDocument &doc, QDomElement &element,
351  Qt::BrushStyle brushStyle, const QColor &color = QColor() );
352  static bool fillFromSld( QDomElement &element,
353  Qt::BrushStyle &brushStyle, QColor &color );
354 
356  static void lineToSld( QDomDocument &doc, QDomElement &element,
357  Qt::PenStyle penStyle, const QColor &color, double width = -1,
358  const Qt::PenJoinStyle *penJoinStyle = nullptr, const Qt::PenCapStyle *penCapStyle = nullptr,
359  const QVector<qreal> *customDashPattern = nullptr, double dashOffset = 0.0 ) SIP_SKIP;
360  static bool lineFromSld( QDomElement &element,
361  Qt::PenStyle &penStyle, QColor &color, double &width,
362  Qt::PenJoinStyle *penJoinStyle = nullptr, Qt::PenCapStyle *penCapStyle = nullptr,
363  QVector<qreal> *customDashPattern = nullptr, double *dashOffset = nullptr );
364 
365  static void externalGraphicToSld( QDomDocument &doc, QDomElement &element,
366  const QString &path, const QString &mime,
367  const QColor &color, double size = -1 );
368  static bool externalGraphicFromSld( QDomElement &element,
369  QString &path, QString &mime,
370  QColor &color, double &size );
371 
372  static void wellKnownMarkerToSld( QDomDocument &doc, QDomElement &element,
373  const QString &name, const QColor &color, const QColor &strokeColor, Qt::PenStyle strokeStyle,
374  double strokeWidth = -1, double size = -1 );
375 
377  static bool wellKnownMarkerFromSld( QDomElement &element,
378  QString &name, QColor &color, QColor &strokeColor, Qt::PenStyle &strokeStyle,
379  double &strokeWidth, double &size ) SIP_PYNAME( wellKnownMarkerFromSld2 );
380 
381  static void externalMarkerToSld( QDomDocument &doc, QDomElement &element,
382  const QString &path, const QString &format, int *markIndex = nullptr,
383  const QColor &color = QColor(), double size = -1 );
384  static bool externalMarkerFromSld( QDomElement &element,
385  QString &path, QString &format, int &markIndex,
386  QColor &color, double &size );
387 
388 
389  static void labelTextToSld( QDomDocument &doc, QDomElement &element, const QString &label,
390  const QFont &font, const QColor &color = QColor(), double size = -1 );
391 
393  static QString ogrFeatureStylePen( double width, double mmScaleFactor, double mapUnitsScaleFactor, const QColor &c,
394  Qt::PenJoinStyle joinStyle = Qt::MiterJoin,
395  Qt::PenCapStyle capStyle = Qt::FlatCap,
396  double offset = 0.0,
397  const QVector<qreal> *dashPattern = nullptr );
398 
402  static QString ogrFeatureStyleBrush( const QColor &fillColr );
403 
404  static void createRotationElement( QDomDocument &doc, QDomElement &element, const QString &rotationFunc );
405  static bool rotationFromSldElement( QDomElement &element, QString &rotationFunc );
406 
407  static void createOpacityElement( QDomDocument &doc, QDomElement &element, const QString &alphaFunc );
408  static bool opacityFromSldElement( QDomElement &element, QString &alphaFunc );
409 
410  static void createDisplacementElement( QDomDocument &doc, QDomElement &element, QPointF offset );
411  static bool displacementFromSldElement( QDomElement &element, QPointF &offset );
412 
419  static void createAnchorPointElement( QDomDocument &doc, QDomElement &element, QPointF anchor );
420 
421  static void createOnlineResourceElement( QDomDocument &doc, QDomElement &element, const QString &path, const QString &format );
422  static bool onlineResourceFromSldElement( QDomElement &element, QString &path, QString &format );
423 
424  static void createGeometryElement( QDomDocument &doc, QDomElement &element, const QString &geomFunc );
425  static bool geometryFromSldElement( QDomElement &element, QString &geomFunc );
426 
433  static bool createExpressionElement( QDomDocument &doc, QDomElement &element, const QString &function );
434  static bool createFunctionElement( QDomDocument &doc, QDomElement &element, const QString &function );
435  static bool functionFromSldElement( QDomElement &element, QString &function );
436 
437  static QDomElement createSvgParameterElement( QDomDocument &doc, const QString &name, const QString &value );
438  static QgsStringMap getSvgParameterList( QDomElement &element );
439 
440  static QDomElement createVendorOptionElement( QDomDocument &doc, const QString &name, const QString &value );
441  static QgsStringMap getVendorOptionList( QDomElement &element );
442 
443  static QgsStringMap parseProperties( QDomElement &element );
444  static void saveProperties( QgsStringMap props, QDomDocument &doc, QDomElement &element );
445 
447  static QgsSymbolMap loadSymbols( QDomElement &element, const QgsReadWriteContext &context ) SIP_FACTORY;
449  static QDomElement saveSymbols( QgsSymbolMap &symbols, const QString &tagName, QDomDocument &doc, const QgsReadWriteContext &context );
450 
451  static void clearSymbolMap( QgsSymbolMap &symbols );
452 
460  static QMimeData *symbolToMimeData( const QgsSymbol *symbol ) SIP_FACTORY;
461 
468  static QgsSymbol *symbolFromMimeData( const QMimeData *data ) SIP_FACTORY;
469 
476  static QgsColorRamp *loadColorRamp( QDomElement &element ) SIP_FACTORY;
477 
486  static QDomElement saveColorRamp( const QString &name, QgsColorRamp *ramp, QDomDocument &doc );
487 
494  static QVariant colorRampToVariant( const QString &name, QgsColorRamp *ramp );
495 
502  static QgsColorRamp *loadColorRamp( const QVariant &value ) SIP_FACTORY;
503 
510  static QString colorToName( const QColor &color );
511 
519  static QList< QColor > parseColorList( const QString &colorStr );
520 
528  static QMimeData *colorToMimeData( const QColor &color ) SIP_FACTORY;
529 
539  static QColor colorFromMimeData( const QMimeData *data, bool &hasAlpha );
540 
547  static QgsNamedColorList colorListFromMimeData( const QMimeData *data );
548 
556  static QMimeData *colorListToMimeData( const QgsNamedColorList &colorList, bool allFormats = true ) SIP_FACTORY;
557 
566  static bool saveColorsToGpl( QFile &file, const QString &paletteName, const QgsNamedColorList &colors );
567 
576  static QgsNamedColorList importColorsFromGpl( QFile &file, bool &ok, QString &name );
577 
586  static QColor parseColor( const QString &colorStr, bool strictEval = false );
587 
597  static QColor parseColorWithAlpha( const QString &colorStr, bool &containsAlpha, bool strictEval = false );
598 
602  static void multiplyImageOpacity( QImage *image, qreal opacity );
603 
605  static void blurImageInPlace( QImage &image, QRect rect, int radius, bool alphaOnly );
606 
611  static void premultiplyColor( QColor &rgb, int alpha );
612 
614  static void sortVariantList( QList<QVariant> &list, Qt::SortOrder order );
616  static QPointF pointOnLineWithDistance( QPointF startPoint, QPointF directionPoint, double distance );
617 
619  static QStringList listSvgFiles();
620 
622  static QStringList listSvgFilesAt( const QString &directory );
623 
630  static QString svgSymbolNameToPath( const QString &name, const QgsPathResolver &pathResolver );
631 
636  static QString svgSymbolPathToName( const QString &path, const QgsPathResolver &pathResolver );
637 
639  static QPointF polygonCentroid( const QPolygonF &points );
640 
642  static QPointF polygonPointOnSurface( const QPolygonF &points, QList<QPolygonF> *rings = nullptr );
643 
645  static bool pointInPolygon( const QPolygonF &points, QPointF point );
646 
654  static QgsExpression *fieldOrExpressionToExpression( const QString &fieldOrExpression ) SIP_FACTORY;
655 
663  static QString fieldOrExpressionFromExpression( QgsExpression *expression );
664 
671  static QList<double> prettyBreaks( double minimum, double maximum, int classes );
672 
678  static double rescaleUom( double size, QgsUnitTypes::RenderUnit unit, const QgsStringMap &props );
679 
685  static QPointF rescaleUom( QPointF point, QgsUnitTypes::RenderUnit unit, const QgsStringMap &props ) SIP_PYNAME( rescalePointUom );
686 
692  static QVector<qreal> rescaleUom( const QVector<qreal> &array, QgsUnitTypes::RenderUnit unit, const QgsStringMap &props ) SIP_PYNAME( rescaleArrayUom );
693 
698  static void applyScaleDependency( QDomDocument &doc, QDomElement &ruleElem, QgsStringMap &props );
699 
704  static void mergeScaleDependencies( double mScaleMinDenom, double mScaleMaxDenom, QgsStringMap &props );
705 
712  static void parametricSvgToSld( QDomDocument &doc, QDomElement &graphicElem,
713  const QString &path,
714  const QColor &fillColor, double size, const QColor &strokeColor, double strokeWidth );
715 
720  static QString getSvgParametricPath( const QString &basePath, const QColor &fillColor, const QColor &strokeColor, double strokeWidth );
721 
722 };
723 
724 class QPolygonF;
725 
727 QList<QPolygonF> offsetLine( QPolygonF polyline, double dist, QgsWkbTypes::GeometryType geometryType ) SIP_SKIP;
728 
729 #endif
730 
731 
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:612
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:126
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
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:76
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:58
QMap< QString, QgsSymbol *> QgsSymbolMap
Definition: qgsrenderer.h:45
QMap< QString, QgsSymbol *> QgsSymbolMap
Resolves relative paths into absolute paths and vice versa.
HeadType
Possible head types.
RenderUnit
Rendering size units.
Definition: qgsunittypes.h:145
#define SIP_PYNAME(name)
Definition: qgis_sip.h:81