/
qgssymbollayerv2.cpp
377 lines (328 loc) · 11.5 KB
/
qgssymbollayerv2.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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
/***************************************************************************
qgssymbollayerv2.cpp
---------------------
begin : November 2009
copyright : (C) 2009 by Martin Dobias
email : wonder dot sk at gmail dot com
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#include "qgssymbollayerv2.h"
#include "qgsclipper.h"
#include "qgsexpression.h"
#include "qgsrendercontext.h"
#include "qgsvectorlayer.h"
#include <QSize>
#include <QPainter>
#include <QPointF>
#include <QPolygonF>
const QgsExpression* QgsSymbolLayerV2::dataDefinedProperty( const QString& property ) const
{
QMap< QString, QgsExpression* >::const_iterator it = mDataDefinedProperties.find( property );
if ( it != mDataDefinedProperties.constEnd() )
{
return it.value();
}
return 0;
}
QgsExpression* QgsSymbolLayerV2::expression( const QString& property )
{
QMap< QString, QgsExpression* >::iterator it = mDataDefinedProperties.find( property );
if ( it != mDataDefinedProperties.end() )
{
return it.value();
}
return 0;
}
QString QgsSymbolLayerV2::dataDefinedPropertyString( const QString& property ) const
{
const QgsExpression* ex = dataDefinedProperty( property );
return ex ? ex->expression() : QString();
}
void QgsSymbolLayerV2::setDataDefinedProperty( const QString& property, const QString& expressionString )
{
removeDataDefinedProperty( property );
mDataDefinedProperties.insert( property, new QgsExpression( expressionString ) );
}
void QgsSymbolLayerV2::removeDataDefinedProperty( const QString& property )
{
QMap< QString, QgsExpression* >::iterator it = mDataDefinedProperties.find( property );
if ( it != mDataDefinedProperties.end() )
{
delete( it.value() );
mDataDefinedProperties.erase( it );
}
}
void QgsSymbolLayerV2::removeDataDefinedProperties()
{
QMap< QString, QgsExpression* >::iterator it = mDataDefinedProperties.begin();
for ( ; it != mDataDefinedProperties.constEnd(); ++it )
{
delete( it.value() );
}
mDataDefinedProperties.clear();
}
void QgsSymbolLayerV2::prepareExpressions( const QgsVectorLayer* vl, double scale )
{
if ( !vl )
{
return;
}
const QgsFields& fields = vl->pendingFields();
QMap< QString, QgsExpression* >::iterator it = mDataDefinedProperties.begin();
for ( ; it != mDataDefinedProperties.end(); ++it )
{
if ( it.value() )
{
it.value()->prepare( fields );
if ( scale > 0 )
{
it.value()->setScale( scale );
}
}
}
}
QSet<QString> QgsSymbolLayerV2::usedAttributes() const
{
QStringList columns;
QMap< QString, QgsExpression* >::const_iterator ddIt = mDataDefinedProperties.constBegin();
for ( ; ddIt != mDataDefinedProperties.constEnd(); ++ddIt )
{
if ( ddIt.value() )
{
columns.append( ddIt.value()->referencedColumns() );
}
}
QSet<QString> attributes;
QStringList::const_iterator it = columns.constBegin();
for ( ; it != columns.constEnd(); ++it )
{
attributes.insert( *it );
}
return attributes;
}
void QgsSymbolLayerV2::saveDataDefinedProperties( QgsStringMap& stringMap ) const
{
QMap< QString, QgsExpression* >::const_iterator ddIt = mDataDefinedProperties.constBegin();
for ( ; ddIt != mDataDefinedProperties.constEnd(); ++ddIt )
{
if ( ddIt.value() )
{
stringMap.insert( ddIt.key() + "_expression", ddIt.value()->expression() );
}
}
}
void QgsSymbolLayerV2::copyDataDefinedProperties( QgsSymbolLayerV2* destLayer ) const
{
if ( !destLayer )
return;
destLayer->removeDataDefinedProperties();
QMap< QString, QgsExpression* >::const_iterator ddIt = mDataDefinedProperties.constBegin();
for ( ; ddIt != mDataDefinedProperties.constEnd(); ++ddIt )
{
if ( ddIt.value() )
{
destLayer->setDataDefinedProperty( ddIt.key(), ddIt.value()->expression() );
}
}
}
QgsMarkerSymbolLayerV2::QgsMarkerSymbolLayerV2( bool locked )
: QgsSymbolLayerV2( QgsSymbolV2::Marker, locked ), mSizeUnit( QgsSymbolV2::MM ), mOffsetUnit( QgsSymbolV2::MM ),
mHorizontalAnchorPoint( HCenter ), mVerticalAnchorPoint( VCenter )
{
}
QgsLineSymbolLayerV2::QgsLineSymbolLayerV2( bool locked )
: QgsSymbolLayerV2( QgsSymbolV2::Line, locked ), mWidthUnit( QgsSymbolV2::MM )
{
}
QgsFillSymbolLayerV2::QgsFillSymbolLayerV2( bool locked )
: QgsSymbolLayerV2( QgsSymbolV2::Fill, locked ), mAngle( 0.0 )
{
}
void QgsMarkerSymbolLayerV2::drawPreviewIcon( QgsSymbolV2RenderContext& context, QSize size )
{
startRender( context );
renderPoint( QPointF( size.width() / 2, size.height() / 2 ), context );
stopRender( context );
}
void QgsMarkerSymbolLayerV2::setOutputUnit( QgsSymbolV2::OutputUnit unit )
{
mSizeUnit = unit;
mOffsetUnit = unit;
}
void QgsMarkerSymbolLayerV2::markerOffset( QgsSymbolV2RenderContext& context, double& offsetX, double& offsetY )
{
markerOffset( context, mSize, mSize, mSizeUnit, mSizeUnit, offsetX, offsetY );
}
void QgsMarkerSymbolLayerV2::markerOffset( QgsSymbolV2RenderContext& context, double width, double height,
QgsSymbolV2::OutputUnit widthUnit, QgsSymbolV2::OutputUnit heightUnit,
double& offsetX, double& offsetY )
{
offsetX = mOffset.x();
offsetY = mOffset.y();
QgsExpression* offsetExpression = expression( "offset" );
if ( offsetExpression )
{
QPointF offset = QgsSymbolLayerV2Utils::decodePoint( offsetExpression->evaluate( const_cast<QgsFeature*>( context.feature() ) ).toString() );
offsetX = offset.x();
offsetY = offset.y();
}
offsetX *= QgsSymbolLayerV2Utils::lineWidthScaleFactor( context.renderContext(), mOffsetUnit );
offsetY *= QgsSymbolLayerV2Utils::lineWidthScaleFactor( context.renderContext(), mOffsetUnit );
HorizontalAnchorPoint horizontalAnchorPoint = mHorizontalAnchorPoint;
VerticalAnchorPoint verticalAnchorPoint = mVerticalAnchorPoint;
QgsExpression* horizontalAnchorExpression = expression( "horizontal_anchor_point" );
if ( horizontalAnchorExpression )
{
horizontalAnchorPoint = decodeHorizontalAnchorPoint( horizontalAnchorExpression->evaluate( const_cast<QgsFeature*>( context.feature() ) ).toString() );
}
QgsExpression* verticalAnchorExpression = expression( "vertical_anchor_point" );
if ( verticalAnchorExpression )
{
verticalAnchorPoint = decodeVerticalAnchorPoint( verticalAnchorExpression->evaluate( const_cast<QgsFeature*>( context.feature() ) ).toString() );
}
//correct horizontal position according to anchor point
if ( horizontalAnchorPoint == HCenter && verticalAnchorPoint == VCenter )
{
return;
}
double anchorPointCorrectionX = width * QgsSymbolLayerV2Utils::lineWidthScaleFactor( context.renderContext(), widthUnit ) / 2.0;
double anchorPointCorrectionY = height * QgsSymbolLayerV2Utils::lineWidthScaleFactor( context.renderContext(), heightUnit ) / 2.0;
if ( horizontalAnchorPoint == Left )
{
offsetX += anchorPointCorrectionX;
}
else if ( horizontalAnchorPoint == Right )
{
offsetX -= anchorPointCorrectionX;
}
//correct vertical position according to anchor point
if ( verticalAnchorPoint == Top )
{
offsetY += anchorPointCorrectionY;
}
else if ( verticalAnchorPoint == Bottom )
{
offsetY -= anchorPointCorrectionY;
}
}
QPointF QgsMarkerSymbolLayerV2::_rotatedOffset( const QPointF& offset, double angle )
{
angle = DEG2RAD( angle );
double c = cos( angle ), s = sin( angle );
return QPointF( offset.x() * c - offset.y() * s, offset.x() * s + offset.y() * c );
}
QgsMarkerSymbolLayerV2::HorizontalAnchorPoint QgsMarkerSymbolLayerV2::decodeHorizontalAnchorPoint( const QString& str )
{
if ( str.compare( "left", Qt::CaseInsensitive ) == 0 )
{
return QgsMarkerSymbolLayerV2::Left;
}
else if ( str.compare( "right", Qt::CaseInsensitive ) == 0 )
{
return QgsMarkerSymbolLayerV2::Right;
}
else
{
return QgsMarkerSymbolLayerV2::HCenter;
}
}
QgsMarkerSymbolLayerV2::VerticalAnchorPoint QgsMarkerSymbolLayerV2::decodeVerticalAnchorPoint( const QString& str )
{
if ( str.compare( "top", Qt::CaseInsensitive ) == 0 )
{
return QgsMarkerSymbolLayerV2::Top;
}
else if ( str.compare( "bottom", Qt::CaseInsensitive ) == 0 )
{
return QgsMarkerSymbolLayerV2::Bottom;
}
else
{
return QgsMarkerSymbolLayerV2::VCenter;
}
}
QgsSymbolV2::OutputUnit QgsMarkerSymbolLayerV2::outputUnit() const
{
QgsSymbolV2::OutputUnit unit = mSizeUnit;
if ( mOffsetUnit != unit )
{
return QgsSymbolV2::Mixed;
}
return unit;
}
void QgsLineSymbolLayerV2::drawPreviewIcon( QgsSymbolV2RenderContext& context, QSize size )
{
QPolygonF points;
// we're adding 0.5 to get rid of blurred preview:
// drawing antialiased lines of width 1 at (x,0)-(x,100) creates 2px line
points << QPointF( 0, size.height() / 2 + 0.5 ) << QPointF( size.width(), size.height() / 2 + 0.5 );
startRender( context );
renderPolyline( points, context );
stopRender( context );
}
void QgsLineSymbolLayerV2::renderPolygonOutline( const QPolygonF& points, QList<QPolygonF>* rings, QgsSymbolV2RenderContext& context )
{
renderPolyline( points, context );
if ( rings )
{
foreach ( const QPolygonF& ring, *rings )
renderPolyline( ring, context );
}
}
void QgsFillSymbolLayerV2::drawPreviewIcon( QgsSymbolV2RenderContext& context, QSize size )
{
QPolygonF poly = QRectF( QPointF( 0, 0 ), QPointF( size.width() - 1, size.height() - 1 ) );
startRender( context );
renderPolygon( poly, NULL, context );
stopRender( context );
}
void QgsFillSymbolLayerV2::_renderPolygon( QPainter* p, const QPolygonF& points, const QList<QPolygonF>* rings, QgsSymbolV2RenderContext& context )
{
if ( !p )
{
return;
}
// Disable 'Antialiasing' if the geometry was generalized in the current RenderContext (We known that it must have least #5 points).
if ( points.size()<=5 && context.layer() && context.layer()->simplifyDrawingCanbeApplied() && QgsFeatureRequest::canbeGeneralizedByWndBoundingBox( points, context.layer()->simplifyDrawingTol() ) && p->renderHints() & QPainter::Antialiasing )
{
p->setRenderHint( QPainter::Antialiasing, false );
p->drawConvexPolygon( points );
p->setRenderHint( QPainter::Antialiasing, true );
return;
}
if ( rings == NULL )
{
// simple polygon without holes
p->drawPolygon( points );
}
else
{
// polygon with holes must be drawn using painter path
QPainterPath path;
QPolygonF outerRing = points;
path.addPolygon( outerRing );
QList<QPolygonF>::const_iterator it = rings->constBegin();
for ( ; it != rings->constEnd(); ++it )
{
QPolygonF ring = *it;
path.addPolygon( ring );
}
p->drawPath( path );
}
}
void QgsMarkerSymbolLayerV2::toSld( QDomDocument &doc, QDomElement &element, QgsStringMap props ) const
{
QDomElement symbolizerElem = doc.createElement( "se:PointSymbolizer" );
if ( !props.value( "uom", "" ).isEmpty() )
symbolizerElem.setAttribute( "uom", props.value( "uom", "" ) );
element.appendChild( symbolizerElem );
// <Geometry>
QgsSymbolLayerV2Utils::createGeometryElement( doc, symbolizerElem, props.value( "geom", "" ) );
writeSldMarker( doc, symbolizerElem, props );
}