30using namespace Qt::StringLiterals;
34 mStrokeWidth = strokeWidth;
44 mStrokeColoring = strokeColoring;
49 return mStrokeColoring;
54 mStrokeWidthUnit = strokeWidthUnit;
59 return mStrokeWidthUnit;
64 QPainter *painter = context.
painter();
68 QPointF dir = p2 - p1;
69 double length = sqrt( pow( dir.x(), 2 ) + pow( dir.y(), 2 ) );
70 QPointF diru = dir / length;
71 QPointF orthu = QPointF( -diru.y(), diru.x() );
73 QList<double> breakValues;
74 QList<QColor> breakColors;
75 QList<QLinearGradient> gradients;
77 mStrokeColoring.graduatedColors( valueColor1, valueColor2, breakValues, breakColors, gradients );
80 if ( gradients.isEmpty() && !breakValues.empty() && !breakColors.isEmpty() )
82 Q_ASSERT( breakColors.count() == breakValues.count() );
83 for (
int i = 0; i < breakValues.count(); ++i )
85 const bool widthIsInverted { valueWidth1 > valueWidth2 };
86 const double value = breakValues.at( i );
87 const double width = context.
convertToPainterUnits( mStrokeWidth.strokeWidth( widthIsInverted ? mStrokeWidth.maximumValue() - value : value ), mStrokeWidthUnit );
88 QPen pen( mSelected ? selectedColor : breakColors.at( i ) );
89 pen.setWidthF( width );
90 pen.setCapStyle( Qt::PenCapStyle::RoundCap );
91 painter->setPen( pen );
92 const QPointF point = p1 + dir * ( value - valueColor1 ) / ( valueColor2 - valueColor1 );
93 painter->drawPoint( point );
98 double width1 = mStrokeWidth.strokeWidth( valueWidth1 );
99 double width2 = mStrokeWidth.strokeWidth( valueWidth2 );
101 if ( !std::isnan( width1 ) && !std::isnan( width2 ) )
104 QBrush brush( Qt::SolidPattern );
107 startAngle = ( acos( -orthu.x() ) / M_PI ) * 180;
109 startAngle = 360 - startAngle;
111 bool outOfRange1 = std::isnan( width1 );
112 bool outOfRange2 = std::isnan( width2 );
117 QRectF capBox1( p1.x() - width1 / 2, p1.y() - width1 / 2, width1, width1 );
118 brush.setColor( mSelected ? selectedColor : mStrokeColoring.color( valueColor1 ) );
119 painter->setBrush( brush );
120 pen.setBrush( brush );
121 painter->setPen( pen );
122 painter->drawPie( capBox1, ( startAngle - 1 ) * 16, 182 * 16 );
128 QRectF capBox2( p2.x() - width2 / 2, p2.y() - width2 / 2, width2, width2 );
129 brush.setColor( mSelected ? selectedColor : mStrokeColoring.color( valueColor2 ) );
130 pen.setBrush( brush );
131 painter->setBrush( brush );
132 painter->setPen( pen );
133 painter->drawPie( capBox2, ( startAngle + 179 ) * 16, 182 * 16 );
136 if ( ( gradients.isEmpty() && breakValues.empty() && breakColors.count() == 1 ) || mSelected )
138 double startAdjusting = 0;
140 adjustLine( valueColor1, valueColor1, valueColor2, width1, startAdjusting );
143 double endAdjusting = 0;
145 adjustLine( valueColor2, valueColor1, valueColor2, width2, endAdjusting );
147 QPointF pointStartAdjusted = p1 + dir * startAdjusting;
148 QPointF pointEndAdjusted = p2 - dir * endAdjusting;
151 double semiWidth1 = width1 / 2;
152 double semiWidth2 = width2 / 2;
154 varLine.append( pointStartAdjusted + orthu * semiWidth1 );
155 varLine.append( pointEndAdjusted + orthu * semiWidth2 );
156 varLine.append( pointEndAdjusted - orthu * semiWidth2 );
157 varLine.append( pointStartAdjusted - orthu * semiWidth1 );
159 QBrush brush( Qt::SolidPattern );
160 brush.setColor( mSelected ? selectedColor : breakColors.first() );
161 painter->setBrush( brush );
162 painter->setPen( pen );
165 pen.setBrush( brush );
167 painter->setPen( pen );
169 painter->drawPolygon( varLine );
171 else if ( !gradients.isEmpty() && !breakValues.isEmpty() && !breakColors.isEmpty() )
173 Q_ASSERT( breakColors.count() == breakValues.count() );
174 Q_ASSERT( breakColors.count() == gradients.count() + 1 );
175 double widthColorVariationValueRatio = ( valueWidth2 - valueWidth1 ) / ( valueColor2 - valueColor1 );
177 for (
int i = 0; i < gradients.count(); ++i )
179 double firstValue = breakValues.at( i );
180 double secondValue = breakValues.at( i + 1 );
181 double w1 = mStrokeWidth.strokeWidth( widthColorVariationValueRatio * ( firstValue - valueColor1 ) + valueWidth1 );
182 double w2 = mStrokeWidth.strokeWidth( widthColorVariationValueRatio * ( secondValue - valueColor1 ) + valueWidth1 );
184 if ( std::isnan( w1 ) && std::isnan( w2 ) )
187 double firstAdjusting = 0;
188 if ( std::isnan( w1 ) )
189 adjustLine( firstValue, valueColor1, valueColor2, w1, firstAdjusting );
192 double secondAdjusting = 0;
193 if ( std::isnan( w2 ) )
194 adjustLine( secondValue, valueColor1, valueColor2, w2, secondAdjusting );
199 QPointF pointStart = p1 + dir * ( firstValue - valueColor1 ) / ( valueColor2 - valueColor1 );
200 QPointF pointEnd = p1 + dir * ( secondValue - valueColor1 ) / ( valueColor2 - valueColor1 );
202 QPointF pointStartAdjusted = pointStart + dir * firstAdjusting;
203 QPointF pointEndAdjusted = pointEnd - dir * secondAdjusting;
209 varLine.append( pointStartAdjusted + orthu * sw1 );
210 varLine.append( pointEndAdjusted + orthu * sw2 );
211 varLine.append( pointEndAdjusted - orthu * sw2 );
212 varLine.append( pointStartAdjusted - orthu * sw1 );
214 QLinearGradient gradient = gradients.at( i );
215 gradient.setStart( pointStart );
216 gradient.setFinalStop( pointEnd );
217 QBrush brush( gradient );
218 painter->setBrush( brush );
221 pen.setBrush( brush );
223 painter->setPen( pen );
225 painter->drawPolygon( varLine );
240 if ( value1 > value2 )
242 std::swap( value1, value2 );
243 std::swap( point1, point2 );
259 if ( valueColor1 > valueColor2 )
261 std::swap( valueColor1, valueColor2 );
262 std::swap( valueWidth1, valueWidth2 );
263 std::swap( point1, point2 );
274 mSelected = selected;
277void QgsInterpolatedLineRenderer::adjustLine(
const double value,
const double value1,
const double value2,
double &width,
double &adjusting )
const
281 adjusting = fabs( ( value - mStrokeWidth.
maximumValue() ) / ( value2 - value1 ) );
286 adjusting = fabs( ( value - mStrokeWidth.minimumValue() ) / ( value2 - value1 ) );
287 width = mStrokeWidth.minimumWidth();
293 return mMinimumValue;
299 mNeedUpdateFormula =
true;
304 return mMaximumValue;
310 mNeedUpdateFormula =
true;
315 return mMinimumWidth;
321 mNeedUpdateFormula =
true;
326 return mMaximumWidth;
332 mNeedUpdateFormula =
true;
337 if ( mIsWidthVariable )
339 if ( mNeedUpdateFormula )
340 updateLinearFormula();
342 if ( mUseAbsoluteValue )
343 value = std::fabs( value );
345 if ( value > mMaximumValue )
347 if ( mIgnoreOutOfRange )
348 return std::numeric_limits<double>::quiet_NaN();
350 return mMaximumWidth;
353 if ( value < mMinimumValue )
355 if ( mIgnoreOutOfRange )
356 return std::numeric_limits<double>::quiet_NaN();
358 return mMinimumWidth;
361 return ( value - mMinimumValue ) * mLinearCoef + mMinimumWidth;
369 QDomElement elem = doc.createElement( u
"mesh-stroke-width"_s );
371 elem.setAttribute( u
"width-varying"_s, mIsWidthVariable ? 1 : 0 );
372 elem.setAttribute( u
"fixed-width"_s, mFixedWidth );
373 elem.setAttribute( u
"minimum-value"_s, mMinimumValue );
374 elem.setAttribute( u
"maximum-value"_s, mMaximumValue );
375 elem.setAttribute( u
"minimum-width"_s, mMinimumWidth );
376 elem.setAttribute( u
"maximum-width"_s, mMaximumWidth );
377 elem.setAttribute( u
"ignore-out-of-range"_s, mIgnoreOutOfRange ? 1 : 0 );
378 elem.setAttribute( u
"use-absolute-value"_s, mUseAbsoluteValue ? 1 : 0 );
385 mIsWidthVariable = elem.attribute( u
"width-varying"_s ).toInt();
386 mFixedWidth = elem.attribute( u
"fixed-width"_s ).toDouble();
387 mMinimumValue = elem.attribute( u
"minimum-value"_s ).toDouble();
388 mMaximumValue = elem.attribute( u
"maximum-value"_s ).toDouble();
389 mMinimumWidth = elem.attribute( u
"minimum-width"_s ).toDouble();
390 mMaximumWidth = elem.attribute( u
"maximum-width"_s ).toDouble();
391 mIgnoreOutOfRange = elem.attribute( u
"ignore-out-of-range"_s ).toInt();
392 mUseAbsoluteValue = elem.attribute( u
"use-absolute-value"_s ).toInt();
397 return mUseAbsoluteValue;
412 return mIgnoreOutOfRange;
422 return mIsWidthVariable;
427 mIsWidthVariable = isWidthVarying;
432 mFixedWidth = fixedWidth;
435void QgsInterpolatedLineWidth::updateLinearFormula()
const
438 mLinearCoef = ( mMaximumWidth - mMinimumWidth ) / ( mMaximumValue - mMinimumValue );
441 mNeedUpdateFormula =
false;
446 mColorRampShader.setMinimumValue( std::numeric_limits<double>::quiet_NaN() );
447 mColorRampShader.setMaximumValue( std::numeric_limits<double>::quiet_NaN() );
459 mColorRampShader.setMinimumValue( std::numeric_limits<double>::quiet_NaN() );
460 mColorRampShader.setMaximumValue( std::numeric_limits<double>::quiet_NaN() );
466 if ( ( mColorRampShader.sourceColorRamp() ) )
474 mSingleColor =
color;
479 QgsColorRamp *lSourceColorRamp = mColorRampShader.sourceColorRamp();
480 if ( mColoringMethod ==
ColorRamp && lSourceColorRamp )
482 if ( mColorRampShader.isEmpty() )
483 return lSourceColorRamp->
color( 0 );
486 if ( mColorRampShader.shade( magnitude, &r, &g, &b, &a ) )
487 return QColor( r, g, b, a );
489 return QColor( 0, 0, 0, 0 );
499 return mColoringMethod;
504 return mColorRampShader;
514 QDomElement elem = doc.createElement( u
"mesh-stroke-color"_s );
517 elem.setAttribute( u
"coloring-method"_s, mColoringMethod );
518 elem.appendChild( mColorRampShader.writeXml( doc ) );
525 QDomElement shaderElem = elem.firstChildElement( u
"colorrampshader"_s );
526 mColorRampShader.
readXml( shaderElem );
539 breakColors.append( mSingleColor );
543 switch ( mColorRampShader.colorRampType() )
546 graduatedColorsInterpolated( value1, value2, breakValues, breakColors, gradients );
549 graduatedColorsDiscrete( value1, value2, breakValues, breakColors, gradients );
552 graduatedColorsExact( value1, value2, breakValues, breakColors, gradients );
562QLinearGradient QgsInterpolatedLineColor::makeSimpleLinearGradient(
const QColor &color1,
const QColor &color2 )
const
564 QLinearGradient gradient;
565 gradient.setColorAt( 0, color1 );
566 gradient.setColorAt( 1, color2 );
571int QgsInterpolatedLineColor::itemColorIndexInf(
double value )
const
573 QList<QgsColorRampShader::ColorRampItem> itemList = mColorRampShader.colorRampItemList();
575 if ( itemList.isEmpty() || itemList.first().value > value )
579 itemList.removeLast();
581 if ( value > itemList.last().value )
582 return itemList.count() - 1;
584 int indSup = itemList.count() - 1;
589 if ( abs( indSup - indInf ) <= 1 )
592 int newInd = ( indInf + indSup ) / 2;
594 if ( std::isnan( itemList.at( newInd ).value ) )
597 if ( itemList.at( newInd ).value <= value )
604void QgsInterpolatedLineColor::graduatedColorsExact(
double value1,
double value2, QList<double> &breakValues, QList<QColor> &breakColors,
const QList<QLinearGradient> &gradients )
const
607 Q_ASSERT( breakValues.isEmpty() );
608 Q_ASSERT( breakColors.isEmpty() );
609 Q_ASSERT( gradients.isEmpty() );
611 const QList<QgsColorRampShader::ColorRampItem> &itemList = mColorRampShader.colorRampItemList();
612 if ( itemList.isEmpty() )
615 int index = itemColorIndexInf( value1 );
616 if ( index < 0 || !
qgsDoubleNear( value1, itemList.at( index ).value ) )
622 breakColors.append( itemList.at( index ).color );
626 while ( index < itemList.count() && itemList.at( index ).value <= value2 )
628 breakValues.append( itemList.at( index ).value );
629 breakColors.append( itemList.at( index ).color );
634void QgsInterpolatedLineColor::graduatedColorsInterpolated(
double value1,
double value2, QList<double> &breakValues, QList<QColor> &breakColors, QList<QLinearGradient> &gradients )
const
637 Q_ASSERT( breakValues.isEmpty() );
638 Q_ASSERT( breakColors.isEmpty() );
639 Q_ASSERT( gradients.isEmpty() );
642 const QList<QgsColorRampShader::ColorRampItem> &itemList = mColorRampShader.colorRampItemList();
643 if ( itemList.empty() )
646 if ( itemList.count() == 1 )
648 breakColors.append( itemList.first().color );
652 if ( value2 <= itemList.first().value )
654 if ( !mColorRampShader.clip() )
655 breakColors.append( itemList.first().color );
659 if ( value1 > itemList.last().value )
661 if ( !mColorRampShader.clip() )
662 breakColors.append( itemList.last().color );
672 if ( mColorRampShader.shade( value1, &r, &g, &b, &a ) )
673 color = QColor( r, g, b, a );
674 breakColors.append(
color );
679 int index = itemColorIndexInf( value1 );
682 QColor
color = itemList.first().color;
683 breakColors.append(
color );
684 if ( mColorRampShader.clip() )
685 breakValues.append( itemList.first().value );
687 breakValues.append( value1 );
694 if ( mColorRampShader.shade( value1, &r, &g, &b, &a ) )
695 color = QColor( r, g, b, a );
696 breakValues.append( value1 );
697 breakColors.append(
color );
702 while ( index < itemList.count() && itemList.at( index ).value < value2 )
704 QColor color1 = breakColors.last();
705 QColor color2 = itemList.at( index ).color;
706 breakValues.append( itemList.at( index ).value );
707 breakColors.append( color2 );
708 gradients.append( makeSimpleLinearGradient( color1, color2 ) );
713 QColor color1 = breakColors.last();
715 if ( value2 < itemList.last().value )
718 if ( mColorRampShader.shade( value2, &r, &g, &b, &a ) )
719 color2 = QColor( r, g, b, a );
720 breakValues.append( value2 );
724 color2 = itemList.last().color;
725 if ( mColorRampShader.clip() )
726 breakValues.append( itemList.last().value );
728 breakValues.append( value2 );
730 breakColors.append( color2 );
731 gradients.append( makeSimpleLinearGradient( color1, color2 ) );
735void QgsInterpolatedLineColor::graduatedColorsDiscrete(
double value1,
double value2, QList<double> &breakValues, QList<QColor> &breakColors, QList<QLinearGradient> &gradients )
const
738 Q_ASSERT( breakValues.isEmpty() );
739 Q_ASSERT( breakColors.isEmpty() );
740 Q_ASSERT( gradients.isEmpty() );
742 const QList<QgsColorRampShader::ColorRampItem> &itemList = mColorRampShader.colorRampItemList();
743 if ( itemList.empty() )
746 if ( itemList.count() == 1 )
748 breakColors.append( itemList.first().color );
752 double lastValue = itemList.at( itemList.count() - 2 ).value;
755 if ( value2 <= itemList.first().value )
757 breakColors.append( itemList.first().color );
761 if ( value1 > lastValue )
763 breakColors.append( itemList.last().color );
768 int index = itemColorIndexInf( value1 );
774 breakColors.append( itemList.at( index + 1 ).color );
780 breakValues.append( value1 );
781 breakColors.append( itemList.first().color );
785 QColor
color = itemList.at( index ).color;
786 breakValues.append( value1 );
787 breakColors.append(
color );
792 while ( index < ( itemList.count() - 1 ) && itemList.at( index ).value < value2 )
794 QColor
color = itemList.at( index ).color;
795 breakValues.append( itemList.at( index ).value );
796 breakColors.append(
color );
797 gradients.append( makeSimpleLinearGradient(
color,
color ) );
802 QColor lastColor = itemList.at( index ).color;
803 breakColors.append( lastColor );
804 breakValues.append( value2 );
805 gradients.append( makeSimpleLinearGradient( lastColor, lastColor ) );
810 return u
"InterpolatedLine"_s;
821 auto l = qgis::unique_ptr_static_cast<QgsInterpolatedLineSymbolLayer>(
create(
properties() ) );
828 std::unique_ptr<QgsInterpolatedLineSymbolLayer> symbolLayer;
829 symbolLayer = std::make_unique<QgsInterpolatedLineSymbolLayer>();
831 if (
properties.contains( u
"start_width_expression"_s ) )
833 if (
properties.contains( u
"end_width_expression"_s ) )
836 if (
properties.contains( u
"start_color_expression"_s ) )
838 if (
properties.contains( u
"end_color_expression"_s ) )
842 symbolLayer->mLineRender.mStrokeWidth.setFixedStrokeWidth(
properties.value( u
"line_width"_s ).toDouble() );
843 if (
properties.contains( u
"line_width_unit"_s ) )
845 if (
properties.contains( u
"width_varying_minimum_value"_s ) )
846 symbolLayer->mLineRender.mStrokeWidth.setMinimumValue(
properties.value( u
"width_varying_minimum_value"_s ).toDouble() );
847 if (
properties.contains( u
"width_varying_maximum_value"_s ) )
848 symbolLayer->mLineRender.mStrokeWidth.setMaximumValue(
properties.value( u
"width_varying_maximum_value"_s ).toDouble() );
849 if (
properties.contains( u
"width_varying_use_absolute_value"_s ) )
850 symbolLayer->mLineRender.mStrokeWidth.setUseAbsoluteValue(
properties.value( u
"width_varying_use_absolute_value"_s ).toInt() );
851 if (
properties.contains( u
"width_varying_minimum_width"_s ) )
852 symbolLayer->mLineRender.mStrokeWidth.setMinimumWidth(
properties.value( u
"width_varying_minimum_width"_s ).toDouble() );
853 if (
properties.contains( u
"width_varying_maximum_width"_s ) )
854 symbolLayer->mLineRender.mStrokeWidth.setMaximumWidth(
properties.value( u
"width_varying_maximum_width"_s ).toDouble() );
855 if (
properties.contains( u
"width_varying_ignore_out_of_range"_s ) )
856 symbolLayer->mLineRender.mStrokeWidth.setIgnoreOutOfRange(
properties.value( u
"width_varying_ignore_out_of_range"_s ).toInt() );
857 if (
properties.contains( u
"width_varying_is_variable_width"_s ) )
858 symbolLayer->mLineRender.mStrokeWidth.setIsVariableWidth(
properties.value( u
"width_varying_is_variable_width"_s ).toInt() );
860 if (
properties.contains( u
"single_color"_s ) )
862 if (
properties.contains( u
"color_ramp_shader"_s ) )
863 symbolLayer->mLineRender.mStrokeColoring.setColor( createColorRampShaderFromProperties(
properties.value( u
"color_ramp_shader"_s ) ) );
864 if (
properties.contains( u
"coloring_method"_s ) )
880 props.insert( u
"line_width"_s, QString::number( mLineRender.mStrokeWidth.fixedStrokeWidth() ) );
882 props.insert( u
"width_varying_minimum_value"_s, mLineRender.mStrokeWidth.minimumValue() );
883 props.insert( u
"width_varying_maximum_value"_s, mLineRender.mStrokeWidth.maximumValue() );
884 props.insert( u
"width_varying_use_absolute_value"_s, mLineRender.mStrokeWidth.useAbsoluteValue() ? 1 : 0 );
885 props.insert( u
"width_varying_minimum_width"_s, mLineRender.mStrokeWidth.minimumWidth() );
886 props.insert( u
"width_varying_maximum_width"_s, mLineRender.mStrokeWidth.maximumWidth() );
887 props.insert( u
"width_varying_ignore_out_of_range"_s, mLineRender.mStrokeWidth.ignoreOutOfRange() ? 1 : 0 );
888 props.insert( u
"width_varying_is_variable_width"_s, mLineRender.mStrokeWidth.isVariableWidth() ? 1 : 0 );
891 props.insert( u
"coloring_method"_s, mLineRender.mStrokeColoring.coloringMethod() );
893 props.insert( u
"color_ramp_shader"_s, colorRampShaderProperties() );
903 double min = std::min( mLineRender.interpolatedLineWidth().minimumValue(), mLineRender.interpolatedColor().colorRampShader().minimumValue() );
904 double max = std::max( mLineRender.interpolatedLineWidth().maximumValue(), mLineRender.interpolatedColor().colorRampShader().maximumValue() );
906 double totalLength = geometry.
length();
910 double variation = ( max - min ) / totalLength;
913 double lengthFromStart = 0;
914 for (
int i = 1; i < points.count(); ++i )
916 QPointF p1 = points.at( i - 1 );
917 QPointF p2 = points.at( i );
919 double v1 = min + variation * lengthFromStart;
920 QPointF vectDist = p2 - p1;
921 lengthFromStart += sqrt( pow( vectDist.x(), 2 ) + pow( vectDist.y(), 2 ) );
922 double v2 = min + variation * lengthFromStart;
923 mLineRender.renderInDeviceCoordinates( v1, v2, v1, v2, p1, p2, context.
renderContext() );
931 switch ( mLineRender.interpolatedColor().coloringMethod() )
934 return mLineRender.interpolatedColor().singleColor();
944 if ( start.isEmpty() )
967 mLineRender.mStrokeWidthUnit = strokeWidthUnit;
973 return mLineRender.widthUnit();
978 mLineRender.mStrokeWidth = interpolatedLineWidth;
983 return mLineRender.interpolatedLineWidth();
988 if ( start.isEmpty() )
1011 mLineRender.setInterpolatedColor( interpolatedLineColor );
1016 return mLineRender.interpolatedColor();
1019QVariant QgsInterpolatedLineSymbolLayer::colorRampShaderProperties()
const
1026 props.insert( u
"color_ramp_shader_type"_s,
static_cast< int >( colorRampShader.
colorRampType() ) );
1027 props.insert( u
"color_ramp_shader_classification_mode"_s,
static_cast< int >( colorRampShader.
classificationMode() ) );
1028 QVariantList colorRampItemListVariant;
1030 const QList<QgsColorRampShader::ColorRampItem> colorRampItemList = colorRampShader.
colorRampItemList();
1033 QVariantMap itemVar;
1034 itemVar[u
"label"_s] = item.label;
1036 itemVar[u
"value"_s] = item.value;
1037 colorRampItemListVariant.append( itemVar );
1039 props.insert( u
"color_ramp_shader_items_list"_s, colorRampItemListVariant );
1041 props.insert( u
"color_ramp_shader_minimum_value"_s, colorRampShader.
minimumValue() );
1042 props.insert( u
"color_ramp_shader_maximum_value"_s, colorRampShader.
maximumValue() );
1043 props.insert( u
"color_ramp_shader_value_out_of_range"_s, colorRampShader.
clip() ? 1 : 0 );
1044 props.insert( u
"color_ramp_shader_label_precision"_s, colorRampShader.
labelPrecision() );
1049QgsColorRampShader QgsInterpolatedLineSymbolLayer::createColorRampShaderFromProperties(
const QVariant &properties )
1051 QgsColorRampShader colorRampShader;
1053 if (
properties.userType() != QMetaType::Type::QVariantMap )
1054 return colorRampShader;
1056 QVariantMap shaderVariantMap =
properties.toMap();
1058 if ( shaderVariantMap.contains( u
"color_ramp_source"_s ) )
1061 if ( shaderVariantMap.contains( u
"color_ramp_shader_type"_s ) )
1063 if ( shaderVariantMap.contains( u
"color_ramp_shader_classification_mode"_s ) )
1066 if ( shaderVariantMap.contains( u
"color_ramp_shader_items_list"_s ) )
1068 QVariant colorRampItemsVar = shaderVariantMap.value( u
"color_ramp_shader_items_list"_s );
1069 if ( colorRampItemsVar.userType() == QMetaType::Type::QVariantList )
1071 QVariantList itemVariantList = colorRampItemsVar.toList();
1072 QList<QgsColorRampShader::ColorRampItem> colorRampItemList;
1073 for (
const QVariant &itemVar : std::as_const( itemVariantList ) )
1075 QgsColorRampShader::ColorRampItem item;
1076 if ( itemVar.userType() != QMetaType::Type::QVariantMap )
1078 QVariantMap itemVarMap = itemVar.toMap();
1079 if ( !itemVarMap.contains( u
"label"_s ) || !itemVarMap.contains( u
"color"_s ) || !itemVarMap.contains( u
"value"_s ) )
1082 item.
label = itemVarMap.value( u
"label"_s ).toString();
1084 item.
value = itemVarMap.value( u
"value"_s ).toDouble();
1086 colorRampItemList.append( item );
1092 if ( shaderVariantMap.contains( u
"color_ramp_shader_minimum_value"_s ) )
1093 colorRampShader.
setMinimumValue( shaderVariantMap.value( u
"color_ramp_shader_minimum_value"_s ).toDouble() );
1095 colorRampShader.
setMinimumValue( std::numeric_limits<double>::quiet_NaN() );
1097 if ( shaderVariantMap.contains( u
"color_ramp_shader_maximum_value"_s ) )
1098 colorRampShader.
setMaximumValue( shaderVariantMap.value( u
"color_ramp_shader_maximum_value"_s ).toDouble() );
1100 colorRampShader.
setMaximumValue( std::numeric_limits<double>::quiet_NaN() );
1102 if ( shaderVariantMap.contains( u
"color_ramp_shader_value_out_of_range"_s ) )
1103 colorRampShader.
setClip( shaderVariantMap.value( u
"color_ramp_shader_value_out_of_range"_s ).toInt() == 1 );
1104 if ( shaderVariantMap.contains( u
"color_ramp_shader_label_precision"_s ) )
1105 colorRampShader.
setLabelPrecision( shaderVariantMap.value( u
"color_ramp_shader_label_precision"_s ).toInt() );
1107 return colorRampShader;
1117 mRenderingFeature =
true;
1123 mRenderingFeature =
false;
1125 if ( mLineParts.empty() )
1128 render( mLineParts, context );
1132void QgsInterpolatedLineSymbolLayer::render(
const QVector< QPolygonF > &parts,
QgsRenderContext &context )
1134 const double totalLength = std::accumulate( parts.begin(), parts.end(), 0.0, [](
double total,
const QPolygonF &part ) { return total + QgsSymbolLayerUtils::polylineLength( part ); } );
1139 double startValWidth = 0;
1140 double variationPerMapUnitWidth = 0;
1141 double startValColor = 0;
1142 double variationPerMapUnitColor = 0;
1155 double endValWidth = 0;
1163 variationPerMapUnitWidth = ( endValWidth - startValWidth ) / totalLength;
1175 double endValColor = 0;
1183 variationPerMapUnitColor = ( endValColor - startValColor ) / totalLength;
1186 for (
const QPolygonF &poly : parts )
1188 double lengthFromStart = 0;
1189 for (
int i = 1; i < poly.count(); ++i )
1191 const QPointF p1 = poly.at( i - 1 );
1192 const QPointF p2 = poly.at( i );
1194 const double v1c = startValColor + variationPerMapUnitColor * lengthFromStart;
1195 const double v1w = startValWidth + variationPerMapUnitWidth * lengthFromStart;
1197 const double v2c = startValColor + variationPerMapUnitColor * lengthFromStart;
1198 const double v2w = startValWidth + variationPerMapUnitWidth * lengthFromStart;
1199 mLineRender.renderInDeviceCoordinates( v1c, v2c, v1w, v2w, p1, p2, context );
1206 mLineRender.setSelected( context.
selected() );
1208 if ( points.empty() )
1211 if ( mRenderingFeature )
1215 mLineParts.append( points );
@ DisableFeatureClipping
If present, indicates that features should never be clipped to the map extent during rendering.
@ CanCalculateMaskGeometryPerFeature
If present, indicates that mask geometry can safely be calculated per feature for the symbol layer....
ShaderInterpolationMethod
Color ramp shader interpolation methods.
@ Exact
Assigns the color of the exact matching value in the color ramp item list.
@ Linear
Interpolates the color between two class breaks linearly.
@ Discrete
Assigns the color of the higher class for every pixel between two class breaks.
QFlags< SymbolLayerFlag > SymbolLayerFlags
Symbol layer flags.
ShaderClassificationMethod
Color ramp shader classification methods.
RenderUnit
Rendering size units.
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...
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.
QgsInterpolatedLineSymbolLayer()
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.
static std::unique_ptr< QgsSymbolLayer > create(const QVariantMap &properties)
Creates the symbol layer.
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.
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.
QPointF toQPointF() const
Converts a point to a QPointF.
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.
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
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
bool qgsDoubleNear(double a, double b, double epsilon=4 *std::numeric_limits< double >::epsilon())
Compare two doubles (but allow some difference).