Class: QgsLayoutUtils¶
-
class
qgis.core.
QgsLayoutUtils
¶ Bases:
sip.wrapper
Utilities for layouts.
Methods
Calculates a “pretty” size which falls between the range [
minimumSize
,maximumSize
].Creates a render context suitable for the specified
layout
andpainter
destination.Creates a render context suitable for the specified layout
map
andpainter
destination.Decodes a
string
representing a paper orientation and returns the decoded orientation.Draws
text
on apainter
at a specificposition
, taking care of layout specific issues (calculation to pixel, scaling of font and painter to work around Qt font bugs).Calculates a
font
ascent in millimeters, including workarounds for QT font rendering issues.Calculate a
font
descent in millimeters, including workarounds for QT font rendering issues.Calculate a
font
height in millimeters of a singlecharacter
, including workarounds for QT font rendering issues.Calculate a
font
height in millimeters, including workarounds for QT font rendering issues.Calculates the largest scaled version of
originalRect
which fits withinboundsRect
, when it is rotated by the a specifiedrotation
amount.Resolves a
string
into a map layer from a givenproject
.Ensures that an
angle
(in degrees) is in the range 0 <= angle < 360.Returns a scaled position given a before and after range
Resizes a QRectF relative to a resized bounding rectangle.
Rotates a point / vector around the origin.
Extracts the scale factor from an item
style
.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.Snaps an
angle
(in degrees) to its closest 45 degree angle.Calculate a
font
height in millimeters for atext
string, including workarounds for QT font rendering issues.Calculate a
font
width in millimeters for atext
string, including workarounds for QT font rendering issues.-
calculatePrettySize
(minimumSize: float, maximumSize: float) → float¶ 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.
New in version 3.10.
- Parameters
minimumSize (float) –
maximumSize (float) –
- Return type
float
-
createRenderContextForLayout
(layout: QgsLayout, painter: QPainter, dpi: float = - 1) → QgsRenderContext¶ Creates a render context suitable for the specified
layout
andpainter
destination. This method returns a newQgsRenderContext
which matches the scale and settings from the layout’sQgsLayout.referenceMap()
If thedpi
argument is not specified then the dpi will be taken from the destination painter device.See also
- Parameters
layout (QgsLayout) –
painter (QPainter) –
dpi (float = -1) –
- Return type
-
createRenderContextForMap
(map: QgsLayoutItemMap, painter: QPainter, dpi: float = - 1) → QgsRenderContext¶ Creates a render context suitable for the specified layout
map
andpainter
destination. This method returns a newQgsRenderContext
which matches the scale and settings of the target map. If thedpi
argument is not specified then the dpi will be taken from the destination painter device.See also
- Parameters
map (QgsLayoutItemMap) –
painter (QPainter) –
dpi (float = -1) –
- Return type
-
decodePaperOrientation
(string: str) → Tuple[QgsLayoutItemPage.Orientation, bool]¶ Decodes a
string
representing a paper orientation and returns the decoded orientation. If the string was correctly decoded,ok
will be set toTrue
.- Parameters
string (str) –
- Return type
Tuple[QgsLayoutItemPage.Orientation, bool]
-
drawText
(painter: QPainter, position: Union[QPointF, QPoint], text: str, font: QFont, color: Union[QColor, Qt.GlobalColor, QGradient] = QColor())¶ Draws
text
on apainter
at a specificposition
, 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.drawText(painter: QPainter, rectangle: QRectF, text: str, font: QFont, color: Union[QColor, Qt.GlobalColor, QGradient] = QColor(), halignment: Qt.AlignmentFlag = Qt.AlignLeft, valignment: Qt.AlignmentFlag = Qt.AlignTop, flags: int = Qt.TextWordWrap) Draws
text
on apainter
within arectangle
, 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 thehalignment
andvalignment
arguments.The
flags
parameter allows for passing Qt.TextFlags to control appearance of rendered text.- Parameters
painter (QPainter) –
position (Union[QPointF) –
text (str) –
font (QFont) –
color (Union[QColor) –
-
fontAscentMM
(font: QFont) → float¶ Calculates a
font
ascent in millimeters, including workarounds for QT font rendering issues.See also
See also
See also
See also
- Parameters
font (QFont) –
- Return type
float
-
fontDescentMM
(font: QFont) → float¶ Calculate a
font
descent in millimeters, including workarounds for QT font rendering issues.See also
See also
See also
See also
- Parameters
font (QFont) –
- Return type
float
-
fontHeightCharacterMM
(font: QFont, character: str) → float¶ Calculate a
font
height in millimeters of a singlecharacter
, including workarounds for QT font rendering issues.See also
See also
See also
See also
- Parameters
font (QFont) –
character (str) –
- Return type
float
-
fontHeightMM
(font: QFont) → float¶ 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
See also
See also
See also
- Parameters
font (QFont) –
- Return type
float
-
largestRotatedRectWithinBounds
(originalRect: QRectF, boundsRect: QRectF, rotation: float) → QRectF¶ Calculates the largest scaled version of
originalRect
which fits withinboundsRect
, when it is rotated by the a specifiedrotation
amount.- Parameters
originalRect (QRectF) – QRectF to be rotated and scaled
boundsRect (QRectF) – QRectF specifying the bounds which the rotated and scaled rectangle must fit within
rotation (float) – the rotation in degrees to be applied to the rectangle
- Return type
QRectF
- Returns
largest scaled version of the rectangle possible
-
mapLayerFromString
(string: str, project: QgsProject) → QgsMapLayer¶ Resolves a
string
into a map layer from a givenproject
. 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.
New in version 3.2.
- Parameters
string (str) –
project (QgsProject) –
- Return type
-
normalizedAngle
(angle: float, allowNegative: bool = False) → float¶ Ensures that an
angle
(in degrees) is in the range 0 <= angle < 360. IfallowNegative
isTrue
then angles between (-360, 360) are allowed. IfFalse
, angles are converted to positive angles in the range [0, 360).- Parameters
angle (float) –
allowNegative (bool = False) –
- Return type
float
-
relativePosition
(position: float, beforeMin: float, beforeMax: float, afterMin: float, afterMax: float) → float¶ Returns a scaled position given a before and after range
- Parameters
position (float) – initial position within before range to scale
beforeMin (float) – minimum value in before range
beforeMax (float) – maximum value in before range
afterMin (float) – minimum value in after range
afterMax (float) – maximum value in after range
- Return type
float
- Returns
position scaled to range specified by afterMin and afterMax
-
relativeResizeRect
(rectToResize: QRectF, boundsBefore: QRectF, boundsAfter: QRectF)¶ Resizes a QRectF relative to a resized bounding rectangle.
- Parameters
rectToResize (QRectF) – QRectF to resize, contained within boundsBefore. The rectangle is linearly scaled to retain its relative position and size within boundsAfter.
boundsBefore (QRectF) – QRectF of bounds before resize
boundsAfter (QRectF) – QRectF of bounds after resize
-
rotate
(angle: float) → Tuple[float, float]¶ Rotates a point / vector around the origin.
- Parameters
angle (float) – rotation angle in degrees, counterclockwise
x – in/out: x coordinate before / after the rotation
y – in/out: y coordinate before / after the rotation
- Return type
Tuple[float, float]
-
scaleFactorFromItemStyle
(style: QStyleOptionGraphicsItem) → float¶ Extracts the scale factor from an item
style
.- Parameters
style (QStyleOptionGraphicsItem) –
- Return type
float
-
scaledFontPixelSize
(font: QFont) → 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. Returns a font with size set in pixels.- Parameters
font (QFont) –
- Return type
QFont
-
snappedAngle
(angle: float) → float¶ Snaps an
angle
(in degrees) to its closest 45 degree angle.- Return type
float
- Returns
angle snapped to 0, 45/90/135/180/225/270 or 315 degrees
- Parameters
angle (float) –
-
textHeightMM
(font: QFont, text: str, multiLineHeight: float = 1) → float¶ Calculate a
font
height in millimeters for atext
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
- Parameters
font (QFont) –
text (str) –
multiLineHeight (float = 1) –
- Return type
float
-
textWidthMM
(font: QFont, text: str) → float¶ Calculate a
font
width in millimeters for atext
string, including workarounds for QT font rendering issues.See also
See also
See also
See also
See also
- Parameters
font (QFont) –
text (str) –
- Return type
float
-