QgsLayoutNodesItem class

An abstract layout item that provides generic methods for node based shapes such as polygon or polylines.

Base classes

class QgsLayoutItem
Base class for graphical items within a QgsLayout.

Derived classes

class QgsLayoutItemPolygon
Layout item for node based polygon shapes.
class QgsLayoutItemPolyline
Layout item for node based polyline shapes.

Constructors, destructors, conversion operators

QgsLayoutNodesItem(QgsLayout* layout) protected
Constructor for QgsLayoutNodesItem, attached to the specified layout.
QgsLayoutNodesItem(const QPolygonF& polygon, QgsLayout* layout) protected
Constructor for a QgsLayoutNodesItem with the given polygon nodes, attached to the specified layout.

Public functions

auto addNode(QPointF point, bool checkArea = true, double radius = 10) -> bool
Add a node in current shape.
void deselectNode()
Deselects any selected nodes.
auto estimatedFrameBleed() const -> double override
Returns the estimated amount the item's frame bleeds outside the item's actual rectangle.
auto moveNode(int index, QPointF node) -> bool
Moves a node to a new position.
auto nodeAtPosition(QPointF point, bool searchInRadius = true, double radius = 10) const -> int
Search for the nearest node in the shape within a maximal area.
auto nodePosition(int index, QPointF& position) const -> bool
Gets the position of a node in scene coordinates.
auto nodes() const -> QPolygonF
Returns the nodes the shape consists of.
auto nodesSize() const -> int
Returns the number of nodes in the shape.
auto removeNode(int index) -> bool
Remove a node with specified index from the shape.
auto selectedNode() const -> int
Returns the currently selected node, or -1 if no node is selected.
void setDisplayNodes(bool display = true)
Set whether the item's nodes should be displayed.
void setNodes(const QPolygonF& nodes)
Sets the nodes the shape consists of.
auto setSelectedNode(int index) -> bool
Selects a node by index.

Protected functions

auto _addNode(int nodeIndex, QPointF newNode, double radius) -> bool pure virtual
Method called in addNode.
void _draw(QgsLayoutItemRenderContext& context, const QStyleOptionGraphicsItem* itemStyle = nullptr) pure virtual
Method called in paint.
void _readXmlStyle(const QDomElement& elmt, const QgsReadWriteContext& context) pure virtual
Method called in readXml.
auto _removeNode(int nodeIndex) -> bool pure virtual
Method called in removeNode.
void _writeXmlStyle(QDomDocument& doc, QDomElement& elmt, const QgsReadWriteContext& context) const pure virtual
Method called in writeXml.
auto computeDistance(QPointF pt1, QPointF pt2) const -> double
Compute an euclidian distance between 2 nodes.
void draw(QgsLayoutItemRenderContext& context) override
Draws the item's contents using the specified item render context.
auto readPropertiesFromElement(const QDomElement& element, const QDomDocument& document, const QgsReadWriteContext& context) -> bool override
Sets item state from a DOM element.
void rescaleToFitBoundingBox()
Rescale the current shape according to the item's bounding box.
void updateSceneRect()
Update the current scene rectangle for this item.
auto writePropertiesToElement(QDomElement& element, QDomDocument& document, const QgsReadWriteContext& context) const -> bool override
Stores item state within an XML DOM element.

Protected slots

void updateBoundingRect() virtual
Called when the bounding rect of the item should recalculated.

Protected variables

QRectF mCurrentRectangle
Current bounding rectangle of shape.
double mMaxSymbolBleed
Max symbol bleed.
QPolygonF mPolygon
Shape's nodes.

Function documentation

bool QgsLayoutNodesItem::addNode(QPointF point, bool checkArea = true, double radius = 10)

Add a node in current shape.

Parameters
point is the location of the new node (in scene coordinates)
checkArea is a flag to indicate if there's a space constraint.
radius is the space contraint and is used only if checkArea is true. Typically, if this flag is true, the new node has to be nearer than radius to the shape to be added.

double QgsLayoutNodesItem::estimatedFrameBleed() const override

Returns the estimated amount the item's frame bleeds outside the item's actual rectangle.

For instance, if the item has a 2mm frame stroke, then 1mm of this frame is drawn outside the item's rect. In this case the return value will be 1.0.

Returned values are in layout units.

bool QgsLayoutNodesItem::moveNode(int index, QPointF node)

Moves a node to a new position.

Parameters
index the index of the node to move
node is the new position in scene coordinate

int QgsLayoutNodesItem::nodeAtPosition(QPointF point, bool searchInRadius = true, double radius = 10) const

Search for the nearest node in the shape within a maximal area.

Parameters
point is the location to search for nodes from (in scene coordinates)
searchInRadius is a flag to indicate if the area of research is limited in space.
radius is only used if searchInRadius is true

Returns the index of the nearest node or -1 if no node was found.

bool QgsLayoutNodesItem::nodePosition(int index, QPointF& position) const

Gets the position of a node in scene coordinates.

Parameters
index of the node
position the position of the node
Returns true if the index is valid and the position is set, false otherwise

QPolygonF QgsLayoutNodesItem::nodes() const

Returns the nodes the shape consists of.

void QgsLayoutNodesItem::setNodes(const QPolygonF& nodes)

Sets the nodes the shape consists of.

void QgsLayoutNodesItem::draw(QgsLayoutItemRenderContext& context) override protected

Draws the item's contents using the specified item render context.

Note that the context's painter has been scaled so that painter units are pixels. Use the QgsRenderContext methods to convert from millimeters or other units to the painter's units.

bool QgsLayoutNodesItem::readPropertiesFromElement(const QDomElement& element, const QDomDocument& document, const QgsReadWriteContext& context) override protected

Sets item state from a DOM element.

Parameters
element is the DOM element for the item
document DOM document
context read write context

Note that item subclasses should not rely on all other items being present in the layout at the time this method is called. Instead, any connections and links to other items must be made in the finalizeRestoreFromXml() method. E.g. when restoring a scalebar, the connection to the linked map's signals should be implemented in finalizeRestoreFromXml(), not readPropertiesFromElement().

void QgsLayoutNodesItem::rescaleToFitBoundingBox() protected

Rescale the current shape according to the item's bounding box.

Useful when the shape is resized thanks to the rubber band.

bool QgsLayoutNodesItem::writePropertiesToElement(QDomElement& element, QDomDocument& document, const QgsReadWriteContext& context) const override protected

Stores item state within an XML DOM element.

Parameters
element is the DOM element to store the item's properties in
document DOM document
context read write context

void QgsLayoutNodesItem::updateBoundingRect() virtual protected slot

Called when the bounding rect of the item should recalculated.

Subclasses should update currentRectangle in their implementations.