QGIS API Documentation 3.99.0-Master (21b3aa880ba)
Loading...
Searching...
No Matches
QgsLayoutUtils Class Reference

Utilities for layouts. More...

#include <qgslayoututils.h>

Static Public Member Functions

static double calculatePrettySize (double minimumSize, double maximumSize)
 Calculates a "pretty" size which falls between the range [minimumSize, maximumSize].
static QgsRenderContext createRenderContextForLayout (QgsLayout *layout, QPainter *painter, double dpi=-1)
 Creates a render context suitable for the specified layout and painter destination.
static QgsRenderContext createRenderContextForMap (QgsLayoutItemMap *map, QPainter *painter, double dpi=-1)
 Creates a render context suitable for the specified layout map and painter destination.
static QgsLayoutItemPage::Orientation decodePaperOrientation (const QString &string, bool &ok)
 Decodes a string representing a paper orientation and returns the decoded orientation.
static void drawText (QPainter *painter, const QRectF &rectangle, const QString &text, const QFont &font, const QColor &color=QColor(), Qt::AlignmentFlag halignment=Qt::AlignLeft, Qt::AlignmentFlag valignment=Qt::AlignTop, int flags=Qt::TextWordWrap)
 Draws text on a painter within a rectangle, taking care of layout specific issues (calculation to pixel, scaling of font and painter to work around Qt font bugs).
static void drawText (QPainter *painter, QPointF position, const QString &text, const QFont &font, const QColor &color=QColor())
 Draws text on a painter at a specific position, taking care of layout specific issues (calculation to pixel, scaling of font and painter to work around Qt font bugs).
static double fontAscentMM (const QFont &font)
 Calculates a font ascent in millimeters, including workarounds for QT font rendering issues.
static double fontDescentMM (const QFont &font)
 Calculate a font descent in millimeters, including workarounds for QT font rendering issues.
static double fontHeightCharacterMM (const QFont &font, QChar character)
 Calculate a font height in millimeters of a single character, including workarounds for QT font rendering issues.
static double fontHeightMM (const QFont &font)
 Calculate a font height in millimeters, including workarounds for QT font rendering issues.
static bool itemIsAClippingSource (const QgsLayoutItem *item)
 Returns true if an item is a clipping item for another layout item.
static QRectF largestRotatedRectWithinBounds (const QRectF &originalRect, const QRectF &boundsRect, double rotation)
 Calculates the largest scaled version of originalRect which fits within boundsRect, when it is rotated by the a specified rotation amount.
static QgsMapLayermapLayerFromString (const QString &string, QgsProject *project)
 Resolves a string into a map layer from a given project.
static double normalizedAngle (double angle, bool allowNegative=false)
 Ensures that an angle (in degrees) is in the range 0 <= angle < 360.
static QVector< double > predefinedScales (const QgsLayout *layout)
 Returns a list of predefined scales associated with a layout.
static double relativePosition (double position, double beforeMin, double beforeMax, double afterMin, double afterMax)
 Returns a scaled position given a before and after range.
static void relativeResizeRect (QRectF &rectToResize, const QRectF &boundsBefore, const QRectF &boundsAfter)
 Resizes a QRectF relative to a resized bounding rectangle.
static void rotate (double angle, double &x, double &y)
 Rotates a point / vector around the origin.
static QFont scaledFontPixelSize (const QFont &font)
 Returns a font where size is set in points and the size has been upscaled with FONT_WORKAROUND_SCALE to workaround QT font rendering bugs.
static Q_DECL_DEPRECATED double scaleFactorFromItemStyle (const QStyleOptionGraphicsItem *style)
 Extracts the scale factor from an item style.
static double scaleFactorFromItemStyle (const QStyleOptionGraphicsItem *style, QPainter *painter)
 Extracts the scale factor from an item style and painter.
static double snappedAngle (double angle)
 Snaps an angle (in degrees) to its closest 45 degree angle.
static double textHeightMM (const QFont &font, const QString &text, double multiLineHeight=1.0)
 Calculate a font height in millimeters for a text string, including workarounds for QT font rendering issues.
static double textWidthMM (const QFont &font, const QString &text)
 Calculate a font width in millimeters for a text string, including workarounds for QT font rendering issues.

Friends

class TestQgsLayoutUtils

Detailed Description

Utilities for layouts.

Definition at line 37 of file qgslayoututils.h.

Member Function Documentation

◆ calculatePrettySize()

double QgsLayoutUtils::calculatePrettySize ( double minimumSize,
double maximumSize )
static

Calculates a "pretty" size which falls between the range [minimumSize, maximumSize].

This method will return an optimal round number which falls within the given range, finding the largest "pretty" number possible.

Since
QGIS 3.10

Definition at line 471 of file qgslayoututils.cpp.

◆ createRenderContextForLayout()

QgsRenderContext QgsLayoutUtils::createRenderContextForLayout ( QgsLayout * layout,
QPainter * painter,
double dpi = -1 )
static

