Skip to content

Commit 3dc9659

Browse files
committed
Integrated qgsellipsesymbollayer
1 parent b76da25 commit 3dc9659

File tree

3 files changed

+308
-0
lines changed

3 files changed

+308
-0
lines changed

src/core/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ SET(QGIS_CORE_SRCS
3737
symbology-ng/qgsvectorcolorrampv2.cpp
3838
symbology-ng/qgsstylev2.cpp
3939
symbology-ng/qgssymbologyv2conversion.cpp
40+
symbology-ng/qgsellipsesymbollayerv2.cpp
4041

4142
qgis.cpp
4243
qgsapplication.cpp
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,219 @@
1+
#include "qgsellipsesymbollayerv2.h"
2+
#include "qgsfeature.h"
3+
#include "qgsrendercontext.h"
4+
#include <QPainter>
5+
#include <QSet>
6+
7+
QgsEllipseSymbolLayerV2::QgsEllipseSymbolLayerV2(): mDataDefinedWidth(-1), mDataDefinedHeight(-1),
8+
mDataDefinedOutlineWidth(-1), mFillColor( Qt::black ), mDataDefinedFillColor(-1), mOutlineColor( Qt::white ), mDataDefinedOutlineColor(-1)
9+
{
10+
mSymbolName = "circle";
11+
mPen.setColor( mOutlineColor );
12+
mPen.setWidth( 1.0 );
13+
mPen.setJoinStyle( Qt::MiterJoin );
14+
mBrush.setColor( mFillColor );
15+
mBrush.setStyle( Qt::SolidPattern );
16+
}
17+
18+
QgsEllipseSymbolLayerV2::~QgsEllipseSymbolLayerV2()
19+
{
20+
}
21+
22+
QgsSymbolLayerV2* QgsEllipseSymbolLayerV2::create( const QgsStringMap& properties )
23+
{
24+
QgsEllipseSymbolLayerV2* layer = new QgsEllipseSymbolLayerV2();
25+
if( properties.contains( "symbol_name" ) )
26+
{
27+
layer->setSymbolName( properties[ "symbol_name" ] );
28+
}
29+
if( properties.contains( "symbol_width" ) )
30+
{
31+
layer->setSymbolWidth( properties["symbol_width"].toDouble() );
32+
}
33+
if( properties.contains( "data_defined_width" ) )
34+
{
35+
layer->setDataDefinedWidth( properties["data_defined_width"].toInt() );
36+
}
37+
if( properties.contains("symbol_height") )
38+
{
39+
layer->setSymbolHeight( properties["symbol_height"].toDouble() );
40+
}
41+
if( properties.contains( "data_defined_height" ) )
42+
{
43+
layer->setDataDefinedHeight( properties["data_defined_height"].toInt() );
44+
}
45+
if( properties.contains( "outline_width" ) )
46+
{
47+
layer->setOutlineWidth( properties["outline_width"].toDouble() );
48+
}
49+
if( properties.contains( "data_defined_outline_width" ) )
50+
{
51+
layer->setDataDefinedOutlineWidth( properties["data_defined_outline_width"].toInt() );
52+
}
53+
if( properties.contains( "fill_color" ) )
54+
{
55+
layer->setFillColor( QgsSymbolLayerV2Utils::decodeColor( properties["fill_color"] ) );
56+
}
57+
if( properties.contains( "data_defined_fill_color" ) )
58+
{
59+
layer->setDataDefinedFillColor( properties["data_defined_fill_color"].toInt() );
60+
}
61+
if( properties.contains( "outline_color" ) )
62+
{
63+
layer->setOutlineColor( QgsSymbolLayerV2Utils::decodeColor( properties["outline_color"] ) );
64+
}
65+
if( properties.contains( "data_defined_outline_color" ) )
66+
{
67+
layer->setDataDefinedOutlineColor( properties[ "data_defined_outline_color" ].toInt() );
68+
}
69+
return layer;
70+
}
71+
72+
void QgsEllipseSymbolLayerV2::renderPoint( const QPointF& point, QgsSymbolV2RenderContext& context )
73+
{
74+
const QgsFeature* f = context.feature();
75+
76+
if( f )
77+
{
78+
if( mDataDefinedOutlineWidth != -1 )
79+
{
80+
double width = context.outputLineWidth( f->attributeMap()[mDataDefinedOutlineWidth].toDouble() );
81+
mPen.setWidth( width );
82+
}
83+
if( mDataDefinedFillColor != -1 )
84+
{
85+
mBrush.setColor( QColor( f->attributeMap()[mDataDefinedFillColor].toString() ) );
86+
}
87+
if( mDataDefinedOutlineColor != -1 )
88+
{
89+
mPen.setColor( QColor( f->attributeMap()[mDataDefinedOutlineColor].toString() ) );
90+
}
91+
if( mDataDefinedWidth != -1 || mDataDefinedHeight != -1 )
92+
{
93+
preparePath( context, f );
94+
}
95+
}
96+
97+
QPainter* p = context.renderContext().painter();
98+
if( !p )
99+
{
100+
return;
101+
}
102+
103+
QPointF off( context.outputLineWidth( mOffset.x() ), context.outputLineWidth( mOffset.y() ) );
104+
105+
QMatrix transform;
106+
transform.translate( point.x() + off.x(), point.y() + off.y() );
107+
108+
p->setPen( mPen );
109+
p->setBrush( mBrush );
110+
p->drawPath( transform.map( mPainterPath ) );
111+
}
112+
113+
QString QgsEllipseSymbolLayerV2::layerType() const
114+
{
115+
return "Ellipse";
116+
}
117+
118+
void QgsEllipseSymbolLayerV2::startRender( QgsSymbolV2RenderContext& context )
119+
{
120+
if( !hasDataDefinedProperty() )
121+
{
122+
preparePath( context );
123+
}
124+
mPen.setColor( mOutlineColor );
125+
mPen.setWidth( context.outputLineWidth( mOutlineWidth ) );
126+
mBrush.setColor( mFillColor );
127+
}
128+
129+
void QgsEllipseSymbolLayerV2::stopRender( QgsSymbolV2RenderContext& context )
130+
{
131+
}
132+
133+
QgsSymbolLayerV2* QgsEllipseSymbolLayerV2::clone() const
134+
{
135+
return QgsEllipseSymbolLayerV2::create( properties() );
136+
}
137+
138+
QgsStringMap QgsEllipseSymbolLayerV2::properties() const
139+
{
140+
QgsStringMap map;
141+
map["symbol_name"] = mSymbolName;
142+
map["symbol_width"] = QString::number( mSymbolWidth );
143+
map["data_defined_width"] = QString::number( mDataDefinedWidth );
144+
map["symbol_height"] = QString::number( mSymbolHeight );
145+
map["data_defined_height"] = QString::number( mDataDefinedHeight );
146+
map["outline_width"] = QString::number( mOutlineWidth );
147+
map["data_defined_outline_width"] = QString::number( mDataDefinedOutlineWidth );
148+
map["fill_color"] = QgsSymbolLayerV2Utils::encodeColor( mFillColor );
149+
map["data_defined_fill_color"] = QString::number( mDataDefinedFillColor );
150+
map["outline_color"] = QgsSymbolLayerV2Utils::encodeColor( mOutlineColor );
151+
map["data_defined_outline_color"] = QString::number( mDataDefinedOutlineColor );
152+
return map;
153+
}
154+
155+
bool QgsEllipseSymbolLayerV2::hasDataDefinedProperty() const
156+
{
157+
return ( mDataDefinedWidth != -1 || mDataDefinedHeight != -1 || mDataDefinedOutlineWidth != -1
158+
|| mDataDefinedFillColor != -1 || mDataDefinedOutlineColor != -1 );
159+
}
160+
161+
void QgsEllipseSymbolLayerV2::preparePath( QgsSymbolV2RenderContext& context, const QgsFeature* f )
162+
{
163+
mPainterPath = QPainterPath();
164+
165+
double width = 0;
166+
if( f && mDataDefinedOutlineWidth != -1 )
167+
{
168+
width = context.outputLineWidth( f->attributeMap()[mDataDefinedOutlineWidth].toDouble() );
169+
}
170+
else
171+
{
172+
width = context.outputLineWidth( mSymbolWidth );
173+
}
174+
175+
double height = 0;
176+
if( f && mDataDefinedHeight != -1 )
177+
{
178+
height = context.outputLineWidth( f->attributeMap()[mDataDefinedHeight].toDouble() );
179+
}
180+
else
181+
{
182+
height = context.outputLineWidth( mSymbolHeight );
183+
}
184+
185+
if( mSymbolName == "circle" )
186+
{
187+
mPainterPath.addEllipse( QRectF( -width / 2.0, -height / 2.0, width, height ) );
188+
}
189+
else if( mSymbolName == "rectangle" )
190+
{
191+
mPainterPath.addRect( QRectF( -width / 2.0, -height / 2.0, width, height ) );
192+
}
193+
else if( mSymbolName == "cross" )
194+
{
195+
mPainterPath.moveTo( 0, -height / 2.0 );
196+
mPainterPath.lineTo( 0, height / 2.0 );
197+
mPainterPath.moveTo( -width / 2.0, 0 );
198+
mPainterPath.lineTo( width / 2.0, 0 );
199+
}
200+
else if( mSymbolName == "triangle" )
201+
{
202+
mPainterPath.moveTo( 0, -height / 2.0 );
203+
mPainterPath.lineTo( -width / 2.0, height / 2.0 );
204+
mPainterPath.lineTo( width / 2.0, height / 2.0 );
205+
mPainterPath.lineTo( 0, -height / 2.0 );
206+
}
207+
}
208+
209+
QSet<QString> QgsEllipseSymbolLayerV2::usedAttributes() const
210+
{
211+
QSet<QString> dataDefinedAttributes;
212+
/*dataDefinedAttributes.insert( mDataDefinedWidth );
213+
dataDefinedAttributes.insert( mDataDefinedHeight );
214+
dataDefinedAttributes.insert( mDataDefinedOutlineWidth );
215+
dataDefinedAttributes.insert( mDataDefinedFillColor );
216+
dataDefinedAttributes.insert( mDataDefinedOutlineColor );
217+
dataDefinedAttributes.remove( -1 );*/
218+
return dataDefinedAttributes;
219+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
#ifndef QGSELLIPSESYMBOLLAYERV2_H
2+
#define QGSELLIPSESYMBOLLAYERV2_H
3+
4+
#include "qgsmarkersymbollayerv2.h"
5+
#include <QPainterPath>
6+
7+
/**A symbol layer for rendering objects with major and minor axis (e.g. ellipse, rectangle )*/
8+
class QgsEllipseSymbolLayerV2: public QgsMarkerSymbolLayerV2
9+
{
10+
public:
11+
QgsEllipseSymbolLayerV2();
12+
~QgsEllipseSymbolLayerV2();
13+
14+
static QgsSymbolLayerV2* create( const QgsStringMap& properties = QgsStringMap() );
15+
16+
void renderPoint( const QPointF& point, QgsSymbolV2RenderContext& context );
17+
QString layerType() const;
18+
void startRender( QgsSymbolV2RenderContext& context );
19+
void stopRender( QgsSymbolV2RenderContext& context );
20+
QgsSymbolLayerV2* clone() const;
21+
QgsStringMap properties() const;
22+
23+
void setSymbolName( const QString& name ){ mSymbolName = name; }
24+
QString symbolName() const{ return mSymbolName; }
25+
26+
void setSymbolWidth( double w ){ mSymbolWidth = w; }
27+
double symbolWidth() const { return mSymbolWidth; }
28+
29+
void setDataDefinedWidth( int c ){ mDataDefinedWidth = c; }
30+
int dataDefinedWidth() const { return mDataDefinedWidth; }
31+
32+
void setSymbolHeight( double h ){ mSymbolHeight = h; }
33+
double symbolHeight() const { return mSymbolHeight; }
34+
35+
void setDataDefinedHeight( int c ){ mDataDefinedHeight = c; }
36+
int dataDefinedHeight() const { return mDataDefinedHeight; }
37+
38+
void setOutlineWidth( double w ){ mOutlineWidth = w; }
39+
double outlineWidth() const { return mOutlineWidth; }
40+
41+
void setDataDefinedOutlineWidth( int c ){ mDataDefinedOutlineWidth = c; }
42+
int dataDefinedOutlineWidth() const { return mDataDefinedOutlineWidth; }
43+
44+
void setFillColor( const QColor& c ){ mFillColor = c;}
45+
QColor fillColor() const { return mFillColor; }
46+
47+
void setDataDefinedFillColor( int c ){ mDataDefinedFillColor = c; }
48+
int dataDefinedFillColor() const { return mDataDefinedFillColor; }
49+
50+
void setOutlineColor( const QColor& c ){ mOutlineColor = c; }
51+
QColor outlineColor() const { return mOutlineColor; }
52+
53+
void setDataDefinedOutlineColor( int c ){ mDataDefinedOutlineColor = c; }
54+
int dataDefinedOutlineColor() const { return mDataDefinedOutlineColor; }
55+
56+
QSet<QString> usedAttributes() const;
57+
58+
private:
59+
QString mSymbolName;
60+
double mSymbolWidth;
61+
/**Take width from attribute (-1 if fixed width)*/
62+
int mDataDefinedWidth;
63+
double mSymbolHeight;
64+
/**Take height from attribute (-1 if fixed height)*/
65+
int mDataDefinedHeight;
66+
double mOutlineWidth;
67+
/**Take outline width from attribute (-1 if fixed outline width)*/
68+
int mDataDefinedOutlineWidth;
69+
QColor mFillColor;
70+
/**Take fill color from attribute (-1 if fixed fill color)*/
71+
int mDataDefinedFillColor;
72+
QColor mOutlineColor;
73+
/**Take outline color from attribute (-1 if fixed outline color)*/
74+
int mDataDefinedOutlineColor;
75+
QPainterPath mPainterPath;
76+
77+
QPen mPen;
78+
QBrush mBrush;
79+
80+
/**Setup mPainterPath
81+
@param feature to render (0 if no data defined rendering)*/
82+
void preparePath( QgsSymbolV2RenderContext& context, const QgsFeature* f = 0 );
83+
84+
/**True if this symbol layer uses a data defined property*/
85+
bool hasDataDefinedProperty() const;
86+
};
87+
88+
#endif // QGSELLIPSESYMBOLLAYERV2_H

0 commit comments

Comments
 (0)