Class: QgsLayoutSnapper

class qgis.core.QgsLayoutSnapper

Bases: QgsLayoutSerializableObject

Manages snapping grids and preset snap lines in a layout, and handles snapping points to the nearest grid coordinate/snap line when possible.

New in version 3.0.

QgsLayoutSnapper(layout: QgsLayout) Constructor for QgsLayoutSnapper, attached to the specified layout.

QgsLayoutSnapper(QgsLayoutSnapper)

Methods

layout

rtype

QgsLayout

readXml

Sets the snapper’s state from a DOM element.

setSnapToGrid

Sets whether snapping to grid is enabled.

setSnapToGuides

Sets whether snapping to guides is enabled.

setSnapToItems

Sets whether snapping to items is enabled.

setSnapTolerance

Sets the snap tolerance (in pixels) to use when snapping.

snapPoint

Snaps a layout coordinate point.

snapPointToGrid

Snaps a layout coordinate point to the grid.

snapPointToGuides

Snaps an original layout coordinate to the guides.

snapPointToItems

Snaps an original layout coordinate to the item bounds.

snapPointsToGrid

Snaps a set of points to the grid.

snapPointsToGuides

Snaps a set of points to the guides.

snapPointsToItems

Snaps a set of points to the item bounds.

snapRect

Snaps a layout coordinate rect.

snapToGrid

Returns True if snapping to grid is enabled.

snapToGuides

Returns True if snapping to guides is enabled.

snapToItems

Returns True if snapping to items is enabled.

snapTolerance

Returns the snap tolerance (in pixels) to use when snapping.

stringType

rtype

str

writeXml

Stores the snapper’s state in a DOM element.

layout(self) QgsLayout
Return type

QgsLayout

readXml(self, gridElement: QDomElement, document: QDomDocument, context: QgsReadWriteContext) bool

Sets the snapper’s state from a DOM element. snapperElement is the DOM node corresponding to the snapper.

See also

writeXml()

Parameters
Return type

bool

setSnapToGrid(self, enabled: bool)

Sets whether snapping to grid is enabled.

See also

snapToGrid()

Parameters

enabled (bool) –

setSnapToGuides(self, enabled: bool)

Sets whether snapping to guides is enabled.

See also

snapToGuides()

Parameters

enabled (bool) –

setSnapToItems(self, enabled: bool)

Sets whether snapping to items is enabled.

See also

snapToItems()

Parameters

enabled (bool) –

setSnapTolerance(self, snapTolerance: int)

Sets the snap tolerance (in pixels) to use when snapping.

See also

snapTolerance()

Parameters

snapTolerance (int) –

snapPoint(self, point: Union[QPointF, QPoint], scaleFactor: float, horizontalSnapLine: QGraphicsLineItem = None, verticalSnapLine: QGraphicsLineItem = None, ignoreItems: Iterable[QgsLayoutItem] = []) Tuple[QPointF, bool]

Snaps a layout coordinate point. If point was snapped, snapped will be set to True.

The scaleFactor argument should be set to the transformation from scalar transform from layout coordinates to pixels, i.e. the graphics view transform().m11() value.

This method considers snapping to the grid, snap lines, etc.

If the horizontalSnapLine and verticalSnapLine arguments are specified, then the snapper will automatically display and position these lines to indicate snapping positions to item bounds.

A list of items to ignore during the snapping can be specified via the ignoreItems list.

See also

snapRect()