Creates a render context suitable for the specified layout and painter destination.

This method returns a new QgsRenderContext which matches the scale and settings from the layout's QgsLayout::referenceMap(). If the dpi argument is not specified then the dpi will be taken from the destination painter device.

See also
createRenderContextForMap()

Definition at line 145 of file qgslayoututils.cpp.

◆ createRenderContextForMap()

QgsRenderContext QgsLayoutUtils::createRenderContextForMap ( QgsLayoutItemMap * map,
QPainter * painter,
double dpi = -1 )
static

Creates a render context suitable for the specified layout map and painter destination.

This method returns a new QgsRenderContext which matches the scale and settings of the target map. If the dpi argument is not specified then the dpi will be taken from the destination painter device.

See also
createRenderContextForLayout()

Definition at line 101 of file qgslayoututils.cpp.

◆ decodePaperOrientation()

QgsLayoutItemPage::Orientation QgsLayoutUtils::decodePaperOrientation ( const QString & string,
bool & ok )
static

Decodes a string representing a paper orientation and returns the decoded orientation.

If the string was correctly decoded, ok will be set to true.

Definition at line 395 of file qgslayoututils.cpp.

◆ drawText() [1/2]

void QgsLayoutUtils::drawText ( QPainter * painter,
const QRectF & rectangle,
const QString & text,
const QFont & font,
const QColor & color = QColor(),
Qt::AlignmentFlag halignment = Qt::AlignLeft,
Qt::AlignmentFlag valignment = Qt::AlignTop,
int flags = Qt::TextWordWrap )
static

Draws text on a painter within a rectangle, taking care of layout specific issues (calculation to pixel, scaling of font and painter to work around Qt font bugs).

If color is specified, text will be rendered in that color. If not specified, the current painter pen color will be used instead.

The text alignment within rectangle can be set via the halignment and valignment arguments.

The flags parameter allows for passing Qt::TextFlags to control appearance of rendered text.

Definition at line 290 of file qgslayoututils.cpp.

◆ drawText() [2/2]

void QgsLayoutUtils::drawText ( QPainter * painter,
QPointF position,
const QString & text,
const QFont & font,
const QColor & color = QColor() )
static

Draws text on a painter at a specific position, taking care of layout specific issues (calculation to pixel, scaling of font and painter to work around Qt font bugs).

If color is specified, text will be rendered in that color. If not specified, the current painter pen color will be used instead.

Definition at line 268 of file qgslayoututils.cpp.

◆ fontAscentMM()

double QgsLayoutUtils::fontAscentMM ( const QFont & font)
static

Calculates a font ascent in millimeters, including workarounds for QT font rendering issues.

See also
fontDescentMM()
fontHeightMM()
fontHeightCharacterMM()
textWidthMM()

Definition at line 197 of file qgslayoututils.cpp.

◆ fontDescentMM()

double QgsLayoutUtils::fontDescentMM ( const QFont & font)
static

Calculate a font descent in millimeters, including workarounds for QT font rendering issues.

See also
fontAscentMM()
fontHeightMM()
fontHeightCharacterMM()
textWidthMM()

Definition at line 206 of file qgslayoututils.cpp.

◆ fontHeightCharacterMM()

double QgsLayoutUtils::fontHeightCharacterMM ( const QFont & font,
QChar character )
static

Calculate a font height in millimeters of a single character, including workarounds for QT font rendering issues.

See also
fontAscentMM()
fontDescentMM()
fontHeightMM()
textWidthMM()

Definition at line 226 of file qgslayoututils.cpp.

◆ fontHeightMM()

double QgsLayoutUtils::fontHeightMM ( const QFont & font)
static

Calculate a font height in millimeters, including workarounds for QT font rendering issues.

The font height is the font ascent + descent + 1 (for the baseline).

See also
fontAscentMM()
fontDescentMM()
fontHeightCharacterMM()
textWidthMM()

Definition at line 216 of file qgslayoututils.cpp.

◆ itemIsAClippingSource()

bool QgsLayoutUtils::itemIsAClippingSource ( const QgsLayoutItem * item)
static

Returns true if an item is a clipping item for another layout item.

Since
QGIS 3.16

Definition at line 499 of file qgslayoututils.cpp.

◆ largestRotatedRectWithinBounds()

QRectF QgsLayoutUtils::largestRotatedRectWithinBounds ( const QRectF & originalRect,
const QRectF & boundsRect,
double rotation )
static

Calculates the largest scaled version of originalRect which fits within boundsRect, when it is rotated by the a specified rotation amount.

Parameters
originalRectQRectF to be rotated and scaled
boundsRectQRectF specifying the bounds which the rotated and scaled rectangle must fit within
rotationthe rotation in degrees to be applied to the rectangle
Returns
largest scaled version of the rectangle possible

Definition at line 315 of file qgslayoututils.cpp.

