/
qgslinesymbollayer.cpp
2613 lines (2264 loc) · 90.7 KB
/
qgslinesymbollayer.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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/***************************************************************************
qgslinesymbollayer.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 "qgslinesymbollayer.h"
#include "qgscurve.h"
#include "qgscurvepolygon.h"
#include "qgsdxfexport.h"
#include "qgssymbollayerutils.h"
#include "qgsexpression.h"
#include "qgsrendercontext.h"
#include "qgslogger.h"
#include "qgsvectorlayer.h"
#include "qgsgeometrysimplifier.h"
#include "qgsunittypes.h"
#include "qgsproperty.h"
#include "qgsexpressioncontextutils.h"
#include <algorithm>
#include <QPainter>
#include <QDomDocument>
#include <QDomElement>
#include <cmath>
QgsSimpleLineSymbolLayer::QgsSimpleLineSymbolLayer( const QColor &color, double width, Qt::PenStyle penStyle )
: mPenStyle( penStyle )
{
mColor = color;
mWidth = width;
mCustomDashVector << 5 << 2;
}
void QgsSimpleLineSymbolLayer::setOutputUnit( QgsUnitTypes::RenderUnit unit )
{
QgsLineSymbolLayer::setOutputUnit( unit );
mWidthUnit = unit;
mOffsetUnit = unit;
mCustomDashPatternUnit = unit;
}
QgsUnitTypes::RenderUnit QgsSimpleLineSymbolLayer::outputUnit() const
{
QgsUnitTypes::RenderUnit unit = QgsLineSymbolLayer::outputUnit();
if ( mWidthUnit != unit || mOffsetUnit != unit || mCustomDashPatternUnit != unit )
{
return QgsUnitTypes::RenderUnknownUnit;
}
return unit;
}
void QgsSimpleLineSymbolLayer::setMapUnitScale( const QgsMapUnitScale &scale )
{
QgsLineSymbolLayer::setMapUnitScale( scale );
mWidthMapUnitScale = scale;
mOffsetMapUnitScale = scale;
mCustomDashPatternMapUnitScale = scale;
}
QgsMapUnitScale QgsSimpleLineSymbolLayer::mapUnitScale() const
{
if ( QgsLineSymbolLayer::mapUnitScale() == mWidthMapUnitScale &&
mWidthMapUnitScale == mOffsetMapUnitScale &&
mOffsetMapUnitScale == mCustomDashPatternMapUnitScale )
{
return mWidthMapUnitScale;
}
return QgsMapUnitScale();
}
QgsSymbolLayer *QgsSimpleLineSymbolLayer::create( const QgsStringMap &props )
{
QColor color = DEFAULT_SIMPLELINE_COLOR;
double width = DEFAULT_SIMPLELINE_WIDTH;
Qt::PenStyle penStyle = DEFAULT_SIMPLELINE_PENSTYLE;
if ( props.contains( QStringLiteral( "line_color" ) ) )
{
color = QgsSymbolLayerUtils::decodeColor( props[QStringLiteral( "line_color" )] );
}
else if ( props.contains( QStringLiteral( "outline_color" ) ) )
{
color = QgsSymbolLayerUtils::decodeColor( props[QStringLiteral( "outline_color" )] );
}
else if ( props.contains( QStringLiteral( "color" ) ) )
{
//pre 2.5 projects used "color"
color = QgsSymbolLayerUtils::decodeColor( props[QStringLiteral( "color" )] );
}
if ( props.contains( QStringLiteral( "line_width" ) ) )
{
width = props[QStringLiteral( "line_width" )].toDouble();
}
else if ( props.contains( QStringLiteral( "outline_width" ) ) )
{
width = props[QStringLiteral( "outline_width" )].toDouble();
}
else if ( props.contains( QStringLiteral( "width" ) ) )
{
//pre 2.5 projects used "width"
width = props[QStringLiteral( "width" )].toDouble();
}
if ( props.contains( QStringLiteral( "line_style" ) ) )
{
penStyle = QgsSymbolLayerUtils::decodePenStyle( props[QStringLiteral( "line_style" )] );
}
else if ( props.contains( QStringLiteral( "outline_style" ) ) )
{
penStyle = QgsSymbolLayerUtils::decodePenStyle( props[QStringLiteral( "outline_style" )] );
}
else if ( props.contains( QStringLiteral( "penstyle" ) ) )
{
penStyle = QgsSymbolLayerUtils::decodePenStyle( props[QStringLiteral( "penstyle" )] );
}
QgsSimpleLineSymbolLayer *l = new QgsSimpleLineSymbolLayer( color, width, penStyle );
if ( props.contains( QStringLiteral( "line_width_unit" ) ) )
{
l->setWidthUnit( QgsUnitTypes::decodeRenderUnit( props[QStringLiteral( "line_width_unit" )] ) );
}
else if ( props.contains( QStringLiteral( "outline_width_unit" ) ) )
{
l->setWidthUnit( QgsUnitTypes::decodeRenderUnit( props[QStringLiteral( "outline_width_unit" )] ) );
}
else if ( props.contains( QStringLiteral( "width_unit" ) ) )
{
//pre 2.5 projects used "width_unit"
l->setWidthUnit( QgsUnitTypes::decodeRenderUnit( props[QStringLiteral( "width_unit" )] ) );
}
if ( props.contains( QStringLiteral( "width_map_unit_scale" ) ) )
l->setWidthMapUnitScale( QgsSymbolLayerUtils::decodeMapUnitScale( props[QStringLiteral( "width_map_unit_scale" )] ) );
if ( props.contains( QStringLiteral( "offset" ) ) )
l->setOffset( props[QStringLiteral( "offset" )].toDouble() );
if ( props.contains( QStringLiteral( "offset_unit" ) ) )
l->setOffsetUnit( QgsUnitTypes::decodeRenderUnit( props[QStringLiteral( "offset_unit" )] ) );
if ( props.contains( QStringLiteral( "offset_map_unit_scale" ) ) )
l->setOffsetMapUnitScale( QgsSymbolLayerUtils::decodeMapUnitScale( props[QStringLiteral( "offset_map_unit_scale" )] ) );
if ( props.contains( QStringLiteral( "joinstyle" ) ) )
l->setPenJoinStyle( QgsSymbolLayerUtils::decodePenJoinStyle( props[QStringLiteral( "joinstyle" )] ) );
if ( props.contains( QStringLiteral( "capstyle" ) ) )
l->setPenCapStyle( QgsSymbolLayerUtils::decodePenCapStyle( props[QStringLiteral( "capstyle" )] ) );
if ( props.contains( QStringLiteral( "use_custom_dash" ) ) )
{
l->setUseCustomDashPattern( props[QStringLiteral( "use_custom_dash" )].toInt() );
}
if ( props.contains( QStringLiteral( "customdash" ) ) )
{
l->setCustomDashVector( QgsSymbolLayerUtils::decodeRealVector( props[QStringLiteral( "customdash" )] ) );
}
if ( props.contains( QStringLiteral( "customdash_unit" ) ) )
{
l->setCustomDashPatternUnit( QgsUnitTypes::decodeRenderUnit( props[QStringLiteral( "customdash_unit" )] ) );
}
if ( props.contains( QStringLiteral( "customdash_map_unit_scale" ) ) )
{
l->setCustomDashPatternMapUnitScale( QgsSymbolLayerUtils::decodeMapUnitScale( props[QStringLiteral( "customdash_map_unit_scale" )] ) );
}
if ( props.contains( QStringLiteral( "draw_inside_polygon" ) ) )
{
l->setDrawInsidePolygon( props[QStringLiteral( "draw_inside_polygon" )].toInt() );
}
if ( props.contains( QStringLiteral( "ring_filter" ) ) )
{
l->setRingFilter( static_cast< RenderRingFilter>( props[QStringLiteral( "ring_filter" )].toInt() ) );
}
if ( props.contains( QStringLiteral( "dash_pattern_offset" ) ) )
l->setDashPatternOffset( props[QStringLiteral( "dash_pattern_offset" )].toDouble() );
if ( props.contains( QStringLiteral( "dash_pattern_offset_unit" ) ) )
l->setDashPatternOffsetUnit( QgsUnitTypes::decodeRenderUnit( props[QStringLiteral( "dash_pattern_offset_unit" )] ) );
if ( props.contains( QStringLiteral( "dash_pattern_offset_map_unit_scale" ) ) )
l->setDashPatternOffsetMapUnitScale( QgsSymbolLayerUtils::decodeMapUnitScale( props[QStringLiteral( "dash_pattern_offset_map_unit_scale" )] ) );
if ( props.contains( QStringLiteral( "align_dash_pattern" ) ) )
l->setAlignDashPattern( props[ QStringLiteral( "align_dash_pattern" )].toInt() );
if ( props.contains( QStringLiteral( "tweak_dash_pattern_on_corners" ) ) )
l->setTweakDashPatternOnCorners( props[ QStringLiteral( "tweak_dash_pattern_on_corners" )].toInt() );
l->restoreOldDataDefinedProperties( props );
return l;
}
QString QgsSimpleLineSymbolLayer::layerType() const
{
return QStringLiteral( "SimpleLine" );
}
void QgsSimpleLineSymbolLayer::startRender( QgsSymbolRenderContext &context )
{
QColor penColor = mColor;
penColor.setAlphaF( mColor.alphaF() * context.opacity() );
mPen.setColor( penColor );
double scaledWidth = context.renderContext().convertToPainterUnits( mWidth, mWidthUnit, mWidthMapUnitScale );
mPen.setWidthF( scaledWidth );
//note that Qt seems to have issues with scaling dash patterns with very small pen widths.
//treating the pen as having no less than a 1 pixel size avoids the worst of these issues
const double dashWidthDiv = std::max( 1.0, scaledWidth );
if ( mUseCustomDashPattern )
{
mPen.setStyle( Qt::CustomDashLine );
//scale pattern vector
QVector<qreal> scaledVector;
QVector<qreal>::const_iterator it = mCustomDashVector.constBegin();
for ( ; it != mCustomDashVector.constEnd(); ++it )
{
//the dash is specified in terms of pen widths, therefore the division
scaledVector << context.renderContext().convertToPainterUnits( ( *it ), mCustomDashPatternUnit, mCustomDashPatternMapUnitScale ) / dashWidthDiv;
}
mPen.setDashPattern( scaledVector );
}
else
{
mPen.setStyle( mPenStyle );
}
if ( mDashPatternOffset && mPen.style() != Qt::SolidLine )
{
mPen.setDashOffset( context.renderContext().convertToPainterUnits( mDashPatternOffset, mDashPatternOffsetUnit, mDashPatternOffsetMapUnitScale ) / dashWidthDiv ) ;
}
mPen.setJoinStyle( mPenJoinStyle );
mPen.setCapStyle( mPenCapStyle );
mSelPen = mPen;
QColor selColor = context.renderContext().selectionColor();
if ( ! SELECTION_IS_OPAQUE )
selColor.setAlphaF( context.opacity() );
mSelPen.setColor( selColor );
}
void QgsSimpleLineSymbolLayer::stopRender( QgsSymbolRenderContext &context )
{
Q_UNUSED( context )
}
void QgsSimpleLineSymbolLayer::renderPolygonStroke( const QPolygonF &points, const QVector<QPolygonF> *rings, QgsSymbolRenderContext &context )
{
QPainter *p = context.renderContext().painter();
if ( !p )
{
return;
}
if ( mDrawInsidePolygon )
p->save();
switch ( mRingFilter )
{
case AllRings:
case ExteriorRingOnly:
{
if ( mDrawInsidePolygon )
{
//only drawing the line on the interior of the polygon, so set clip path for painter
QPainterPath clipPath;
clipPath.addPolygon( points );
if ( rings )
{
//add polygon rings
for ( auto it = rings->constBegin(); it != rings->constEnd(); ++it )
{
QPolygonF ring = *it;
clipPath.addPolygon( ring );
}
}
//use intersect mode, as a clip path may already exist (e.g., for composer maps)
p->setClipPath( clipPath, Qt::IntersectClip );
}
renderPolyline( points, context );
}
break;
case InteriorRingsOnly:
break;
}
if ( rings )
{
switch ( mRingFilter )
{
case AllRings:
case InteriorRingsOnly:
{
mOffset = -mOffset; // invert the offset for rings!
for ( const QPolygonF &ring : qgis::as_const( *rings ) )
renderPolyline( ring, context );
mOffset = -mOffset;
}
break;
case ExteriorRingOnly:
break;
}
}
if ( mDrawInsidePolygon )
{
//restore painter to reset clip path
p->restore();
}
}
void QgsSimpleLineSymbolLayer::renderPolyline( const QPolygonF &points, QgsSymbolRenderContext &context )
{
QPainter *p = context.renderContext().painter();
if ( !p )
{
return;
}
double offset = mOffset;
applyDataDefinedSymbology( context, mPen, mSelPen, offset );
const QPen pen = context.selected() ? mSelPen : mPen;
p->setBrush( Qt::NoBrush );
// Disable 'Antialiasing' if the geometry was generalized in the current RenderContext (We known that it must have least #2 points).
std::unique_ptr< QgsScopedQPainterState > painterState;
if ( points.size() <= 2 &&
( context.renderContext().vectorSimplifyMethod().simplifyHints() & QgsVectorSimplifyMethod::AntialiasingSimplification ) &&
QgsAbstractGeometrySimplifier::isGeneralizableByDeviceBoundingBox( points, context.renderContext().vectorSimplifyMethod().threshold() ) &&
( p->renderHints() & QPainter::Antialiasing ) )
{
painterState = qgis::make_unique< QgsScopedQPainterState >( p );
p->setRenderHint( QPainter::Antialiasing, false );
}
const bool applyPatternTweaks = mAlignDashPattern
&& pen.widthF() > 1.0
&& ( pen.style() != Qt::SolidLine || !pen.dashPattern().empty() )
&& pen.dashOffset() == 0;
if ( qgsDoubleNear( offset, 0 ) )
{
if ( applyPatternTweaks )
{
drawPathWithDashPatternTweaks( p, points, pen );
}
else
{
p->setPen( pen );
QPainterPath path;
path.addPolygon( points );
p->drawPath( path );
}
}
else
{
double scaledOffset = context.renderContext().convertToPainterUnits( offset, mOffsetUnit, mOffsetMapUnitScale );
if ( mOffsetUnit == QgsUnitTypes::RenderMetersInMapUnits && context.renderContext().flags() & QgsRenderContext::RenderSymbolPreview )
{
// rendering for symbol previews -- a size in meters in map units can't be calculated, so treat the size as millimeters
// and clamp it to a reasonable range. It's the best we can do in this situation!
scaledOffset = std::min( std::max( context.renderContext().convertToPainterUnits( offset, QgsUnitTypes::RenderMillimeters ), 3.0 ), 100.0 );
}
QList<QPolygonF> mline = ::offsetLine( points, scaledOffset, context.originalGeometryType() != QgsWkbTypes::UnknownGeometry ? context.originalGeometryType() : QgsWkbTypes::LineGeometry );
for ( const QPolygonF &part : mline )
{
if ( applyPatternTweaks )
{
drawPathWithDashPatternTweaks( p, part, pen );
}
else
{
p->setPen( pen );
QPainterPath path;
path.addPolygon( part );
p->drawPath( path );
}
}
}
}
QgsStringMap QgsSimpleLineSymbolLayer::properties() const
{
QgsStringMap map;
map[QStringLiteral( "line_color" )] = QgsSymbolLayerUtils::encodeColor( mColor );
map[QStringLiteral( "line_width" )] = QString::number( mWidth );
map[QStringLiteral( "line_width_unit" )] = QgsUnitTypes::encodeUnit( mWidthUnit );
map[QStringLiteral( "width_map_unit_scale" )] = QgsSymbolLayerUtils::encodeMapUnitScale( mWidthMapUnitScale );
map[QStringLiteral( "line_style" )] = QgsSymbolLayerUtils::encodePenStyle( mPenStyle );
map[QStringLiteral( "joinstyle" )] = QgsSymbolLayerUtils::encodePenJoinStyle( mPenJoinStyle );
map[QStringLiteral( "capstyle" )] = QgsSymbolLayerUtils::encodePenCapStyle( mPenCapStyle );
map[QStringLiteral( "offset" )] = QString::number( mOffset );
map[QStringLiteral( "offset_unit" )] = QgsUnitTypes::encodeUnit( mOffsetUnit );
map[QStringLiteral( "offset_map_unit_scale" )] = QgsSymbolLayerUtils::encodeMapUnitScale( mOffsetMapUnitScale );
map[QStringLiteral( "use_custom_dash" )] = ( mUseCustomDashPattern ? QStringLiteral( "1" ) : QStringLiteral( "0" ) );
map[QStringLiteral( "customdash" )] = QgsSymbolLayerUtils::encodeRealVector( mCustomDashVector );
map[QStringLiteral( "customdash_unit" )] = QgsUnitTypes::encodeUnit( mCustomDashPatternUnit );
map[QStringLiteral( "customdash_map_unit_scale" )] = QgsSymbolLayerUtils::encodeMapUnitScale( mCustomDashPatternMapUnitScale );
map[QStringLiteral( "dash_pattern_offset" )] = QString::number( mDashPatternOffset );
map[QStringLiteral( "dash_pattern_offset_unit" )] = QgsUnitTypes::encodeUnit( mDashPatternOffsetUnit );
map[QStringLiteral( "dash_pattern_offset_map_unit_scale" )] = QgsSymbolLayerUtils::encodeMapUnitScale( mDashPatternOffsetMapUnitScale );
map[QStringLiteral( "draw_inside_polygon" )] = ( mDrawInsidePolygon ? QStringLiteral( "1" ) : QStringLiteral( "0" ) );
map[QStringLiteral( "ring_filter" )] = QString::number( static_cast< int >( mRingFilter ) );
map[QStringLiteral( "align_dash_pattern" )] = mAlignDashPattern ? QStringLiteral( "1" ) : QStringLiteral( "0" );
map[QStringLiteral( "tweak_dash_pattern_on_corners" )] = mPatternCartographicTweakOnSharpCorners ? QStringLiteral( "1" ) : QStringLiteral( "0" );
return map;
}
QgsSimpleLineSymbolLayer *QgsSimpleLineSymbolLayer::clone() const
{
QgsSimpleLineSymbolLayer *l = new QgsSimpleLineSymbolLayer( mColor, mWidth, mPenStyle );
l->setWidthUnit( mWidthUnit );
l->setWidthMapUnitScale( mWidthMapUnitScale );
l->setOffsetUnit( mOffsetUnit );
l->setOffsetMapUnitScale( mOffsetMapUnitScale );
l->setCustomDashPatternUnit( mCustomDashPatternUnit );
l->setCustomDashPatternMapUnitScale( mCustomDashPatternMapUnitScale );
l->setOffset( mOffset );
l->setPenJoinStyle( mPenJoinStyle );
l->setPenCapStyle( mPenCapStyle );
l->setUseCustomDashPattern( mUseCustomDashPattern );
l->setCustomDashVector( mCustomDashVector );
l->setDrawInsidePolygon( mDrawInsidePolygon );
l->setRingFilter( mRingFilter );
l->setDashPatternOffset( mDashPatternOffset );
l->setDashPatternOffsetUnit( mDashPatternOffsetUnit );
l->setDashPatternOffsetMapUnitScale( mDashPatternOffsetMapUnitScale );
l->setAlignDashPattern( mAlignDashPattern );
l->setTweakDashPatternOnCorners( mPatternCartographicTweakOnSharpCorners );
copyDataDefinedProperties( l );
copyPaintEffect( l );
return l;
}
void QgsSimpleLineSymbolLayer::toSld( QDomDocument &doc, QDomElement &element, const QgsStringMap &props ) const
{
if ( mPenStyle == Qt::NoPen )
return;
QDomElement symbolizerElem = doc.createElement( QStringLiteral( "se:LineSymbolizer" ) );
if ( !props.value( QStringLiteral( "uom" ), QString() ).isEmpty() )
symbolizerElem.setAttribute( QStringLiteral( "uom" ), props.value( QStringLiteral( "uom" ), QString() ) );
element.appendChild( symbolizerElem );
// <Geometry>
QgsSymbolLayerUtils::createGeometryElement( doc, symbolizerElem, props.value( QStringLiteral( "geom" ), QString() ) );
// <Stroke>
QDomElement strokeElem = doc.createElement( QStringLiteral( "se:Stroke" ) );
symbolizerElem.appendChild( strokeElem );
Qt::PenStyle penStyle = mUseCustomDashPattern ? Qt::CustomDashLine : mPenStyle;
double width = QgsSymbolLayerUtils::rescaleUom( mWidth, mWidthUnit, props );
QVector<qreal> customDashVector = QgsSymbolLayerUtils::rescaleUom( mCustomDashVector, mCustomDashPatternUnit, props );
QgsSymbolLayerUtils::lineToSld( doc, strokeElem, penStyle, mColor, width,
&mPenJoinStyle, &mPenCapStyle, &customDashVector );
// <se:PerpendicularOffset>
if ( !qgsDoubleNear( mOffset, 0.0 ) )
{
QDomElement perpOffsetElem = doc.createElement( QStringLiteral( "se:PerpendicularOffset" ) );
double offset = QgsSymbolLayerUtils::rescaleUom( mOffset, mOffsetUnit, props );
perpOffsetElem.appendChild( doc.createTextNode( qgsDoubleToString( offset ) ) );
symbolizerElem.appendChild( perpOffsetElem );
}
}
QString QgsSimpleLineSymbolLayer::ogrFeatureStyle( double mmScaleFactor, double mapUnitScaleFactor ) const
{
if ( mUseCustomDashPattern )
{
return QgsSymbolLayerUtils::ogrFeatureStylePen( mWidth, mmScaleFactor, mapUnitScaleFactor,
mPen.color(), mPenJoinStyle,
mPenCapStyle, mOffset, &mCustomDashVector );
}
else
{
return QgsSymbolLayerUtils::ogrFeatureStylePen( mWidth, mmScaleFactor, mapUnitScaleFactor, mPen.color(), mPenJoinStyle,
mPenCapStyle, mOffset );
}
}
QgsSymbolLayer *QgsSimpleLineSymbolLayer::createFromSld( QDomElement &element )
{
QgsDebugMsgLevel( QStringLiteral( "Entered." ), 4 );
QDomElement strokeElem = element.firstChildElement( QStringLiteral( "Stroke" ) );
if ( strokeElem.isNull() )
return nullptr;
Qt::PenStyle penStyle;
QColor color;
double width;
Qt::PenJoinStyle penJoinStyle;
Qt::PenCapStyle penCapStyle;
QVector<qreal> customDashVector;
if ( !QgsSymbolLayerUtils::lineFromSld( strokeElem, penStyle,
color, width,
&penJoinStyle, &penCapStyle,
&customDashVector ) )
return nullptr;
double offset = 0.0;
QDomElement perpOffsetElem = element.firstChildElement( QStringLiteral( "PerpendicularOffset" ) );
if ( !perpOffsetElem.isNull() )
{
bool ok;
double d = perpOffsetElem.firstChild().nodeValue().toDouble( &ok );
if ( ok )
offset = d;
}
QString uom = element.attribute( QStringLiteral( "uom" ) );
width = QgsSymbolLayerUtils::sizeInPixelsFromSldUom( uom, width );
offset = QgsSymbolLayerUtils::sizeInPixelsFromSldUom( uom, offset );
QgsSimpleLineSymbolLayer *l = new QgsSimpleLineSymbolLayer( color, width, penStyle );
l->setOutputUnit( QgsUnitTypes::RenderUnit::RenderPixels );
l->setOffset( offset );
l->setPenJoinStyle( penJoinStyle );
l->setPenCapStyle( penCapStyle );
l->setUseCustomDashPattern( penStyle == Qt::CustomDashLine );
l->setCustomDashVector( customDashVector );
return l;
}
void QgsSimpleLineSymbolLayer::applyDataDefinedSymbology( QgsSymbolRenderContext &context, QPen &pen, QPen &selPen, double &offset )
{
if ( !dataDefinedProperties().hasActiveProperties() )
return; // shortcut
//data defined properties
bool hasStrokeWidthExpression = false;
if ( mDataDefinedProperties.isActive( QgsSymbolLayer::PropertyStrokeWidth ) )
{
context.setOriginalValueVariable( mWidth );
double scaledWidth = context.renderContext().convertToPainterUnits(
mDataDefinedProperties.valueAsDouble( QgsSymbolLayer::PropertyStrokeWidth, context.renderContext().expressionContext(), mWidth ),
mWidthUnit, mWidthMapUnitScale );
pen.setWidthF( scaledWidth );
selPen.setWidthF( scaledWidth );
hasStrokeWidthExpression = true;
}
//color
if ( mDataDefinedProperties.isActive( QgsSymbolLayer::PropertyStrokeColor ) )
{
context.setOriginalValueVariable( QgsSymbolLayerUtils::encodeColor( mColor ) );
pen.setColor( mDataDefinedProperties.valueAsColor( QgsSymbolLayer::PropertyStrokeColor, context.renderContext().expressionContext(), mColor ) );
}
//offset
if ( mDataDefinedProperties.isActive( QgsSymbolLayer::PropertyOffset ) )
{
context.setOriginalValueVariable( mOffset );
offset = mDataDefinedProperties.valueAsDouble( QgsSymbolLayer::PropertyOffset, context.renderContext().expressionContext(), offset );
}
//dash dot vector
//note that Qt seems to have issues with scaling dash patterns with very small pen widths.
//treating the pen as having no less than a 1 pixel size avoids the worst of these issues
const double dashWidthDiv = std::max( hasStrokeWidthExpression ? pen.widthF() : mPen.widthF(), 1.0 );
if ( mDataDefinedProperties.isActive( QgsSymbolLayer::PropertyCustomDash ) )
{
QVector<qreal> dashVector;
QVariant exprVal = mDataDefinedProperties.value( QgsSymbolLayer::PropertyCustomDash, context.renderContext().expressionContext() );
if ( exprVal.isValid() )
{
QStringList dashList = exprVal.toString().split( ';' );
QStringList::const_iterator dashIt = dashList.constBegin();
for ( ; dashIt != dashList.constEnd(); ++dashIt )
{
dashVector.push_back( context.renderContext().convertToPainterUnits( dashIt->toDouble(), mCustomDashPatternUnit, mCustomDashPatternMapUnitScale ) / dashWidthDiv );
}
pen.setDashPattern( dashVector );
}
}
// dash pattern offset
double patternOffset = mDashPatternOffset;
if ( mDataDefinedProperties.isActive( QgsSymbolLayer::PropertyDashPatternOffset ) && pen.style() != Qt::SolidLine )
{
context.setOriginalValueVariable( mDashPatternOffset );
patternOffset = mDataDefinedProperties.valueAsDouble( QgsSymbolLayer::PropertyDashPatternOffset, context.renderContext().expressionContext(), mDashPatternOffset );
pen.setDashOffset( context.renderContext().convertToPainterUnits( patternOffset, mDashPatternOffsetUnit, mDashPatternOffsetMapUnitScale ) / dashWidthDiv );
}
//line style
if ( mDataDefinedProperties.isActive( QgsSymbolLayer::PropertyStrokeStyle ) )
{
context.setOriginalValueVariable( QgsSymbolLayerUtils::encodePenStyle( mPenStyle ) );
QVariant exprVal = mDataDefinedProperties.value( QgsSymbolLayer::PropertyStrokeStyle, context.renderContext().expressionContext() );
if ( exprVal.isValid() )
pen.setStyle( QgsSymbolLayerUtils::decodePenStyle( exprVal.toString() ) );
}
//join style
if ( mDataDefinedProperties.isActive( QgsSymbolLayer::PropertyJoinStyle ) )
{
context.setOriginalValueVariable( QgsSymbolLayerUtils::encodePenJoinStyle( mPenJoinStyle ) );
QVariant exprVal = mDataDefinedProperties.value( QgsSymbolLayer::PropertyJoinStyle, context.renderContext().expressionContext() );
if ( exprVal.isValid() )
pen.setJoinStyle( QgsSymbolLayerUtils::decodePenJoinStyle( exprVal.toString() ) );
}
//cap style
if ( mDataDefinedProperties.isActive( QgsSymbolLayer::PropertyCapStyle ) )
{
context.setOriginalValueVariable( QgsSymbolLayerUtils::encodePenCapStyle( mPenCapStyle ) );
QVariant exprVal = mDataDefinedProperties.value( QgsSymbolLayer::PropertyCapStyle, context.renderContext().expressionContext() );
if ( exprVal.isValid() )
pen.setCapStyle( QgsSymbolLayerUtils::decodePenCapStyle( exprVal.toString() ) );
}
}
void QgsSimpleLineSymbolLayer::drawPathWithDashPatternTweaks( QPainter *painter, const QPolygonF &points, QPen pen ) const
{
if ( pen.dashPattern().empty() || points.size() < 2 )
return;
QVector< qreal > sourcePattern = pen.dashPattern();
const double dashWidthDiv = std::max( 1.0, pen.widthF() );
// back to painter units
for ( int i = 0; i < sourcePattern.size(); ++ i )
sourcePattern[i] *= dashWidthDiv;
QVector< qreal > buffer;
QPolygonF bufferedPoints;
QPolygonF previousSegmentBuffer;
// we iterate through the line points, building a custom dash pattern and adding it to the buffer
// as soon as we hit a sharp bend, we scale the buffered pattern in order to nicely place a dash component over the bend
// and then append the buffer to the output pattern.
auto ptIt = points.constBegin();
double totalBufferLength = 0;
int patternIndex = 0;
double currentRemainingDashLength = 0;
double currentRemainingGapLength = 0;
auto compressPattern = []( const QVector< qreal > &buffer ) -> QVector< qreal >
{
QVector< qreal > result;
result.reserve( buffer.size() );
for ( auto it = buffer.begin(); it != buffer.end(); )
{
qreal dash = *it++;
qreal gap = *it++;
while ( dash == 0 && !result.empty() )
{
result.last() += gap;
if ( it == buffer.end() )
return result;
dash = *it++;
gap = *it++;
}
while ( gap == 0 && it != buffer.end() )
{
dash += *it++;
gap = *it++;
}
result << dash << gap;
}
return result;
};
double currentBufferLineLength = 0;
auto flushBuffer = [pen, painter, &buffer, &bufferedPoints, &previousSegmentBuffer, ¤tRemainingDashLength, ¤tRemainingGapLength, ¤tBufferLineLength, &totalBufferLength,
dashWidthDiv, &compressPattern]( QPointF * nextPoint )
{
if ( buffer.empty() || bufferedPoints.size() < 2 )
{
return;
}
if ( currentRemainingDashLength )
{
// ended midway through a dash -- we want to finish this off
buffer << currentRemainingDashLength << 0.0;
totalBufferLength += currentRemainingDashLength;
}
QVector< qreal > compressed = compressPattern( buffer );
if ( !currentRemainingDashLength )
{
// ended midway through a gap -- we don't want this, we want to end at previous dash
totalBufferLength -= compressed.last();
compressed.last() = 0;
}
// rescale buffer for final bit of line -- we want to end at the end of a dash, not a gap
const double scaleFactor = currentBufferLineLength / totalBufferLength;
bool shouldFlushPreviousSegmentBuffer = false;
if ( !previousSegmentBuffer.empty() )
{
// add first dash from current buffer
QPolygonF firstDashSubstring = QgsSymbolLayerUtils::polylineSubstring( bufferedPoints, 0, compressed.first() * scaleFactor );
if ( !firstDashSubstring.empty() )
QgsSymbolLayerUtils::appendPolyline( previousSegmentBuffer, firstDashSubstring );
// then we skip over the first dash and gap for this segment
bufferedPoints = QgsSymbolLayerUtils::polylineSubstring( bufferedPoints, ( compressed.first() + compressed.at( 1 ) ) * scaleFactor, 0 );
compressed = compressed.mid( 2 );
shouldFlushPreviousSegmentBuffer = !compressed.empty();
}
if ( !previousSegmentBuffer.empty() && ( shouldFlushPreviousSegmentBuffer || !nextPoint ) )
{
QPen adjustedPen = pen;
adjustedPen.setStyle( Qt::SolidLine );
painter->setPen( adjustedPen );
QPainterPath path;
path.addPolygon( previousSegmentBuffer );
painter->drawPath( path );
previousSegmentBuffer.clear();
}
double finalDash = 0;
if ( nextPoint )
{
// sharp bend:
// 1. rewind buffered points line by final dash and gap length
// (later) 2. draw the bend with a solid line of length 2 * final dash size
if ( !compressed.empty() )
{
finalDash = compressed.at( compressed.size() - 2 );
const double finalGap = compressed.size() > 2 ? compressed.at( compressed.size() - 3 ) : 0;
const QPolygonF thisPoints = bufferedPoints;
bufferedPoints = QgsSymbolLayerUtils::polylineSubstring( thisPoints, 0, -( finalDash + finalGap ) * scaleFactor );
previousSegmentBuffer = QgsSymbolLayerUtils::polylineSubstring( thisPoints, - finalDash * scaleFactor, 0 );
}
else
{
previousSegmentBuffer << bufferedPoints;
}
}
currentBufferLineLength = 0;
currentRemainingDashLength = 0;
currentRemainingGapLength = 0;
totalBufferLength = 0;
buffer.clear();
if ( !bufferedPoints.empty() && ( !compressed.empty() || !nextPoint ) )
{
QPen adjustedPen = pen;
if ( !compressed.empty() )
{
// maximum size of dash pattern is 32 elements
compressed = compressed.mid( 0, 32 );
std::for_each( compressed.begin(), compressed.end(), [scaleFactor, dashWidthDiv]( qreal & element ) { element *= scaleFactor / dashWidthDiv; } );
adjustedPen.setDashPattern( compressed );
}
else
{
adjustedPen.setStyle( Qt::SolidLine );
}
painter->setPen( adjustedPen );
QPainterPath path;
path.addPolygon( bufferedPoints );
painter->drawPath( path );
}
bufferedPoints.clear();
};
QPointF p1;
QPointF p2 = *ptIt;
ptIt++;
bufferedPoints << p2;
for ( ; ptIt != points.constEnd(); ++ptIt )
{
p1 = *ptIt;
if ( qgsDoubleNear( p1.y(), p2.y() ) && qgsDoubleNear( p1.x(), p2.x() ) )
{
continue;
}
double remainingSegmentDistance = std::sqrt( std::pow( p2.x() - p1.x(), 2.0 ) + std::pow( p2.y() - p1.y(), 2.0 ) );
currentBufferLineLength += remainingSegmentDistance;
while ( true )
{
// handle currentRemainingDashLength/currentRemainingGapLength
if ( currentRemainingDashLength > 0 )
{
// bit more of dash to insert
if ( remainingSegmentDistance >= currentRemainingDashLength )
{
// all of dash fits in
buffer << currentRemainingDashLength << 0.0;
totalBufferLength += currentRemainingDashLength;
remainingSegmentDistance -= currentRemainingDashLength;
patternIndex++;
currentRemainingDashLength = 0.0;
currentRemainingGapLength = sourcePattern.at( patternIndex );
}
else
{
// only part of remaining dash fits in
buffer << remainingSegmentDistance << 0.0;
totalBufferLength += remainingSegmentDistance;
currentRemainingDashLength -= remainingSegmentDistance;
break;
}
}
if ( currentRemainingGapLength > 0 )
{
// bit more of gap to insert
if ( remainingSegmentDistance >= currentRemainingGapLength )
{
// all of gap fits in
buffer << 0.0 << currentRemainingGapLength;
totalBufferLength += currentRemainingGapLength;
remainingSegmentDistance -= currentRemainingGapLength;
currentRemainingGapLength = 0.0;
patternIndex++;
}
else
{
// only part of remaining gap fits in
buffer << 0.0 << remainingSegmentDistance;
totalBufferLength += remainingSegmentDistance;
currentRemainingGapLength -= remainingSegmentDistance;
break;
}
}
if ( patternIndex >= sourcePattern.size() )
patternIndex = 0;
const double nextPatternDashLength = sourcePattern.at( patternIndex );
const double nextPatternGapLength = sourcePattern.at( patternIndex + 1 );
if ( nextPatternDashLength + nextPatternGapLength <= remainingSegmentDistance )
{
buffer << nextPatternDashLength << nextPatternGapLength;
remainingSegmentDistance -= nextPatternDashLength + nextPatternGapLength;
totalBufferLength += nextPatternDashLength + nextPatternGapLength;
patternIndex += 2;
}
else if ( nextPatternDashLength <= remainingSegmentDistance )
{
// can fit in "dash", but not "gap"
buffer << nextPatternDashLength << remainingSegmentDistance - nextPatternDashLength;
totalBufferLength += remainingSegmentDistance;
currentRemainingGapLength = nextPatternGapLength - ( remainingSegmentDistance - nextPatternDashLength );
currentRemainingDashLength = 0;
patternIndex++;
break;
}
else
{
// can't fit in "dash"
buffer << remainingSegmentDistance << 0.0;
totalBufferLength += remainingSegmentDistance;
currentRemainingGapLength = 0;
currentRemainingDashLength = nextPatternDashLength - remainingSegmentDistance;
break;
}
}
bufferedPoints << p1;
if ( mPatternCartographicTweakOnSharpCorners && ptIt + 1 != points.constEnd() )
{
QPointF nextPoint = *( ptIt + 1 );
// extreme angles form more than 45 degree angle at a node
if ( QgsSymbolLayerUtils::isSharpCorner( p2, p1, nextPoint ) )
{
// extreme angle. Rescale buffer and flush
flushBuffer( &nextPoint );
bufferedPoints << p1;
// restart the line with the full length of the most recent dash element -- see
// "Cartographic Generalization" (Swiss Society of Cartography) p33, example #8
if ( patternIndex % 2 == 1 )
{
patternIndex--;
}
currentRemainingDashLength = sourcePattern.at( patternIndex );
}
}
p2 = p1;
}
flushBuffer( nullptr );
if ( !previousSegmentBuffer.empty() )
{
QPen adjustedPen = pen;
adjustedPen.setStyle( Qt::SolidLine );
painter->setPen( adjustedPen );
QPainterPath path;
path.addPolygon( previousSegmentBuffer );
painter->drawPath( path );
previousSegmentBuffer.clear();
}
}
double QgsSimpleLineSymbolLayer::estimateMaxBleed( const QgsRenderContext &context ) const
{
if ( mDrawInsidePolygon )
{
//set to clip line to the interior of polygon, so we expect no bleed
return 0;
}
else
{
return context.convertToPainterUnits( ( mWidth / 2.0 ), mWidthUnit, mWidthMapUnitScale ) +
context.convertToPainterUnits( std::fabs( mOffset ), mOffsetUnit, mOffsetMapUnitScale );
}
}
QVector<qreal> QgsSimpleLineSymbolLayer::dxfCustomDashPattern( QgsUnitTypes::RenderUnit &unit ) const
{
unit = mCustomDashPatternUnit;
return mUseCustomDashPattern ? mCustomDashVector : QVector<qreal>();
}
Qt::PenStyle QgsSimpleLineSymbolLayer::dxfPenStyle() const
{
return mPenStyle;
}
double QgsSimpleLineSymbolLayer::dxfWidth( const QgsDxfExport &e, QgsSymbolRenderContext &context ) const
{
double width = mWidth;
if ( mDataDefinedProperties.isActive( QgsSymbolLayer::PropertyStrokeWidth ) )
{
context.setOriginalValueVariable( mWidth );
width = mDataDefinedProperties.valueAsDouble( QgsSymbolLayer::PropertyStrokeWidth, context.renderContext().expressionContext(), mWidth );
}
width *= e.mapUnitScaleFactor( e.symbologyScale(), widthUnit(), e.mapUnits(), context.renderContext().mapToPixel().mapUnitsPerPixel() );
if ( mWidthUnit == QgsUnitTypes::RenderMapUnits )
{
e.clipValueToMapUnitScale( width, mWidthMapUnitScale, context.renderContext().scaleFactor() );
}
return width;
}
QColor QgsSimpleLineSymbolLayer::dxfColor( QgsSymbolRenderContext &context ) const
{
if ( mDataDefinedProperties.isActive( QgsSymbolLayer::PropertyStrokeColor ) )
{
context.setOriginalValueVariable( QgsSymbolLayerUtils::encodeColor( mColor ) );
return mDataDefinedProperties.valueAsColor( QgsSymbolLayer::PropertyStrokeColor, context.renderContext().expressionContext(), mColor );
}
return mColor;
}
bool QgsSimpleLineSymbolLayer::alignDashPattern() const
{
return mAlignDashPattern;
}
void QgsSimpleLineSymbolLayer::setAlignDashPattern( bool enabled )
{
mAlignDashPattern = enabled;
}
bool QgsSimpleLineSymbolLayer::tweakDashPatternOnCorners() const
{
return mPatternCartographicTweakOnSharpCorners;
}
void QgsSimpleLineSymbolLayer::setTweakDashPatternOnCorners( bool enabled )
{
mPatternCartographicTweakOnSharpCorners = enabled;
}
double QgsSimpleLineSymbolLayer::dxfOffset( const QgsDxfExport &e, QgsSymbolRenderContext &context ) const
{
Q_UNUSED( e )
double offset = mOffset;
if ( mDataDefinedProperties.isActive( QgsSymbolLayer::PropertyOffset ) )