Skip to content

Commit 77d1fd0

Browse files
author
wonder
committedNov 30, 2010
[FEATURE] Added "centroid fill" symbol layer which draws a marker on polygon's centroid.
Developed for Faunalia (http://www.faunalia.it) with funding from Regione Toscana - Sistema Informativo per la Gestione del Territorio e dell' Ambiente [RT-SIGTA]. For the project: "Sviluppo di prodotti software GIS open-source basati sui prodotti QuantumGIS e Postgis (CIG 037728516E)" git-svn-id: http://svn.osgeo.org/qgis/trunk@14810 c8812cc2-4d05-0410-92ff-de0c093fc19c
1 parent 40a2457 commit 77d1fd0

File tree

8 files changed

+327
-38
lines changed

8 files changed

+327
-38
lines changed
 

‎src/core/symbology-ng/qgsfillsymbollayerv2.cpp

Lines changed: 132 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
#include <QSvgRenderer>
1111

1212
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 )
1414
{
1515
mColor = color;
1616
}
@@ -25,17 +25,17 @@ QgsSymbolLayerV2* QgsSimpleFillSymbolLayerV2::create( const QgsStringMap& props
2525
double borderWidth = DEFAULT_SIMPLEFILL_BORDERWIDTH;
2626
QPointF offset;
2727

28-
if( props.contains( "color" ) )
28+
if ( props.contains( "color" ) )
2929
color = QgsSymbolLayerV2Utils::decodeColor( props["color"] );
30-
if( props.contains( "style" ) )
30+
if ( props.contains( "style" ) )
3131
style = QgsSymbolLayerV2Utils::decodeBrushStyle( props["style"] );
32-
if( props.contains( "color_border" ) )
32+
if ( props.contains( "color_border" ) )
3333
borderColor = QgsSymbolLayerV2Utils::decodeColor( props["color_border"] );
34-
if( props.contains( "style_border" ) )
34+
if ( props.contains( "style_border" ) )
3535
borderStyle = QgsSymbolLayerV2Utils::decodePenStyle( props["style_border"] );
36-
if( props.contains( "width_border" ) )
36+
if ( props.contains( "width_border" ) )
3737
borderWidth = props["width_border"].toDouble();
38-
if( props.contains( "offset" ) )
38+
if ( props.contains( "offset" ) )
3939
offset = QgsSymbolLayerV2Utils::decodePoint( props["offset"] );
4040

4141
QgsSimpleFillSymbolLayerV2* sl = new QgsSimpleFillSymbolLayerV2( color, style, borderColor, borderStyle, borderWidth );
@@ -56,15 +56,15 @@ void QgsSimpleFillSymbolLayerV2::startRender( QgsSymbolV2RenderContext& context
5656

5757
// scale brush content for printout
5858
double rasterScaleFactor = context.renderContext().rasterScaleFactor();
59-
if( rasterScaleFactor != 1.0 )
59+
if ( rasterScaleFactor != 1.0 )
6060
{
6161
mBrush.setMatrix( QMatrix().scale( 1.0 / rasterScaleFactor, 1.0 / rasterScaleFactor ) );
6262
}
6363

6464
QColor selColor = context.selectionColor();
6565
// selColor.setAlphaF( context.alpha() );
6666
mSelBrush = QBrush( selColor );
67-
if( selectFillStyle ) mSelBrush.setStyle( mBrushStyle );
67+
if ( selectFillStyle ) mSelBrush.setStyle( mBrushStyle );
6868
mBorderColor.setAlphaF( context.alpha() );
6969
mPen = QPen( mBorderColor );
7070
mPen.setStyle( mBorderStyle );
@@ -78,20 +78,20 @@ void QgsSimpleFillSymbolLayerV2::stopRender( QgsSymbolV2RenderContext& context )
7878
void QgsSimpleFillSymbolLayerV2::renderPolygon( const QPolygonF& points, QList<QPolygonF>* rings, QgsSymbolV2RenderContext& context )
7979
{
8080
QPainter* p = context.renderContext().painter();
81-
if( !p )
81+
if ( !p )
8282
{
8383
return;
8484
}
8585

8686
p->setBrush( context.selected() ? mSelBrush : mBrush );
8787
p->setPen( mPen );
8888

89-
if( !mOffset.isNull() )
89+
if ( !mOffset.isNull() )
9090
p->translate( mOffset );
9191

9292
_renderPolygon( p, points, rings );
9393

94-
if( !mOffset.isNull() )
94+
if ( !mOffset.isNull() )
9595
p->translate( -mOffset );
9696
}
9797

@@ -138,7 +138,7 @@ QgsSVGFillSymbolLayer::~QgsSVGFillSymbolLayer()
138138
void QgsSVGFillSymbolLayer::setSvgFilePath( const QString& svgPath )
139139
{
140140
QFile svgFile( svgPath );
141-
if( svgFile.open( QFile::ReadOnly ) )
141+
if ( svgFile.open( QFile::ReadOnly ) )
142142
{
143143
mSvgData = svgFile.readAll();
144144
storeViewBox();
@@ -153,24 +153,24 @@ QgsSymbolLayerV2* QgsSVGFillSymbolLayer::create( const QgsStringMap& properties
153153
QString svgFilePath;
154154

155155

156-
if( properties.contains( "width" ) )
156+
if ( properties.contains( "width" ) )
157157
{
158158
width = properties["width"].toDouble();
159159
}
160-
if( properties.contains( "svgFile" ) )
160+
if ( properties.contains( "svgFile" ) )
161161
{
162162
QString svgName = properties["svgFile"];
163163
QString savePath = QgsSvgMarkerSymbolLayerV2::symbolNameToPath( svgName );
164164
svgFilePath = ( savePath.isEmpty() ? svgName : savePath );
165165
}
166166

167-
if( !svgFilePath.isEmpty() )
167+
if ( !svgFilePath.isEmpty() )
168168
{
169169
return new QgsSVGFillSymbolLayer( svgFilePath, width );
170170
}
171171
else
172172
{
173-
if( properties.contains( "data" ) )
173+
if ( properties.contains( "data" ) )
174174
{
175175
data = QByteArray::fromHex( properties["data"].toLocal8Bit() );
176176
}
@@ -186,7 +186,7 @@ QString QgsSVGFillSymbolLayer::layerType() const
186186

187187
void QgsSVGFillSymbolLayer::startRender( QgsSymbolV2RenderContext& context )
188188
{
189-
if( mSvgViewBox.isNull() )
189+
if ( mSvgViewBox.isNull() )
190190
{
191191
return;
192192
}
@@ -201,13 +201,13 @@ void QgsSVGFillSymbolLayer::startRender( QgsSymbolV2RenderContext& context )
201201
//rasterise byte array to image
202202
QPainter p( &textureImage );
203203
QSvgRenderer r( mSvgData );
204-
if( !r.isValid() )
204+
if ( !r.isValid() )
205205
{
206206
return;
207207
}
208208
r.render( &p );
209209

210-
if( context.alpha() < 1.0 )
210+
if ( context.alpha() < 1.0 )
211211
{
212212
QgsSymbolLayerV2Utils::multiplyImageOpacity( &textureImage, context.alpha() );
213213
}
@@ -217,15 +217,15 @@ void QgsSVGFillSymbolLayer::startRender( QgsSymbolV2RenderContext& context )
217217
mBrush.setTextureImage( textureImage );
218218
mBrush.setTransform( brushTransform );
219219

220-
if( mOutline )
220+
if ( mOutline )
221221
{
222222
mOutline->startRender( context.renderContext() );
223223
}
224224
}
225225

226226
void QgsSVGFillSymbolLayer::stopRender( QgsSymbolV2RenderContext& context )
227227
{
228-
if( mOutline )
228+
if ( mOutline )
229229
{
230230
mOutline->stopRender( context.renderContext() );
231231
}
@@ -234,27 +234,27 @@ void QgsSVGFillSymbolLayer::stopRender( QgsSymbolV2RenderContext& context )
234234
void QgsSVGFillSymbolLayer::renderPolygon( const QPolygonF& points, QList<QPolygonF>* rings, QgsSymbolV2RenderContext& context )
235235
{
236236
QPainter* p = context.renderContext().painter();
237-
if( !p )
237+
if ( !p )
238238
{
239239
return;
240240
}
241241
p->setPen( QPen( Qt::NoPen ) );
242-
if( context.selected() )
242+
if ( context.selected() )
243243
{
244244
QColor selColor = context.selectionColor();
245-
if( ! selectionIsOpaque ) selColor.setAlphaF( context.alpha() );
245+
if ( ! selectionIsOpaque ) selColor.setAlphaF( context.alpha() );
246246
p->setBrush( QBrush( selColor ) );
247247
_renderPolygon( p, points, rings );
248248
}
249249
p->setBrush( mBrush );
250250
_renderPolygon( p, points, rings );
251-
if( mOutline )
251+
if ( mOutline )
252252
{
253253
mOutline->renderPolyline( points, context.renderContext(), -1, selectFillBorder && context.selected() );
254-
if( rings )
254+
if ( rings )
255255
{
256256
QList<QPolygonF>::const_iterator ringIt = rings->constBegin();
257-
for( ; ringIt != rings->constEnd(); ++ringIt )
257+
for ( ; ringIt != rings->constEnd(); ++ringIt )
258258
{
259259
mOutline->renderPolyline( *ringIt, context.renderContext(), -1, selectFillBorder && context.selected() );
260260
}
@@ -265,7 +265,7 @@ void QgsSVGFillSymbolLayer::renderPolygon( const QPolygonF& points, QList<QPolyg
265265
QgsStringMap QgsSVGFillSymbolLayer::properties() const
266266
{
267267
QgsStringMap map;
268-
if( !mSvgFilePath.isEmpty() )
268+
if ( !mSvgFilePath.isEmpty() )
269269
{
270270
map.insert( "svgFile", QgsSvgMarkerSymbolLayerV2::symbolPathToName( mSvgFilePath ) );
271271
}
@@ -281,7 +281,7 @@ QgsStringMap QgsSVGFillSymbolLayer::properties() const
281281
QgsSymbolLayerV2* QgsSVGFillSymbolLayer::clone() const
282282
{
283283
QgsSymbolLayerV2* clonedLayer = 0;
284-
if( !mSvgFilePath.isEmpty() )
284+
if ( !mSvgFilePath.isEmpty() )
285285
{
286286
clonedLayer = new QgsSVGFillSymbolLayer( mSvgFilePath, mPatternWidth );
287287
}
@@ -290,7 +290,7 @@ QgsSymbolLayerV2* QgsSVGFillSymbolLayer::clone() const
290290
clonedLayer = new QgsSVGFillSymbolLayer( mSvgData, mPatternWidth );
291291
}
292292

293-
if( mOutline )
293+
if ( mOutline )
294294
{
295295
clonedLayer->setSubSymbol( mOutline->clone() );
296296
}
@@ -299,10 +299,10 @@ QgsSymbolLayerV2* QgsSVGFillSymbolLayer::clone() const
299299

300300
void QgsSVGFillSymbolLayer::storeViewBox()
301301
{
302-
if( !mSvgData.isEmpty() )
302+
if ( !mSvgData.isEmpty() )
303303
{
304304
QSvgRenderer r( mSvgData );
305-
if( r.isValid() )
305+
if ( r.isValid() )
306306
{
307307
mSvgViewBox = r.viewBoxF();
308308
return;
@@ -315,14 +315,14 @@ void QgsSVGFillSymbolLayer::storeViewBox()
315315

316316
bool QgsSVGFillSymbolLayer::setSubSymbol( QgsSymbolV2* symbol )
317317
{
318-
if( !symbol || symbol->type() != QgsSymbolV2::Line )
318+
if ( !symbol || symbol->type() != QgsSymbolV2::Line )
319319
{
320320
delete symbol;
321321
return false;
322322
}
323323

324324
QgsLineSymbolV2* lineSymbol = dynamic_cast<QgsLineSymbolV2*>( symbol );
325-
if( lineSymbol )
325+
if ( lineSymbol )
326326
{
327327
delete mOutline;
328328
mOutline = lineSymbol;
@@ -332,3 +332,100 @@ bool QgsSVGFillSymbolLayer::setSubSymbol( QgsSymbolV2* symbol )
332332
delete symbol;
333333
return false;
334334
}
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+
}

‎src/core/symbology-ng/qgsfillsymbollayerv2.h

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,4 +121,39 @@ class CORE_EXPORT QgsSVGFillSymbolLayer: public QgsFillSymbolLayerV2
121121
void storeViewBox();
122122
};
123123

124+
125+
126+
class CORE_EXPORT QgsCentroidFillSymbolLayerV2 : public QgsFillSymbolLayerV2
127+
{
128+
public:
129+
QgsCentroidFillSymbolLayerV2();
130+
~QgsCentroidFillSymbolLayerV2();
131+
132+
// static stuff
133+
134+
static QgsSymbolLayerV2* create( const QgsStringMap& properties = QgsStringMap() );
135+
136+
// implemented from base classes
137+
138+
QString layerType() const;
139+
140+
void startRender( QgsSymbolV2RenderContext& context );
141+
142+
void stopRender( QgsSymbolV2RenderContext& context );
143+
144+
void renderPolygon( const QPolygonF& points, QList<QPolygonF>* rings, QgsSymbolV2RenderContext& context );
145+
146+
QgsStringMap properties() const;
147+
148+
QgsSymbolLayerV2* clone() const;
149+
150+
void setColor( const QColor& color );
151+
152+
QgsSymbolV2* subSymbol();
153+
bool setSubSymbol( QgsSymbolV2* symbol );
154+
155+
protected:
156+
QgsMarkerSymbolV2* mMarker;
157+
};
158+
124159
#endif

‎src/core/symbology-ng/qgssymbollayerv2registry.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,10 @@ QgsSymbolLayerV2Registry::QgsSymbolLayerV2Registry()
2626

2727
addSymbolLayerType( new QgsSymbolLayerV2Metadata( "SimpleFill", QObject::tr( "Simple fill" ), QgsSymbolV2::Fill,
2828
QgsSimpleFillSymbolLayerV2::create ) );
29-
addSymbolLayerType( new QgsSymbolLayerV2Metadata( "SVGFill", QObject::tr( "SVG fill" ), QgsSymbolV2::Fill, QgsSVGFillSymbolLayer::create ) );
29+
addSymbolLayerType( new QgsSymbolLayerV2Metadata( "SVGFill", QObject::tr( "SVG fill" ), QgsSymbolV2::Fill,
30+
QgsSVGFillSymbolLayer::create ) );
31+
addSymbolLayerType( new QgsSymbolLayerV2Metadata( "CentroidFill", QObject::tr( "Centroid fill" ), QgsSymbolV2::Fill,
32+
QgsCentroidFillSymbolLayerV2::create ) );
3033
}
3134

3235
QgsSymbolLayerV2Registry::~QgsSymbolLayerV2Registry()

0 commit comments

Comments
 (0)
Please sign in to comment.