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