28#include <QStyleOptionGraphicsItem>
30#include "moc_qgslayoutitemnodeitem.cpp"
32using namespace Qt::StringLiterals;
67 mPolygon = polygon.translated( -topLeft );
70void QgsLayoutNodesItem::init()
74 setCacheMode( QGraphicsItem::NoCache );
84 painter->setPen( Qt::NoPen );
85 painter->setBrush( Qt::NoBrush );
93 if ( mDrawNodes &&
layout()->renderContext().isPreviewRender() )
105 return std::sqrt( std::pow( pt1.x() - pt2.x(), 2 ) + std::pow( pt1.y() - pt2.y(), 2 ) );
109 const bool checkArea,
110 const double radius )
112 const QPointF start = mapFromScene( pt );
113 double minDistance = std::numeric_limits<double>::max();
114 const double maxDistance = ( checkArea ) ? radius : minDistance;
118 for (
int i = 0; i !=
mPolygon.size(); i++ )
121 const QPointF pt1 =
mPolygon.at( i );
127 const double coef = ( pt2.y() - pt1.y() ) / ( pt2.x() - pt1.x() );
128 const double b = pt1.y() - coef * pt1.x();
130 double distance = std::numeric_limits<double>::max();
131 if ( std::isinf( coef ) )
132 distance = std::fabs( pt1.x() - start.x() );
135 const double coef2 = ( -1 / coef );
136 const double b2 = start.y() - coef2 * start.x();
139 if ( std::isinf( coef2 ) )
141 distance = std::fabs( pt1.y() - start.y() );
142 inter.setX( start.x() );
143 inter.setY( pt1.y() );
147 const double interx = ( b - b2 ) / ( coef2 - coef );
148 const double intery = interx * coef2 + b2;
149 inter.setX( interx );
150 inter.setY( intery );
157 const double length4 = length1 + length2;
159 if ( std::fabs( length3 - length4 ) < std::numeric_limits<float>::epsilon() )
163 if ( distance < minDistance && distance < maxDistance )
165 minDistance = distance;
172 rc =
_addNode( idx, start, maxDistance );
186 QVariantMap properties;
187 properties.insert( u
"name"_s, u
"cross"_s );
188 properties.insert( u
"color_border"_s, u
"red"_s );
191 symbol->setSize( rectSize );
192 symbol->setAngle( 45 );
195 for (
const QPointF pt : std::as_const(
mPolygon ) )
199 if ( mSelectedNode >= 0 && mSelectedNode <
mPolygon.size() )
200 drawSelectedNode( context );
207 QVariantMap properties;
208 properties.insert( u
"name"_s, u
"square"_s );
209 properties.insert( u
"color"_s, u
"0, 0, 0, 0"_s );
210 properties.insert( u
"color_border"_s, u
"blue"_s );
211 properties.insert( u
"width_border"_s, u
"4"_s );
214 symbol->setSize( rectSize );
222 const bool searchInRadius,
223 const double radius )
const
225 const QPointF pt = mapFromScene( node );
226 double nearestDistance = std::numeric_limits<double>::max();
227 const double maxDistance = ( searchInRadius ) ? radius : nearestDistance;
232 for (
const QPointF polyPt : std::as_const(
mPolygon ) )
235 if ( distance < nearestDistance && distance < maxDistance )
237 nearestDistance = distance;
250 if ( index >= 0 && index <
mPolygon.size() )
252 position = mapToScene(
mPolygon.at( index ) );
274 if ( index >= 0 && index <
mPolygon.size() )
276 const QPointF nodeItem = mapFromScene( pt );
277 mPolygon.replace( index, nodeItem );
290 const QDomElement styleSymbolElem = itemElem.firstChildElement( u
"symbol"_s );
291 if ( !styleSymbolElem.isNull() )
296 const QDomNodeList nodesList = itemElem.elementsByTagName( u
"node"_s );
297 for (
int i = 0; i < nodesList.size(); i++ )
299 const QDomElement nodeElem = nodesList.at( i ).toElement();
301 newPt.setX( nodeElem.attribute( u
"x"_s ).toDouble() );
302 newPt.setY( nodeElem.attribute( u
"y"_s ).toDouble() );
324 trans = trans.scale( ratioX, ratioY );
333 if ( index >= 0 && index <
mPolygon.size() )
335 mSelectedNode = index;
345 const QRectF br =
mPolygon.boundingRect();
347 const QPointF topLeft = mapToScene( br.topLeft() );
352 mPolygon.translate( -br.topLeft().x(), -br.topLeft().y() );
359 prepareGeometryChange();
372 QDomElement nodesElem = doc.createElement( u
"nodes"_s );
373 for (
const QPointF pt : std::as_const(
mPolygon ) )
375 QDomElement nodeElem = doc.createElement( u
"node"_s );
376 nodeElem.setAttribute( u
"x"_s, QString::number( pt.x() ) );
377 nodeElem.setAttribute( u
"y"_s, QString::number( pt.y() ) );
378 nodesElem.appendChild( nodeElem );
380 elem.appendChild( nodesElem );
@ Default
Allow raster-based rendering in situations where it is required for correct rendering or where it wil...
@ PreferVector
Prefer vector-based rendering, when the result will still be visually near-identical to a raster-base...
Contains settings and helpers relating to a render of a QgsLayoutItem.
QgsRenderContext & renderContext()
Returns a reference to the context's render context.
double viewScaleFactor() const
Returns the current view zoom (scale factor).
QgsLayoutItem(QgsLayout *layout, bool manageZValue=true)
Constructor for QgsLayoutItem, with the specified parent layout.
virtual void setFrameEnabled(bool drawFrame)
Sets whether this item has a frame drawn around it or not.
@ FlagDisableSceneCaching
Item should not have QGraphicsItem caching enabled.
void sizePositionChanged()
Emitted when the item's size or position changes.
void clipPathChanged()
Emitted when the item's clipping path has changed.
void attemptSetSceneRect(const QRectF &rect, bool includesFrame=false)
Attempts to update the item's position and size to match the passed rect in layout coordinates.
void setBackgroundEnabled(bool drawBackground)
Sets whether this item has a background drawn under it or not.
QgsLayoutItem::Flags itemFlags() const override
Returns the item's flags, which indicate how the item behaves.
virtual void updateBoundingRect()
Called when the bounding rect of the item should recalculated.
virtual bool _removeNode(int nodeIndex)=0
Method called in removeNode.
QPolygonF nodes() const
Returns the nodes the shape consists of.
double mMaxSymbolBleed
Max symbol bleed.
virtual void _writeXmlStyle(QDomDocument &doc, QDomElement &elmt, const QgsReadWriteContext &context) const =0
Method called in writeXml.
QRectF mCurrentRectangle
Current bounding rectangle of shape.
bool readPropertiesFromElement(const QDomElement &element, const QDomDocument &document, const QgsReadWriteContext &context) override
Sets item state from a DOM element.
bool setSelectedNode(int index)
Selects a node by index.
bool writePropertiesToElement(QDomElement &element, QDomDocument &document, const QgsReadWriteContext &context) const override
Stores item state within an XML DOM element.
void updateSceneRect()
Update the current scene rectangle for this item.
virtual void _readXmlStyle(const QDomElement &elmt, const QgsReadWriteContext &context)=0
Method called in readXml.
bool removeNode(int index)
Remove a node with specified index from the shape.
QRectF boundingRect() const override
double computeDistance(QPointF pt1, QPointF pt2) const
Compute an euclidean distance between 2 nodes.
bool nodePosition(int index, QPointF &position) const
Gets the position of a node in scene coordinates.
bool addNode(QPointF point, bool checkArea=true, double radius=10)
Add a node in current shape.
bool moveNode(int index, QPointF node)
Moves a node to a new position.
double estimatedFrameBleed() const override
Returns the estimated amount the item's frame bleeds outside the item's actual rectangle.
void draw(QgsLayoutItemRenderContext &context) override
Draws the item's contents using the specified item render context.
virtual void _draw(QgsLayoutItemRenderContext &context, const QStyleOptionGraphicsItem *itemStyle=nullptr)=0
Method called in paint.
QgsLayoutNodesItem(QgsLayout *layout)
Constructor for QgsLayoutNodesItem, attached to the specified layout.
virtual bool _addNode(int nodeIndex, QPointF newNode, double radius)=0
Method called in addNode.
void rescaleToFitBoundingBox()
Rescale the current shape according to the item's bounding box.
void setNodes(const QPolygonF &nodes)
Sets the nodes the shape consists of.
QPolygonF mPolygon
Shape's nodes.
int nodeAtPosition(QPointF point, bool searchInRadius=true, double radius=10) const
Search for the nearest node in the shape within a maximal area.
const QgsLayout * layout() const
Returns the layout the object is attached to.
void changed()
Emitted when the object's properties change.
static std::unique_ptr< QgsMarkerSymbol > createSimple(const QVariantMap &properties)
Create a marker symbol with one symbol layer: SimpleMarker with specified properties.
A container for the context for various read/write operations on objects.
QPainter * painter()
Returns the destination QPainter for the render operation.
void setRasterizedRenderingPolicy(Qgis::RasterizedRenderingPolicy policy)
Sets the policy controlling when rasterisation of content during renders is permitted.
Qgis::RasterizedRenderingPolicy rasterizedRenderingPolicy() const
Returns the policy controlling when rasterisation of content during renders is permitted.
bool qgsDoubleNear(double a, double b, double epsilon=4 *std::numeric_limits< double >::epsilon())
Compare two doubles (but allow some difference).