10
10
#include < QSvgRenderer>
11
11
12
12
QgsSimpleFillSymbolLayerV2::QgsSimpleFillSymbolLayerV2 ( QColor color, Qt::BrushStyle style, QColor borderColor, Qt::PenStyle borderStyle, double borderWidth )
13
- : mBrushStyle( style ), mBorderColor( borderColor ), mBorderStyle( borderStyle ), mBorderWidth( borderWidth )
13
+ : mBrushStyle( style ), mBorderColor( borderColor ), mBorderStyle( borderStyle ), mBorderWidth( borderWidth )
14
14
{
15
15
mColor = color;
16
16
}
@@ -25,17 +25,17 @@ QgsSymbolLayerV2* QgsSimpleFillSymbolLayerV2::create( const QgsStringMap& props
25
25
double borderWidth = DEFAULT_SIMPLEFILL_BORDERWIDTH;
26
26
QPointF offset;
27
27
28
- if ( props.contains ( " color" ) )
28
+ if ( props.contains ( " color" ) )
29
29
color = QgsSymbolLayerV2Utils::decodeColor ( props[" color" ] );
30
- if ( props.contains ( " style" ) )
30
+ if ( props.contains ( " style" ) )
31
31
style = QgsSymbolLayerV2Utils::decodeBrushStyle ( props[" style" ] );
32
- if ( props.contains ( " color_border" ) )
32
+ if ( props.contains ( " color_border" ) )
33
33
borderColor = QgsSymbolLayerV2Utils::decodeColor ( props[" color_border" ] );
34
- if ( props.contains ( " style_border" ) )
34
+ if ( props.contains ( " style_border" ) )
35
35
borderStyle = QgsSymbolLayerV2Utils::decodePenStyle ( props[" style_border" ] );
36
- if ( props.contains ( " width_border" ) )
36
+ if ( props.contains ( " width_border" ) )
37
37
borderWidth = props[" width_border" ].toDouble ();
38
- if ( props.contains ( " offset" ) )
38
+ if ( props.contains ( " offset" ) )
39
39
offset = QgsSymbolLayerV2Utils::decodePoint ( props[" offset" ] );
40
40
41
41
QgsSimpleFillSymbolLayerV2* sl = new QgsSimpleFillSymbolLayerV2 ( color, style, borderColor, borderStyle, borderWidth );
@@ -56,15 +56,15 @@ void QgsSimpleFillSymbolLayerV2::startRender( QgsSymbolV2RenderContext& context
56
56
57
57
// scale brush content for printout
58
58
double rasterScaleFactor = context.renderContext ().rasterScaleFactor ();
59
- if ( rasterScaleFactor != 1.0 )
59
+ if ( rasterScaleFactor != 1.0 )
60
60
{
61
61
mBrush .setMatrix ( QMatrix ().scale ( 1.0 / rasterScaleFactor, 1.0 / rasterScaleFactor ) );
62
62
}
63
63
64
64
QColor selColor = context.selectionColor ();
65
65
// selColor.setAlphaF( context.alpha() );
66
66
mSelBrush = QBrush ( selColor );
67
- if ( selectFillStyle ) mSelBrush .setStyle ( mBrushStyle );
67
+ if ( selectFillStyle ) mSelBrush .setStyle ( mBrushStyle );
68
68
mBorderColor .setAlphaF ( context.alpha () );
69
69
mPen = QPen ( mBorderColor );
70
70
mPen .setStyle ( mBorderStyle );
@@ -78,20 +78,20 @@ void QgsSimpleFillSymbolLayerV2::stopRender( QgsSymbolV2RenderContext& context )
78
78
void QgsSimpleFillSymbolLayerV2::renderPolygon ( const QPolygonF& points, QList<QPolygonF>* rings, QgsSymbolV2RenderContext& context )
79
79
{
80
80
QPainter* p = context.renderContext ().painter ();
81
- if ( !p )
81
+ if ( !p )
82
82
{
83
83
return ;
84
84
}
85
85
86
86
p->setBrush ( context.selected () ? mSelBrush : mBrush );
87
87
p->setPen ( mPen );
88
88
89
- if ( !mOffset .isNull () )
89
+ if ( !mOffset .isNull () )
90
90
p->translate ( mOffset );
91
91
92
92
_renderPolygon ( p, points, rings );
93
93
94
- if ( !mOffset .isNull () )
94
+ if ( !mOffset .isNull () )
95
95
p->translate ( -mOffset );
96
96
}
97
97
@@ -138,7 +138,7 @@ QgsSVGFillSymbolLayer::~QgsSVGFillSymbolLayer()
138
138
void QgsSVGFillSymbolLayer::setSvgFilePath ( const QString& svgPath )
139
139
{
140
140
QFile svgFile ( svgPath );
141
- if ( svgFile.open ( QFile::ReadOnly ) )
141
+ if ( svgFile.open ( QFile::ReadOnly ) )
142
142
{
143
143
mSvgData = svgFile.readAll ();
144
144
storeViewBox ();
@@ -153,24 +153,24 @@ QgsSymbolLayerV2* QgsSVGFillSymbolLayer::create( const QgsStringMap& properties
153
153
QString svgFilePath;
154
154
155
155
156
- if ( properties.contains ( " width" ) )
156
+ if ( properties.contains ( " width" ) )
157
157
{
158
158
width = properties[" width" ].toDouble ();
159
159
}
160
- if ( properties.contains ( " svgFile" ) )
160
+ if ( properties.contains ( " svgFile" ) )
161
161
{
162
162
QString svgName = properties[" svgFile" ];
163
163
QString savePath = QgsSvgMarkerSymbolLayerV2::symbolNameToPath ( svgName );
164
164
svgFilePath = ( savePath.isEmpty () ? svgName : savePath );
165
165
}
166
166
167
- if ( !svgFilePath.isEmpty () )
167
+ if ( !svgFilePath.isEmpty () )
168
168
{
169
169
return new QgsSVGFillSymbolLayer ( svgFilePath, width );
170
170
}
171
171
else
172
172
{
173
- if ( properties.contains ( " data" ) )
173
+ if ( properties.contains ( " data" ) )
174
174
{
175
175
data = QByteArray::fromHex ( properties[" data" ].toLocal8Bit () );
176
176
}
@@ -186,7 +186,7 @@ QString QgsSVGFillSymbolLayer::layerType() const
186
186
187
187
void QgsSVGFillSymbolLayer::startRender ( QgsSymbolV2RenderContext& context )
188
188
{
189
- if ( mSvgViewBox .isNull () )
189
+ if ( mSvgViewBox .isNull () )
190
190
{
191
191
return ;
192
192
}
@@ -201,13 +201,13 @@ void QgsSVGFillSymbolLayer::startRender( QgsSymbolV2RenderContext& context )
201
201
// rasterise byte array to image
202
202
QPainter p ( &textureImage );
203
203
QSvgRenderer r ( mSvgData );
204
- if ( !r.isValid () )
204
+ if ( !r.isValid () )
205
205
{
206
206
return ;
207
207
}
208
208
r.render ( &p );
209
209
210
- if ( context.alpha () < 1.0 )
210
+ if ( context.alpha () < 1.0 )
211
211
{
212
212
QgsSymbolLayerV2Utils::multiplyImageOpacity ( &textureImage, context.alpha () );
213
213
}
@@ -217,15 +217,15 @@ void QgsSVGFillSymbolLayer::startRender( QgsSymbolV2RenderContext& context )
217
217
mBrush .setTextureImage ( textureImage );
218
218
mBrush .setTransform ( brushTransform );
219
219
220
- if ( mOutline )
220
+ if ( mOutline )
221
221
{
222
222
mOutline ->startRender ( context.renderContext () );
223
223
}
224
224
}
225
225
226
226
void QgsSVGFillSymbolLayer::stopRender ( QgsSymbolV2RenderContext& context )
227
227
{
228
- if ( mOutline )
228
+ if ( mOutline )
229
229
{
230
230
mOutline ->stopRender ( context.renderContext () );
231
231
}
@@ -234,27 +234,27 @@ void QgsSVGFillSymbolLayer::stopRender( QgsSymbolV2RenderContext& context )
234
234
void QgsSVGFillSymbolLayer::renderPolygon ( const QPolygonF& points, QList<QPolygonF>* rings, QgsSymbolV2RenderContext& context )
235
235
{
236
236
QPainter* p = context.renderContext ().painter ();
237
- if ( !p )
237
+ if ( !p )
238
238
{
239
239
return ;
240
240
}
241
241
p->setPen ( QPen ( Qt::NoPen ) );
242
- if ( context.selected () )
242
+ if ( context.selected () )
243
243
{
244
244
QColor selColor = context.selectionColor ();
245
- if ( ! selectionIsOpaque ) selColor.setAlphaF ( context.alpha () );
245
+ if ( ! selectionIsOpaque ) selColor.setAlphaF ( context.alpha () );
246
246
p->setBrush ( QBrush ( selColor ) );
247
247
_renderPolygon ( p, points, rings );
248
248
}
249
249
p->setBrush ( mBrush );
250
250
_renderPolygon ( p, points, rings );
251
- if ( mOutline )
251
+ if ( mOutline )
252
252
{
253
253
mOutline ->renderPolyline ( points, context.renderContext (), -1 , selectFillBorder && context.selected () );
254
- if ( rings )
254
+ if ( rings )
255
255
{
256
256
QList<QPolygonF>::const_iterator ringIt = rings->constBegin ();
257
- for ( ; ringIt != rings->constEnd (); ++ringIt )
257
+ for ( ; ringIt != rings->constEnd (); ++ringIt )
258
258
{
259
259
mOutline ->renderPolyline ( *ringIt, context.renderContext (), -1 , selectFillBorder && context.selected () );
260
260
}
@@ -265,7 +265,7 @@ void QgsSVGFillSymbolLayer::renderPolygon( const QPolygonF& points, QList<QPolyg
265
265
QgsStringMap QgsSVGFillSymbolLayer::properties () const
266
266
{
267
267
QgsStringMap map;
268
- if ( !mSvgFilePath .isEmpty () )
268
+ if ( !mSvgFilePath .isEmpty () )
269
269
{
270
270
map.insert ( " svgFile" , QgsSvgMarkerSymbolLayerV2::symbolPathToName ( mSvgFilePath ) );
271
271
}
@@ -281,7 +281,7 @@ QgsStringMap QgsSVGFillSymbolLayer::properties() const
281
281
QgsSymbolLayerV2* QgsSVGFillSymbolLayer::clone () const
282
282
{
283
283
QgsSymbolLayerV2* clonedLayer = 0 ;
284
- if ( !mSvgFilePath .isEmpty () )
284
+ if ( !mSvgFilePath .isEmpty () )
285
285
{
286
286
clonedLayer = new QgsSVGFillSymbolLayer ( mSvgFilePath , mPatternWidth );
287
287
}
@@ -290,7 +290,7 @@ QgsSymbolLayerV2* QgsSVGFillSymbolLayer::clone() const
290
290
clonedLayer = new QgsSVGFillSymbolLayer ( mSvgData , mPatternWidth );
291
291
}
292
292
293
- if ( mOutline )
293
+ if ( mOutline )
294
294
{
295
295
clonedLayer->setSubSymbol ( mOutline ->clone () );
296
296
}
@@ -299,10 +299,10 @@ QgsSymbolLayerV2* QgsSVGFillSymbolLayer::clone() const
299
299
300
300
void QgsSVGFillSymbolLayer::storeViewBox ()
301
301
{
302
- if ( !mSvgData .isEmpty () )
302
+ if ( !mSvgData .isEmpty () )
303
303
{
304
304
QSvgRenderer r ( mSvgData );
305
- if ( r.isValid () )
305
+ if ( r.isValid () )
306
306
{
307
307
mSvgViewBox = r.viewBoxF ();
308
308
return ;
@@ -315,14 +315,14 @@ void QgsSVGFillSymbolLayer::storeViewBox()
315
315
316
316
bool QgsSVGFillSymbolLayer::setSubSymbol ( QgsSymbolV2* symbol )
317
317
{
318
- if ( !symbol || symbol->type () != QgsSymbolV2::Line )
318
+ if ( !symbol || symbol->type () != QgsSymbolV2::Line )
319
319
{
320
320
delete symbol;
321
321
return false ;
322
322
}
323
323
324
324
QgsLineSymbolV2* lineSymbol = dynamic_cast <QgsLineSymbolV2*>( symbol );
325
- if ( lineSymbol )
325
+ if ( lineSymbol )
326
326
{
327
327
delete mOutline ;
328
328
mOutline = lineSymbol;
@@ -332,3 +332,100 @@ bool QgsSVGFillSymbolLayer::setSubSymbol( QgsSymbolV2* symbol )
332
332
delete symbol;
333
333
return false ;
334
334
}
335
+
336
+
337
+ // ////////////
338
+
339
+
340
+ QgsCentroidFillSymbolLayerV2::QgsCentroidFillSymbolLayerV2 ()
341
+ {
342
+ mMarker = NULL ;
343
+ setSubSymbol ( new QgsMarkerSymbolV2 () );
344
+ }
345
+
346
+ QgsCentroidFillSymbolLayerV2::~QgsCentroidFillSymbolLayerV2 ()
347
+ {
348
+ delete mMarker ;
349
+ }
350
+
351
+ QgsSymbolLayerV2* QgsCentroidFillSymbolLayerV2::create ( const QgsStringMap& /* properties*/ )
352
+ {
353
+ return new QgsCentroidFillSymbolLayerV2 ();
354
+ }
355
+
356
+ QString QgsCentroidFillSymbolLayerV2::layerType () const
357
+ {
358
+ return " CentroidFill" ;
359
+ }
360
+
361
+ void QgsCentroidFillSymbolLayerV2::setColor ( const QColor& color )
362
+ {
363
+ mMarker ->setColor ( color );
364
+ mColor = color;
365
+ }
366
+
367
+ void QgsCentroidFillSymbolLayerV2::startRender ( QgsSymbolV2RenderContext& context )
368
+ {
369
+ mMarker ->setAlpha ( context.alpha () );
370
+ mMarker ->setOutputUnit ( context.outputUnit () );
371
+
372
+ mMarker ->startRender ( context.renderContext () );
373
+ }
374
+
375
+ void QgsCentroidFillSymbolLayerV2::stopRender ( QgsSymbolV2RenderContext& context )
376
+ {
377
+ mMarker ->stopRender ( context.renderContext () );
378
+ }
379
+
380
+ void QgsCentroidFillSymbolLayerV2::renderPolygon ( const QPolygonF& points, QList<QPolygonF>* rings, QgsSymbolV2RenderContext& context )
381
+ {
382
+ // calculate centroid
383
+ double cx = 0 , cy = 0 ;
384
+ double area, sum = 0 ;
385
+ for ( int i = points.count () - 1 , j = 0 ; j < points.count (); i = j++ )
386
+ {
387
+ const QPointF& p1 = points[i];
388
+ const QPointF& p2 = points[j];
389
+ area = p1.x () * p2.y () - p1.y () * p2.x ();
390
+ sum += area;
391
+ cx += ( p1.x () + p2.x () ) * area;
392
+ cy += ( p1.y () + p2.y () ) * area;
393
+ }
394
+ sum *= 3.0 ;
395
+ cx /= sum;
396
+ cy /= sum;
397
+
398
+ mMarker ->renderPoint ( QPointF ( cx, cy ), context.renderContext (), -1 , context.selected () );
399
+ }
400
+
401
+ QgsStringMap QgsCentroidFillSymbolLayerV2::properties () const
402
+ {
403
+ return QgsStringMap ();
404
+ }
405
+
406
+ QgsSymbolLayerV2* QgsCentroidFillSymbolLayerV2::clone () const
407
+ {
408
+ QgsCentroidFillSymbolLayerV2* x = new QgsCentroidFillSymbolLayerV2 ();
409
+ x->setSubSymbol ( mMarker ->clone () );
410
+ return x;
411
+ }
412
+
413
+
414
+ QgsSymbolV2* QgsCentroidFillSymbolLayerV2::subSymbol ()
415
+ {
416
+ return mMarker ;
417
+ }
418
+
419
+ bool QgsCentroidFillSymbolLayerV2::setSubSymbol ( QgsSymbolV2* symbol )
420
+ {
421
+ if ( symbol == NULL || symbol->type () != QgsSymbolV2::Marker )
422
+ {
423
+ delete symbol;
424
+ return false ;
425
+ }
426
+
427
+ delete mMarker ;
428
+ mMarker = static_cast <QgsMarkerSymbolV2*>( symbol );
429
+ mColor = mMarker ->color ();
430
+ return true ;
431
+ }
0 commit comments