QGIS API Documentation 3.41.0-Master (af5edcb665c)
Loading...
Searching...
No Matches
qgscolorramplegendnode.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgscolorramplegendnode.cpp
3 --------------------------------------
4 Date : December 2020
5 Copyright : (C) 2020 by Nyall Dawson
6 Email : nyall dot dawson 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
16#include "qgsapplication.h"
18#include "moc_qgscolorramplegendnode.cpp"
19#include "qgscolorrampimpl.h"
20#include "qgslegendsettings.h"
21#include "qgslayertreemodel.h"
22#include "qgslayertreelayer.h"
23#include "qgssymbollayerutils.h"
25#include "qgstextrenderer.h"
26#include "qgsnumericformat.h"
27
28#include <QPalette>
29#include <QBuffer>
30
31QgsColorRampLegendNode::QgsColorRampLegendNode( QgsLayerTreeLayer *nodeLayer, QgsColorRamp *ramp, const QString &minimumLabel, const QString &maximumLabel, QObject *parent, const QString &key, const QString &parentKey )
32 : QgsLayerTreeModelLegendNode( nodeLayer, parent )
33 , mRamp( ramp )
34 , mKey( key )
35 , mParentKey( parentKey )
36{
37 mSettings.setMinimumLabel( minimumLabel );
38 mSettings.setMaximumLabel( maximumLabel );
39
40 init( nodeLayer );
41}
42
43QgsColorRampLegendNode::QgsColorRampLegendNode( QgsLayerTreeLayer *nodeLayer, QgsColorRamp *ramp, const QgsColorRampLegendNodeSettings &settings, double minimumValue, double maximumValue, QObject *parent, const QString &key, const QString &parentKey )
44 : QgsLayerTreeModelLegendNode( nodeLayer, parent )
45 , mRamp( ramp )
46 , mSettings( settings )
47 , mMinimumValue( minimumValue )
48 , mMaximumValue( maximumValue )
49 , mKey( key )
50 , mParentKey( parentKey )
51{
52 init( nodeLayer );
53}
54
55void QgsColorRampLegendNode::init( QgsLayerTreeLayer *nodeLayer )
56{
58 mIconSize = mSettings.orientation() == Qt::Vertical ? QSize( iconSize, iconSize * 6 ) : QSize( iconSize * 6, iconSize );
59
60 connect( nodeLayer, &QObject::destroyed, this, [this]() { mLayerNode = nullptr; } );
61}
62
64{
65 return mRamp.get();
66}
67
72
74{
75 mSettings = settings;
76}
77
78QString QgsColorRampLegendNode::labelForMinimum() const
79{
80 if ( !mSettings.minimumLabel().isEmpty() )
81 return mSettings.prefix() + mSettings.minimumLabel() + mSettings.suffix();
82
83 const QgsNumericFormatContext numericContext;
84 return mSettings.prefix() + mSettings.numericFormat()->formatDouble( mMinimumValue, numericContext ) + mSettings.suffix();
85}
86
87QString QgsColorRampLegendNode::labelForMaximum() const
88{
89 if ( !mSettings.maximumLabel().isEmpty() )
90 return mSettings.prefix() + mSettings.maximumLabel() + mSettings.suffix();
91
92 const QgsNumericFormatContext numericContext;
93 return mSettings.prefix() + mSettings.numericFormat()->formatDouble( mMaximumValue, numericContext ) + mSettings.suffix();
94}
95
96QVariant QgsColorRampLegendNode::data( int role ) const
97{
98 if ( role == Qt::DisplayRole )
99 {
100 return QString();
101 }
102 else if ( role == Qt::EditRole )
103 {
104 return QString();
105 }
106 else if ( role == Qt::DecorationRole )
107 {
108 if ( mPixmap.isNull() || mPixmap.size() != mIconSize )
109 {
110 const QFont font = data( Qt::FontRole ).value< QFont >();
111
112 const QString minLabel = labelForMinimum();
113 const QString maxLabel = labelForMaximum();
114
115 const QFontMetrics fm( font );
116
117 const QRect minBoundingRect = fm.boundingRect( minLabel );
118 const QRect maxBoundingRect = fm.boundingRect( maxLabel );
119
120 const int minLabelWidth = minBoundingRect.width();
121 const int maxLabelWidth = maxBoundingRect.width();
122 const int maxTextWidth = std::max( minLabelWidth, maxLabelWidth );
123 const int labelGapFromRamp = fm.boundingRect( QStringLiteral( "x" ) ).width();
124 const int extraAllowance = labelGapFromRamp * 0.4; // extra allowance to avoid text clipping on right
125 QRect labelRect;
126 QSize rampSize;
127 switch ( mSettings.orientation() )
128 {
129 case Qt::Vertical:
130 labelRect = QRect( mIconSize.width() + labelGapFromRamp, 0, maxTextWidth + extraAllowance, mIconSize.height() );
131 mPixmap = QPixmap( mIconSize.width() + maxTextWidth + labelGapFromRamp + extraAllowance, mIconSize.height() );
132 rampSize = mIconSize;
133 break;
134
135 case Qt::Horizontal:
136 labelRect = QRect( 0, mIconSize.height() + labelGapFromRamp, std::max( mIconSize.width(), minLabelWidth + maxLabelWidth + labelGapFromRamp ), std::max( minBoundingRect.height(),
137 maxBoundingRect.height() ) + extraAllowance );
138 mPixmap = QPixmap( std::max( mIconSize.width(), minLabelWidth + maxLabelWidth + labelGapFromRamp ), mIconSize.height() + maxTextWidth + labelGapFromRamp + extraAllowance );
139 rampSize = QSize( labelRect.width(), mIconSize.height() );
140 break;
141 }
142
143 mPixmap.fill( Qt::transparent );
144
145 QPixmap pix;
146
147 if ( mRamp )
148 {
149 pix = QgsSymbolLayerUtils::colorRampPreviewPixmap( mRamp.get(), rampSize, 0, mSettings.orientation(),
150 mSettings.orientation() == Qt::Vertical ? mSettings.direction() != QgsColorRampLegendNodeSettings::MaximumToMinimum
152 false );
153 }
154 else
155 {
156 pix = QPixmap( rampSize );
157 pix.fill( Qt::transparent );
158 }
159
160 QPainter p( &mPixmap );
161 p.drawPixmap( 0, 0, pix );
162 p.setFont( font );
163 p.setPen( qApp->palette().color( QPalette::Text ) );
164
165 switch ( mSettings.orientation() )
166 {
167 case Qt::Vertical:
168 p.drawText( labelRect, Qt::AlignBottom | Qt::AlignLeft, mSettings.direction() == QgsColorRampLegendNodeSettings::MinimumToMaximum ? minLabel : maxLabel );
169 p.drawText( labelRect, Qt::AlignTop | Qt::AlignLeft, mSettings.direction() == QgsColorRampLegendNodeSettings::MinimumToMaximum ? maxLabel : minLabel );
170 break;
171
172 case Qt::Horizontal:
173 p.drawText( labelRect, Qt::AlignTop | Qt::AlignLeft, mSettings.direction() == QgsColorRampLegendNodeSettings::MinimumToMaximum ? minLabel : maxLabel );
174 p.drawText( labelRect, Qt::AlignTop | Qt::AlignRight, mSettings.direction() == QgsColorRampLegendNodeSettings::MinimumToMaximum ? maxLabel : minLabel );
175 break;
176 }
177
178 p.end();
179 }
180 return mPixmap;
181 }
182 else if ( role == static_cast< int >( QgsLayerTreeModelLegendNode::CustomRole::NodeType ) )
183 {
185 }
186 else if ( role == static_cast< int >( QgsLayerTreeModelLegendNode::CustomRole::RuleKey ) )
187 return mKey;
188 else if ( role == static_cast< int >( QgsLayerTreeModelLegendNode::CustomRole::ParentRuleKey ) )
189 return mParentKey;
190 return QVariant();
191}
192
193QSizeF QgsColorRampLegendNode::drawSymbol( const QgsLegendSettings &settings, ItemContext *ctx, double ) const
194{
195 if ( !mRamp )
196 {
197 return QSizeF();
198 }
199
200 // setup temporary render context
201 QgsRenderContext *context = nullptr;
202 std::unique_ptr< QgsRenderContext > tempRenderContext;
203 if ( ctx && ctx->context )
204 context = ctx->context;
205 else
206 {
207 tempRenderContext = std::make_unique< QgsRenderContext >();
208 // QGIS 4.0 - make ItemContext compulsory, so we don't have to construct temporary render contexts here
210 tempRenderContext->setScaleFactor( settings.dpi() / 25.4 );
211 tempRenderContext->setRendererScale( settings.mapScale() );
212 tempRenderContext->setFlag( Qgis::RenderContextFlag::Antialiasing, true );
213 tempRenderContext->setMapToPixel( QgsMapToPixel( 1 / ( settings.mmPerMapUnit() * tempRenderContext->scaleFactor() ) ) );
215 tempRenderContext->setForceVectorOutput( true );
216 tempRenderContext->setPainter( ctx ? ctx->painter : nullptr );
217
218 // setup a minimal expression context
219 QgsExpressionContext expContext;
221 tempRenderContext->setExpressionContext( expContext );
222 context = tempRenderContext.get();
223 }
224
225 const QgsTextFormat format = mSettings.textFormat().isValid() ? mSettings.textFormat() : settings.style( QgsLegendStyle::SymbolLabel ).textFormat();
226 const QString minLabel = labelForMinimum();
227 const QString maxLabel = labelForMaximum();
228
229 const double patchWidth = ctx && ctx->patchSize.width() > 0 ? ctx->patchSize.width() : settings.symbolSize().width();
230 const double patchHeight = ctx && ctx->patchSize.height() > 0 ? ctx->patchSize.height() : settings.symbolSize().height();
231
232 double minHeightMm = 0;
233 double minWidthMm = 0;
234 double rampHeight = 0;
235 double rampWidth = 0;
236 switch ( mSettings.orientation() )
237 {
238 case Qt::Vertical:
239 // vertical bar, min height is the text height of the min and max labels
240 minHeightMm = QgsTextRenderer::textHeight( *context, format, QStringList() << minLabel << maxLabel, Qgis::TextLayoutMode::Rectangle ) / context->scaleFactor();
241 rampHeight = ctx && ctx->patchSize.height() > 0 ? std::max( minHeightMm / 2, ctx->patchSize.height() ) : std::max( minHeightMm, settings.symbolSize().height() );
242 rampWidth = patchWidth;
243 break;
244
245 case Qt::Horizontal:
246 // horizontal bar, min width is text width of the min and max labels
247 minWidthMm = ( QgsTextRenderer::textWidth( *context, format, QStringList() << minLabel ) +
248 QgsTextRenderer::textWidth( *context, format, QStringList() << maxLabel ) ) / context->scaleFactor();
249 rampHeight = patchHeight;
250 rampWidth = std::max( minWidthMm, patchWidth );
251 break;
252 }
253
254 if ( ctx && ctx->painter )
255 {
256 const double currentYCoord = ctx->top;
257 QPainter *p = ctx->painter;
258
259 //setup painter scaling to dots so that raster symbology is drawn to scale
260 const double dotsPerMM = context->scaleFactor();
261
262 double opacity = 1;
263 if ( QgsMapLayer *layer = layerNode()->layer() )
264 opacity = layer->opacity();
265
266 const QgsScopedQPainterState painterState( p );
267 context->setPainterFlagsUsingContext( p );
268
269 double rampLeftMm = 0;
270 const double rampTopMm = currentYCoord;
271 switch ( settings.symbolAlignment() )
272 {
273 case Qt::AlignLeft:
274 default:
275 rampLeftMm = ctx->columnLeft;
276 break;
277
278 case Qt::AlignRight:
279 rampLeftMm = ctx->columnRight - rampWidth;
280 break;
281 }
282
283 p->scale( 1.0 / dotsPerMM, 1.0 / dotsPerMM );
284
285 QLinearGradient gradient;
286 switch ( mSettings.orientation() )
287 {
288 case Qt::Vertical:
289 {
290 const double gradientTop = rampTopMm * dotsPerMM;
291 const double gradientBottom = gradientTop + rampHeight * dotsPerMM;
292 gradient = QLinearGradient( 0, mSettings.direction() == QgsColorRampLegendNodeSettings::MinimumToMaximum ? gradientBottom : gradientTop,
293 0, mSettings.direction() == QgsColorRampLegendNodeSettings::MinimumToMaximum ? gradientTop : gradientBottom );
294 break;
295 }
296
297 case Qt::Horizontal:
298 {
299 const double gradientLeft = rampLeftMm * dotsPerMM;
300 const double gradientRight = gradientLeft + rampWidth * dotsPerMM;
301 gradient = QLinearGradient( mSettings.direction() == QgsColorRampLegendNodeSettings::MinimumToMaximum ? gradientLeft : gradientRight, 0,
302 mSettings.direction() == QgsColorRampLegendNodeSettings::MinimumToMaximum ? gradientRight : gradientLeft, 0 );
303 break;
304 }
305 }
306
307
308 if ( mRamp->type() == QgsGradientColorRamp::typeString() || mRamp->type() == QgsCptCityColorRamp::typeString() )
309 {
310 //color ramp gradient
311 QgsGradientColorRamp *gradRamp = static_cast<QgsGradientColorRamp *>( mRamp.get() );
312 gradRamp->addStopsToGradient( &gradient, opacity );
313 }
314
315 if ( settings.drawRasterStroke() )
316 {
317 QPen pen;
318 pen.setColor( settings.rasterStrokeColor() );
319 pen.setWidthF( settings.rasterStrokeWidth() * dotsPerMM );
320 pen.setJoinStyle( Qt::MiterJoin );
321 ctx->painter->setPen( pen );
322 }
323 else
324 {
325 ctx->painter->setPen( Qt::NoPen );
326 }
327
328 p->setBrush( QBrush( gradient ) );
329 p->drawRect( rampLeftMm * dotsPerMM, rampTopMm * dotsPerMM, rampWidth * dotsPerMM, rampHeight * dotsPerMM );
330 }
331
332 double labelHeight = 0;
333 if ( mSettings.orientation() == Qt::Horizontal )
334 {
335 // we treat the text as part of the symbol for horizontal bar items
336 if ( ctx && ctx->painter )
337 {
338 const double currentYCoord = ctx->top;
339 QPainter *p = ctx->painter;
340
341 //setup painter scaling to dots so that raster symbology is drawn to scale
342 const double dotsPerMM = context->scaleFactor();
343
344 const QgsScopedQPainterState painterState( p );
345 context->setPainterFlagsUsingContext( p );
346
347 p->scale( 1.0 / dotsPerMM, 1.0 / dotsPerMM );
348
349 double labelXMin = 0;
350 double labelXMax = 0;
351 // NOTE -- while the below calculations use the flipped margins from the style, that's only done because
352 // those are the only margins we expose and use for now! (and we expose them as generic margins, not side-specific
353 // ones) TODO when/if we expose other margin settings, these should be reversed...
354 const double labelYMin = currentYCoord + rampHeight + settings.style( QgsLegendStyle::Symbol ).margin( QgsLegendStyle::Right )
356 const double labelHeight = std::max( QgsTextRenderer::textHeight( *context, format, QStringList() << minLabel ),
357 QgsTextRenderer::textHeight( *context, format, QStringList() << maxLabel ) ) / dotsPerMM;
358 switch ( settings.symbolAlignment() )
359 {
360 case Qt::AlignLeft:
361 default:
362 labelXMin = ctx->columnLeft;
363 labelXMax = ctx->columnLeft + rampWidth;
364 break;
365
366 case Qt::AlignRight:
367 labelXMin = ctx->columnRight - rampWidth;
368 labelXMax = ctx->columnRight;
369 break;
370 }
371
372 const QRectF textRect( labelXMin * dotsPerMM, labelYMin * dotsPerMM, ( labelXMax - labelXMin ) * dotsPerMM, labelHeight * dotsPerMM );
374 QStringList() << ( mSettings.direction() == QgsColorRampLegendNodeSettings::MinimumToMaximum ? minLabel : maxLabel ),
375 *context, format, true, Qgis::TextVerticalAlignment::Top );
377 QStringList() << ( mSettings.direction() == QgsColorRampLegendNodeSettings::MinimumToMaximum ? maxLabel : minLabel ),
378 *context, format, true, Qgis::TextVerticalAlignment::Bottom );
379 }
380 else
381 {
382 // we only need this when we are calculating the size of the node, not at render time
383 labelHeight = std::max( QgsTextRenderer::textHeight( *context, format, QStringList() << minLabel ),
384 QgsTextRenderer::textHeight( *context, format, QStringList() << maxLabel ) ) / context->scaleFactor()
387 }
388 }
389
390 return QSizeF( rampWidth, rampHeight + labelHeight );
391}
392
394{
395 if ( !mRamp || mSettings.orientation() == Qt::Horizontal )
396 {
397 return QSizeF();
398 }
399
400 // setup temporary render context
401 QgsRenderContext *context = nullptr;
402 std::unique_ptr< QgsRenderContext > tempRenderContext;
403 if ( ctx && ctx->context )
404 context = ctx->context;
405 else
406 {
407 tempRenderContext = std::make_unique< QgsRenderContext >();
408 // QGIS 4.0 - make ItemContext compulsory, so we don't have to construct temporary render contexts here
410 tempRenderContext->setScaleFactor( settings.dpi() / 25.4 );
411 tempRenderContext->setRendererScale( settings.mapScale() );
412 tempRenderContext->setFlag( Qgis::RenderContextFlag::Antialiasing, true );
413 tempRenderContext->setMapToPixel( QgsMapToPixel( 1 / ( settings.mmPerMapUnit() * tempRenderContext->scaleFactor() ) ) );
415 tempRenderContext->setForceVectorOutput( true );
416 tempRenderContext->setPainter( ctx ? ctx->painter : nullptr );
417
418 // setup a minimal expression context
419 QgsExpressionContext expContext;
421 tempRenderContext->setExpressionContext( expContext );
422 context = tempRenderContext.get();
423 }
424
425 const QgsTextFormat format = mSettings.textFormat().isValid() ? mSettings.textFormat() : settings.style( QgsLegendStyle::SymbolLabel ).textFormat();
426
427 const QString minLabel = labelForMinimum();
428 const QString maxLabel = labelForMaximum();
429
430 const double rampHeight = symbolSize.height();
431 const double rampWidth = symbolSize.width();
432 double textWidth = 0;
433 double textHeight = 0;
434
435 if ( ctx && ctx->painter )
436 {
437 const double currentYCoord = ctx->top;
438 QPainter *p = ctx->painter;
439
440 //setup painter scaling to dots so that raster symbology is drawn to scale
441 const double dotsPerMM = context->scaleFactor();
442
443 const QgsScopedQPainterState painterState( p );
444 context->setPainterFlagsUsingContext( p );
445
446 p->scale( 1.0 / dotsPerMM, 1.0 / dotsPerMM );
447
448 double labelXMin = 0;
449 double labelXMax = 0;
450 switch ( settings.symbolAlignment() )
451 {
452 case Qt::AlignLeft:
453 default:
454 labelXMin = ctx->columnLeft + std::max( rampWidth, ctx->maxSiblingSymbolWidth )
457 labelXMax = ctx->columnRight;
458 break;
459
460 case Qt::AlignRight:
461 labelXMin = ctx->columnLeft;
462 // NOTE -- while the below calculations use the flipped margins from the style, that's only done because
463 // those are the only margins we expose and use for now! (and we expose them as generic margins, not side-specific
464 // ones) TODO when/if we expose other margin settings, these should be reversed...
465 labelXMax = ctx->columnRight - std::max( rampWidth, ctx->maxSiblingSymbolWidth )
468 break;
469 }
470
471 const QRectF textRect( labelXMin * dotsPerMM, currentYCoord * dotsPerMM, ( labelXMax - labelXMin ) * dotsPerMM, rampHeight * dotsPerMM );
473 QStringList() << ( mSettings.direction() == QgsColorRampLegendNodeSettings::MinimumToMaximum ? maxLabel : minLabel ),
474 *context, format, true, Qgis::TextVerticalAlignment::Top );
476 QStringList() << ( mSettings.direction() == QgsColorRampLegendNodeSettings::MinimumToMaximum ? minLabel : maxLabel ),
477 *context, format, true, Qgis::TextVerticalAlignment::Bottom );
478 }
479 else
480 {
481 // we only need this when we are calculating the size of the node, not at render time
482 textWidth = QgsTextRenderer::textWidth( *context, format, QStringList() << minLabel << maxLabel ) / context->scaleFactor();
483 textHeight = rampHeight;
484 }
485
486 return QSizeF( textWidth, textHeight );
487}
488
489QJsonObject QgsColorRampLegendNode::exportSymbolToJson( const QgsLegendSettings &settings, const QgsRenderContext &context ) const
490{
491 Q_UNUSED( settings );
492 Q_UNUSED( context );
493
494 QJsonObject json;
495
496 const QPixmap icon = data( Qt::DecorationRole ).value<QPixmap>();
497
498 if ( ! icon.isNull() )
499 {
500 const QImage image( icon.toImage() );
501 QByteArray byteArray;
502 QBuffer buffer( &byteArray );
503 image.save( &buffer, "PNG" );
504 const QString base64 = QString::fromLatin1( byteArray.toBase64().data() );
505 json[ QStringLiteral( "icon" ) ] = base64;
506 }
507
508 json [ QStringLiteral( "min" ) ] = mMinimumValue;
509 json [ QStringLiteral( "max" ) ] = mMaximumValue;
510
511 return json;
512}
@ Rectangle
Text within rectangle layout mode.
@ Antialiasing
Use antialiasing while drawing.
@ Bottom
Align to bottom.
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...
@ 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 * 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...
@ Right
Right side.
@ Left
Left side.
@ Symbol
Symbol icon (excluding label)
@ SymbolLabel
Symbol label (excluding icon)
Base class for all map layer types.
Definition qgsmaplayer.h:76
Perform transforms between map coordinates and device coordinates.
A context for numeric formats.
virtual QString formatDouble(double value, const QgsNumericFormatContext &context) const =0
Returns a formatted string representation of a numeric double value.
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
Definition qgis.h:6643
#define Q_NOWARN_DEPRECATED_PUSH
Definition qgis.h:6642
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.