34 , mParentKey( parentKey )
45 , mSettings( settings )
46 , mMinimumValue( minimumValue )
47 , mMaximumValue( maximumValue )
49 , mParentKey( parentKey )
59 connect( nodeLayer, &QObject::destroyed,
this, [
this]() {
mLayerNode =
nullptr; } );
77QString QgsColorRampLegendNode::labelForMinimum()
const
86QString QgsColorRampLegendNode::labelForMaximum()
const
97 if ( role == Qt::DisplayRole )
101 else if ( role == Qt::EditRole )
105 else if ( role == Qt::DecorationRole )
107 if ( mPixmap.isNull() || mPixmap.size() != mIconSize )
109 const QFont font =
data( Qt::FontRole ).value< QFont >();
111 const QString minLabel = labelForMinimum();
112 const QString maxLabel = labelForMaximum();
114 const QFontMetrics fm( font );
116 const QRect minBoundingRect = fm.boundingRect( minLabel );
117 const QRect maxBoundingRect = fm.boundingRect( maxLabel );
119 const int minLabelWidth = minBoundingRect.width();
120 const int maxLabelWidth = maxBoundingRect.width();
121 const int maxTextWidth = std::max( minLabelWidth, maxLabelWidth );
122 const int labelGapFromRamp = fm.boundingRect( QStringLiteral(
"x" ) ).width();
123 const int extraAllowance = labelGapFromRamp * 0.4;
129 labelRect = QRect( mIconSize.width() + labelGapFromRamp, 0, maxTextWidth + extraAllowance, mIconSize.height() );
130 mPixmap = QPixmap( mIconSize.width() + maxTextWidth + labelGapFromRamp + extraAllowance, mIconSize.height() );
131 rampSize = mIconSize;
135 labelRect = QRect( 0, mIconSize.height() + labelGapFromRamp, std::max( mIconSize.width(), minLabelWidth + maxLabelWidth + labelGapFromRamp ), std::max( minBoundingRect.height(),
136 maxBoundingRect.height() ) + extraAllowance );
137 mPixmap = QPixmap( std::max( mIconSize.width(), minLabelWidth + maxLabelWidth + labelGapFromRamp ), mIconSize.height() + maxTextWidth + labelGapFromRamp + extraAllowance );
138 rampSize = QSize( labelRect.width(), mIconSize.height() );
142 mPixmap.fill( Qt::transparent );
155 pix = QPixmap( rampSize );
156 pix.fill( Qt::transparent );
159 QPainter p( &mPixmap );
160 p.drawPixmap( 0, 0, pix );
162 p.setPen( qApp->palette().color( QPalette::Text ) );
201 std::unique_ptr< QgsRenderContext > tempRenderContext;
206 tempRenderContext = std::make_unique< QgsRenderContext >();
209 tempRenderContext->setScaleFactor(
settings.dpi() / 25.4 );
210 tempRenderContext->setRendererScale(
settings.mapScale() );
212 tempRenderContext->setMapToPixel(
QgsMapToPixel( 1 / (
settings.mmPerMapUnit() * tempRenderContext->scaleFactor() ) ) );
214 tempRenderContext->setForceVectorOutput(
true );
215 tempRenderContext->setPainter( ctx ? ctx->
painter : nullptr );
220 tempRenderContext->setExpressionContext( expContext );
221 context = tempRenderContext.get();
225 const QString minLabel = labelForMinimum();
226 const QString maxLabel = labelForMaximum();
231 double minHeightMm = 0;
232 double minWidthMm = 0;
233 double rampHeight = 0;
234 double rampWidth = 0;
240 rampHeight = ctx && ctx->
patchSize.height() > 0 ? std::max( minHeightMm / 2, ctx->
patchSize.height() ) : std::max( minHeightMm,
settings.symbolSize().height() );
241 rampWidth = patchWidth;
248 rampHeight = patchHeight;
249 rampWidth = std::max( minWidthMm, patchWidth );
255 const double currentYCoord = ctx->
top;
263 opacity = layer->opacity();
268 double rampLeftMm = 0;
269 const double rampTopMm = currentYCoord;
270 switch (
settings.symbolAlignment() )
282 p->scale( 1.0 / dotsPerMM, 1.0 / dotsPerMM );
284 QLinearGradient gradient;
289 const double gradientTop = rampTopMm * dotsPerMM;
290 const double gradientBottom = gradientTop + rampHeight * dotsPerMM;
298 const double gradientLeft = rampLeftMm * dotsPerMM;
299 const double gradientRight = gradientLeft + rampWidth * dotsPerMM;
317 pen.setColor(
settings.rasterStrokeColor() );
318 pen.setWidthF(
settings.rasterStrokeWidth() * dotsPerMM );
319 pen.setJoinStyle( Qt::MiterJoin );
324 ctx->
painter->setPen( Qt::NoPen );
327 p->setBrush( QBrush( gradient ) );
328 p->drawRect( rampLeftMm * dotsPerMM, rampTopMm * dotsPerMM, rampWidth * dotsPerMM, rampHeight * dotsPerMM );
331 double labelHeight = 0;
337 const double currentYCoord = ctx->
top;
346 p->scale( 1.0 / dotsPerMM, 1.0 / dotsPerMM );
348 double labelXMin = 0;
349 double labelXMax = 0;
357 switch (
settings.symbolAlignment() )
371 const QRectF textRect( labelXMin * dotsPerMM, labelYMin * dotsPerMM, ( labelXMax - labelXMin ) * dotsPerMM, labelHeight * dotsPerMM );
389 return QSizeF( rampWidth, rampHeight + labelHeight );
394 if ( !mRamp || mSettings.
orientation() == Qt::Horizontal )
401 std::unique_ptr< QgsRenderContext > tempRenderContext;
406 tempRenderContext = std::make_unique< QgsRenderContext >();
409 tempRenderContext->setScaleFactor(
settings.dpi() / 25.4 );
410 tempRenderContext->setRendererScale(
settings.mapScale() );
412 tempRenderContext->setMapToPixel(
QgsMapToPixel( 1 / (
settings.mmPerMapUnit() * tempRenderContext->scaleFactor() ) ) );
414 tempRenderContext->setForceVectorOutput(
true );
415 tempRenderContext->setPainter( ctx ? ctx->
painter : nullptr );
420 tempRenderContext->setExpressionContext( expContext );
421 context = tempRenderContext.get();
426 const QString minLabel = labelForMinimum();
427 const QString maxLabel = labelForMaximum();
429 const double rampHeight = symbolSize.height();
430 const double rampWidth = symbolSize.width();
431 double textWidth = 0;
432 double textHeight = 0;
436 const double currentYCoord = ctx->
top;
445 p->scale( 1.0 / dotsPerMM, 1.0 / dotsPerMM );
447 double labelXMin = 0;
448 double labelXMax = 0;
449 switch (
settings.symbolAlignment() )
470 const QRectF textRect( labelXMin * dotsPerMM, currentYCoord * dotsPerMM, ( labelXMax - labelXMin ) * dotsPerMM, rampHeight * dotsPerMM );
482 textHeight = rampHeight;
485 return QSizeF( textWidth, textHeight );
495 const QPixmap icon =
data( Qt::DecorationRole ).value<QPixmap>();
497 if ( ! icon.isNull() )
499 const QImage image( icon.toImage() );
500 QByteArray byteArray;
501 QBuffer buffer( &byteArray );
502 image.save( &buffer,
"PNG" );
503 const QString base64 = QString::fromLatin1( byteArray.toBase64().data() );
504 json[ QStringLiteral(
"icon" ) ] = base64;
507 json [ QStringLiteral(
"min" ) ] = mMinimumValue;
508 json [ QStringLiteral(
"max" ) ] = mMaximumValue;
@ Rectangle
Text within rectangle layout mode.
@ 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.
QgsColorRampLegendNode(QgsLayerTreeLayer *nodeLayer, QgsColorRamp *ramp, const QString &minimumLabel, const QString &maximumLabel, QObject *parent=nullptr, const QString &key=QString(), const QString &parentKey=QString())
Constructor for QgsColorRampLegendNode.
QJsonObject exportSymbolToJson(const QgsLegendSettings &settings, const QgsRenderContext &context) const override
Adds a symbol in base64 string within a JSON object with the key "icon".
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.
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) const
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.
@ ParentRuleKey
Rule key of the parent legend node - for legends with tree hierarchy (QString). Added in 2....
@ NodeType
Type of node. Added in 3.16.
@ RuleKey
Rule key of the node (QString)
QgsLayerTreeLayer * mLayerNode
QgsLayerTreeLayer * layerNode() const
Returns pointer to the parent layer node.
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.
bool isValid() const
Returns true if the format is valid.
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.
static void drawText(const QRectF &rect, double rotation, Qgis::TextHorizontalAlignment alignment, const QStringList &textLines, QgsRenderContext &context, const QgsTextFormat &format, bool drawAsOutlines=true, Qgis::TextVerticalAlignment vAlignment=Qgis::TextVerticalAlignment::Top, Qgis::TextRendererFlags flags=Qgis::TextRendererFlags(), Qgis::TextLayoutMode mode=Qgis::TextLayoutMode::Rectangle)
Draws text within a rectangle using the specified settings.
static double textHeight(const QgsRenderContext &context, const QgsTextFormat &format, const QStringList &textLines, Qgis::TextLayoutMode mode=Qgis::TextLayoutMode::Point, QFontMetricsF *fontMetrics=nullptr, Qgis::TextRendererFlags flags=Qgis::TextRendererFlags(), double maxLineWidth=0)
Returns the height of a text based on a given format.
static Qgis::TextHorizontalAlignment convertQtHAlignment(Qt::Alignment alignment)
Converts a Qt horizontal alignment flag to a Qgis::TextHorizontalAlignment value.
#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.