38 , mSettings( settings )
39 , mMinimumValue( minimumValue )
40 , mMaximumValue( maximumValue )
50 connect( nodeLayer, &QObject::destroyed,
this, [ = ]() {
mLayerNode =
nullptr; } );
68 QString QgsColorRampLegendNode::labelForMinimum()
const
77 QString QgsColorRampLegendNode::labelForMaximum()
const
88 if ( role == Qt::DisplayRole )
92 else if ( role == Qt::EditRole )
96 else if ( role == Qt::DecorationRole )
98 if ( mPixmap.isNull() || mPixmap.size() != mIconSize )
100 const QFont font =
data( Qt::FontRole ).value< QFont >();
102 const QString minLabel = labelForMinimum();
103 const QString maxLabel = labelForMaximum();
105 const QFontMetrics fm( font );
107 const QRect minBoundingRect = fm.boundingRect( minLabel );
108 const QRect maxBoundingRect = fm.boundingRect( maxLabel );
110 const int minLabelWidth = minBoundingRect.width();
111 const int maxLabelWidth = maxBoundingRect.width();
112 const int maxTextWidth = std::max( minLabelWidth, maxLabelWidth );
113 const int labelGapFromRamp = fm.boundingRect( QStringLiteral(
"x" ) ).width();
114 const int extraAllowance = labelGapFromRamp * 0.4;
120 labelRect = QRect( mIconSize.width() + labelGapFromRamp, 0, maxTextWidth + extraAllowance, mIconSize.height() );
121 mPixmap = QPixmap( mIconSize.width() + maxTextWidth + labelGapFromRamp + extraAllowance, mIconSize.height() );
122 rampSize = mIconSize;
126 labelRect = QRect( 0, mIconSize.height() + labelGapFromRamp, std::max( mIconSize.width(), minLabelWidth + maxLabelWidth + labelGapFromRamp ), std::max( minBoundingRect.height(),
127 maxBoundingRect.height() ) + extraAllowance );
128 mPixmap = QPixmap( std::max( mIconSize.width(), minLabelWidth + maxLabelWidth + labelGapFromRamp ), mIconSize.height() + maxTextWidth + labelGapFromRamp + extraAllowance );
129 rampSize = QSize( labelRect.width(), mIconSize.height() );
133 mPixmap.fill( Qt::transparent );
146 pix = QPixmap( rampSize );
147 pix.fill( Qt::transparent );
150 QPainter p( &mPixmap );
151 p.drawPixmap( 0, 0, pix );
188 std::unique_ptr< QgsRenderContext > tempRenderContext;
193 tempRenderContext = std::make_unique< QgsRenderContext >();
196 tempRenderContext->setScaleFactor(
settings.dpi() / 25.4 );
197 tempRenderContext->setRendererScale(
settings.mapScale() );
199 tempRenderContext->setMapToPixel(
QgsMapToPixel( 1 / (
settings.mmPerMapUnit() * tempRenderContext->scaleFactor() ) ) );
201 tempRenderContext->setForceVectorOutput(
true );
202 tempRenderContext->setPainter( ctx ? ctx->
painter :
nullptr );
207 tempRenderContext->setExpressionContext( expContext );
208 context = tempRenderContext.get();
216 const QString minLabel = labelForMinimum();
217 const QString maxLabel = labelForMaximum();
222 double minHeightMm = 0;
223 double minWidthMm = 0;
224 double rampHeight = 0;
225 double rampWidth = 0;
231 rampHeight = ctx && ctx->
patchSize.height() > 0 ? std::max( minHeightMm / 2, ctx->
patchSize.height() ) : std::max( minHeightMm,
settings.symbolSize().height() );
232 rampWidth = patchWidth;
239 rampHeight = patchHeight;
240 rampWidth = std::max( minWidthMm, patchWidth );
246 const double currentYCoord = ctx->
top;
254 opacity = layer->opacity();
259 double rampLeftMm = 0;
260 const double rampTopMm = currentYCoord;
261 switch (
settings.symbolAlignment() )
273 p->scale( 1.0 / dotsPerMM, 1.0 / dotsPerMM );
275 QLinearGradient gradient;
280 const double gradientTop = rampTopMm * dotsPerMM;
281 const double gradientBottom = gradientTop + rampHeight * dotsPerMM;
289 const double gradientLeft = rampLeftMm * dotsPerMM;
290 const double gradientRight = gradientLeft + rampWidth * dotsPerMM;
308 pen.setColor(
settings.rasterStrokeColor() );
309 pen.setWidthF(
settings.rasterStrokeWidth() * dotsPerMM );
310 pen.setJoinStyle( Qt::MiterJoin );
315 ctx->
painter->setPen( Qt::NoPen );
318 p->setBrush( QBrush( gradient ) );
319 p->drawRect( rampLeftMm * dotsPerMM, rampTopMm * dotsPerMM, rampWidth * dotsPerMM, rampHeight * dotsPerMM );
322 double labelHeight = 0;
328 const double currentYCoord = ctx->
top;
337 p->scale( 1.0 / dotsPerMM, 1.0 / dotsPerMM );
339 double labelXMin = 0;
340 double labelXMax = 0;
348 switch (
settings.symbolAlignment() )
362 const QRectF textRect( labelXMin * dotsPerMM, labelYMin * dotsPerMM, ( labelXMax - labelXMin ) * dotsPerMM, labelHeight * dotsPerMM );
380 return QSizeF( rampWidth, rampHeight + labelHeight );
385 if ( !mRamp || mSettings.
orientation() == Qt::Horizontal )
392 std::unique_ptr< QgsRenderContext > tempRenderContext;
397 tempRenderContext = std::make_unique< QgsRenderContext >();
400 tempRenderContext->setScaleFactor(
settings.dpi() / 25.4 );
401 tempRenderContext->setRendererScale(
settings.mapScale() );
403 tempRenderContext->setMapToPixel(
QgsMapToPixel( 1 / (
settings.mmPerMapUnit() * tempRenderContext->scaleFactor() ) ) );
405 tempRenderContext->setForceVectorOutput(
true );
406 tempRenderContext->setPainter( ctx ? ctx->
painter :
nullptr );
411 tempRenderContext->setExpressionContext( expContext );
412 context = tempRenderContext.get();
420 const QString minLabel = labelForMinimum();
421 const QString maxLabel = labelForMaximum();
423 const double rampHeight = symbolSize.height();
424 const double rampWidth = symbolSize.width();
425 double textWidth = 0;
426 double textHeight = 0;
430 const double currentYCoord = ctx->
top;
439 p->scale( 1.0 / dotsPerMM, 1.0 / dotsPerMM );
441 double labelXMin = 0;
442 double labelXMax = 0;
443 switch (
settings.symbolAlignment() )
464 const QRectF textRect( labelXMin * dotsPerMM, currentYCoord * dotsPerMM, ( labelXMax - labelXMin ) * dotsPerMM, rampHeight * dotsPerMM );
476 textHeight = rampHeight;
479 return QSizeF( textWidth, textHeight );
@ Antialiasing
Use antialiasing while drawing.
Settings for a color ramp legend node.
void setMaximumLabel(const QString &label)
Sets the label for the maximum value on the ramp.
const QgsNumericFormat * numericFormat() const
Returns the numeric format used for numbers in the scalebar.
QString maximumLabel() const
Returns the label for the maximum value on the ramp.
QString suffix() const
Returns the suffix to show after legend text.
@ MaximumToMinimum
Maximum value on bottom, minimum value on top.
@ MinimumToMaximum
Minimum value on bottom, maximum value on top.
QString prefix() const
Returns the prefix to show before legend text.
Qt::Orientation orientation() const
Returns the ramp orientation (i.e.
QgsColorRampLegendNodeSettings::Direction direction() const
Returns the direction of the ramp.
QgsTextFormat textFormat() const
Returns the text format used to render text in the legend item.
void setMinimumLabel(const QString &label)
Sets the label for the minimum value on the ramp.
QString minimumLabel() const
Returns the label for the minimum value on the ramp.
QSizeF drawSymbolText(const QgsLegendSettings &settings, ItemContext *ctx, QSizeF symbolSize) const override
Draws label on the right side of the item.
const QgsColorRamp * ramp() const
Returns the color ramp used by the node.
QSize iconSize() const
Returns the icon size, which is how large the ramp will render in a layer tree widget.
void setSettings(const QgsColorRampLegendNodeSettings &settings)
Sets the node's settings.
QgsColorRampLegendNodeSettings settings() const
Returns the node's settings.
QVariant data(int role) const override
Returns data associated with the item. Must be implemented in derived class.
QSizeF drawSymbol(const QgsLegendSettings &settings, ItemContext *ctx, double itemHeight) const override
Draws symbol on the left side of the item.
QgsColorRampLegendNode(QgsLayerTreeLayer *nodeLayer, QgsColorRamp *ramp, const QString &minimumLabel, const QString &maximumLabel, QObject *parent=nullptr)
Constructor for QgsColorRampLegendNode.
Abstract base class for color ramps.
static QString typeString()
Returns the string identifier for QgsCptCityColorRamp.
static QList< QgsExpressionContextScope * > globalProjectLayerScopes(const QgsMapLayer *layer)
Creates a list of three scopes: global, layer's project and layer.
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
void appendScopes(const QList< QgsExpressionContextScope * > &scopes)
Appends a list of scopes to the end of the context.
Gradient color ramp, which smoothly interpolates between two colors and also supports optional extra ...
static QString typeString()
Returns the string identifier for QgsGradientColorRamp.
void addStopsToGradient(QGradient *gradient, double opacity=1)
Copy color ramp stops to a QGradient.
Layer tree node points to a map layer.
The QgsLegendRendererItem class is abstract interface for legend items returned from QgsMapLayerLegen...
@ ColorRampLegend
Color ramp legend (since QGIS 3.18)
QgsLayerTreeLayer * layerNode() const
Returns pointer to the parent layer node.
QgsLayerTreeLayer * mLayerNode
@ NodeTypeRole
Type of node. Added in 3.16.
static int scaleIconSize(int standardSize)
Scales an layer tree model icon size to compensate for display pixel density, making the icon size hi...
The QgsLegendSettings class stores the appearance and layout settings for legend drawing with QgsLege...
@ Symbol
Symbol icon (excluding label)
@ SymbolLabel
Symbol label (excluding icon)
Base class for all map layer types.
Perform transforms between map coordinates and device coordinates.
A context for numeric formats.
Contains information about the context of a rendering operation.
double scaleFactor() const
Returns the scaling factor for the render to convert painter units to physical sizes.
void setPainterFlagsUsingContext(QPainter *painter=nullptr) const
Sets relevant flags on a destination painter, using the flags and settings currently defined for the ...
Scoped object for saving and restoring a QPainter object's state.
static QPixmap colorRampPreviewPixmap(QgsColorRamp *ramp, QSize size, int padding=0, Qt::Orientation direction=Qt::Horizontal, bool flipDirection=false, bool drawTransparentBackground=true)
Returns a pixmap preview for a color ramp.
Container for all settings relating to text rendering.
void setColor(const QColor &color)
Sets the color that text will be rendered in.
bool isValid() const
Returns true if the format is valid.
static QgsTextFormat fromQFont(const QFont &font)
Returns a text format matching the settings from an input font.
static double textWidth(const QgsRenderContext &context, const QgsTextFormat &format, const QStringList &textLines, QFontMetricsF *fontMetrics=nullptr)
Returns the width of a text based on a given format.
@ AlignBottom
Align to bottom.
static double textHeight(const QgsRenderContext &context, const QgsTextFormat &format, const QStringList &textLines, DrawMode mode=Point, QFontMetricsF *fontMetrics=nullptr)
Returns the height of a text based on a given format.
static HAlignment convertQtHAlignment(Qt::Alignment alignment)
Converts a Qt horizontal alignment flag to a QgsTextRenderer::HAlignment value.
static void drawText(const QRectF &rect, double rotation, HAlignment alignment, const QStringList &textLines, QgsRenderContext &context, const QgsTextFormat &format, bool drawAsOutlines=true, VAlignment vAlignment=AlignTop)
Draws text within a rectangle using the specified settings.
@ Rect
Text within rectangle draw mode.
#define Q_NOWARN_DEPRECATED_POP
#define Q_NOWARN_DEPRECATED_PUSH
QPainter * painter
Painter.
double top
Top y-position of legend item.
double maxSiblingSymbolWidth
Largest symbol width, considering all other sibling legend components associated with the current com...
QSizeF patchSize
Symbol patch size to render for the node.
double columnLeft
Left side of current legend column.
double columnRight
Right side of current legend column.
Q_NOWARN_DEPRECATED_POP QgsRenderContext * context
Render context, if available.