39 , mSettings( settings )
40 , mMinimumValue( minimumValue )
41 , mMaximumValue( maximumValue )
51 connect( nodeLayer, &QObject::destroyed,
this, [ = ]() {
mLayerNode =
nullptr; } );
69 QString QgsColorRampLegendNode::labelForMinimum()
const
78 QString QgsColorRampLegendNode::labelForMaximum()
const
89 if ( role == Qt::DisplayRole )
93 else if ( role == Qt::EditRole )
97 else if ( role == Qt::DecorationRole )
99 if ( mPixmap.isNull() || mPixmap.size() != mIconSize )
101 const QFont font =
data( Qt::FontRole ).value< QFont >();
103 const QString minLabel = labelForMinimum();
104 const QString maxLabel = labelForMaximum();
106 const QFontMetrics fm( font );
108 const QRect minBoundingRect = fm.boundingRect( minLabel );
109 const QRect maxBoundingRect = fm.boundingRect( maxLabel );
111 const int minLabelWidth = minBoundingRect.width();
112 const int maxLabelWidth = maxBoundingRect.width();
113 const int maxTextWidth = std::max( minLabelWidth, maxLabelWidth );
114 const int labelGapFromRamp = fm.boundingRect( QStringLiteral(
"x" ) ).width();
115 const int extraAllowance = labelGapFromRamp * 0.4;
121 labelRect = QRect( mIconSize.width() + labelGapFromRamp, 0, maxTextWidth + extraAllowance, mIconSize.height() );
122 mPixmap = QPixmap( mIconSize.width() + maxTextWidth + labelGapFromRamp + extraAllowance, mIconSize.height() );
123 rampSize = mIconSize;
127 labelRect = QRect( 0, mIconSize.height() + labelGapFromRamp, std::max( mIconSize.width(), minLabelWidth + maxLabelWidth + labelGapFromRamp ), std::max( minBoundingRect.height(),
128 maxBoundingRect.height() ) + extraAllowance );
129 mPixmap = QPixmap( std::max( mIconSize.width(), minLabelWidth + maxLabelWidth + labelGapFromRamp ), mIconSize.height() + maxTextWidth + labelGapFromRamp + extraAllowance );
130 rampSize = QSize( labelRect.width(), mIconSize.height() );
134 mPixmap.fill( Qt::transparent );
147 pix = QPixmap( rampSize );
148 pix.fill( Qt::transparent );
151 QPainter p( &mPixmap );
152 p.drawPixmap( 0, 0, pix );
189 std::unique_ptr< QgsRenderContext > tempRenderContext;
194 tempRenderContext = std::make_unique< QgsRenderContext >();
197 tempRenderContext->setScaleFactor(
settings.dpi() / 25.4 );
198 tempRenderContext->setRendererScale(
settings.mapScale() );
200 tempRenderContext->setMapToPixel(
QgsMapToPixel( 1 / (
settings.mmPerMapUnit() * tempRenderContext->scaleFactor() ) ) );
202 tempRenderContext->setForceVectorOutput(
true );
203 tempRenderContext->setPainter( ctx ? ctx->
painter :
nullptr );
208 tempRenderContext->setExpressionContext( expContext );
209 context = tempRenderContext.get();
217 const QString minLabel = labelForMinimum();
218 const QString maxLabel = labelForMaximum();
223 double minHeightMm = 0;
224 double minWidthMm = 0;
225 double rampHeight = 0;
226 double rampWidth = 0;
232 rampHeight = ctx && ctx->
patchSize.height() > 0 ? std::max( minHeightMm / 2, ctx->
patchSize.height() ) : std::max( minHeightMm,
settings.symbolSize().height() );
233 rampWidth = patchWidth;
240 rampHeight = patchHeight;
241 rampWidth = std::max( minWidthMm, patchWidth );
247 const double currentYCoord = ctx->
top;
255 opacity = layer->opacity();
260 double rampLeftMm = 0;
261 const double rampTopMm = currentYCoord;
262 switch (
settings.symbolAlignment() )
274 p->scale( 1.0 / dotsPerMM, 1.0 / dotsPerMM );
276 QLinearGradient gradient;
281 const double gradientTop = rampTopMm * dotsPerMM;
282 const double gradientBottom = gradientTop + rampHeight * dotsPerMM;
290 const double gradientLeft = rampLeftMm * dotsPerMM;
291 const double gradientRight = gradientLeft + rampWidth * dotsPerMM;
309 pen.setColor(
settings.rasterStrokeColor() );
310 pen.setWidthF(
settings.rasterStrokeWidth() * dotsPerMM );
311 pen.setJoinStyle( Qt::MiterJoin );
316 ctx->
painter->setPen( Qt::NoPen );
319 p->setBrush( QBrush( gradient ) );
320 p->drawRect( rampLeftMm * dotsPerMM, rampTopMm * dotsPerMM, rampWidth * dotsPerMM, rampHeight * dotsPerMM );
323 double labelHeight = 0;
329 const double currentYCoord = ctx->
top;
338 p->scale( 1.0 / dotsPerMM, 1.0 / dotsPerMM );
340 double labelXMin = 0;
341 double labelXMax = 0;
349 switch (
settings.symbolAlignment() )
363 const QRectF textRect( labelXMin * dotsPerMM, labelYMin * dotsPerMM, ( labelXMax - labelXMin ) * dotsPerMM, labelHeight * dotsPerMM );
381 return QSizeF( rampWidth, rampHeight + labelHeight );
386 if ( !mRamp || mSettings.
orientation() == Qt::Horizontal )
393 std::unique_ptr< QgsRenderContext > tempRenderContext;
398 tempRenderContext = std::make_unique< QgsRenderContext >();
401 tempRenderContext->setScaleFactor(
settings.dpi() / 25.4 );
402 tempRenderContext->setRendererScale(
settings.mapScale() );
404 tempRenderContext->setMapToPixel(
QgsMapToPixel( 1 / (
settings.mmPerMapUnit() * tempRenderContext->scaleFactor() ) ) );
406 tempRenderContext->setForceVectorOutput(
true );
407 tempRenderContext->setPainter( ctx ? ctx->
painter :
nullptr );
412 tempRenderContext->setExpressionContext( expContext );
413 context = tempRenderContext.get();
421 const QString minLabel = labelForMinimum();
422 const QString maxLabel = labelForMaximum();
424 const double rampHeight = symbolSize.height();
425 const double rampWidth = symbolSize.width();
426 double textWidth = 0;
427 double textHeight = 0;
431 const double currentYCoord = ctx->
top;
440 p->scale( 1.0 / dotsPerMM, 1.0 / dotsPerMM );
442 double labelXMin = 0;
443 double labelXMax = 0;
444 switch (
settings.symbolAlignment() )
465 const QRectF textRect( labelXMin * dotsPerMM, currentYCoord * dotsPerMM, ( labelXMax - labelXMin ) * dotsPerMM, rampHeight * dotsPerMM );
477 textHeight = rampHeight;
480 return QSizeF( textWidth, textHeight );