34#include <QGraphicsPathItem>
36#include <QSvgRenderer>
39#include "moc_qgslayoutitempolyline.cpp"
41using namespace Qt::StringLiterals;
46 createDefaultPolylineStyleSymbol();
52 createDefaultPolylineStyleSymbol();
77 if ( indexPoint == (
mPolygon.size() - 1 ) )
79 if ( distEnd < radius )
81 else if ( distStart < radius )
85 mPolygon.insert( indexPoint + 1, newPoint );
97 int newSelectNode = index;
105void QgsLayoutItemPolyline::createDefaultPolylineStyleSymbol()
107 QVariantMap properties;
108 properties.insert( u
"color"_s, u
"0,0,0,255"_s );
109 properties.insert( u
"width"_s, u
"0.3"_s );
110 properties.insert( u
"capstyle"_s, u
"square"_s );
116void QgsLayoutItemPolyline::refreshSymbol()
118 if (
auto *lLayout =
layout() )
129void QgsLayoutItemPolyline::drawStartMarker( QPainter *painter )
134 switch ( mStartMarker )
145 const double angle = startLine.angle();
148 const QVector2D dir = QVector2D( startLine.dx(), startLine.dy() ).normalized();
149 QPointF startPoint = startLine.p2();
150 startPoint += ( dir * 0.5 * mArrowHeadWidth ).toPointF();
152 drawArrow( painter, startPoint, angle );
154 else if ( mVersion == 1 )
157 const double angle = startLine.angle();
158 drawArrow( painter,
mPolygon.at( 0 ), angle );
172 else if ( mVersion == 1 )
176 const double angle = startLine.angle();
177 drawSvgMarker( painter,
mPolygon.at( 0 ), angle, mStartMarkerFile, mStartArrowHeadHeight );
183void QgsLayoutItemPolyline::drawEndMarker( QPainter *painter )
188 switch ( mEndMarker )
197 const double angle = endLine.angle();
200 const QVector2D dir = QVector2D( endLine.dx(), endLine.dy() ).normalized();
201 QPointF endPoint = endLine.p2();
202 endPoint += ( dir * 0.5 * mArrowHeadWidth ).toPointF();
204 drawArrow( painter, endPoint, angle );
211 const double angle = endLine.angle();
212 drawSvgMarker( painter, endLine.p2(), angle, mEndMarkerFile, mEndArrowHeadHeight );
218void QgsLayoutItemPolyline::drawArrow( QPainter *painter, QPointF center,
double angle )
223 p.setColor( mArrowHeadStrokeColor );
224 p.setWidthF( mArrowHeadStrokeWidth );
225 painter->setPen( p );
227 b.setColor( mArrowHeadFillColor );
228 painter->setBrush( b );
230 drawArrowHead( painter, center.x(), center.y(), angle, mArrowHeadWidth );
233void QgsLayoutItemPolyline::updateMarkerSvgSizes()
239void QgsLayoutItemPolyline::drawArrowHead( QPainter *p,
const double x,
const double y,
const double angle,
const double arrowHeadWidth )
244 const double angleRad =
angle / 180.0 * M_PI;
245 const QPointF middlePoint( x, y );
251 QPointF p1Rotated, p2Rotated;
252 p1Rotated.setX( p1.x() * std::cos( angleRad ) + p1.y() * -std::sin( angleRad ) );
253 p1Rotated.setY( p1.x() * std::sin( angleRad ) + p1.y() * std::cos( angleRad ) );
254 p2Rotated.setX( p2.x() * std::cos( angleRad ) + p2.y() * -std::sin( angleRad ) );
255 p2Rotated.setY( p2.x() * std::sin( angleRad ) + p2.y() * std::cos( angleRad ) );
257 QPolygonF arrowHeadPoly;
258 arrowHeadPoly << middlePoint;
259 arrowHeadPoly << QPointF( middlePoint.x() + p1Rotated.x(), middlePoint.y() + p1Rotated.y() );
260 arrowHeadPoly << QPointF( middlePoint.x() + p2Rotated.x(), middlePoint.y() + p2Rotated.y() );
261 QPen arrowPen = p->pen();
262 arrowPen.setJoinStyle( Qt::RoundJoin );
263 QBrush arrowBrush = p->brush();
264 arrowBrush.setStyle( Qt::SolidPattern );
265 p->setPen( arrowPen );
266 p->setBrush( arrowBrush );
267 arrowBrush.setStyle( Qt::SolidPattern );
268 p->drawPolygon( arrowHeadPoly );
271void QgsLayoutItemPolyline::drawSvgMarker( QPainter *p, QPointF point,
double angle,
const QString &markerPath,
double height )
const
276 if ( mArrowHeadWidth <= 0 || height <= 0 )
282 if ( markerPath.isEmpty() )
286 const QByteArray &svgContent =
QgsApplication::svgCache()->
svgContent( markerPath, mArrowHeadWidth, mArrowHeadFillColor, mArrowHeadStrokeColor, mArrowHeadStrokeWidth, 1.0 );
287 r.load( svgContent );
289 const QgsScopedQPainterState painterState( p );
290 p->translate( point.x(), point.y() );
292 p->translate( -mArrowHeadWidth / 2.0, -height / 2.0 );
293 r.render( p, QRectF( 0, 0, mArrowHeadWidth, height ) );
298 if ( !
id().isEmpty() )
301 return tr(
"<Polyline>" );
314 const QTransform t = QTransform::fromScale( scale, scale );
316 mPolylineStyleSymbol->startRender( renderContext );
317 mPolylineStyleSymbol->renderPolyline( t.map(
mPolygon ),
nullptr, renderContext );
318 mPolylineStyleSymbol->stopRender( renderContext );
323 drawStartMarker( renderContext.
painter() );
324 drawEndMarker( renderContext.
painter() );
352 mArrowHeadWidth = width;
353 updateMarkerSvgSizes();
362 QPainterPathStroker ps;
365 const QPainterPath strokedOutline = ps.createStroke( path );
367 return strokedOutline;
373 QList<QPointF> uniquePoints;
377 if ( !uniquePoints.contains( point ) )
379 uniquePoints.append( point );
389 return mPolylineStyleSymbol.get();
395 mStartMarkerFile = path;
396 if ( path.isEmpty() || !r.load( path ) )
398 mStartArrowHeadHeight = 0;
403 const QRect viewBox = r.viewBox();
404 mStartArrowHeadHeight = mArrowHeadWidth / viewBox.width() * viewBox.height();
412 mEndMarkerFile = path;
413 if ( path.isEmpty() || !r.load( path ) )
415 mEndArrowHeadHeight = 0;
420 const QRect viewBox = r.viewBox();
421 mEndArrowHeadHeight = mArrowHeadWidth / viewBox.width() * viewBox.height();
428 mArrowHeadStrokeColor = color;
434 mArrowHeadFillColor = color;
440 mArrowHeadStrokeWidth = width;
447 if ( mPolylineStyleSymbol )
460 elmt.appendChild( pe );
471 elmt.setAttribute( u
"arrowHeadWidth"_s, QString::number( mArrowHeadWidth ) );
474 elmt.setAttribute( u
"outlineWidth"_s, QString::number( mArrowHeadStrokeWidth ) );
475 elmt.setAttribute( u
"markerMode"_s, mEndMarker );
476 elmt.setAttribute( u
"startMarkerMode"_s, mStartMarker );
477 elmt.setAttribute( u
"startMarkerFile"_s, startMarkerPath );
478 elmt.setAttribute( u
"endMarkerFile"_s, endMarkerPath );
479 elmt.setAttribute( u
"version"_s, mVersion );
486 mArrowHeadWidth = elmt.attribute( u
"arrowHeadWidth"_s, u
"2.0"_s ).toDouble();
489 mArrowHeadStrokeWidth = elmt.attribute( u
"outlineWidth"_s, u
"1.0"_s ).toDouble();
491 const QString startMarkerPath = elmt.attribute( u
"startMarkerFile"_s, QString() );
492 const QString endMarkerPath = elmt.attribute( u
"endMarkerFile"_s, QString() );
497 mVersion = elmt.attribute( u
"version"_s, u
"1"_s ).toInt();
512 margin += 0.5 * mArrowHeadWidth;
514 br.adjust( -margin, -margin, margin, margin );
515 prepareGeometryChange();
523double QgsLayoutItemPolyline::computeMarkerMargin()
const
529 margin = mArrowHeadStrokeWidth / 2.0 + mArrowHeadWidth * M_SQRT2;
534 const double startMarkerMargin = std::sqrt( 0.25 * ( mStartArrowHeadHeight * mStartArrowHeadHeight + mArrowHeadWidth * mArrowHeadWidth ) );
535 margin = std::max( startMarkerMargin, margin );
540 const double endMarkerMargin = std::sqrt( 0.25 * ( mEndArrowHeadHeight * mEndArrowHeadHeight + mArrowHeadWidth * mArrowHeadWidth ) );
541 margin = std::max( endMarkerMargin, margin );
@ Millimeters
Millimeters.
@ DisableSymbolClippingToExtent
Force symbol clipping to map extent to be disabled in all situations. This will result in slower rend...
static QIcon getThemeIcon(const QString &name, const QColor &fillColor=QColor(), const QColor &strokeColor=QColor())
Helper to get a theme icon.
static QgsSvgCache * svgCache()
Returns the application's SVG cache, used for caching SVG images and handling parameter replacement w...
static QColor colorFromString(const QString &string)
Decodes a string into a color value.
static QString colorToString(const QColor &color)
Encodes a color into a string value.
bool isValid() const override
Must be reimplemented in subclasses.
void setArrowHeadWidth(double width)
Sets the width of line arrow heads in mm.
void setEndMarker(MarkerMode mode)
Sets the end marker mode, which controls what marker is drawn at the end of the line.
void setEndSvgMarkerPath(const QString &path)
Sets the path to a SVG marker to draw at the end of the line.
void _writeXmlStyle(QDomDocument &doc, QDomElement &elmt, const QgsReadWriteContext &context) const override
Method called in writeXml.
void setArrowHeadStrokeWidth(double width)
Sets the pen width in millimeters for the stroke of the arrow head.
void _readXmlStyle(const QDomElement &elmt, const QgsReadWriteContext &context) override
Method called in readXml.
void updateBoundingRect() override
void setArrowHeadFillColor(const QColor &color)
Sets the color used to fill the arrow head.
bool _removeNode(int nodeIndex) override
Method called in removeNode.
QgsLineSymbol * symbol()
Returns the line symbol used to draw the shape.
void setArrowHeadStrokeColor(const QColor &color)
Sets the color used to draw the stroke around the arrow head.
void setStartMarker(MarkerMode mode)
Sets the start marker mode, which controls what marker is drawn at the start of the line.
MarkerMode
Vertex marker mode.
@ ArrowHead
Show arrow marker.
@ SvgMarker
Show SVG marker.
@ NoMarker
Don't show marker.
int type() const override
QPainterPath shape() const override
~QgsLayoutItemPolyline() override
bool accept(QgsStyleEntityVisitorInterface *visitor) const override
Accepts the specified style entity visitor, causing it to visit all style entities associated with th...
bool readPropertiesFromElement(const QDomElement &element, const QDomDocument &document, const QgsReadWriteContext &context) override
Sets item state from a DOM element.
QString displayName() const override
Gets item display name.
static QgsLayoutItemPolyline * create(QgsLayout *layout)
Returns a new polyline item for the specified layout.
bool _addNode(int indexPoint, QPointF newPoint, double radius) override
Method called in addNode.
void setSymbol(QgsLineSymbol *symbol)
Sets the symbol used to draw the shape.
QIcon icon() const override
Returns the item's icon.
double arrowHeadWidth() const
Returns the width of line arrow heads in mm.
bool writePropertiesToElement(QDomElement &element, QDomDocument &document, const QgsReadWriteContext &context) const override
Stores item state within an XML DOM element.
void _draw(QgsLayoutItemRenderContext &context, const QStyleOptionGraphicsItem *itemStyle=nullptr) override
Method called in paint.
void setStartSvgMarkerPath(const QString &path)
Sets the path to a SVG marker to draw at the start of the line.
QgsLayoutItemPolyline(QgsLayout *layout)
Constructor for QgsLayoutItemPolyline for the specified layout.
@ LayoutPolyline
Polyline shape item.
Contains settings and helpers relating to a render of a QgsLayoutItem.
QgsRenderContext & renderContext()
Returns a reference to the context's render context.
virtual QString uuid() const
Returns the item identification string.
QString id() const
Returns the item's ID name.
void frameChanged()
Emitted if the item's frame style changes.
double mMaxSymbolBleed
Max symbol bleed.
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.
double computeDistance(QPointF pt1, QPointF pt2) const
Compute an euclidean distance between 2 nodes.
QgsLayoutNodesItem(QgsLayout *layout)
Constructor for QgsLayoutNodesItem, attached to the specified layout.
QPolygonF mPolygon
Shape's nodes.
const QgsLayout * layout() const
Returns the layout the object is attached to.
static QgsRenderContext createRenderContextForLayout(QgsLayout *layout, QPainter *painter, double dpi=-1)
Creates a render context suitable for the specified layout and painter destination.
A line symbol type, for rendering LineString and MultiLineString geometries.
static std::unique_ptr< QgsLineSymbol > createSimple(const QVariantMap &properties)
Create a line symbol with one symbol layer: SimpleLine with specified properties.
A container for the context for various read/write operations on objects.
const QgsPathResolver & pathResolver() const
Returns path resolver for conversion between relative and absolute paths.
Contains information about the context of a rendering operation.
double scaleFactor() const
Returns the scaling factor for the render to convert painter units to physical sizes.
double convertToPainterUnits(double size, Qgis::RenderUnit unit, const QgsMapUnitScale &scale=QgsMapUnitScale(), Qgis::RenderSubcomponentProperty property=Qgis::RenderSubcomponentProperty::Generic) const
Converts a size from the specified units to painter units (pixels).
QPainter * painter()
Returns the destination QPainter for the render operation.
void setFlag(Qgis::RenderContextFlag flag, bool on=true)
Enable or disable a particular flag (other flags are not affected).
Scoped object for saving and restoring a QPainter object's state.
An interface for classes which can visit style entity (e.g.
virtual bool visit(const QgsStyleEntityVisitorInterface::StyleLeaf &entity)
Called when the visitor will visit a style entity.
A symbol entity for QgsStyle databases.
QByteArray svgContent(const QString &path, double size, const QColor &fill, const QColor &stroke, double strokeWidth, double widthScaleFactor, double fixedAspectRatio=0, bool blocking=false, const QMap< QString, QString > ¶meters=QMap< QString, QString >(), bool *isMissingImage=nullptr)
Gets the SVG content corresponding to the given path.
static QString svgSymbolPathToName(const QString &path, const QgsPathResolver &pathResolver)
Determines an SVG symbol's name from its path.
static std::unique_ptr< QgsSymbol > loadSymbol(const QDomElement &element, const QgsReadWriteContext &context)
Attempts to load a symbol from a DOM element.
static QString svgSymbolNameToPath(const QString &name, const QgsPathResolver &pathResolver)
Determines an SVG symbol's path from its name.
static QDomElement saveSymbol(const QString &symbolName, const QgsSymbol *symbol, QDomDocument &doc, const QgsReadWriteContext &context)
Writes a symbol definition to XML.
static double estimateMaxSymbolBleed(QgsSymbol *symbol, const QgsRenderContext &context)
Returns the maximum estimated bleed for the symbol.
double ANALYSIS_EXPORT angle(QgsPoint *p1, QgsPoint *p2, QgsPoint *p3, QgsPoint *p4)
Calculates the angle between two segments (in 2 dimension, z-values are ignored).
Contains information relating to the style entity currently being visited.