forked from qgis/QGIS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqgssymbol.cpp
2351 lines (2009 loc) · 78.7 KB
/
qgssymbol.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
/***************************************************************************
qgssymbol.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 <QColor>
#include <QImage>
#include <QPainter>
#include <QSize>
#include <QSvgGenerator>
#include <QPicture>
#include <cmath>
#include <map>
#include <random>
#include "qgssymbol.h"
#include "qgspolyhedralsurface.h"
#include "qgsrectangle.h"
#include "qgssymbollayer.h"
#include "qgsgeometrygeneratorsymbollayer.h"
#include "qgsmaptopixelgeometrysimplifier.h"
#include "qgslogger.h"
#include "qgsrendercontext.h" // for bigSymbolPreview
#include "qgsproject.h"
#include "qgsprojectstylesettings.h"
#include "qgsstyle.h"
#include "qgspainteffect.h"
#include "qgsvectorlayer.h"
#include "qgsfeature.h"
#include "qgsgeometry.h"
#include "qgsmultipoint.h"
#include "qgsgeometrycollection.h"
#include "qgslinestring.h"
#include "qgspolygon.h"
#include "qgsclipper.h"
#include "qgsproperty.h"
#include "qgscolorschemeregistry.h"
#include "qgsapplication.h"
#include "qgsexpressioncontextutils.h"
#include "qgsrenderedfeaturehandlerinterface.h"
#include "qgslegendpatchshape.h"
#include "qgsgeos.h"
#include "qgsmarkersymbol.h"
#include "qgslinesymbol.h"
#include "qgsfillsymbol.h"
#include "qgsfillsymbollayer.h"
#include "qgscolorutils.h"
#include "qgsunittypes.h"
#include "qgsgeometrypaintdevice.h"
#include "qgspainting.h"
QgsPropertiesDefinition QgsSymbol::sPropertyDefinitions;
//
// QgsSymbolBufferSettings
//
QgsSymbolBufferSettings::QgsSymbolBufferSettings()
{
mFillSymbol = std::make_unique< QgsFillSymbol >( QgsSymbolLayerList() << new QgsSimpleFillSymbolLayer( QColor( 255, 255, 255 ), Qt::SolidPattern, QColor( 200, 200, 200 ), Qt::NoPen ) );
}
QgsSymbolBufferSettings::QgsSymbolBufferSettings( const QgsSymbolBufferSettings &other )
: mEnabled( other.mEnabled )
, mSize( other.mSize )
, mSizeUnit( other.mSizeUnit )
, mSizeMapUnitScale( other.mSizeMapUnitScale )
, mJoinStyle( other.mJoinStyle )
, mFillSymbol( other.mFillSymbol ? other.mFillSymbol->clone() : nullptr )
{
}
QgsSymbolBufferSettings &QgsSymbolBufferSettings::operator=( const QgsSymbolBufferSettings &other )
{
mEnabled = other.mEnabled;
mSize = other.mSize;
mSizeUnit = other.mSizeUnit;
mSizeMapUnitScale = other.mSizeMapUnitScale;
mJoinStyle = other.mJoinStyle;
mFillSymbol.reset( other.mFillSymbol ? other.mFillSymbol->clone() : nullptr );
return *this;
}
QgsFillSymbol *QgsSymbolBufferSettings::fillSymbol() const
{
return mFillSymbol.get();
}
void QgsSymbolBufferSettings::setFillSymbol( QgsFillSymbol *symbol )
{
mFillSymbol.reset( symbol );
}
QgsSymbolBufferSettings::~QgsSymbolBufferSettings() = default;
void QgsSymbolBufferSettings::writeXml( QDomElement &element, const QgsReadWriteContext &context ) const
{
QDomElement symbolBufferElem = element.ownerDocument().createElement( QStringLiteral( "buffer" ) );
symbolBufferElem.setAttribute( QStringLiteral( "enabled" ), mEnabled );
symbolBufferElem.setAttribute( QStringLiteral( "size" ), mSize );
symbolBufferElem.setAttribute( QStringLiteral( "sizeUnits" ), QgsUnitTypes::encodeUnit( mSizeUnit ) );
symbolBufferElem.setAttribute( QStringLiteral( "sizeMapUnitScale" ), QgsSymbolLayerUtils::encodeMapUnitScale( mSizeMapUnitScale ) );
symbolBufferElem.setAttribute( QStringLiteral( "joinStyle" ), static_cast< unsigned int >( mJoinStyle ) );
if ( mFillSymbol )
{
QDomDocument document = element.ownerDocument();
const QDomElement fillElem = QgsSymbolLayerUtils::saveSymbol( QString(), mFillSymbol.get(), document, context );
symbolBufferElem.appendChild( fillElem );
}
element.appendChild( symbolBufferElem );
}
void QgsSymbolBufferSettings::readXml( const QDomElement &element, const QgsReadWriteContext &context )
{
const QDomElement symbolBufferElem = element.firstChildElement( QStringLiteral( "buffer" ) );
mEnabled = symbolBufferElem.attribute( QStringLiteral( "enabled" ), QStringLiteral( "0" ) ).toInt();
mSize = symbolBufferElem.attribute( QStringLiteral( "size" ), QStringLiteral( "1" ) ).toDouble();
mSizeUnit = QgsUnitTypes::decodeRenderUnit( symbolBufferElem.attribute( QStringLiteral( "sizeUnits" ) ) );
mSizeMapUnitScale = QgsSymbolLayerUtils::decodeMapUnitScale( symbolBufferElem.attribute( QStringLiteral( "sizeMapUnitScale" ) ) );
mJoinStyle = static_cast< Qt::PenJoinStyle >( symbolBufferElem.attribute( QStringLiteral( "joinStyle" ), QString::number( Qt::RoundJoin ) ).toUInt() );
const QDomElement fillSymbolElem = symbolBufferElem.firstChildElement( QStringLiteral( "symbol" ) );
if ( !fillSymbolElem.isNull() )
{
mFillSymbol.reset( QgsSymbolLayerUtils::loadSymbol<QgsFillSymbol>( fillSymbolElem, context ) );
}
else
{
mFillSymbol = std::make_unique< QgsFillSymbol >( QgsSymbolLayerList() << new QgsSimpleFillSymbolLayer( QColor( 255, 255, 255 ), Qt::SolidPattern, QColor( 200, 200, 200 ), Qt::NoPen ) );
}
}
//
// QgsSymbol
//
Q_NOWARN_DEPRECATED_PUSH // because of deprecated mLayer
QgsSymbol::QgsSymbol( Qgis::SymbolType type, const QgsSymbolLayerList &layers )
: mType( type )
, mLayers( layers )
{
// check they're all correct symbol layers
for ( int i = 0; i < mLayers.count(); i++ )
{
if ( !mLayers.at( i ) )
{
mLayers.removeAt( i-- );
}
else if ( !mLayers.at( i )->isCompatibleWithSymbol( this ) )
{
delete mLayers.at( i );
mLayers.removeAt( i-- );
}
}
}
Q_NOWARN_DEPRECATED_POP
QPolygonF QgsSymbol::_getLineString( QgsRenderContext &context, const QgsCurve &curve, bool clipToExtent )
{
if ( curve.is3D() )
return _getLineString3d( context, curve, clipToExtent );
else
return _getLineString2d( context, curve, clipToExtent );
}
QPolygonF QgsSymbol::_getLineString3d( QgsRenderContext &context, const QgsCurve &curve, bool clipToExtent )
{
const unsigned int nPoints = curve.numPoints();
QgsCoordinateTransform ct = context.coordinateTransform();
const QgsMapToPixel &mtp = context.mapToPixel();
QVector< double > pointsX;
QVector< double > pointsY;
QVector< double > pointsZ;
// apply clipping for large lines to achieve a better rendering performance
if ( clipToExtent && nPoints > 1 && !( context.flags() & Qgis::RenderContextFlag::ApplyClipAfterReprojection ) )
{
const QgsRectangle e = context.extent();
const double cw = e.width() / 10;
const double ch = e.height() / 10;
const QgsBox3D clipRect( e.xMinimum() - cw, e.yMinimum() - ch, -HUGE_VAL, e.xMaximum() + cw, e.yMaximum() + ch, HUGE_VAL ); // TODO also need to be clipped according to z axis
const QgsLineString *lineString = nullptr;
std::unique_ptr< QgsLineString > segmentized;
if ( const QgsLineString *ls = qgsgeometry_cast< const QgsLineString * >( &curve ) )
{
lineString = ls;
}
else
{
segmentized.reset( qgsgeometry_cast< QgsLineString * >( curve.segmentize( ) ) );
lineString = segmentized.get();
}
QgsClipper::clipped3dLine( lineString->xVector(), lineString->yVector(), lineString->zVector(), pointsX, pointsY, pointsZ, clipRect );
}
else
{
// clone...
if ( const QgsLineString *ls = qgsgeometry_cast<const QgsLineString *>( &curve ) )
{
pointsX = ls->xVector();
pointsY = ls->yVector();
pointsZ = ls->zVector();
}
else
{
std::unique_ptr< QgsLineString > segmentized;
segmentized.reset( qgsgeometry_cast< QgsLineString * >( curve.segmentize( ) ) );
pointsX = segmentized->xVector();
pointsY = segmentized->yVector();
pointsZ = segmentized->zVector();
}
}
// transform the points to screen coordinates
const QVector< double > preTransformPointsZ = pointsZ;
bool wasTransformed = false;
if ( ct.isValid() )
{
//create x, y arrays
const int nVertices = pointsX.size();
wasTransformed = true;
try
{
ct.transformCoords( nVertices, pointsX.data(), pointsY.data(), pointsZ.data(), Qgis::TransformDirection::Forward );
}
catch ( QgsCsException & )
{
// we don't abort the rendering here, instead we remove any invalid points and just plot those which ARE valid
}
}
// remove non-finite points, e.g. infinite or NaN points caused by reprojecting errors
{
const int size = pointsX.size();
const double *xIn = pointsX.data();
const double *yIn = pointsY.data();
const double *zIn = pointsZ.data();
const double *preTransformZIn = wasTransformed ? preTransformPointsZ.constData() : nullptr;
double *xOut = pointsX.data();
double *yOut = pointsY.data();
double *zOut = pointsZ.data();
int outSize = 0;
for ( int i = 0; i < size; ++i )
{
bool pointOk = std::isfinite( *xIn ) && std::isfinite( *yIn );
// skip z points which have been made non-finite during transformations only. Ie if:
// - we did no transformation, then always render even if non-finite z
// - we did transformation and z is finite then render
// - we did transformation and z is non-finite BUT input z was also non finite then render
// - we did transformation and z is non-finite AND input z WAS finite then skip
pointOk &= !wasTransformed || std::isfinite( *zIn ) || !std::isfinite( *preTransformZIn );
if ( pointOk )
{
*xOut++ = *xIn++;
*yOut++ = *yIn++;
*zOut++ = *zIn++;
outSize++;
}
else
{
xIn++;
yIn++;
zIn++;
}
if ( preTransformZIn )
preTransformZIn++;
}
pointsX.resize( outSize );
pointsY.resize( outSize );
pointsZ.resize( outSize );
}
if ( clipToExtent && nPoints > 1 && context.flags() & Qgis::RenderContextFlag::ApplyClipAfterReprojection )
{
// early clipping was not possible, so we have to apply it here after transformation
const QgsRectangle e = context.mapExtent();
const double cw = e.width() / 10;
const double ch = e.height() / 10;
const QgsBox3D clipRect( e.xMinimum() - cw, e.yMinimum() - ch, -HUGE_VAL, e.xMaximum() + cw, e.yMaximum() + ch, HUGE_VAL ); // TODO also need to be clipped according to z axis
QVector< double > tempX;
QVector< double > tempY;
QVector< double > tempZ;
QgsClipper::clipped3dLine( pointsX, pointsY, pointsZ, tempX, tempY, tempZ, clipRect );
pointsX = tempX;
pointsY = tempY;
pointsZ = tempZ;
}
const int polygonSize = pointsX.size();
QPolygonF out( polygonSize );
const double *x = pointsX.constData();
const double *y = pointsY.constData();
QPointF *dest = out.data();
for ( int i = 0; i < polygonSize; ++i )
{
double screenX = *x++;
double screenY = *y++;
mtp.transformInPlace( screenX, screenY );
*dest++ = QPointF( screenX, screenY );
}
return out;
}
QPolygonF QgsSymbol::_getLineString2d( QgsRenderContext &context, const QgsCurve &curve, bool clipToExtent )
{
const unsigned int nPoints = curve.numPoints();
QgsCoordinateTransform ct = context.coordinateTransform();
const QgsMapToPixel &mtp = context.mapToPixel();
QPolygonF pts;
// apply clipping for large lines to achieve a better rendering performance
if ( clipToExtent && nPoints > 1 && !( context.flags() & Qgis::RenderContextFlag::ApplyClipAfterReprojection ) )
{
const QgsRectangle e = context.extent();
const double cw = e.width() / 10;
const double ch = e.height() / 10;
const QgsRectangle clipRect( e.xMinimum() - cw, e.yMinimum() - ch, e.xMaximum() + cw, e.yMaximum() + ch );
pts = QgsClipper::clippedLine( curve, clipRect );
}
else
{
pts = curve.asQPolygonF();
}
// transform the QPolygonF to screen coordinates
if ( ct.isValid() )
{
try
{
ct.transformPolygon( pts );
}
catch ( QgsCsException & )
{
// we don't abort the rendering here, instead we remove any invalid points and just plot those which ARE valid
}
}
// remove non-finite points, e.g. infinite or NaN points caused by reprojecting errors
pts.erase( std::remove_if( pts.begin(), pts.end(),
[]( const QPointF point )
{
return !std::isfinite( point.x() ) || !std::isfinite( point.y() );
} ), pts.end() );
if ( clipToExtent && nPoints > 1 && context.flags() & Qgis::RenderContextFlag::ApplyClipAfterReprojection )
{
// early clipping was not possible, so we have to apply it here after transformation
const QgsRectangle e = context.mapExtent();
const double cw = e.width() / 10;
const double ch = e.height() / 10;
const QgsRectangle clipRect( e.xMinimum() - cw, e.yMinimum() - ch, e.xMaximum() + cw, e.yMaximum() + ch );
pts = QgsClipper::clippedLine( pts, clipRect );
}
QPointF *ptr = pts.data();
for ( int i = 0; i < pts.size(); ++i, ++ptr )
{
mtp.transformInPlace( ptr->rx(), ptr->ry() );
}
return pts;
}
QPolygonF QgsSymbol::_getPolygonRing( QgsRenderContext &context, const QgsCurve &curve, const bool clipToExtent, const bool isExteriorRing, const bool correctRingOrientation )
{
if ( curve.is3D() )
return _getPolygonRing3d( context, curve, clipToExtent, isExteriorRing, correctRingOrientation );
else
return _getPolygonRing2d( context, curve, clipToExtent, isExteriorRing, correctRingOrientation );
}
QPolygonF QgsSymbol::_getPolygonRing3d( QgsRenderContext &context, const QgsCurve &curve, const bool clipToExtent, const bool isExteriorRing, const bool correctRingOrientation )
{
const QgsCoordinateTransform ct = context.coordinateTransform();
const QgsMapToPixel &mtp = context.mapToPixel();
QVector< double > pointsX;
QVector< double > pointsY;
QVector< double > pointsZ;
if ( curve.numPoints() < 1 )
return QPolygonF();
bool reverseRing = false;
if ( correctRingOrientation )
{
// ensure consistent polygon ring orientation
if ( ( isExteriorRing && curve.orientation() != Qgis::AngularDirection::Clockwise ) || ( !isExteriorRing && curve.orientation() != Qgis::AngularDirection::CounterClockwise ) )
{
reverseRing = true;
}
}
//clip close to view extent, if needed
if ( clipToExtent && !( context.flags() & Qgis::RenderContextFlag::ApplyClipAfterReprojection ) && !context.extent().contains( curve.boundingBox() ) )
{
const QgsRectangle e = context.extent();
const double cw = e.width() / 10;
const double ch = e.height() / 10;
const QgsBox3D clipRect( e.xMinimum() - cw, e.yMinimum() - ch, -HUGE_VAL, e.xMaximum() + cw, e.yMaximum() + ch, HUGE_VAL ); // TODO also need to be clipped according to z axis
const QgsLineString *lineString = nullptr;
std::unique_ptr< QgsLineString > segmentized;
if ( const QgsLineString *ls = qgsgeometry_cast< const QgsLineString * >( &curve ) )
{
lineString = ls;
}
else
{
segmentized.reset( qgsgeometry_cast< QgsLineString * >( curve.segmentize( ) ) );
lineString = segmentized.get();
}
pointsX = lineString->xVector();
pointsY = lineString->yVector();
pointsZ = lineString->zVector();
QgsClipper::trimPolygon( pointsX, pointsY, pointsZ, clipRect );
}
else
{
// clone...
if ( const QgsLineString *ls = qgsgeometry_cast<const QgsLineString *>( &curve ) )
{
pointsX = ls->xVector();
pointsY = ls->yVector();
pointsZ = ls->zVector();
}
else
{
std::unique_ptr< QgsLineString > segmentized;
segmentized.reset( qgsgeometry_cast< QgsLineString * >( curve.segmentize( ) ) );
pointsX = segmentized->xVector();
pointsY = segmentized->yVector();
pointsZ = segmentized->zVector();
}
}
if ( reverseRing )
{
std::reverse( pointsX.begin(), pointsX.end() );
std::reverse( pointsY.begin(), pointsY.end() );
std::reverse( pointsZ.begin(), pointsZ.end() );
}
//transform the QPolygonF to screen coordinates
const QVector< double > preTransformPointsZ = pointsZ;
bool wasTransformed = false;
if ( ct.isValid() )
{
const int nVertices = pointsX.size();
wasTransformed = true;
try
{
ct.transformCoords( nVertices, pointsX.data(), pointsY.data(), pointsZ.data(), Qgis::TransformDirection::Forward );
}
catch ( QgsCsException & )
{
// we don't abort the rendering here, instead we remove any invalid points and just plot those which ARE valid
}
}
// remove non-finite points, e.g. infinite or NaN points caused by reprojecting errors
{
const int size = pointsX.size();
const double *xIn = pointsX.data();
const double *yIn = pointsY.data();
const double *zIn = pointsZ.data();
const double *preTransformZIn = wasTransformed ? preTransformPointsZ.constData() : nullptr;
double *xOut = pointsX.data();
double *yOut = pointsY.data();
double *zOut = pointsZ.data();
int outSize = 0;
for ( int i = 0; i < size; ++i )
{
bool pointOk = std::isfinite( *xIn ) && std::isfinite( *yIn );
// skip z points which have been made non-finite during transformations only. Ie if:
// - we did no transformation, then always render even if non-finite z
// - we did transformation and z is finite then render
// - we did transformation and z is non-finite BUT input z was also non finite then render
// - we did transformation and z is non-finite AND input z WAS finite then skip
pointOk &= !wasTransformed || std::isfinite( *zIn ) || !std::isfinite( *preTransformZIn );
if ( pointOk )
{
*xOut++ = *xIn++;
*yOut++ = *yIn++;
*zOut++ = *zIn++;
outSize++;
}
else
{
xIn++;
yIn++;
zIn++;
}
if ( preTransformZIn )
preTransformZIn++;
}
pointsX.resize( outSize );
pointsY.resize( outSize );
pointsZ.resize( outSize );
}
if ( clipToExtent && context.flags() & Qgis::RenderContextFlag::ApplyClipAfterReprojection && !context.mapExtent().contains( curve.boundingBox() ) )
{
// early clipping was not possible, so we have to apply it here after transformation
const QgsRectangle e = context.mapExtent();
const double cw = e.width() / 10;
const double ch = e.height() / 10;
const QgsBox3D clipRect( e.xMinimum() - cw, e.yMinimum() - ch, -HUGE_VAL, e.xMaximum() + cw, e.yMaximum() + ch, HUGE_VAL ); // TODO also need to be clipped according to z axis
QgsClipper::trimPolygon( pointsX, pointsY, pointsZ, clipRect );
}
const int polygonSize = pointsX.size();
QPolygonF out( polygonSize );
const double *x = pointsX.constData();
const double *y = pointsY.constData();
QPointF *dest = out.data();
for ( int i = 0; i < polygonSize; ++i )
{
double screenX = *x++;
double screenY = *y++;
mtp.transformInPlace( screenX, screenY );
*dest++ = QPointF( screenX, screenY );
}
if ( !out.empty() && !out.isClosed() )
out << out.at( 0 );
return out;
}
QPolygonF QgsSymbol::_getPolygonRing2d( QgsRenderContext &context, const QgsCurve &curve, const bool clipToExtent, const bool isExteriorRing, const bool correctRingOrientation )
{
const QgsCoordinateTransform ct = context.coordinateTransform();
const QgsMapToPixel &mtp = context.mapToPixel();
QPolygonF poly = curve.asQPolygonF();
if ( curve.numPoints() < 1 )
return QPolygonF();
if ( correctRingOrientation )
{
// ensure consistent polygon ring orientation
if ( isExteriorRing && curve.orientation() != Qgis::AngularDirection::Clockwise )
std::reverse( poly.begin(), poly.end() );
else if ( !isExteriorRing && curve.orientation() != Qgis::AngularDirection::CounterClockwise )
std::reverse( poly.begin(), poly.end() );
}
//clip close to view extent, if needed
if ( clipToExtent && !( context.flags() & Qgis::RenderContextFlag::ApplyClipAfterReprojection ) && !context.extent().contains( poly.boundingRect() ) )
{
const QgsRectangle e = context.extent();
const double cw = e.width() / 10;
const double ch = e.height() / 10;
const QgsRectangle clipRect( e.xMinimum() - cw, e.yMinimum() - ch, e.xMaximum() + cw, e.yMaximum() + ch );
QgsClipper::trimPolygon( poly, clipRect );
}
//transform the QPolygonF to screen coordinates
if ( ct.isValid() )
{
try
{
ct.transformPolygon( poly );
}
catch ( QgsCsException & )
{
// we don't abort the rendering here, instead we remove any invalid points and just plot those which ARE valid
}
}
// remove non-finite points, e.g. infinite or NaN points caused by reprojecting errors
poly.erase( std::remove_if( poly.begin(), poly.end(),
[]( const QPointF point )
{
return !std::isfinite( point.x() ) || !std::isfinite( point.y() );
} ), poly.end() );
if ( clipToExtent && context.flags() & Qgis::RenderContextFlag::ApplyClipAfterReprojection && !context.mapExtent().contains( poly.boundingRect() ) )
{
// early clipping was not possible, so we have to apply it here after transformation
const QgsRectangle e = context.mapExtent();
const double cw = e.width() / 10;
const double ch = e.height() / 10;
const QgsRectangle clipRect( e.xMinimum() - cw, e.yMinimum() - ch, e.xMaximum() + cw, e.yMaximum() + ch );
QgsClipper::trimPolygon( poly, clipRect );
}
QPointF *ptr = poly.data();
for ( int i = 0; i < poly.size(); ++i, ++ptr )
{
mtp.transformInPlace( ptr->rx(), ptr->ry() );
}
if ( !poly.empty() && !poly.isClosed() )
poly << poly.at( 0 );
return poly;
}
void QgsSymbol::_getPolygon( QPolygonF &pts, QVector<QPolygonF> &holes, QgsRenderContext &context, const QgsPolygon &polygon, const bool clipToExtent, const bool correctRingOrientation )
{
holes.clear();
pts = _getPolygonRing( context, *polygon.exteriorRing(), clipToExtent, true, correctRingOrientation );
const int ringCount = polygon.numInteriorRings();
holes.reserve( ringCount );
for ( int idx = 0; idx < ringCount; idx++ )
{
const QPolygonF hole = _getPolygonRing( context, *( polygon.interiorRing( idx ) ), clipToExtent, false, correctRingOrientation );
if ( !hole.isEmpty() )
holes.append( hole );
}
}
QString QgsSymbol::symbolTypeToString( Qgis::SymbolType type )
{
switch ( type )
{
case Qgis::SymbolType::Marker:
return QObject::tr( "Marker" );
case Qgis::SymbolType::Line:
return QObject::tr( "Line" );
case Qgis::SymbolType::Fill:
return QObject::tr( "Fill" );
case Qgis::SymbolType::Hybrid:
return QObject::tr( "Hybrid" );
}
return QString();
}
Qgis::SymbolType QgsSymbol::symbolTypeForGeometryType( Qgis::GeometryType type )
{
switch ( type )
{
case Qgis::GeometryType::Point:
return Qgis::SymbolType::Marker;
case Qgis::GeometryType::Line:
return Qgis::SymbolType::Line;
case Qgis::GeometryType::Polygon:
return Qgis::SymbolType::Fill;
case Qgis::GeometryType::Unknown:
case Qgis::GeometryType::Null:
return Qgis::SymbolType::Hybrid;
}
return Qgis::SymbolType::Hybrid;
}
const QgsPropertiesDefinition &QgsSymbol::propertyDefinitions()
{
QgsSymbol::initPropertyDefinitions();
return sPropertyDefinitions;
}
QgsSymbol::~QgsSymbol()
{
// delete all symbol layers (we own them, so it's okay)
qDeleteAll( mLayers );
}
Qgis::RenderUnit QgsSymbol::outputUnit() const
{
if ( mLayers.empty() )
{
return Qgis::RenderUnit::Unknown;
}
QgsSymbolLayerList::const_iterator it = mLayers.constBegin();
Qgis::RenderUnit unit = ( *it )->outputUnit();
for ( ; it != mLayers.constEnd(); ++it )
{
if ( ( *it )->outputUnit() != unit )
{
return Qgis::RenderUnit::Unknown;
}
}
return unit;
}
bool QgsSymbol::usesMapUnits() const
{
if ( mLayers.empty() )
{
return false;
}
for ( const QgsSymbolLayer *layer : mLayers )
{
if ( layer->usesMapUnits() )
{
return true;
}
}
return false;
}
QgsMapUnitScale QgsSymbol::mapUnitScale() const
{
if ( mLayers.empty() )
{
return QgsMapUnitScale();
}
QgsSymbolLayerList::const_iterator it = mLayers.constBegin();
if ( it == mLayers.constEnd() )
return QgsMapUnitScale();
QgsMapUnitScale scale = ( *it )->mapUnitScale();
++it;
for ( ; it != mLayers.constEnd(); ++it )
{
if ( ( *it )->mapUnitScale() != scale )
{
return QgsMapUnitScale();
}
}
return scale;
}
void QgsSymbol::setOutputUnit( Qgis::RenderUnit u ) const
{
const auto constMLayers = mLayers;
for ( QgsSymbolLayer *layer : constMLayers )
{
layer->setOutputUnit( u );
}
}
void QgsSymbol::setMapUnitScale( const QgsMapUnitScale &scale ) const
{
const auto constMLayers = mLayers;
for ( QgsSymbolLayer *layer : constMLayers )
{
layer->setMapUnitScale( scale );
}
}
QgsSymbolBufferSettings *QgsSymbol::bufferSettings()
{
return mBufferSettings.get();
}
const QgsSymbolBufferSettings *QgsSymbol::bufferSettings() const
{
return mBufferSettings.get();
}
void QgsSymbol::setBufferSettings( QgsSymbolBufferSettings *settings )
{
if ( mBufferSettings.get() == settings )
return;
mBufferSettings.reset( settings );
}
QgsSymbolAnimationSettings &QgsSymbol::animationSettings()
{
return mAnimationSettings;
}
const QgsSymbolAnimationSettings &QgsSymbol::animationSettings() const
{
return mAnimationSettings;
}
void QgsSymbol::setAnimationSettings( const QgsSymbolAnimationSettings &settings )
{
mAnimationSettings = settings;
}
QgsSymbol *QgsSymbol::defaultSymbol( Qgis::GeometryType geomType )
{
std::unique_ptr< QgsSymbol > s;
// override global default if project has a default for this type
switch ( geomType )
{
case Qgis::GeometryType::Point:
s.reset( QgsProject::instance()->styleSettings()->defaultSymbol( Qgis::SymbolType::Marker ) ); // skip-keyword-check
break;
case Qgis::GeometryType::Line:
s.reset( QgsProject::instance()->styleSettings()->defaultSymbol( Qgis::SymbolType::Line ) ); // skip-keyword-check
break;
case Qgis::GeometryType::Polygon:
s.reset( QgsProject::instance()->styleSettings()->defaultSymbol( Qgis::SymbolType::Fill ) ); // skip-keyword-check
break;
default:
break;
}
// if no default found for this type, get global default (as previously)
if ( !s )
{
switch ( geomType )
{
case Qgis::GeometryType::Point:
s = std::make_unique< QgsMarkerSymbol >();
break;
case Qgis::GeometryType::Line:
s = std::make_unique< QgsLineSymbol >();
break;
case Qgis::GeometryType::Polygon:
s = std::make_unique< QgsFillSymbol >();
break;
default:
QgsDebugError( QStringLiteral( "unknown layer's geometry type" ) );
break;
}
}
if ( !s )
return nullptr;
// set opacity
s->setOpacity( QgsProject::instance()->styleSettings()->defaultSymbolOpacity() ); // skip-keyword-check
// set random color, it project prefs allow
if ( QgsProject::instance()->styleSettings()->randomizeDefaultSymbolColor() ) // skip-keyword-check
{
s->setColor( QgsApplication::colorSchemeRegistry()->fetchRandomStyleColor() );
}
const bool isCmyk = QgsProject::instance()->styleSettings() && QgsProject::instance()->styleSettings()->colorModel() == Qgis::ColorModel::Cmyk; // skip-keyword-check
if ( s->color().spec() == QColor::Spec::Rgb && isCmyk )
{
s->setColor( s->color().toCmyk() );
}
else if ( s->color().spec() == QColor::Spec::Cmyk && !isCmyk )
{
s->setColor( s->color().toRgb() );
}
return s.release();
}
QgsSymbolLayer *QgsSymbol::symbolLayer( int layer )
{
return mLayers.value( layer );
}
const QgsSymbolLayer *QgsSymbol::symbolLayer( int layer ) const
{
return mLayers.value( layer );
}
bool QgsSymbol::insertSymbolLayer( int index, QgsSymbolLayer *layer )
{
if ( index < 0 || index > mLayers.count() ) // can be added also after the last index
return false;
if ( !layer || !layer->isCompatibleWithSymbol( this ) )
return false;
mLayers.insert( index, layer );
return true;
}
bool QgsSymbol::appendSymbolLayer( QgsSymbolLayer *layer )
{
if ( !layer || !layer->isCompatibleWithSymbol( this ) )
return false;
mLayers.append( layer );
return true;
}
bool QgsSymbol::deleteSymbolLayer( int index )
{
if ( index < 0 || index >= mLayers.count() )
return false;
delete mLayers.at( index );
mLayers.removeAt( index );
return true;
}
QgsSymbolLayer *QgsSymbol::takeSymbolLayer( int index )
{
if ( index < 0 || index >= mLayers.count() )
return nullptr;
return mLayers.takeAt( index );
}
bool QgsSymbol::changeSymbolLayer( int index, QgsSymbolLayer *layer )
{
QgsSymbolLayer *oldLayer = mLayers.value( index );
if ( oldLayer == layer )
return false;
if ( !layer || !layer->isCompatibleWithSymbol( this ) )
return false;
delete oldLayer; // first delete the original layer
mLayers[index] = layer; // set new layer
return true;
}
void QgsSymbol::startRender( QgsRenderContext &context, const QgsFields &fields )
{
Q_ASSERT_X( !mStarted, "startRender", "Rendering has already been started for this symbol instance!" );
mStarted = true;
const Qgis::SymbolRenderHints renderHints = QgsSymbol::renderHints();
mSymbolRenderContext.reset( new QgsSymbolRenderContext( context, Qgis::RenderUnit::Unknown, mOpacity, false, renderHints, nullptr, fields ) );
// Why do we need a copy here ? Is it to make sure the symbol layer rendering does not mess with the symbol render context ?
// Or is there another profound reason ?
QgsSymbolRenderContext symbolContext( context, Qgis::RenderUnit::Unknown, mOpacity, false, renderHints, nullptr, fields );
std::unique_ptr< QgsExpressionContextScope > scope( QgsExpressionContextUtils::updateSymbolScope( this, new QgsExpressionContextScope() ) );
if ( mAnimationSettings.isAnimated() )
{
const long long mapFrameNumber = context.currentFrame();
double animationTimeSeconds = 0;
if ( mapFrameNumber >= 0 && context.frameRate() > 0 )
{
// render is part of an animation, so we base the calculated frame on that
animationTimeSeconds = mapFrameNumber / context.frameRate();
}
else
{
// render is outside of animation, so base the calculated frame on the current epoch
animationTimeSeconds = QDateTime::currentMSecsSinceEpoch() / 1000.0;
}
const long long symbolFrame = static_cast< long long >( std::floor( animationTimeSeconds * mAnimationSettings.frameRate() ) );
scope->setVariable( QStringLiteral( "symbol_frame" ), symbolFrame, true );
}
mSymbolRenderContext->setExpressionContextScope( scope.release() );
mDataDefinedProperties.prepare( context.expressionContext() );
if ( mBufferSettings && mBufferSettings->enabled() && mBufferSettings->fillSymbol() )
{
mBufferSettings->fillSymbol()->startRender( context, fields );
}
for ( QgsSymbolLayer *layer : std::as_const( mLayers ) )
{
if ( !layer->enabled() || !context.isSymbolLayerEnabled( layer ) )
continue;
layer->prepareExpressions( symbolContext );