Parameters
  • point (Union[QPointF) –

  • scaleFactor (float) –

  • horizontalSnapLine (QGraphicsLineItem = None) –

  • verticalSnapLine (QGraphicsLineItem = None) –

  • ignoreItems (Iterable[QgsLayoutItem] = []) –

Return type

Tuple[QPointF, bool]

snapPointToGrid(self, point: Union[QPointF, QPoint], scaleFactor: float) Tuple[QPointF, bool, bool]

Snaps a layout coordinate point to the grid. If point was snapped horizontally, snappedX will be set to True. If point was snapped vertically, snappedY will be set to True.

The scaleFactor argument should be set to the transformation from scalar transform from layout coordinates to pixels, i.e. the graphics view transform().m11() value.

If snapToGrid() is disabled, this method will return the point unchanged.

Parameters
  • point (Union[QPointF) –

  • scaleFactor (float) –

Return type

Tuple[QPointF, bool, bool]

snapPointToGuides(self, original: float, orientation: Qt.Orientation, scaleFactor: float) Tuple[float, bool]

Snaps an original layout coordinate to the guides. If the point was snapped, snapped will be set to True.

The scaleFactor argument should be set to the transformation from scalar transform from layout coordinates to pixels, i.e. the graphics view transform().m11() value.

If snapToGuides() is disabled, this method will return the point unchanged.

Parameters
  • original (float) –

  • orientation (Qt.Orientation) –

  • scaleFactor (float) –

Return type

Tuple[float, bool]

snapPointToItems(self, original: float, orientation: Qt.Orientation, scaleFactor: float, ignoreItems: Iterable[QgsLayoutItem], snapLine: QGraphicsLineItem = None) Tuple[float, bool]

Snaps an original layout coordinate to the item bounds. If the point was snapped, snapped will be set to True.

The scaleFactor argument should be set to the transformation from scalar transform from layout coordinates to pixels, i.e. the graphics view transform().m11() value.

If snapToItems() is disabled, this method will return the point unchanged.

A list of items to ignore during the snapping can be specified via the ignoreItems list.

If snapLine is specified, the snapper will automatically show (or hide) the snap line based on the result of the snap, and position it at the correct location for the snap.

Parameters
  • original (float) –

  • orientation (Qt.Orientation) –

  • scaleFactor (float) –

  • ignoreItems (Iterable[QgsLayoutItem]) –

  • snapLine (QGraphicsLineItem = None) –

Return type

Tuple[float, bool]

snapPointsToGrid(self, points: Iterable[Union[QPointF, QPoint]], scaleFactor: float) Tuple[QPointF, bool, bool]

Snaps a set of points to the grid. If the points were snapped, snapped will be set to True.

The scaleFactor argument should be set to the transformation from scalar transform from layout coordinates to pixels, i.e. the graphics view transform().m11() value.

If snapToGrid() is disabled, this method will not attempt to snap the points.

The returned value is the smallest delta which the points need to be shifted by in order to align one of the points to the grid.

Parameters
  • points (Iterable[Union[QPointF) –

  • scaleFactor (float) –

Return type

Tuple[QPointF, bool, bool]

snapPointsToGuides(self, points: Iterable[float], orientation: Qt.Orientation, scaleFactor: float) Tuple[float, bool]

Snaps a set of points to the guides. If the points were snapped, snapped will be set to True.

The scaleFactor argument should be set to the transformation from scalar transform from layout coordinates to pixels, i.e. the graphics view transform().m11() value.

If snapToGuides() is disabled, this method will not attempt to snap the points.

The returned value is the smallest delta which the points need to be shifted by in order to align one of the points to a guide.

Parameters
  • points (Iterable[float]) –

  • orientation (Qt.Orientation) –

  • scaleFactor (float) –

Return type

Tuple[float, bool]

snapPointsToItems(self, points: Iterable[float], orientation: Qt.Orientation, scaleFactor: float, ignoreItems: Iterable[QgsLayoutItem], snapLine: QGraphicsLineItem = None) Tuple[float, bool]

Snaps a set of points to the item bounds. If the points were snapped, snapped will be set to True.

The scaleFactor argument should be set to the transformation from scalar transform from layout coordinates to pixels, i.e. the graphics view transform().m11() value.

If snapToItems() is disabled, this method will not attempt to snap the points.

The returned value is the smallest delta which the points need to be shifted by in order to align one of the points to an item bound.

Parameters
  • points (Iterable[float]) –

  • orientation (Qt.Orientation) –

  • scaleFactor (float) –

  • ignoreItems (Iterable[QgsLayoutItem]) –

  • snapLine (QGraphicsLineItem = None) –

Return type

Tuple[float, bool]

snapRect(self, rect: QRectF, scaleFactor: float, horizontalSnapLine: QGraphicsLineItem = None, verticalSnapLine: QGraphicsLineItem = None, ignoreItems: Iterable[QgsLayoutItem] = []) Tuple[QRectF, bool]

Snaps a layout coordinate rect. If rect was snapped, snapped will be set to True.

Snapping occurs by moving the rectangle alone. The rectangle will not be resized as a result of the snap operation.

The scaleFactor argument should be set to the transformation from scalar transform from layout coordinates to pixels, i.e. the graphics view transform().m11() value.

This method considers snapping to the grid, snap lines, etc.

If the horizontalSnapLine and verticalSnapLine arguments are specified, then the snapper will automatically display and position these lines to indicate snapping positions to item bounds.

A list of items to ignore during the snapping can be specified via the ignoreItems list.

See also

snapPoint()

Parameters
  • rect (QRectF) –

  • scaleFactor (float) –

  • horizontalSnapLine (QGraphicsLineItem = None) –

  • verticalSnapLine (QGraphicsLineItem = None) –

  • ignoreItems (Iterable[QgsLayoutItem] = []) –

Return type

Tuple[QRectF, bool]

snapToGrid(self) bool

Returns True if snapping to grid is enabled.

See also

setSnapToGrid()

Return type

bool

snapToGuides(self) bool

Returns True if snapping to guides is enabled.

Return type

bool

snapToItems(self) bool

Returns True if snapping to items is enabled.

See also

setSnapToItems()

Return type

bool

snapTolerance(self) int

Returns the snap tolerance (in pixels) to use when snapping.

Return type

int

stringType(self) str
Return type

str

writeXml(self, parentElement: QDomElement, document: QDomDocument, context: QgsReadWriteContext) bool

Stores the snapper’s state in a DOM element. The parentElement should refer to the parent layout’s DOM element.

See also

readXml()

Parameters
  • parentElement (QDomElement) –

  • document (QDomDocument) –

  • context (QgsReadWriteContext) –

Return type

bool