QGIS API Documentation  2.11.0-Master
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
qgssymbollayerv2utils.h
Go to the documentation of this file.
1 /***************************************************************************
2  qgssymbollayerv2utils.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 QGSSYMBOLLAYERV2UTILS_H
18 #define QGSSYMBOLLAYERV2UTILS_H
19 
20 #include <QMap>
21 #include <Qt>
22 #include <QtCore>
23 #include <QFont>
24 #include <QColor>
25 #include <QPainter>
26 #include "qgssymbolv2.h"
27 #include "qgis.h"
28 #include "qgsmapunitscale.h"
29 
30 class QgsExpression;
31 class QgsSymbolLayerV2;
33 
37 
38 class QDomDocument;
39 class QDomElement;
40 class QIcon;
41 class QPixmap;
42 class QPointF;
43 class QSize;
44 
45 class CORE_EXPORT QgsSymbolLayerV2Utils
46 {
47  public:
48 
49  static QString encodeColor( QColor color );
50  static QColor decodeColor( QString str );
51 
52  static QString encodeSldAlpha( int alpha );
53  static int decodeSldAlpha( QString str );
54 
55  static QString encodeSldFontStyle( QFont::Style style );
56  static QFont::Style decodeSldFontStyle( QString str );
57 
58  static QString encodeSldFontWeight( int weight );
59  static int decodeSldFontWeight( QString str );
60 
61  static QString encodePenStyle( Qt::PenStyle style );
62  static Qt::PenStyle decodePenStyle( QString str );
63 
64  static QString encodePenJoinStyle( Qt::PenJoinStyle style );
65  static Qt::PenJoinStyle decodePenJoinStyle( QString str );
66 
67  static QString encodePenCapStyle( Qt::PenCapStyle style );
68  static Qt::PenCapStyle decodePenCapStyle( QString str );
69 
70  static QString encodeSldLineJoinStyle( Qt::PenJoinStyle style );
71  static Qt::PenJoinStyle decodeSldLineJoinStyle( QString str );
72 
73  static QString encodeSldLineCapStyle( Qt::PenCapStyle style );
74  static Qt::PenCapStyle decodeSldLineCapStyle( QString str );
75 
76  static QString encodeBrushStyle( Qt::BrushStyle style );
77  static Qt::BrushStyle decodeBrushStyle( QString str );
78 
79  static QString encodeSldBrushStyle( Qt::BrushStyle style );
80  static Qt::BrushStyle decodeSldBrushStyle( QString str );
81 
82  static QString encodePoint( QPointF point );
83  static QPointF decodePoint( QString str );
84 
85  static QString encodeMapUnitScale( const QgsMapUnitScale& mapUnitScale );
86  static QgsMapUnitScale decodeMapUnitScale( const QString& str );
87 
88  static QString encodeRealVector( const QVector<qreal>& v );
89  static QVector<qreal> decodeRealVector( const QString& s );
90 
91  static QString encodeSldRealVector( const QVector<qreal>& v );
92  static QVector<qreal> decodeSldRealVector( const QString& s );
93 
94  static QString encodeOutputUnit( QgsSymbolV2::OutputUnit unit );
95  static QgsSymbolV2::OutputUnit decodeOutputUnit( QString str );
96 
97  static QString encodeSldUom( QgsSymbolV2::OutputUnit unit, double *scaleFactor );
98  static QgsSymbolV2::OutputUnit decodeSldUom( QString str, double *scaleFactor );
99 
100  static QString encodeScaleMethod( QgsSymbolV2::ScaleMethod scaleMethod );
101  static QgsSymbolV2::ScaleMethod decodeScaleMethod( QString str );
102 
103  static QPainter::CompositionMode decodeBlendMode( const QString& s );
104 
105  static QIcon symbolPreviewIcon( QgsSymbolV2* symbol, QSize size );
106 
115  static QPicture symbolLayerPreviewPicture( QgsSymbolLayerV2* layer, QgsSymbolV2::OutputUnit units, QSize size, const QgsMapUnitScale& scale = QgsMapUnitScale() );
116  static QIcon symbolLayerPreviewIcon( QgsSymbolLayerV2* layer, QgsSymbolV2::OutputUnit u, QSize size, const QgsMapUnitScale& scale = QgsMapUnitScale() );
117  static QIcon colorRampPreviewIcon( QgsVectorColorRampV2* ramp, QSize size );
118 
119  static void drawStippledBackground( QPainter* painter, QRect rect );
120 
122  static QPixmap symbolPreviewPixmap( QgsSymbolV2* symbol, QSize size, QgsRenderContext* customContext = 0 );
123  static QPixmap colorRampPreviewPixmap( QgsVectorColorRampV2* ramp, QSize size );
124 
126  static double estimateMaxSymbolBleed( QgsSymbolV2* symbol );
127 
132  static QgsSymbolV2* loadSymbol( const QDomElement& element );
133 
140  template <class SymbolType> static SymbolType* loadSymbol( const QDomElement& element )
141  {
142  QgsSymbolV2* tmpSymbol = QgsSymbolLayerV2Utils::loadSymbol( element );
143  SymbolType* symbolCastToType = dynamic_cast<SymbolType*>( tmpSymbol );
144 
145  if ( symbolCastToType )
146  {
147  return symbolCastToType;
148  }
149  else
150  {
151  //could not cast
152  delete tmpSymbol;
153  return NULL;
154  }
155  }
156 
157  static QgsSymbolLayerV2* loadSymbolLayer( QDomElement& element );
158  static QDomElement saveSymbol( QString symbolName, QgsSymbolV2* symbol, QDomDocument& doc );
159 
160  static bool createSymbolLayerV2ListFromSld( QDomElement& element, QGis::GeometryType geomType, QgsSymbolLayerV2List &layers );
161 
162  static QgsSymbolLayerV2* createFillLayerFromSld( QDomElement &element );
163  static QgsSymbolLayerV2* createLineLayerFromSld( QDomElement &element );
164  static QgsSymbolLayerV2* createMarkerLayerFromSld( QDomElement &element );
165 
166  static bool convertPolygonSymbolizerToPointMarker( QDomElement &element, QgsSymbolLayerV2List &layerList );
167  static bool hasExternalGraphic( QDomElement &element );
168  static bool hasWellKnownMark( QDomElement &element );
169 
170  static bool needFontMarker( QDomElement &element );
171  static bool needSvgMarker( QDomElement &element );
172  static bool needEllipseMarker( QDomElement &element );
173  static bool needMarkerLine( QDomElement &element );
174  static bool needLinePatternFill( QDomElement &element );
175  static bool needPointPatternFill( QDomElement &element );
176  static bool needSvgFill( QDomElement &element );
177 
178  static void fillToSld( QDomDocument &doc, QDomElement &element,
179  Qt::BrushStyle brushStyle, QColor color = QColor() );
180  static bool fillFromSld( QDomElement &element,
181  Qt::BrushStyle &brushStyle, QColor &color );
182 
184  static void lineToSld( QDomDocument &doc, QDomElement &element,
185  Qt::PenStyle penStyle, QColor color, double width = -1,
186  const Qt::PenJoinStyle *penJoinStyle = 0, const Qt::PenCapStyle *penCapStyle = 0,
187  const QVector<qreal> *customDashPattern = 0, double dashOffset = 0.0 );
188  static bool lineFromSld( QDomElement &element,
189  Qt::PenStyle &penStyle, QColor &color, double &width,
190  Qt::PenJoinStyle *penJoinStyle = 0, Qt::PenCapStyle *penCapStyle = 0,
191  QVector<qreal> *customDashPattern = 0, double *dashOffset = 0 );
192 
193  static void externalGraphicToSld( QDomDocument &doc, QDomElement &element,
194  QString path, QString mime,
195  QColor color, double size = -1 );
196  static bool externalGraphicFromSld( QDomElement &element,
197  QString &path, QString &mime,
198  QColor &color, double &size );
199 
201  Q_DECL_DEPRECATED static void wellKnownMarkerToSld( QDomDocument &doc, QDomElement &element,
202  QString name, QColor color, QColor borderColor = QColor(),
203  double borderWidth = -1, double size = -1 );
204  static void wellKnownMarkerToSld( QDomDocument &doc, QDomElement &element,
205  QString name, QColor color, QColor borderColor, Qt::PenStyle borderStyle,
206  double borderWidth = -1, double size = -1 );
208  Q_DECL_DEPRECATED static bool wellKnownMarkerFromSld( QDomElement &element,
209  QString &name, QColor &color, QColor &borderColor,
210  double &borderWidth, double &size );
211  static bool wellKnownMarkerFromSld( QDomElement &element,
212  QString &name, QColor &color, QColor &borderColor, Qt::PenStyle &borderStyle,
213  double &borderWidth, double &size );
214 
215  static void externalMarkerToSld( QDomDocument &doc, QDomElement &element,
216  QString path, QString format, int *markIndex = 0,
217  QColor color = QColor(), double size = -1 );
218  static bool externalMarkerFromSld( QDomElement &element,
219  QString &path, QString &format, int &markIndex,
220  QColor &color, double &size );
221 
222 
223  static void labelTextToSld( QDomDocument &doc, QDomElement &element, QString label,
224  QFont font, QColor color = QColor(), double size = -1 );
225 
227  static QString ogrFeatureStylePen( double width, double mmScaleFactor, double mapUnitsScaleFactor, const QColor& c,
228  Qt::PenJoinStyle joinStyle = Qt::MiterJoin,
229  Qt::PenCapStyle capStyle = Qt::FlatCap,
230  double offset = 0.0,
231  const QVector<qreal>* dashPattern = 0 );
234  static QString ogrFeatureStyleBrush( const QColor& fillColr );
235 
236  static void createRotationElement( QDomDocument &doc, QDomElement &element, QString rotationFunc );
237  static bool rotationFromSldElement( QDomElement &element, QString &rotationFunc );
238 
239  static void createOpacityElement( QDomDocument &doc, QDomElement &element, QString alphaFunc );
240  static bool opacityFromSldElement( QDomElement &element, QString &alphaFunc );
241 
242  static void createDisplacementElement( QDomDocument &doc, QDomElement &element, QPointF offset );
243  static bool displacementFromSldElement( QDomElement &element, QPointF &offset );
244 
245  static void createOnlineResourceElement( QDomDocument &doc, QDomElement &element, QString path, QString format );
246  static bool onlineResourceFromSldElement( QDomElement &element, QString &path, QString &format );
247 
248  static void createGeometryElement( QDomDocument &doc, QDomElement &element, QString geomFunc );
249  static bool geometryFromSldElement( QDomElement &element, QString &geomFunc );
250 
251  static bool createFunctionElement( QDomDocument &doc, QDomElement &element, QString function );
252  static bool functionFromSldElement( QDomElement &element, QString &function );
253 
254  static QDomElement createSvgParameterElement( QDomDocument &doc, QString name, QString value );
255  static QgsStringMap getSvgParameterList( QDomElement &element );
256 
257  static QDomElement createVendorOptionElement( QDomDocument &doc, QString name, QString value );
258  static QgsStringMap getVendorOptionList( QDomElement &element );
259 
260  static QgsStringMap parseProperties( QDomElement& element );
261  static void saveProperties( QgsStringMap props, QDomDocument& doc, QDomElement& element );
262 
263  static QgsSymbolV2Map loadSymbols( QDomElement& element );
264  static QDomElement saveSymbols( QgsSymbolV2Map& symbols, QString tagName, QDomDocument& doc );
265 
266  static void clearSymbolMap( QgsSymbolV2Map& symbols );
267 
268  static QgsVectorColorRampV2* loadColorRamp( QDomElement& element );
269  static QDomElement saveColorRamp( QString name, QgsVectorColorRampV2* ramp, QDomDocument& doc );
270 
277  static QString colorToName( const QColor& color );
278 
286  static QList< QColor > parseColorList( const QString colorStr );
287 
295  static QMimeData *colorToMimeData( const QColor &color );
296 
306  static QColor colorFromMimeData( const QMimeData *data, bool& hasAlpha );
307 
314  static QgsNamedColorList colorListFromMimeData( const QMimeData *data );
315 
323  static QMimeData* colorListToMimeData( const QgsNamedColorList colorList, const bool allFormats = true );
324 
333  static bool saveColorsToGpl( QFile &file, const QString paletteName, QgsNamedColorList colors );
334 
343  static QgsNamedColorList importColorsFromGpl( QFile &file, bool &ok, QString& name );
344 
353  static QColor parseColor( QString colorStr, bool strictEval = false );
354 
364  static QColor parseColorWithAlpha( const QString colorStr, bool &containsAlpha, bool strictEval = false );
365 
367  static double lineWidthScaleFactor( const QgsRenderContext& c, QgsSymbolV2::OutputUnit u, const QgsMapUnitScale& scale = QgsMapUnitScale() );
369  static double pixelSizeScaleFactor( const QgsRenderContext& c, QgsSymbolV2::OutputUnit u, const QgsMapUnitScale& scale = QgsMapUnitScale() );
371  static QgsRenderContext createRenderContext( QPainter* p );
372 
374  static void multiplyImageOpacity( QImage* image, qreal alpha );
375 
377  static void blurImageInPlace( QImage& image, const QRect& rect, int radius, bool alphaOnly );
378 
382  static void premultiplyColor( QColor& rgb, int alpha );
383 
385  static void sortVariantList( QList<QVariant>& list, Qt::SortOrder order );
387  static QPointF pointOnLineWithDistance( const QPointF& startPoint, const QPointF& directionPoint, double distance );
388 
390  static QStringList listSvgFiles();
391 
393  static QStringList listSvgFilesAt( QString directory );
394 
399  static QString symbolNameToPath( QString name );
400 
402  static QString symbolPathToName( QString path );
403 
405  static QPointF polygonCentroid( const QPolygonF& points );
406 
408  static QPointF polygonPointOnSurface( const QPolygonF& points );
409 
411  static bool pointInPolygon( const QPolygonF &points, const QPointF &point );
412 
419  static QgsExpression* fieldOrExpressionToExpression( const QString& fieldOrExpression );
420 
427  static QString fieldOrExpressionFromExpression( QgsExpression* expression );
428 
434  static QList<double> prettyBreaks( double minimum, double maximum, int classes );
435 
436 };
437 
438 class QPolygonF;
439 
441 QList<QPolygonF> offsetLine( QPolygonF polyline, double dist );
443 QList<QPolygonF> offsetLine( QPolygonF polyline, double dist, QGis::GeometryType geometryType );
444 
445 #endif
446 
447 
Class for parsing and evaluation of expressions (formerly called "search strings").
Definition: qgsexpression.h:88
QMap< QString, QgsSymbolV2 * > QgsSymbolV2Map
GeometryType
Definition: qgis.h:155
static QgsSymbolV2 * loadSymbol(const QDomElement &element)
Attempts to load a symbol from a DOM element.
QMap< QString, QString > QgsStringMap
Contains information about the context of a rendering operation.
QList< QPolygonF > offsetLine(QPolygonF polyline, double dist)
QList< QPair< QColor, QString > > QgsNamedColorList
static SymbolType * loadSymbol(const QDomElement &element)
Attempts to load a symbol from a DOM element and cast it to a particular symbol type.