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