QgsLayoutUtils class
Utilities for layouts.
Contents
- Reference
Public static functions
- static auto createRenderContextForLayout(QgsLayout* layout, QPainter* painter, double dpi = -1) -> QgsRenderContext
- Creates a render context suitable for the specified layout and painter destination.
- static auto createRenderContextForMap(QgsLayoutItemMap* map, QPainter* painter, double dpi = -1) -> QgsRenderContext
- Creates a render context suitable for the specified layout map and painter destination.
-
static auto decodePaperOrientation(const QString& string,
bool& ok) -> QgsLayoutItemPage::
Orientation - Decodes a string representing a paper orientation and returns the decoded orientation.
- 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 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 auto fontAscentMM(const QFont& font) -> double
- Calculates a font ascent in millimeters, including workarounds for QT font rendering issues.
- static auto fontDescentMM(const QFont& font) -> double
- Calculate a font descent in millimeters, including workarounds for QT font rendering issues.
- static auto fontHeightCharacterMM(const QFont& font, QChar character) -> double
- Calculate a font height in millimeters of a single character, including workarounds for QT font rendering issues.
- static auto fontHeightMM(const QFont& font) -> double
- Calculate a font height in millimeters, including workarounds for QT font rendering issues.
- static auto largestRotatedRectWithinBounds(const QRectF& originalRect, const QRectF& boundsRect, double rotation) -> QRectF
- Calculates the largest scaled version of originalRect which fits within boundsRect, when it is rotated by the a specified rotation amount.
- static auto mapLayerFromString(const QString& string, QgsProject* project) -> QgsMapLayer*
- Resolves a string into a map layer from a given project.
- static auto normalizedAngle(double angle, bool allowNegative = false) -> double
- Ensures that an angle (in degrees) is in the range 0 <= angle < 360.
- static auto relativePosition(double position, double beforeMin, double beforeMax, double afterMin, double afterMax) -> double
- 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 auto scaledFontPixelSize(const QFont& font) -> QFont
- 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 auto scaleFactorFromItemStyle(const QStyleOptionGraphicsItem* style) -> double
- Extracts the scale factor from an item style.
- static auto snappedAngle(double angle) -> double
- Snaps an angle (in degrees) to its closest 45 degree angle.
- static auto textHeightMM(const QFont& font, const QString& text, double multiLineHeight = 1.0) -> double
- Calculate a font height in millimeters for a text string, including workarounds for QT font rendering issues.
- static auto textWidthMM(const QFont& font, const QString& text) -> double
- Calculate a font width in millimeters for a text string, including workarounds for QT font rendering issues.
Function documentation
static QgsRenderContext QgsLayoutUtils:: createRenderContextForLayout(QgsLayout* layout,
QPainter* painter,
double dpi = -1)
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::
static QgsRenderContext QgsLayoutUtils:: createRenderContextForMap(QgsLayoutItemMap* map,
QPainter* painter,
double dpi = -1)
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.
static QgsLayoutItemPage:: Orientation QgsLayoutUtils:: decodePaperOrientation(const QString& string,
bool& ok)
Decodes a string representing a paper orientation and returns the decoded orientation.
If the string was correctly decoded, ok will be set to true.
static void QgsLayoutUtils:: 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).
If color is specified, text will be rendered in that color. If not specified, the current painter pen color will be used instead.
static 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)
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.
static double QgsLayoutUtils:: fontAscentMM(const QFont& font)
Calculates a font ascent in millimeters, including workarounds for QT font rendering issues.
static double QgsLayoutUtils:: fontDescentMM(const QFont& font)
Calculate a font descent in millimeters, including workarounds for QT font rendering issues.
static double QgsLayoutUtils:: 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 QgsLayoutUtils:: fontHeightMM(const QFont& font)
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).
static QRectF QgsLayoutUtils:: 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.
| Parameters | |
|---|---|
| originalRect | QRectF to be rotated and scaled |
| boundsRect | QRectF specifying the bounds which the rotated and scaled rectangle must fit within |
| rotation | the rotation in degrees to be applied to the rectangle |
| Returns | largest scaled version of the rectangle possible |
static QgsMapLayer* QgsLayoutUtils:: mapLayerFromString(const QString& string,
QgsProject* project)
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.
static double QgsLayoutUtils:: normalizedAngle(double angle,
bool allowNegative = false)
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).
static double QgsLayoutUtils:: relativePosition(double position,
double beforeMin,
double beforeMax,
double afterMin,
double afterMax)
Returns a scaled position given a before and after range.
| Parameters | |
|---|---|
| position | initial position within before range to scale |
| beforeMin | minimum value in before range |
| beforeMax | maximum value in before range |
| afterMin | minimum value in after range |
| afterMax | maximum value in after range |
| Returns | position scaled to range specified by afterMin and afterMax |
static void QgsLayoutUtils:: relativeResizeRect(QRectF& rectToResize,
const QRectF& boundsBefore,
const QRectF& boundsAfter)
Resizes a QRectF relative to a resized bounding rectangle.
| Parameters | |
|---|---|
| rectToResize | QRectF to resize, contained within boundsBefore. The rectangle is linearly scaled to retain its relative position and size within boundsAfter. |
| boundsBefore | QRectF of bounds before resize |
| boundsAfter | QRectF of bounds after resize |
static void QgsLayoutUtils:: rotate(double angle,
double& x,
double& y)
Rotates a point / vector around the origin.
| Parameters | |
|---|---|
| angle | rotation angle in degrees, counterclockwise |
| x | in/out: x coordinate before / after the rotation |
| y | in/out: y coordinate before / after the rotation |
static QFont QgsLayoutUtils:: 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.
Returns a font with size set in pixels.
static double QgsLayoutUtils:: snappedAngle(double angle)
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 |
|---|
static double QgsLayoutUtils:: 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.
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.
static double QgsLayoutUtils:: textWidthMM(const QFont& font,
const QString& text)
Calculate a font width in millimeters for a text string, including workarounds for QT font rendering issues.