/
qgsfillsymbollayerv2.cpp
293 lines (246 loc) · 7.25 KB
/
qgsfillsymbollayerv2.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
#include "qgsfillsymbollayerv2.h"
#include "qgssymbollayerv2utils.h"
#include "qgsrendercontext.h"
#include <QPainter>
QgsSimpleFillSymbolLayerV2::QgsSimpleFillSymbolLayerV2( QColor color, Qt::BrushStyle style, QColor borderColor, Qt::PenStyle borderStyle, double borderWidth )
: mBrushStyle( style ), mBorderColor( borderColor ), mBorderStyle( borderStyle ), mBorderWidth( borderWidth )
{
mColor = color;
}
QgsSymbolLayerV2* QgsSimpleFillSymbolLayerV2::create( const QgsStringMap& props )
{
QColor color = DEFAULT_SIMPLEFILL_COLOR;
Qt::BrushStyle style = DEFAULT_SIMPLEFILL_STYLE;
QColor borderColor = DEFAULT_SIMPLEFILL_BORDERCOLOR;
Qt::PenStyle borderStyle = DEFAULT_SIMPLEFILL_BORDERSTYLE;
double borderWidth = DEFAULT_SIMPLEFILL_BORDERWIDTH;
if ( props.contains( "color" ) )
color = QgsSymbolLayerV2Utils::decodeColor( props["color"] );
if ( props.contains( "style" ) )
style = QgsSymbolLayerV2Utils::decodeBrushStyle( props["style"] );
if ( props.contains( "color_border" ) )
borderColor = QgsSymbolLayerV2Utils::decodeColor( props["color_border"] );
if ( props.contains( "style_border" ) )
borderStyle = QgsSymbolLayerV2Utils::decodePenStyle( props["style_border"] );
if ( props.contains( "width_border" ) )
borderWidth = props["width_border"].toDouble();
return new QgsSimpleFillSymbolLayerV2( color, style, borderColor, borderStyle, borderWidth );
}
QString QgsSimpleFillSymbolLayerV2::layerType() const
{
return "SimpleFill";
}
void QgsSimpleFillSymbolLayerV2::startRender( QgsSymbolV2RenderContext& context )
{
mBrush = QBrush( mColor, mBrushStyle );
mPen = QPen( mBorderColor );
mPen.setStyle( mBorderStyle );
mPen.setWidthF( context.outputLineWidth( mBorderWidth ) );
}
void QgsSimpleFillSymbolLayerV2::stopRender( QgsSymbolV2RenderContext& context )
{
}
void QgsSimpleFillSymbolLayerV2::renderPolygon( const QPolygonF& points, QList<QPolygonF>* rings, QgsSymbolV2RenderContext& context )
{
QPainter* p = context.renderContext().painter();
if ( !p )
{
return;
}
p->setBrush( mBrush );
p->setPen( mPen );
_renderPolygon( p, points, rings );
}
QgsStringMap QgsSimpleFillSymbolLayerV2::properties() const
{
QgsStringMap map;
map["color"] = QgsSymbolLayerV2Utils::encodeColor( mColor );
map["style"] = QgsSymbolLayerV2Utils::encodeBrushStyle( mBrushStyle );
map["color_border"] = QgsSymbolLayerV2Utils::encodeColor( mBorderColor );
map["style_border"] = QgsSymbolLayerV2Utils::encodePenStyle( mBorderStyle );
map["width_border"] = QString::number( mBorderWidth );
return map;
}
QgsSymbolLayerV2* QgsSimpleFillSymbolLayerV2::clone() const
{
return new QgsSimpleFillSymbolLayerV2( mColor, mBrushStyle, mBorderColor, mBorderStyle, mBorderWidth );
}
//QgsSVGFillSymbolLayer
#include <QFile>
#include <QSvgRenderer>
QgsSVGFillSymbolLayer::QgsSVGFillSymbolLayer( const QString& svgFilePath, double width ): mPatternWidth( width ), mOutline( 0 )
{
setSvgFilePath( svgFilePath );
mOutlineWidth = 0.3;
setSubSymbol( new QgsLineSymbolV2() );
}
QgsSVGFillSymbolLayer::QgsSVGFillSymbolLayer( const QByteArray& svgData, double width ): mSvgData( svgData ), mPatternWidth( width ), mOutline( 0 )
{
storeViewBox();
mOutlineWidth = 0.3;
setSubSymbol( new QgsLineSymbolV2() );
}
QgsSVGFillSymbolLayer::~QgsSVGFillSymbolLayer()
{
delete mOutline;
}
void QgsSVGFillSymbolLayer::setSvgFilePath( const QString& svgPath )
{
QFile svgFile( svgPath );
if ( svgFile.open( QFile::ReadOnly ) )
{
mSvgData = svgFile.readAll();
storeViewBox();
}
mSvgFilePath = svgPath;
}
QgsSymbolLayerV2* QgsSVGFillSymbolLayer::create( const QgsStringMap& properties )
{
QByteArray data;
double width = 20;
QString svgFilePath;
if ( properties.contains( "width" ) )
{
width = properties["width"].toDouble();
}
if ( properties.contains( "svgFile" ) )
{
svgFilePath = properties["svgFile"];
}
if ( !svgFilePath.isEmpty() )
{
return new QgsSVGFillSymbolLayer( svgFilePath, width );
}
else
{
if ( properties.contains( "data" ) )
{
data = QByteArray::fromHex( properties["data"].toLocal8Bit() );
}
return new QgsSVGFillSymbolLayer( data, width );
}
}
QString QgsSVGFillSymbolLayer::layerType() const
{
return "SVGFill";
}
void QgsSVGFillSymbolLayer::startRender( QgsSymbolV2RenderContext& context )
{
if ( mSvgViewBox.isNull() )
{
return;
}
//create QImage with appropriate dimensions
int pixelWidth = context.outputPixelSize( mPatternWidth );//mPatternWidth.value( context, QgsOutputUnit::Pixel );
int pixelHeight = pixelWidth / mSvgViewBox.width() * mSvgViewBox.height();
QImage textureImage( pixelWidth, pixelHeight, QImage::Format_ARGB32_Premultiplied );
textureImage.fill( QColor( 255, 255, 255, 0 ).rgba() );
//rasterise byte array to image
QPainter p( &textureImage );
QSvgRenderer r( mSvgData );
if ( !r.isValid() )
{
return;
}
r.render( &p );
QTransform brushTransform;
brushTransform.scale( 1.0 / context.renderContext().rasterScaleFactor(), 1.0 / context.renderContext().rasterScaleFactor() );
mBrush.setTextureImage( textureImage );
mBrush.setTransform( brushTransform );
if ( mOutline )
{
mOutline->startRender( context.renderContext() );
}
}
void QgsSVGFillSymbolLayer::stopRender( QgsSymbolV2RenderContext& context )
{
if ( mOutline )
{
mOutline->stopRender( context.renderContext() );
}
}
void QgsSVGFillSymbolLayer::renderPolygon( const QPolygonF& points, QList<QPolygonF>* rings, QgsSymbolV2RenderContext& context )
{
QPainter* p = context.renderContext().painter();
if ( !p )
{
return;
}
p->setBrush( mBrush );
p->setPen( QPen( Qt::NoPen ) );
_renderPolygon( p, points, rings );
if ( mOutline )
{
mOutline->renderPolyline( points, context.renderContext() );
if ( rings )
{
QList<QPolygonF>::const_iterator ringIt = rings->constBegin();
for ( ; ringIt != rings->constEnd(); ++ringIt )
{
mOutline->renderPolyline( *ringIt, context.renderContext() );
}
}
}
}
QgsStringMap QgsSVGFillSymbolLayer::properties() const
{
QgsStringMap map;
if ( !mSvgFilePath.isEmpty() )
{
map.insert( "svgFile", mSvgFilePath );
}
else
{
map.insert( "data", QString( mSvgData.toHex() ) );
}
map.insert( "width", QString::number( mPatternWidth ) );
return map;
}
QgsSymbolLayerV2* QgsSVGFillSymbolLayer::clone() const
{
QgsSymbolLayerV2* clonedLayer = 0;
if ( !mSvgFilePath.isEmpty() )
{
clonedLayer = new QgsSVGFillSymbolLayer( mSvgFilePath, mPatternWidth );
}
else
{
clonedLayer = new QgsSVGFillSymbolLayer( mSvgData, mPatternWidth );
}
if ( mOutline )
{
clonedLayer->setSubSymbol( mOutline->clone() );
}
return clonedLayer;
}
void QgsSVGFillSymbolLayer::storeViewBox()
{
if ( !mSvgData.isEmpty() )
{
QSvgRenderer r( mSvgData );
if ( r.isValid() )
{
mSvgViewBox = r.viewBoxF();
return;
}
}
mSvgViewBox = QRectF();
return;
}
bool QgsSVGFillSymbolLayer::setSubSymbol( QgsSymbolV2* symbol )
{
if ( !symbol || symbol->type() != QgsSymbolV2::Line )
{
delete symbol;
return false;
}
QgsLineSymbolV2* lineSymbol = dynamic_cast<QgsLineSymbolV2*>( symbol );
if ( lineSymbol )
{
delete mOutline;
mOutline = lineSymbol;
return true;
}
delete symbol;
return false;
}