QGIS API Documentation  2.7.0-Master
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator 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 
34 typedef QMap<QString, QString> QgsStringMap;
35 typedef QMap<QString, QgsSymbolV2* > QgsSymbolV2Map;
36 typedef QList< QPair< QColor, QString > > QgsNamedColorList;
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  static QIcon symbolLayerPreviewIcon( QgsSymbolLayerV2* layer, QgsSymbolV2::OutputUnit u, QSize size, const QgsMapUnitScale& scale = QgsMapUnitScale() );
107  static QIcon colorRampPreviewIcon( QgsVectorColorRampV2* ramp, QSize size );
108 
109  static void drawStippledBackground( QPainter* painter, QRect rect );
110 
112  static QPixmap symbolPreviewPixmap( QgsSymbolV2* symbol, QSize size, QgsRenderContext* customContext = 0 );
113  static QPixmap colorRampPreviewPixmap( QgsVectorColorRampV2* ramp, QSize size );
114 
116  static double estimateMaxSymbolBleed( QgsSymbolV2* symbol );
117 
118  static QgsSymbolV2* loadSymbol( QDomElement& element );
119  static QgsSymbolLayerV2* loadSymbolLayer( QDomElement& element );
120  static QDomElement saveSymbol( QString symbolName, QgsSymbolV2* symbol, QDomDocument& doc );
121 
122  static bool createSymbolLayerV2ListFromSld( QDomElement& element, QGis::GeometryType geomType, QgsSymbolLayerV2List &layers );
123 
124  static QgsSymbolLayerV2* createFillLayerFromSld( QDomElement &element );
125  static QgsSymbolLayerV2* createLineLayerFromSld( QDomElement &element );
126  static QgsSymbolLayerV2* createMarkerLayerFromSld( QDomElement &element );
127 
128  static bool convertPolygonSymbolizerToPointMarker( QDomElement &element, QgsSymbolLayerV2List &layerList );
129  static bool hasExternalGraphic( QDomElement &element );
130  static bool hasWellKnownMark( QDomElement &element );
131 
132  static bool needFontMarker( QDomElement &element );
133  static bool needSvgMarker( QDomElement &element );
134  static bool needEllipseMarker( QDomElement &element );
135  static bool needMarkerLine( QDomElement &element );
136  static bool needLinePatternFill( QDomElement &element );
137  static bool needPointPatternFill( QDomElement &element );
138  static bool needSvgFill( QDomElement &element );
139 
140  static void fillToSld( QDomDocument &doc, QDomElement &element,
141  Qt::BrushStyle brushStyle, QColor color = QColor() );
142  static bool fillFromSld( QDomElement &element,
143  Qt::BrushStyle &brushStyle, QColor &color );
144 
146  static void lineToSld( QDomDocument &doc, QDomElement &element,
147  Qt::PenStyle penStyle, QColor color, double width = -1,
148  const Qt::PenJoinStyle *penJoinStyle = 0, const Qt::PenCapStyle *penCapStyle = 0,
149  const QVector<qreal> *customDashPattern = 0, double dashOffset = 0.0 );
150  static bool lineFromSld( QDomElement &element,
151  Qt::PenStyle &penStyle, QColor &color, double &width,
152  Qt::PenJoinStyle *penJoinStyle = 0, Qt::PenCapStyle *penCapStyle = 0,
153  QVector<qreal> *customDashPattern = 0, double *dashOffset = 0 );
154 
155  static void externalGraphicToSld( QDomDocument &doc, QDomElement &element,
156  QString path, QString mime,
157  QColor color, double size = -1 );
158  static bool externalGraphicFromSld( QDomElement &element,
159  QString &path, QString &mime,
160  QColor &color, double &size );
161 
163  Q_DECL_DEPRECATED static void wellKnownMarkerToSld( QDomDocument &doc, QDomElement &element,
164  QString name, QColor color, QColor borderColor = QColor(),
165  double borderWidth = -1, double size = -1 );
166  static void wellKnownMarkerToSld( QDomDocument &doc, QDomElement &element,
167  QString name, QColor color, QColor borderColor, Qt::PenStyle borderStyle,
168  double borderWidth = -1, double size = -1 );
170  Q_DECL_DEPRECATED static bool wellKnownMarkerFromSld( QDomElement &element,
171  QString &name, QColor &color, QColor &borderColor,
172  double &borderWidth, double &size );
173  static bool wellKnownMarkerFromSld( QDomElement &element,
174  QString &name, QColor &color, QColor &borderColor, Qt::PenStyle &borderStyle,
175  double &borderWidth, double &size );
176 
177  static void externalMarkerToSld( QDomDocument &doc, QDomElement &element,
178  QString path, QString format, int *markIndex = 0,
179  QColor color = QColor(), double size = -1 );
180  static bool externalMarkerFromSld( QDomElement &element,
181  QString &path, QString &format, int &markIndex,
182  QColor &color, double &size );
183 
184 
185  static void labelTextToSld( QDomDocument &doc, QDomElement &element, QString label,
186  QFont font, QColor color = QColor(), double size = -1 );
187 
189  static QString ogrFeatureStylePen( double width, double mmScaleFactor, double mapUnitsScaleFactor, const QColor& c,
190  Qt::PenJoinStyle joinStyle = Qt::MiterJoin,
191  Qt::PenCapStyle capStyle = Qt::FlatCap,
192  double offset = 0.0,
193  const QVector<qreal>* dashPattern = 0 );
196  static QString ogrFeatureStyleBrush( const QColor& fillColr );
197 
198  static void createRotationElement( QDomDocument &doc, QDomElement &element, QString rotationFunc );
199  static bool rotationFromSldElement( QDomElement &element, QString &rotationFunc );
200 
201  static void createOpacityElement( QDomDocument &doc, QDomElement &element, QString alphaFunc );
202  static bool opacityFromSldElement( QDomElement &element, QString &alphaFunc );
203 
204  static void createDisplacementElement( QDomDocument &doc, QDomElement &element, QPointF offset );
205  static bool displacementFromSldElement( QDomElement &element, QPointF &offset );
206 
207  static void createOnlineResourceElement( QDomDocument &doc, QDomElement &element, QString path, QString format );
208  static bool onlineResourceFromSldElement( QDomElement &element, QString &path, QString &format );
209 
210  static void createGeometryElement( QDomDocument &doc, QDomElement &element, QString geomFunc );
211  static bool geometryFromSldElement( QDomElement &element, QString &geomFunc );
212 
213  static bool createFunctionElement( QDomDocument &doc, QDomElement &element, QString function );
214  static bool functionFromSldElement( QDomElement &element, QString &function );
215 
216  static QDomElement createSvgParameterElement( QDomDocument &doc, QString name, QString value );
217  static QgsStringMap getSvgParameterList( QDomElement &element );
218 
219  static QDomElement createVendorOptionElement( QDomDocument &doc, QString name, QString value );
220  static QgsStringMap getVendorOptionList( QDomElement &element );
221 
222  static QgsStringMap parseProperties( QDomElement& element );
223  static void saveProperties( QgsStringMap props, QDomDocument& doc, QDomElement& element );
224 
225  static QgsSymbolV2Map loadSymbols( QDomElement& element );
226  static QDomElement saveSymbols( QgsSymbolV2Map& symbols, QString tagName, QDomDocument& doc );
227 
228  static void clearSymbolMap( QgsSymbolV2Map& symbols );
229 
230  static QgsVectorColorRampV2* loadColorRamp( QDomElement& element );
231  static QDomElement saveColorRamp( QString name, QgsVectorColorRampV2* ramp, QDomDocument& doc );
232 
239  static QString colorToName( const QColor& color );
240 
248  static QList< QColor > parseColorList( const QString colorStr );
249 
257  static QMimeData *colorToMimeData( const QColor &color );
258 
268  static QColor colorFromMimeData( const QMimeData *data, bool& hasAlpha );
269 
276  static QgsNamedColorList colorListFromMimeData( const QMimeData *data );
277 
285  static QMimeData* colorListToMimeData( const QgsNamedColorList colorList, const bool allFormats = true );
286 
295  static bool saveColorsToGpl( QFile &file, const QString paletteName, QgsNamedColorList colors );
296 
305  static QgsNamedColorList importColorsFromGpl( QFile &file, bool &ok, QString& name );
306 
315  static QColor parseColor( QString colorStr, bool strictEval = false );
316 
326  static QColor parseColorWithAlpha( const QString colorStr, bool &containsAlpha, bool strictEval = false );
327 
329  static double lineWidthScaleFactor( const QgsRenderContext& c, QgsSymbolV2::OutputUnit u, const QgsMapUnitScale& scale = QgsMapUnitScale() );
331  static double pixelSizeScaleFactor( const QgsRenderContext& c, QgsSymbolV2::OutputUnit u, const QgsMapUnitScale& scale = QgsMapUnitScale() );
333  static QgsRenderContext createRenderContext( QPainter* p );
334 
336  static void multiplyImageOpacity( QImage* image, qreal alpha );
337 
339  static void blurImageInPlace( QImage& image, const QRect& rect, int radius, bool alphaOnly );
340 
344  static void premultiplyColor( QColor& rgb, int alpha );
345 
347  static void sortVariantList( QList<QVariant>& list, Qt::SortOrder order );
349  static QPointF pointOnLineWithDistance( const QPointF& startPoint, const QPointF& directionPoint, double distance );
350 
352  static QStringList listSvgFiles();
353 
355  static QStringList listSvgFilesAt( QString directory );
356 
361  static QString symbolNameToPath( QString name );
362 
364  static QString symbolPathToName( QString path );
365 
367  static QPointF polygonCentroid( const QPolygonF& points );
368 
370  static QPointF polygonPointOnSurface( const QPolygonF& points );
371 
373  static bool pointInPolygon( const QPolygonF &points, const QPointF &point );
374 
381  static QgsExpression* fieldOrExpressionToExpression( const QString& fieldOrExpression );
382 
389  static QString fieldOrExpressionFromExpression( QgsExpression* expression );
390 
391 };
392 
393 class QPolygonF;
394 
396 QList<QPolygonF> offsetLine( QPolygonF polyline, double dist );
398 QList<QPolygonF> offsetLine( QPolygonF polyline, double dist, QGis::GeometryType geometryType );
399 
400 #endif
401 
402 
QMap< QString, QgsSymbolV2 * > QgsSymbolV2Map
Definition: qgsrendererv2.h:38
Class for parsing and evaluation of expressions (formerly called "search strings").
Definition: qgsexpression.h:87
QMap< QString, QgsSymbolV2 * > QgsSymbolV2Map
QList< QPair< QColor, QString > > QgsNamedColorList
List of colors paired with a friendly display name identifying the color.
GeometryType
Definition: qgis.h:155
QMap< QString, QString > QgsStringMap
Definition: qgis.h:412
QMap< QString, QString > QgsStringMap
QString file
Definition: qgssvgcache.cpp:76
Contains information about the context of a rendering operation.
QList< QPolygonF > offsetLine(QPolygonF polyline, double dist)
QList< QgsSymbolLayerV2 * > QgsSymbolLayerV2List
Definition: qgssymbolv2.h:39
QList< QPair< QColor, QString > > QgsNamedColorList
double size
Definition: qgssvgcache.cpp:77