◆ mapLayerFromString()

QgsMapLayer * QgsLayoutUtils::mapLayerFromString ( const QString & string,
QgsProject * project )
static

Resolves a string into a map layer from a given project.

Attempts different forms of layer matching such as matching by layer id or layer name.

Layer names are matched using a case-insensitive check, ONLY if an exact case match was not found.

Since
QGIS 3.2

Definition at line 436 of file qgslayoututils.cpp.

◆ normalizedAngle()

double QgsLayoutUtils::normalizedAngle ( double angle,
bool allowNegative = false )
static

Ensures that an angle (in degrees) is in the range 0 <= angle < 360.

If allowNegative is true then angles between (-360, 360) are allowed. If false, angles are converted to positive angles in the range [0, 360).

Definition at line 47 of file qgslayoututils.cpp.

◆ predefinedScales()

QVector< double > QgsLayoutUtils::predefinedScales ( const QgsLayout * layout)
static

Returns a list of predefined scales associated with a layout.

Since
QGIS 3.20

Definition at line 527 of file qgslayoututils.cpp.

◆ relativePosition()

double QgsLayoutUtils::relativePosition ( double position,
double beforeMin,
double beforeMax,
double afterMin,
double afterMax )
static

Returns a scaled position given a before and after range.

Parameters
positioninitial position within before range to scale
beforeMinminimum value in before range
beforeMaxmaximum value in before range
afterMinminimum value in after range
afterMaxmaximum value in after range
Returns
position scaled to range specified by afterMin and afterMax

Definition at line 178 of file qgslayoututils.cpp.

◆ relativeResizeRect()

void QgsLayoutUtils::relativeResizeRect ( QRectF & rectToResize,
const QRectF & boundsBefore,
const QRectF & boundsAfter )
static

Resizes a QRectF relative to a resized bounding rectangle.

Parameters
rectToResizeQRectF to resize, contained within boundsBefore. The rectangle is linearly scaled to retain its relative position and size within boundsAfter.
boundsBeforeQRectF of bounds before resize
boundsAfterQRectF of bounds after resize

Definition at line 159 of file qgslayoututils.cpp.

◆ rotate()

void QgsLayoutUtils::rotate ( double angle,
double & x,
double & y )
static

Rotates a point / vector around the origin.

Parameters
anglerotation angle in degrees, counterclockwise
xin/out: x coordinate before / after the rotation
yin/out: y coordinate before / after the rotation

Definition at line 37 of file qgslayoututils.cpp.

◆ scaledFontPixelSize()

QFont QgsLayoutUtils::scaledFontPixelSize ( const QFont & font)
static

Returns a font where size is set in points and the size has been upscaled with FONT_WORKAROUND_SCALE to workaround QT font rendering bugs.

Returns a font with size set in pixels.

Definition at line 187 of file qgslayoututils.cpp.

◆ scaleFactorFromItemStyle() [1/2]

double QgsLayoutUtils::scaleFactorFromItemStyle ( const QStyleOptionGraphicsItem * style)
static

Extracts the scale factor from an item style.

Deprecated
QGIS 3.40. Use the variant with a QPainter argument instead.

Definition at line 412 of file qgslayoututils.cpp.

◆ scaleFactorFromItemStyle() [2/2]

double QgsLayoutUtils::scaleFactorFromItemStyle ( const QStyleOptionGraphicsItem * style,
QPainter * painter )
static

Extracts the scale factor from an item style and painter.

Since
QGIS 3.20

Definition at line 430 of file qgslayoututils.cpp.

◆ snappedAngle()

double QgsLayoutUtils::snappedAngle ( double angle)
static

Snaps an angle (in degrees) to its closest 45 degree angle.

Returns
angle snapped to 0, 45/90/135/180/225/270 or 315 degrees

Definition at line 61 of file qgslayoututils.cpp.

◆ textHeightMM()

double QgsLayoutUtils::textHeightMM ( const QFont & font,
const QString & text,
double multiLineHeight = 1.0 )
static

Calculate a font height in millimeters for a text string, including workarounds for QT font rendering issues.

Note that this method uses a non-standard measure of text height, where only the font ascent is considered for the first line of text.

The multiLineHeight parameter specifies the line spacing factor.

See also
textWidthMM()

Definition at line 252 of file qgslayoututils.cpp.

◆ textWidthMM()

double QgsLayoutUtils::textWidthMM ( const QFont & font,
const QString & text )
static

Calculate a font width in millimeters for a text string, including workarounds for QT font rendering issues.

See also
fontAscentMM()
fontDescentMM()
fontHeightMM()
fontHeightCharacterMM()
textHeightMM()

Definition at line 235 of file qgslayoututils.cpp.

◆ TestQgsLayoutUtils

friend class TestQgsLayoutUtils
friend

Definition at line 275 of file qgslayoututils.h.


The documentation for this class was generated from the following files: