QGIS API Documentation 4.0.0-Norrköping (1ddcee3d0e4)
Loading...
Searching...
No Matches
qgsinterpolatedlinerenderer.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsinterpolatedlinerenderer.cpp
3 --------------------------------------
4 Date : April 2020
5 Copyright : (C) 2020 by Vincent Cloarec
6 Email : vcloarec at gmail dot com
7 ***************************************************************************
8 * *
9 * This program is free software; you can redistribute it and/or modify *
10 * it under the terms of the GNU General Public License as published by *
11 * the Free Software Foundation; either version 2 of the License, or *
12 * (at your option) any later version. *
13 * *
14 ***************************************************************************/
15
17
18#include <memory>
19
20#include "qgscolorutils.h"
22#include "qgsstyle.h"
23#include "qgssymbollayerutils.h"
24#include "qgsunittypes.h"
25
26#include <QPainter>
27#include <QString>
28
29using namespace Qt::StringLiterals;
30
32{
33 mStrokeWidth = strokeWidth;
34}
35
40
42{
43 mStrokeColoring = strokeColoring;
44}
45
50
52{
53 mStrokeWidthUnit = strokeWidthUnit;
54}
55
57{
58 return mStrokeWidthUnit;
59}
60
61void QgsInterpolatedLineRenderer::renderInDeviceCoordinates( double valueColor1, double valueColor2, double valueWidth1, double valueWidth2, QPointF p1, QPointF p2, QgsRenderContext &context ) const
62{
63 QPainter *painter = context.painter();
64 QgsScopedQPainterState painterState( painter );
65 context.setPainterFlagsUsingContext( painter );
66
67 QPointF dir = p2 - p1;
68 double length = sqrt( pow( dir.x(), 2 ) + pow( dir.y(), 2 ) );
69 QPointF diru = dir / length;
70 QPointF orthu = QPointF( -diru.y(), diru.x() );
71
72 QList<double> breakValues;
73 QList<QColor> breakColors;
74 QList<QLinearGradient> gradients;
75
76 mStrokeColoring.graduatedColors( valueColor1, valueColor2, breakValues, breakColors, gradients );
77 QColor selectedColor = context.selectionColor();
78
79 if ( gradients.isEmpty() && !breakValues.empty() && !breakColors.isEmpty() ) //exact colors to render
80 {
81 Q_ASSERT( breakColors.count() == breakValues.count() );
82 for ( int i = 0; i < breakValues.count(); ++i )
83 {
84 const bool widthIsInverted { valueWidth1 > valueWidth2 };
85 const double value = breakValues.at( i );
86 const double width = context.convertToPainterUnits( mStrokeWidth.strokeWidth( widthIsInverted ? mStrokeWidth.maximumValue() - value : value ), mStrokeWidthUnit );
87 QPen pen( mSelected ? selectedColor : breakColors.at( i ) );
88 pen.setWidthF( width );
89 pen.setCapStyle( Qt::PenCapStyle::RoundCap );
90 painter->setPen( pen );
91 const QPointF point = p1 + dir * ( value - valueColor1 ) / ( valueColor2 - valueColor1 );
92 painter->drawPoint( point );
93 }
94 }
95 else
96 {
97 double width1 = mStrokeWidth.strokeWidth( valueWidth1 );
98 double width2 = mStrokeWidth.strokeWidth( valueWidth2 );
99
100 if ( !std::isnan( width1 ) && !std::isnan( width2 ) ) // the two widths on extremity are not out of range and ignored
101 {
102 //Draw line cap
103 QBrush brush( Qt::SolidPattern );
104 QPen pen;
105 int startAngle;
106 startAngle = ( acos( -orthu.x() ) / M_PI ) * 180;
107 if ( orthu.y() < 0 )
108 startAngle = 360 - startAngle;
109
110 bool outOfRange1 = std::isnan( width1 );
111 bool outOfRange2 = std::isnan( width2 );
112
113 if ( !outOfRange1 )
114 {
115 width1 = context.convertToPainterUnits( width1, mStrokeWidthUnit );
116 QRectF capBox1( p1.x() - width1 / 2, p1.y() - width1 / 2, width1, width1 );
117 brush.setColor( mSelected ? selectedColor : mStrokeColoring.color( valueColor1 ) );
118 painter->setBrush( brush );
119 pen.setBrush( brush );
120 painter->setPen( pen );
121 painter->drawPie( capBox1, ( startAngle - 1 ) * 16, 182 * 16 );
122 }
123
124 if ( !outOfRange2 )
125 {
126 width2 = context.convertToPainterUnits( width2, mStrokeWidthUnit );
127 QRectF capBox2( p2.x() - width2 / 2, p2.y() - width2 / 2, width2, width2 );
128 brush.setColor( mSelected ? selectedColor : mStrokeColoring.color( valueColor2 ) );
129 pen.setBrush( brush );
130 painter->setBrush( brush );
131 painter->setPen( pen );
132 painter->drawPie( capBox2, ( startAngle + 179 ) * 16, 182 * 16 );
133 }
134
135 if ( ( gradients.isEmpty() && breakValues.empty() && breakColors.count() == 1 ) || mSelected ) //only one color to render
136 {
137 double startAdjusting = 0;
138 if ( outOfRange1 )
139 adjustLine( valueColor1, valueColor1, valueColor2, width1, startAdjusting );
140
141
142 double endAdjusting = 0;
143 if ( outOfRange2 )
144 adjustLine( valueColor2, valueColor1, valueColor2, width2, endAdjusting );
145
146 QPointF pointStartAdjusted = p1 + dir * startAdjusting;
147 QPointF pointEndAdjusted = p2 - dir * endAdjusting;
148
149 QPolygonF varLine;
150 double semiWidth1 = width1 / 2;
151 double semiWidth2 = width2 / 2;
152
153 varLine.append( pointStartAdjusted + orthu * semiWidth1 );
154 varLine.append( pointEndAdjusted + orthu * semiWidth2 );
155 varLine.append( pointEndAdjusted - orthu * semiWidth2 );
156 varLine.append( pointStartAdjusted - orthu * semiWidth1 );
157
158 QBrush brush( Qt::SolidPattern );
159 brush.setColor( mSelected ? selectedColor : breakColors.first() );
160 painter->setBrush( brush );
161 painter->setPen( pen );
162
163 QPen pen;
164 pen.setBrush( brush );
165 pen.setWidthF( 0 );
166 painter->setPen( pen );
167
168 painter->drawPolygon( varLine );
169 }
170 else if ( !gradients.isEmpty() && !breakValues.isEmpty() && !breakColors.isEmpty() )
171 {
172 Q_ASSERT( breakColors.count() == breakValues.count() );
173 Q_ASSERT( breakColors.count() == gradients.count() + 1 );
174 double widthColorVariationValueRatio = ( valueWidth2 - valueWidth1 ) / ( valueColor2 - valueColor1 );
175
176 for ( int i = 0; i < gradients.count(); ++i )
177 {
178 double firstValue = breakValues.at( i );
179 double secondValue = breakValues.at( i + 1 );
180 double w1 = mStrokeWidth.strokeWidth( widthColorVariationValueRatio * ( firstValue - valueColor1 ) + valueWidth1 );
181 double w2 = mStrokeWidth.strokeWidth( widthColorVariationValueRatio * ( secondValue - valueColor1 ) + valueWidth1 );
182
183 if ( std::isnan( w1 ) && std::isnan( w2 ) )
184 continue;
185
186 double firstAdjusting = 0;
187 if ( std::isnan( w1 ) )
188 adjustLine( firstValue, valueColor1, valueColor2, w1, firstAdjusting );
189
190
191 double secondAdjusting = 0;
192 if ( std::isnan( w2 ) )
193 adjustLine( secondValue, valueColor1, valueColor2, w2, secondAdjusting );
194
195 w1 = context.convertToPainterUnits( w1, mStrokeWidthUnit );
196 w2 = context.convertToPainterUnits( w2, mStrokeWidthUnit );
197
198 QPointF pointStart = p1 + dir * ( firstValue - valueColor1 ) / ( valueColor2 - valueColor1 );
199 QPointF pointEnd = p1 + dir * ( secondValue - valueColor1 ) / ( valueColor2 - valueColor1 );
200
201 QPointF pointStartAdjusted = pointStart + dir * firstAdjusting;
202 QPointF pointEndAdjusted = pointEnd - dir * secondAdjusting;
203
204 QPolygonF varLine;
205 double sw1 = w1 / 2;
206 double sw2 = w2 / 2;
207
208 varLine.append( pointStartAdjusted + orthu * sw1 );
209 varLine.append( pointEndAdjusted + orthu * sw2 );
210 varLine.append( pointEndAdjusted - orthu * sw2 );
211 varLine.append( pointStartAdjusted - orthu * sw1 );
212
213 QLinearGradient gradient = gradients.at( i );
214 gradient.setStart( pointStart );
215 gradient.setFinalStop( pointEnd );
216 QBrush brush( gradient );
217 painter->setBrush( brush );
218
219 QPen pen;
220 pen.setBrush( brush );
221 pen.setWidthF( 0 );
222 painter->setPen( pen );
223
224 painter->drawPolygon( varLine );
225 }
226 }
227 }
228 }
229}
230
231
232void QgsInterpolatedLineRenderer::render( double value1, double value2, const QgsPointXY &pt1, const QgsPointXY &pt2, QgsRenderContext &context ) const
233{
234 const QgsMapToPixel &mapToPixel = context.mapToPixel();
235
236 QgsPointXY point1 = pt1;
237 QgsPointXY point2 = pt2;
238
239 if ( value1 > value2 )
240 {
241 std::swap( value1, value2 );
242 std::swap( point1, point2 );
243 }
244
245 QPointF p1 = mapToPixel.transform( point1 ).toQPointF();
246 QPointF p2 = mapToPixel.transform( point2 ).toQPointF();
247
248 renderInDeviceCoordinates( value1, value2, value1, value2, p1, p2, context );
249}
250
251void QgsInterpolatedLineRenderer::render( double valueColor1, double valueColor2, double valueWidth1, double valueWidth2, const QgsPointXY &pt1, const QgsPointXY &pt2, QgsRenderContext &context ) const
252{
253 const QgsMapToPixel &mapToPixel = context.mapToPixel();
254
255 QgsPointXY point1 = pt1;
256 QgsPointXY point2 = pt2;
257
258 if ( valueColor1 > valueColor2 )
259 {
260 std::swap( valueColor1, valueColor2 );
261 std::swap( valueWidth1, valueWidth2 );
262 std::swap( point1, point2 );
263 }
264
265 QPointF p1 = mapToPixel.transform( point1 ).toQPointF();
266 QPointF p2 = mapToPixel.transform( point2 ).toQPointF();
267
268 renderInDeviceCoordinates( valueColor1, valueColor2, valueWidth1, valueWidth2, p1, p2, context );
269}
270
272{
273 mSelected = selected;
274}
275
276void QgsInterpolatedLineRenderer::adjustLine( const double value, const double value1, const double value2, double &width, double &adjusting ) const
277{
278 if ( value > mStrokeWidth.maximumValue() )
279 {
280 adjusting = fabs( ( value - mStrokeWidth.maximumValue() ) / ( value2 - value1 ) );
281 width = mStrokeWidth.maximumWidth();
282 }
283 else
284 {
285 adjusting = fabs( ( value - mStrokeWidth.minimumValue() ) / ( value2 - value1 ) );
286 width = mStrokeWidth.minimumWidth();
287 }
288}
289
291{
292 return mMinimumValue;
293}
294
296{
297 mMinimumValue = minimumValue;
298 mNeedUpdateFormula = true;
299}
300
302{
303 return mMaximumValue;
304}
305
307{
308 mMaximumValue = maximumValue;
309 mNeedUpdateFormula = true;
310}
311
313{
314 return mMinimumWidth;
315}
316
318{
319 mMinimumWidth = minimumWidth;
320 mNeedUpdateFormula = true;
321}
322
324{
325 return mMaximumWidth;
326}
327
329{
330 mMaximumWidth = maximumWidth;
331 mNeedUpdateFormula = true;
332}
333
334double QgsInterpolatedLineWidth::strokeWidth( double value ) const
335{
336 if ( mIsWidthVariable )
337 {
338 if ( mNeedUpdateFormula )
339 updateLinearFormula();
340
341 if ( mUseAbsoluteValue )
342 value = std::fabs( value );
343
344 if ( value > mMaximumValue )
345 {
346 if ( mIgnoreOutOfRange )
347 return std::numeric_limits<double>::quiet_NaN();
348 else
349 return mMaximumWidth;
350 }
351
352 if ( value < mMinimumValue )
353 {
354 if ( mIgnoreOutOfRange )
355 return std::numeric_limits<double>::quiet_NaN();
356 else
357 return mMinimumWidth;
358 }
359
360 return ( value - mMinimumValue ) * mLinearCoef + mMinimumWidth;
361 }
362 else
363 return fixedStrokeWidth();
364}
365
366QDomElement QgsInterpolatedLineWidth::writeXml( QDomDocument &doc, const QgsReadWriteContext & ) const
367{
368 QDomElement elem = doc.createElement( u"mesh-stroke-width"_s );
369
370 elem.setAttribute( u"width-varying"_s, mIsWidthVariable ? 1 : 0 );
371 elem.setAttribute( u"fixed-width"_s, mFixedWidth );
372 elem.setAttribute( u"minimum-value"_s, mMinimumValue );
373 elem.setAttribute( u"maximum-value"_s, mMaximumValue );
374 elem.setAttribute( u"minimum-width"_s, mMinimumWidth );
375 elem.setAttribute( u"maximum-width"_s, mMaximumWidth );
376 elem.setAttribute( u"ignore-out-of-range"_s, mIgnoreOutOfRange ? 1 : 0 );
377 elem.setAttribute( u"use-absolute-value"_s, mUseAbsoluteValue ? 1 : 0 );
378
379 return elem;
380}
381
382void QgsInterpolatedLineWidth::readXml( const QDomElement &elem, const QgsReadWriteContext & )
383{
384 mIsWidthVariable = elem.attribute( u"width-varying"_s ).toInt();
385 mFixedWidth = elem.attribute( u"fixed-width"_s ).toDouble();
386 mMinimumValue = elem.attribute( u"minimum-value"_s ).toDouble();
387 mMaximumValue = elem.attribute( u"maximum-value"_s ).toDouble();
388 mMinimumWidth = elem.attribute( u"minimum-width"_s ).toDouble();
389 mMaximumWidth = elem.attribute( u"maximum-width"_s ).toDouble();
390 mIgnoreOutOfRange = elem.attribute( u"ignore-out-of-range"_s ).toInt();
391 mUseAbsoluteValue = elem.attribute( u"use-absolute-value"_s ).toInt();
392}
393
395{
396 return mUseAbsoluteValue;
397}
398
403
405{
406 return mFixedWidth;
407}
408
410{
411 return mIgnoreOutOfRange;
412}
413
418
420{
421 return mIsWidthVariable;
422}
423
425{
426 mIsWidthVariable = isWidthVarying;
427}
428
430{
431 mFixedWidth = fixedWidth;
432}
433
434void QgsInterpolatedLineWidth::updateLinearFormula() const
435{
436 if ( !qgsDoubleNear( mMaximumWidth, mMinimumWidth ) )
437 mLinearCoef = ( mMaximumWidth - mMinimumWidth ) / ( mMaximumValue - mMinimumValue );
438 else
439 mLinearCoef = 0;
440 mNeedUpdateFormula = false;
441}
442
444{
445 mColorRampShader.setMinimumValue( std::numeric_limits<double>::quiet_NaN() );
446 mColorRampShader.setMaximumValue( std::numeric_limits<double>::quiet_NaN() );
447}
448
453
455{
456 setColor( color );
457 mColoringMethod = SingleColor;
458 mColorRampShader.setMinimumValue( std::numeric_limits<double>::quiet_NaN() );
459 mColorRampShader.setMaximumValue( std::numeric_limits<double>::quiet_NaN() );
460}
461
463{
464 mColorRampShader = colorRampShader;
465 if ( ( mColorRampShader.sourceColorRamp() ) )
466 mColoringMethod = ColorRamp;
467 else
468 mColoringMethod = SingleColor;
469}
470
472{
473 mSingleColor = color;
474}
475
476QColor QgsInterpolatedLineColor::color( double magnitude ) const
477{
478 QgsColorRamp *lSourceColorRamp = mColorRampShader.sourceColorRamp();
479 if ( mColoringMethod == ColorRamp && lSourceColorRamp )
480 {
481 if ( mColorRampShader.isEmpty() )
482 return lSourceColorRamp->color( 0 );
483
484 int r, g, b, a;
485 if ( mColorRampShader.shade( magnitude, &r, &g, &b, &a ) )
486 return QColor( r, g, b, a );
487 else
488 return QColor( 0, 0, 0, 0 );
489 }
490 else
491 {
492 return mSingleColor;
493 }
494}
495
500
502{
503 return mColorRampShader;
504}
505
507{
508 return mSingleColor;
509}
510
511QDomElement QgsInterpolatedLineColor::writeXml( QDomDocument &doc, const QgsReadWriteContext & ) const
512{
513 QDomElement elem = doc.createElement( u"mesh-stroke-color"_s );
514
515 elem.setAttribute( u"single-color"_s, QgsColorUtils::colorToString( mSingleColor ) );
516 elem.setAttribute( u"coloring-method"_s, mColoringMethod );
517 elem.appendChild( mColorRampShader.writeXml( doc ) );
518
519 return elem;
520}
521
522void QgsInterpolatedLineColor::readXml( const QDomElement &elem, const QgsReadWriteContext & )
523{
524 QDomElement shaderElem = elem.firstChildElement( u"colorrampshader"_s );
525 mColorRampShader.readXml( shaderElem );
526
527 mSingleColor = QgsColorUtils::colorFromString( elem.attribute( u"single-color"_s ) );
528 mColoringMethod = static_cast<QgsInterpolatedLineColor::ColoringMethod>( elem.attribute( u"coloring-method"_s ).toInt() );
529}
530
531void QgsInterpolatedLineColor::graduatedColors( double value1, double value2, QList<double> &breakValues, QList<QColor> &breakColors, QList<QLinearGradient> &gradients ) const
532{
533 breakValues.clear();
534 breakColors.clear();
535 gradients.clear();
536 if ( mColoringMethod == SingleColor )
537 {
538 breakColors.append( mSingleColor );
539 return;
540 }
541
542 switch ( mColorRampShader.colorRampType() )
543 {
545 graduatedColorsInterpolated( value1, value2, breakValues, breakColors, gradients );
546 break;
548 graduatedColorsDiscrete( value1, value2, breakValues, breakColors, gradients );
549 break;
551 graduatedColorsExact( value1, value2, breakValues, breakColors, gradients );
552 break;
553 }
554}
555
560
561QLinearGradient QgsInterpolatedLineColor::makeSimpleLinearGradient( const QColor &color1, const QColor &color2 ) const
562{
563 QLinearGradient gradient;
564 gradient.setColorAt( 0, color1 );
565 gradient.setColorAt( 1, color2 );
566
567 return gradient;
568}
569
570int QgsInterpolatedLineColor::itemColorIndexInf( double value ) const
571{
572 QList<QgsColorRampShader::ColorRampItem> itemList = mColorRampShader.colorRampItemList();
573
574 if ( itemList.isEmpty() || itemList.first().value > value )
575 return -1;
576
577 if ( mColorRampShader.colorRampType() == Qgis::ShaderInterpolationMethod::Discrete )
578 itemList.removeLast(); //remove the inf value
579
580 if ( value > itemList.last().value )
581 return itemList.count() - 1;
582
583 int indSup = itemList.count() - 1;
584 int indInf = 0;
585
586 while ( true )
587 {
588 if ( abs( indSup - indInf ) <= 1 ) //always indSup>indInf, but abs to prevent infinity loop
589 return indInf;
590
591 int newInd = ( indInf + indSup ) / 2;
592
593 if ( std::isnan( itemList.at( newInd ).value ) )
594 return -1;
595
596 if ( itemList.at( newInd ).value <= value )
597 indInf = newInd;
598 else
599 indSup = newInd;
600 }
601}
602
603void QgsInterpolatedLineColor::graduatedColorsExact( double value1, double value2, QList<double> &breakValues, QList<QColor> &breakColors, const QList<QLinearGradient> &gradients ) const
604{
605 Q_ASSERT( mColorRampShader.colorRampType() == Qgis::ShaderInterpolationMethod::Exact );
606 Q_ASSERT( breakValues.isEmpty() );
607 Q_ASSERT( breakColors.isEmpty() );
608 Q_ASSERT( gradients.isEmpty() );
609
610 const QList<QgsColorRampShader::ColorRampItem> &itemList = mColorRampShader.colorRampItemList();
611 if ( itemList.isEmpty() )
612 return;
613
614 int index = itemColorIndexInf( value1 );
615 if ( index < 0 || !qgsDoubleNear( value1, itemList.at( index ).value ) )
616 index++;
617
618 if ( qgsDoubleNear( value1, value2 ) && qgsDoubleNear( value1, itemList.at( index ).value ) )
619 {
620 //the two value are the same and are equal to the value in the item list --> render only one color
621 breakColors.append( itemList.at( index ).color );
622 return;
623 }
624
625 while ( index < itemList.count() && itemList.at( index ).value <= value2 )
626 {
627 breakValues.append( itemList.at( index ).value );
628 breakColors.append( itemList.at( index ).color );
629 index++;
630 }
631}
632
633void QgsInterpolatedLineColor::graduatedColorsInterpolated( double value1, double value2, QList<double> &breakValues, QList<QColor> &breakColors, QList<QLinearGradient> &gradients ) const
634{
635 Q_ASSERT( mColorRampShader.colorRampType() == Qgis::ShaderInterpolationMethod::Linear );
636 Q_ASSERT( breakValues.isEmpty() );
637 Q_ASSERT( breakColors.isEmpty() );
638 Q_ASSERT( gradients.isEmpty() );
639
640
641 const QList<QgsColorRampShader::ColorRampItem> &itemList = mColorRampShader.colorRampItemList();
642 if ( itemList.empty() )
643 return;
644
645 if ( itemList.count() == 1 )
646 {
647 breakColors.append( itemList.first().color );
648 return;
649 }
650
651 if ( value2 <= itemList.first().value ) // completely out of range and less
652 {
653 if ( !mColorRampShader.clip() )
654 breakColors.append( itemList.first().color ); // render only the first color in the whole range if not clipped
655 return;
656 }
657
658 if ( value1 > itemList.last().value ) // completely out of range and greater
659 {
660 if ( !mColorRampShader.clip() )
661 breakColors.append( itemList.last().color ); // render only the last color in the whole range if not clipped
662 return;
663 }
664
665 if ( qgsDoubleNear( value1, value2 ) )
666 {
667 // the two values are the same
668 // --> render only one color
669 int r, g, b, a;
670 QColor color;
671 if ( mColorRampShader.shade( value1, &r, &g, &b, &a ) )
672 color = QColor( r, g, b, a );
673 breakColors.append( color );
674 return;
675 }
676
677 // index of the inf value of the interval where value1 is in the color ramp shader
678 int index = itemColorIndexInf( value1 );
679 if ( index < 0 ) // value1 out of range
680 {
681 QColor color = itemList.first().color;
682 breakColors.append( color );
683 if ( mColorRampShader.clip() ) // The first value/color returned is the first of the item list
684 breakValues.append( itemList.first().value );
685 else // The first value/color returned is the first color of the item list and value1
686 breakValues.append( value1 );
687 }
688 else
689 {
690 // shade the color
691 int r, g, b, a;
692 QColor color;
693 if ( mColorRampShader.shade( value1, &r, &g, &b, &a ) )
694 color = QColor( r, g, b, a );
695 breakValues.append( value1 );
696 breakColors.append( color );
697 }
698
699 index++; // increment the index before go through the intervals
700
701 while ( index < itemList.count() && itemList.at( index ).value < value2 )
702 {
703 QColor color1 = breakColors.last();
704 QColor color2 = itemList.at( index ).color;
705 breakValues.append( itemList.at( index ).value );
706 breakColors.append( color2 );
707 gradients.append( makeSimpleLinearGradient( color1, color2 ) );
708 index++;
709 }
710
711 // close the lists with value2 or last item if >value2
712 QColor color1 = breakColors.last();
713 QColor color2;
714 if ( value2 < itemList.last().value )
715 {
716 int r, g, b, a;
717 if ( mColorRampShader.shade( value2, &r, &g, &b, &a ) )
718 color2 = QColor( r, g, b, a );
719 breakValues.append( value2 );
720 }
721 else
722 {
723 color2 = itemList.last().color;
724 if ( mColorRampShader.clip() )
725 breakValues.append( itemList.last().value );
726 else
727 breakValues.append( value2 );
728 }
729 breakColors.append( color2 );
730 gradients.append( makeSimpleLinearGradient( color1, color2 ) );
731}
732
733
734void QgsInterpolatedLineColor::graduatedColorsDiscrete( double value1, double value2, QList<double> &breakValues, QList<QColor> &breakColors, QList<QLinearGradient> &gradients ) const
735{
736 Q_ASSERT( mColorRampShader.colorRampType() == Qgis::ShaderInterpolationMethod::Discrete );
737 Q_ASSERT( breakValues.isEmpty() );
738 Q_ASSERT( breakColors.isEmpty() );
739 Q_ASSERT( gradients.isEmpty() );
740
741 const QList<QgsColorRampShader::ColorRampItem> &itemList = mColorRampShader.colorRampItemList();
742 if ( itemList.empty() )
743 return;
744
745 if ( itemList.count() == 1 )
746 {
747 breakColors.append( itemList.first().color );
748 return;
749 }
750
751 double lastValue = itemList.at( itemList.count() - 2 ).value;
752
753
754 if ( value2 <= itemList.first().value ) // completely out of range and less
755 {
756 breakColors.append( itemList.first().color ); // render only the first color in the whole range
757 return;
758 }
759
760 if ( value1 > lastValue ) // completely out of range and greater
761 {
762 breakColors.append( itemList.last().color ); // render only the last color in the whole range
763 return;
764 }
765
766 // index of the inf value of the interval where value1 is in the color ramp shader
767 int index = itemColorIndexInf( value1 );
768
769 if ( qgsDoubleNear( value1, value2 ) )
770 {
771 // the two values are the same and are equal to the value in the item list
772 // --> render only one color, the sup one
773 breakColors.append( itemList.at( index + 1 ).color );
774 return;
775 }
776
777 if ( index < 0 ) // value1 out of range
778 {
779 breakValues.append( value1 );
780 breakColors.append( itemList.first().color );
781 }
782 else // append the first value with corresponding color
783 {
784 QColor color = itemList.at( index ).color;
785 breakValues.append( value1 );
786 breakColors.append( color );
787 }
788
789 index++; // increment the index before go through the intervals
790
791 while ( index < ( itemList.count() - 1 ) && itemList.at( index ).value < value2 )
792 {
793 QColor color = itemList.at( index ).color;
794 breakValues.append( itemList.at( index ).value );
795 breakColors.append( color );
796 gradients.append( makeSimpleLinearGradient( color, color ) );
797 index++;
798 }
799
800 // add value2 to close
801 QColor lastColor = itemList.at( index ).color;
802 breakColors.append( lastColor );
803 breakValues.append( value2 );
804 gradients.append( makeSimpleLinearGradient( lastColor, lastColor ) );
805}
806
808{
809 return u"InterpolatedLine"_s;
810}
811
814
817
824
826{
827 std::unique_ptr<QgsInterpolatedLineSymbolLayer> symbolLayer;
828 symbolLayer = std::make_unique<QgsInterpolatedLineSymbolLayer>();
829
830 if ( properties.contains( u"start_width_expression"_s ) )
831 symbolLayer->setDataDefinedProperty( QgsSymbolLayer::Property::LineStartWidthValue, QgsProperty::fromExpression( properties.value( u"start_width_expression"_s ).toString() ) );
832 if ( properties.contains( u"end_width_expression"_s ) )
833 symbolLayer->setDataDefinedProperty( QgsSymbolLayer::Property::LineEndWidthValue, QgsProperty::fromExpression( properties.value( u"end_width_expression"_s ).toString() ) );
834
835 if ( properties.contains( u"start_color_expression"_s ) )
836 symbolLayer->setDataDefinedProperty( QgsSymbolLayer::Property::LineStartColorValue, QgsProperty::fromExpression( properties.value( u"start_color_expression"_s ).toString() ) );
837 if ( properties.contains( u"end_color_expression"_s ) )
838 symbolLayer->setDataDefinedProperty( QgsSymbolLayer::Property::LineEndColorValue, QgsProperty::fromExpression( properties.value( u"end_color_expression"_s ).toString() ) );
839
840 if ( properties.contains( u"line_width"_s ) )
841 symbolLayer->mLineRender.mStrokeWidth.setFixedStrokeWidth( properties.value( u"line_width"_s ).toDouble() );
842 if ( properties.contains( u"line_width_unit"_s ) )
843 symbolLayer->mLineRender.setWidthUnit( QgsUnitTypes::decodeRenderUnit( properties.value( u"line_width_unit"_s ).toString() ) );
844 if ( properties.contains( u"width_varying_minimum_value"_s ) )
845 symbolLayer->mLineRender.mStrokeWidth.setMinimumValue( properties.value( u"width_varying_minimum_value"_s ).toDouble() );
846 if ( properties.contains( u"width_varying_maximum_value"_s ) )
847 symbolLayer->mLineRender.mStrokeWidth.setMaximumValue( properties.value( u"width_varying_maximum_value"_s ).toDouble() );
848 if ( properties.contains( u"width_varying_use_absolute_value"_s ) )
849 symbolLayer->mLineRender.mStrokeWidth.setUseAbsoluteValue( properties.value( u"width_varying_use_absolute_value"_s ).toInt() );
850 if ( properties.contains( u"width_varying_minimum_width"_s ) )
851 symbolLayer->mLineRender.mStrokeWidth.setMinimumWidth( properties.value( u"width_varying_minimum_width"_s ).toDouble() );
852 if ( properties.contains( u"width_varying_maximum_width"_s ) )
853 symbolLayer->mLineRender.mStrokeWidth.setMaximumWidth( properties.value( u"width_varying_maximum_width"_s ).toDouble() );
854 if ( properties.contains( u"width_varying_ignore_out_of_range"_s ) )
855 symbolLayer->mLineRender.mStrokeWidth.setIgnoreOutOfRange( properties.value( u"width_varying_ignore_out_of_range"_s ).toInt() );
856 if ( properties.contains( u"width_varying_is_variable_width"_s ) )
857 symbolLayer->mLineRender.mStrokeWidth.setIsVariableWidth( properties.value( u"width_varying_is_variable_width"_s ).toInt() );
858
859 if ( properties.contains( u"single_color"_s ) )
860 symbolLayer->mLineRender.mStrokeColoring.setColor( QgsColorUtils::colorFromString( properties.value( u"single_color"_s ).toString() ) );
861 if ( properties.contains( u"color_ramp_shader"_s ) )
862 symbolLayer->mLineRender.mStrokeColoring.setColor( createColorRampShaderFromProperties( properties.value( u"color_ramp_shader"_s ) ) );
863 if ( properties.contains( u"coloring_method"_s ) )
864 symbolLayer->mLineRender.mStrokeColoring.setColoringMethod( static_cast<QgsInterpolatedLineColor::ColoringMethod>( properties.value( u"coloring_method"_s ).toInt() ) );
865
866 return symbolLayer.release();
867}
868
873
875{
876 QVariantMap props;
877
878 // Line width varying
879 props.insert( u"line_width"_s, QString::number( mLineRender.mStrokeWidth.fixedStrokeWidth() ) );
880 props.insert( u"line_width_unit"_s, QgsUnitTypes::encodeUnit( mLineRender.widthUnit() ) );
881 props.insert( u"width_varying_minimum_value"_s, mLineRender.mStrokeWidth.minimumValue() );
882 props.insert( u"width_varying_maximum_value"_s, mLineRender.mStrokeWidth.maximumValue() );
883 props.insert( u"width_varying_use_absolute_value"_s, mLineRender.mStrokeWidth.useAbsoluteValue() ? 1 : 0 );
884 props.insert( u"width_varying_minimum_width"_s, mLineRender.mStrokeWidth.minimumWidth() );
885 props.insert( u"width_varying_maximum_width"_s, mLineRender.mStrokeWidth.maximumWidth() );
886 props.insert( u"width_varying_ignore_out_of_range"_s, mLineRender.mStrokeWidth.ignoreOutOfRange() ? 1 : 0 );
887 props.insert( u"width_varying_is_variable_width"_s, mLineRender.mStrokeWidth.isVariableWidth() ? 1 : 0 );
888
889 // Color varying
890 props.insert( u"coloring_method"_s, mLineRender.mStrokeColoring.coloringMethod() );
891 props.insert( u"single_color"_s, QgsColorUtils::colorToString( mLineRender.mStrokeColoring.singleColor() ) );
892 props.insert( u"color_ramp_shader"_s, colorRampShaderProperties() );
893
894 return props;
895}
896
898{
900
901 startRender( context );
902 double min = std::min( mLineRender.interpolatedLineWidth().minimumValue(), mLineRender.interpolatedColor().colorRampShader().minimumValue() );
903 double max = std::max( mLineRender.interpolatedLineWidth().maximumValue(), mLineRender.interpolatedColor().colorRampShader().maximumValue() );
904
905 double totalLength = geometry.length();
906 if ( qgsDoubleNear( totalLength, 0 ) )
907 return;
908
909 double variation = ( max - min ) / totalLength;
910
911 QPolygonF points = geometry.asQPolygonF();
912 double lengthFromStart = 0;
913 for ( int i = 1; i < points.count(); ++i )
914 {
915 QPointF p1 = points.at( i - 1 );
916 QPointF p2 = points.at( i );
917
918 double v1 = min + variation * lengthFromStart;
919 QPointF vectDist = p2 - p1;
920 lengthFromStart += sqrt( pow( vectDist.x(), 2 ) + pow( vectDist.y(), 2 ) );
921 double v2 = min + variation * lengthFromStart;
922 mLineRender.renderInDeviceCoordinates( v1, v2, v1, v2, p1, p2, context.renderContext() );
923 }
924
925 renderPolyline( points, context );
926}
927
929{
930 switch ( mLineRender.interpolatedColor().coloringMethod() )
931 {
933 return mLineRender.interpolatedColor().singleColor();
935 return QColor();
936 }
938}
939
940
953
958
963
964void QgsInterpolatedLineSymbolLayer::setWidthUnit( Qgis::RenderUnit strokeWidthUnit ) // cppcheck-suppress duplInheritedMember
965{
966 mLineRender.mStrokeWidthUnit = strokeWidthUnit;
967}
968
969// cppcheck-suppress duplInheritedMember
971{
972 return mLineRender.widthUnit();
973}
974
976{
977 mLineRender.mStrokeWidth = interpolatedLineWidth;
978}
979
981{
982 return mLineRender.interpolatedLineWidth();
983}
984
997
1002
1007
1009{
1010 mLineRender.setInterpolatedColor( interpolatedLineColor );
1011}
1012
1014{
1015 return mLineRender.interpolatedColor();
1016}
1017
1018QVariant QgsInterpolatedLineSymbolLayer::colorRampShaderProperties() const
1019{
1020 const QgsColorRampShader &colorRampShader = mLineRender.mStrokeColoring.colorRampShader();
1021
1022 QVariantMap props;
1023 if ( colorRampShader.sourceColorRamp() )
1024 props.insert( u"color_ramp_source"_s, QgsSymbolLayerUtils::colorRampToVariant( QString(), colorRampShader.sourceColorRamp() ) );
1025 props.insert( u"color_ramp_shader_type"_s, static_cast< int >( colorRampShader.colorRampType() ) );
1026 props.insert( u"color_ramp_shader_classification_mode"_s, static_cast< int >( colorRampShader.classificationMode() ) );
1027 QVariantList colorRampItemListVariant;
1028
1029 const QList<QgsColorRampShader::ColorRampItem> colorRampItemList = colorRampShader.colorRampItemList();
1030 for ( const QgsColorRampShader::ColorRampItem &item : colorRampItemList )
1031 {
1032 QVariantMap itemVar;
1033 itemVar[u"label"_s] = item.label;
1034 itemVar[u"color"_s] = QgsColorUtils::colorToString( item.color );
1035 itemVar[u"value"_s] = item.value;
1036 colorRampItemListVariant.append( itemVar );
1037 }
1038 props.insert( u"color_ramp_shader_items_list"_s, colorRampItemListVariant );
1039
1040 props.insert( u"color_ramp_shader_minimum_value"_s, colorRampShader.minimumValue() );
1041 props.insert( u"color_ramp_shader_maximum_value"_s, colorRampShader.maximumValue() );
1042 props.insert( u"color_ramp_shader_value_out_of_range"_s, colorRampShader.clip() ? 1 : 0 );
1043 props.insert( u"color_ramp_shader_label_precision"_s, colorRampShader.labelPrecision() );
1044
1045 return props;
1046}
1047
1048QgsColorRampShader QgsInterpolatedLineSymbolLayer::createColorRampShaderFromProperties( const QVariant &properties )
1049{
1050 QgsColorRampShader colorRampShader;
1051
1052 if ( properties.userType() != QMetaType::Type::QVariantMap )
1053 return colorRampShader;
1054
1055 QVariantMap shaderVariantMap = properties.toMap();
1056
1057 if ( shaderVariantMap.contains( u"color_ramp_source"_s ) )
1058 colorRampShader.setSourceColorRamp( QgsSymbolLayerUtils::loadColorRamp( shaderVariantMap.value( u"color_ramp_source"_s ) ).release() );
1059
1060 if ( shaderVariantMap.contains( u"color_ramp_shader_type"_s ) )
1061 colorRampShader.setColorRampType( static_cast<Qgis::ShaderInterpolationMethod>( shaderVariantMap.value( u"color_ramp_shader_type"_s ).toInt() ) );
1062 if ( shaderVariantMap.contains( u"color_ramp_shader_classification_mode"_s ) )
1063 colorRampShader.setClassificationMode( static_cast<Qgis::ShaderClassificationMethod>( shaderVariantMap.value( u"color_ramp_shader_classification_mode"_s ).toInt() ) );
1064
1065 if ( shaderVariantMap.contains( u"color_ramp_shader_items_list"_s ) )
1066 {
1067 QVariant colorRampItemsVar = shaderVariantMap.value( u"color_ramp_shader_items_list"_s );
1068 if ( colorRampItemsVar.userType() == QMetaType::Type::QVariantList )
1069 {
1070 QVariantList itemVariantList = colorRampItemsVar.toList();
1071 QList<QgsColorRampShader::ColorRampItem> colorRampItemList;
1072 for ( const QVariant &itemVar : std::as_const( itemVariantList ) )
1073 {
1074 QgsColorRampShader::ColorRampItem item;
1075 if ( itemVar.userType() != QMetaType::Type::QVariantMap )
1076 continue;
1077 QVariantMap itemVarMap = itemVar.toMap();
1078 if ( !itemVarMap.contains( u"label"_s ) || !itemVarMap.contains( u"color"_s ) || !itemVarMap.contains( u"value"_s ) )
1079 continue;
1080
1081 item.label = itemVarMap.value( u"label"_s ).toString();
1082 item.color = QgsColorUtils::colorFromString( itemVarMap.value( u"color"_s ).toString() );
1083 item.value = itemVarMap.value( u"value"_s ).toDouble();
1084
1085 colorRampItemList.append( item );
1086 }
1087 colorRampShader.setColorRampItemList( colorRampItemList );
1088 }
1089 }
1090
1091 if ( shaderVariantMap.contains( u"color_ramp_shader_minimum_value"_s ) )
1092 colorRampShader.setMinimumValue( shaderVariantMap.value( u"color_ramp_shader_minimum_value"_s ).toDouble() );
1093 else
1094 colorRampShader.setMinimumValue( std::numeric_limits<double>::quiet_NaN() );
1095
1096 if ( shaderVariantMap.contains( u"color_ramp_shader_maximum_value"_s ) )
1097 colorRampShader.setMaximumValue( shaderVariantMap.value( u"color_ramp_shader_maximum_value"_s ).toDouble() );
1098 else
1099 colorRampShader.setMaximumValue( std::numeric_limits<double>::quiet_NaN() );
1100
1101 if ( shaderVariantMap.contains( u"color_ramp_shader_value_out_of_range"_s ) )
1102 colorRampShader.setClip( shaderVariantMap.value( u"color_ramp_shader_value_out_of_range"_s ).toInt() == 1 );
1103 if ( shaderVariantMap.contains( u"color_ramp_shader_label_precision"_s ) )
1104 colorRampShader.setLabelPrecision( shaderVariantMap.value( u"color_ramp_shader_label_precision"_s ).toInt() );
1105
1106 return colorRampShader;
1107}
1108
1112
1113
1115{
1116 mRenderingFeature = true;
1117 mLineParts.clear();
1118}
1119
1121{
1122 mRenderingFeature = false;
1123
1124 if ( mLineParts.empty() )
1125 return;
1126
1127 render( mLineParts, context );
1128 mLineParts.clear();
1129}
1130
1131void QgsInterpolatedLineSymbolLayer::render( const QVector< QPolygonF > &parts, QgsRenderContext &context )
1132{
1133 const double totalLength = std::accumulate( parts.begin(), parts.end(), 0.0, []( double total, const QPolygonF &part ) { return total + QgsSymbolLayerUtils::polylineLength( part ); } );
1134
1135 if ( qgsDoubleNear( totalLength, 0 ) )
1136 return;
1137
1138 double startValWidth = 0;
1139 double variationPerMapUnitWidth = 0;
1140 double startValColor = 0;
1141 double variationPerMapUnitColor = 0;
1142
1143 bool ok = true;
1144
1145 if ( mLineRender.interpolatedLineWidth().isVariableWidth() )
1146 {
1148 {
1150 if ( !ok )
1151 return;
1152 }
1153
1154 double endValWidth = 0;
1156 {
1157 endValWidth = mDataDefinedProperties.valueAsDouble( QgsSymbolLayer::Property::LineEndWidthValue, context.expressionContext(), 0, &ok );
1158 if ( !ok )
1159 return;
1160 }
1161
1162 variationPerMapUnitWidth = ( endValWidth - startValWidth ) / totalLength;
1163 }
1164
1165 if ( mLineRender.interpolatedColor().coloringMethod() == QgsInterpolatedLineColor::ColorRamp )
1166 {
1168 {
1169 startValColor = mDataDefinedProperties.valueAsDouble( QgsSymbolLayer::Property::LineStartColorValue, context.expressionContext(), 0, &ok );
1170 if ( !ok )
1171 return;
1172 }
1173
1174 double endValColor = 0;
1176 {
1177 endValColor = mDataDefinedProperties.valueAsDouble( QgsSymbolLayer::Property::LineEndColorValue, context.expressionContext(), 0, &ok );
1178 if ( !ok )
1179 return;
1180 }
1181
1182 variationPerMapUnitColor = ( endValColor - startValColor ) / totalLength;
1183 }
1184
1185 for ( const QPolygonF &poly : parts )
1186 {
1187 double lengthFromStart = 0;
1188 for ( int i = 1; i < poly.count(); ++i )
1189 {
1190 const QPointF p1 = poly.at( i - 1 );
1191 const QPointF p2 = poly.at( i );
1192
1193 const double v1c = startValColor + variationPerMapUnitColor * lengthFromStart;
1194 const double v1w = startValWidth + variationPerMapUnitWidth * lengthFromStart;
1195 lengthFromStart += QgsGeometryUtilsBase::distance2D( p1.x(), p1.y(), p2.x(), p2.y() );
1196 const double v2c = startValColor + variationPerMapUnitColor * lengthFromStart;
1197 const double v2w = startValWidth + variationPerMapUnitWidth * lengthFromStart;
1198 mLineRender.renderInDeviceCoordinates( v1c, v2c, v1w, v2w, p1, p2, context );
1199 }
1200 }
1201}
1202
1204{
1205 mLineRender.setSelected( context.selected() );
1206
1207 if ( points.empty() )
1208 return;
1209
1210 if ( mRenderingFeature )
1211 {
1212 // in the middle of rendering a possibly multi-part feature, so we collect all the parts and defer the actual rendering
1213 // until after we've received the final part
1214 mLineParts.append( points );
1215 }
1216 else
1217 {
1218 // not rendering a feature, so we can just render the polyline immediately
1219 render( { points }, context.renderContext() );
1220 }
1221}
1222
@ DisableFeatureClipping
If present, indicates that features should never be clipped to the map extent during rendering.
Definition qgis.h:907
@ CanCalculateMaskGeometryPerFeature
If present, indicates that mask geometry can safely be calculated per feature for the symbol layer....
Definition qgis.h:908
ShaderInterpolationMethod
Color ramp shader interpolation methods.
Definition qgis.h:1503
@ Exact
Assigns the color of the exact matching value in the color ramp item list.
Definition qgis.h:1506
@ Linear
Interpolates the color between two class breaks linearly.
Definition qgis.h:1504
@ Discrete
Assigns the color of the higher class for every pixel between two class breaks.
Definition qgis.h:1505
QFlags< SymbolLayerFlag > SymbolLayerFlags
Symbol layer flags.
Definition qgis.h:913
ShaderClassificationMethod
Color ramp shader classification methods.
Definition qgis.h:1518
RenderUnit
Rendering size units.
Definition qgis.h:5340
@ Line
Line symbol.
Definition qgis.h:638
double valueAsDouble(int key, const QgsExpressionContext &context, double defaultValue=0.0, bool *ok=nullptr) const
Calculates the current value of the property with the specified key and interprets it as a double.
A ramp shader will color a raster pixel based on a list of values ranges in a ramp.
Qgis::ShaderClassificationMethod classificationMode() const
Returns the classification mode.
Qgis::ShaderInterpolationMethod colorRampType() const
Returns the color ramp interpolation method.
void setSourceColorRamp(QgsColorRamp *colorramp)
Set the source color ramp.
void setClassificationMode(Qgis::ShaderClassificationMethod classificationMode)
Sets the classification mode.
QList< QgsColorRampShader::ColorRampItem > colorRampItemList() const
Returns the custom color map.
void setClip(bool clip)
Sets whether the shader should not render values out of range.
bool clip() const
Returns whether the shader will clip values which are out of range.
QgsColorRamp * sourceColorRamp() const
Returns the source color ramp.
void setColorRampType(Qgis::ShaderInterpolationMethod colorRampType)
Sets the color ramp interpolation method.
void setColorRampItemList(const QList< QgsColorRampShader::ColorRampItem > &list)
Sets a custom color map.
void readXml(const QDomElement &elem, const QgsReadWriteContext &context=QgsReadWriteContext())
Reads configuration from the given DOM element.
Abstract base class for color ramps.
virtual QColor color(double value) const =0
Returns the color corresponding to a specified value.
static QColor colorFromString(const QString &string)
Decodes a string into a color value.
static QString colorToString(const QColor &color)
Encodes a color into a string value.
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
Definition qgsfeature.h:60
static double distance2D(double x1, double y1, double x2, double y2)
Returns the 2D distance between (x1, y1) and (x2, y2).
A geometry is the spatial representation of a feature.
QPolygonF asQPolygonF() const
Returns contents of the geometry as a QPolygonF.
double length() const
Returns the planar, 2-dimensional length of geometry.
Defines color interpolation for rendering mesh datasets.
QgsInterpolatedLineColor::ColoringMethod coloringMethod() const
Returns the coloring method used.
QgsColorRampShader colorRampShader() const
Returns the color ramp shader.
void readXml(const QDomElement &elem, const QgsReadWriteContext &context)
Reads configuration from the given DOM element.
QColor color(double magnitude) const
Returns the color corresponding to the magnitude.
void graduatedColors(double value1, double value2, QList< double > &breakValues, QList< QColor > &breakColors, QList< QLinearGradient > &gradients) const
Returns the break values, graduated colors and the associated gradients between two values.
QgsInterpolatedLineColor()
Default constructor.
QDomElement writeXml(QDomDocument &doc, const QgsReadWriteContext &context) const
Writes configuration to a new DOM element.
void setColor(const QgsColorRampShader &colorRampShader)
Sets the color ramp to define the coloring.
QColor singleColor() const
Returns the single color that is used if SingleColor coloring mode is set.
void setColoringMethod(ColoringMethod coloringMethod)
Sets the coloring method used.
ColoringMethod
Defines how the color is defined.
@ ColorRamp
Render with a color ramp.
@ SingleColor
Render with a single color.
void setInterpolatedColor(const QgsInterpolatedLineColor &strokeColoring)
Sets the stroke color used to render.
void renderInDeviceCoordinates(double valueColor1, double valueColor2, double valueWidth1, double valueWidth2, QPointF point1, QPointF point2, QgsRenderContext &context) const
Renders a line in the context between point1 and point2 in device (painter) coordinates with color th...
Qgis::RenderUnit widthUnit() const
Returns the unit of the stroke width.
void setInterpolatedWidth(const QgsInterpolatedLineWidth &strokeWidth)
Sets the stroke width used to render.
void setSelected(bool selected)
Sets if the rendering must be done as the element is selected.
void render(double value1, double value2, const QgsPointXY &point1, const QgsPointXY &point2, QgsRenderContext &context) const
Renders a line in the context between point1 and point2 with color and width that vary depending on v...
QgsInterpolatedLineWidth interpolatedLineWidth() const
Returns the stroke width used to render.
QgsInterpolatedLineColor interpolatedColor() const
Returns the stroke color used to render.
void setWidthUnit(Qgis::RenderUnit strokeWidthUnit)
Sets the unit of the stroke width.
Q_DECL_DEPRECATED QString endValueExpressionForWidth() const
Returns the expression related to the end extremity value for width.
QColor color() const override
Returns the "representative" color of the symbol layer.
Q_DECL_DEPRECATED QString endValueExpressionForColor() const
Returns the expression related to the end extremity value for width for color.
Q_DECL_DEPRECATED void setExpressionsStringForColor(const QString &start, const QString &end)
Sets the expressions (as string) that define the extremety values af the line feature for color.
Qgis::RenderUnit widthUnit() const
Returns the width unit.
Qgis::SymbolLayerFlags flags() const override
Returns flags which control the symbol layer's behavior.
QgsInterpolatedLineSymbolLayer * clone() const override
Shall be reimplemented by subclasses to create a deep copy of the instance.
bool canCauseArtifactsBetweenAdjacentTiles() const override
Returns true if the symbol layer rendering can cause visible artifacts across a single feature when t...
void renderPolyline(const QPolygonF &points, QgsSymbolRenderContext &context) override
Renders the line symbol layer along the line joining points, using the given render context.
QgsInterpolatedLineColor interpolatedColor() const
Returns the interpolated color used to render the colors of lines, see QgsInterpolatedLineColor.
Q_DECL_DEPRECATED QString startValueExpressionForColor() const
Returns the epression related to the start extremity value for width for color.
void startRender(QgsSymbolRenderContext &context) override
Called before a set of rendering operations commences on the supplied render context.
void startFeatureRender(const QgsFeature &feature, QgsRenderContext &context) override
Called before the layer will be rendered for a particular feature.
QString layerType() const override
Returns a string that represents this layer type.
Q_DECL_DEPRECATED void setExpressionsStringForWidth(const QString &start, const QString &end)
Sets the expressions (as string) that define the extremety values af the line feature for width.
void setInterpolatedWidth(const QgsInterpolatedLineWidth &interpolatedLineWidth)
Sets the interpolated width used to render the width of lines, see QgsInterpolatedLineWidth.
void setInterpolatedColor(const QgsInterpolatedLineColor &interpolatedLineColor)
Sets the interpolated color used to render the colors of lines, see QgsInterpolatedLineColor.
Q_DECL_DEPRECATED QString startValueExpressionForWidth() const
Returns the epression related to the start extremity value for width.
void drawPreviewIcon(QgsSymbolRenderContext &context, QSize size) override
QVariantMap properties() const override
Should be reimplemented by subclasses to return a string map that contains the configuration informat...
QgsInterpolatedLineWidth interpolatedWidth() const
Returns the interpolated width used to render the width of lines, see QgsInterpolatedLineWidth.
void stopFeatureRender(const QgsFeature &feature, QgsRenderContext &context) override
Called after the layer has been rendered for a particular feature.
void setWidthUnit(Qgis::RenderUnit strokeWidthUnit)
Sets the width unit.
static QgsSymbolLayer * create(const QVariantMap &properties)
Creates the symbol layer.
void stopRender(QgsSymbolRenderContext &context) override
Called after a set of rendering operations has finished on the supplied render context.
Represents a width that can vary depending on values.
void setFixedStrokeWidth(double fixedWidth)
Sets the fixed width.
double strokeWidth(double value) const
Returns the variable width depending on value, if not varying returns the fixed width.
void setUseAbsoluteValue(bool useAbsoluteValue)
Sets whether absolute value are used as input.
double minimumValue() const
Returns the minimum value used to defined the variable width.
void readXml(const QDomElement &elem, const QgsReadWriteContext &context)
Reads configuration from the given DOM element.
void setIgnoreOutOfRange(bool ignoreOutOfRange)
Sets whether the variable width ignores out of range value.
void setMaximumValue(double maximumValue)
Sets the maximum value used to defined the variable width.
bool useAbsoluteValue() const
Returns whether absolute value are used as input.
void setIsVariableWidth(bool isVariableWidth)
Returns whether the width is variable.
void setMinimumValue(double minimumValue)
Sets the minimum value used to defined the variable width.
double maximumWidth() const
Returns the maximum width used to defined the variable width.
void setMaximumWidth(double maximumWidth)
Sets the maximum width used to defined the variable width.
double maximumValue() const
Returns the maximum value used to defined the variable width.
void setMinimumWidth(double minimumWidth)
Sets the minimum width used to defined the variable width.
QDomElement writeXml(QDomDocument &doc, const QgsReadWriteContext &context) const
Writes configuration to a new DOM element.
bool ignoreOutOfRange() const
Returns whether the variable width ignores out of range value.
double minimumWidth() const
Returns the minimum width used to defined the variable width.
double fixedStrokeWidth() const
Returns the fixed width.
bool isVariableWidth() const
Returns whether the width is variable.
QgsGeometry geometry() const
Returns the geometry for the patch shape.
QgsLineSymbolLayer(const QgsLineSymbolLayer &other)=delete
Perform transforms between map coordinates and device coordinates.
QgsPointXY transform(const QgsPointXY &p) const
Transforms a point p from map (world) coordinates to device coordinates.
Represents a 2D point.
Definition qgspointxy.h:62
QPointF toQPointF() const
Converts a point to a QPointF.
Definition qgspointxy.h:168
bool isActive(int key) const final
Returns true if the collection contains an active property with the specified key.
A store for object properties.
static QgsProperty fromExpression(const QString &expression, bool isActive=true)
Returns a new ExpressionBasedProperty created from the specified expression.
double maximumValue() const
Returns the minimum value for the raster shader.
void setLabelPrecision(int labelPrecision)
Sets label precision to labelPrecision.
int labelPrecision() const
Returns label precision.
virtual void setMaximumValue(double value)
Sets the maximum value for the raster shader.
virtual void setMinimumValue(double value)
Sets the minimum value for the raster shader.
double minimumValue() const
Returns the maximum value for the raster shader.
A container for the context for various read/write operations on objects.
Contains information about the context of a rendering operation.
double convertToPainterUnits(double size, Qgis::RenderUnit unit, const QgsMapUnitScale &scale=QgsMapUnitScale(), Qgis::RenderSubcomponentProperty property=Qgis::RenderSubcomponentProperty::Generic) const
Converts a size from the specified units to painter units (pixels).
QPainter * painter()
Returns the destination QPainter for the render operation.
void setPainterFlagsUsingContext(QPainter *painter=nullptr) const
Sets relevant flags on a destination painter, using the flags and settings currently defined for the ...
QgsExpressionContext & expressionContext()
Gets the expression context.
const QgsMapToPixel & mapToPixel() const
Returns the context's map to pixel transform, which transforms between map coordinates and device coo...
QColor selectionColor() const
Returns the color to use when rendering selected features.
Scoped object for saving and restoring a QPainter object's state.
QgsLegendPatchShape defaultPatch(Qgis::SymbolType type, QSizeF size) const
Returns the default legend patch shape for the given symbol type.
static QgsStyle * defaultStyle(bool initialize=true)
Returns the default application-wide style.
Definition qgsstyle.cpp:148
static QVariant colorRampToVariant(const QString &name, QgsColorRamp *ramp)
Saves a color ramp to a QVariantMap, wrapped in a QVariant.
static std::unique_ptr< QgsColorRamp > loadColorRamp(QDomElement &element)
Creates a color ramp from the settings encoded in an XML element.
void copyCommonProperties(QgsSymbolLayer *destLayer) const
Copies all common base class properties from this layer to another symbol layer.
@ LineEndColorValue
End line color for interpolated line renderer.
@ LineStartColorValue
Start line color for interpolated line renderer.
@ LineEndWidthValue
End line width for interpolated line renderer.
@ LineStartWidthValue
Start line width for interpolated line renderer.
virtual Qgis::SymbolLayerFlags flags() const
Returns flags which control the symbol layer's behavior.
QgsPropertyCollection mDataDefinedProperties
QgsSymbolLayer(const QgsSymbolLayer &other)
Encapsulates the context in which a symbol is being rendered.
bool selected() const
Returns true if symbols should be rendered using the selected symbol coloring and style.
const QgsLegendPatchShape * patchShape() const
Returns the symbol patch shape, to use if rendering symbol preview icons.
QgsRenderContext & renderContext()
Returns a reference to the context's render context.
static Q_INVOKABLE Qgis::RenderUnit decodeRenderUnit(const QString &string, bool *ok=nullptr)
Decodes a render unit from a string.
static Q_INVOKABLE QString encodeUnit(Qgis::DistanceUnit unit)
Encodes a distance unit to a string.
#define BUILTIN_UNREACHABLE
Definition qgis.h:7540
bool qgsDoubleNear(double a, double b, double epsilon=4 *std::numeric_limits< double >::epsilon())
Compare two doubles (but allow some difference).
Definition qgis.h:6975