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