Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Merge pull request #9129 from mhugent/itempolyline_end_arrow
Move end arrowhead by half arrow width
  • Loading branch information
mhugent committed Feb 8, 2019
2 parents b7138b1 + 14e5176 commit 4eaae77
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 1 deletion.
15 changes: 14 additions & 1 deletion src/core/layout/qgslayoutitempolyline.cpp
Expand Up @@ -26,6 +26,7 @@
#include <QSvgRenderer>
#include <limits>
#include <QGraphicsPathItem>
#include <QVector2D>

QgsLayoutItemPolyline::QgsLayoutItemPolyline( QgsLayout *layout )
: QgsLayoutNodesItem( layout )
Expand Down Expand Up @@ -164,7 +165,13 @@ void QgsLayoutItemPolyline::drawEndMarker( QPainter *painter )
// calculate angle at end of line
QLineF endLine( mPolygon.at( mPolygon.count() - 2 ), mPolygon.at( mPolygon.count() - 1 ) );
double angle = endLine.angle();
drawArrow( painter, endLine.p2(), angle );

//move end point depending on arrow width
QVector2D dir = QVector2D( endLine.dx(), endLine.dy() ).normalized();
QPointF endPoint = endLine.p2();
endPoint += ( dir * 0.5 * mArrowHeadWidth ).toPointF();

drawArrow( painter, endPoint, angle );
break;
}
case MarkerMode::SvgMarker:
Expand All @@ -189,6 +196,7 @@ void QgsLayoutItemPolyline::drawArrow( QPainter *painter, QPointF center, double
QBrush b;
b.setColor( mArrowHeadFillColor );
painter->setBrush( b );

drawArrowHead( painter, center.x(), center.y(), angle, mArrowHeadWidth );
}

Expand All @@ -205,6 +213,7 @@ void QgsLayoutItemPolyline::drawArrowHead( QPainter *p, const double x, const do

double angleRad = angle / 180.0 * M_PI;
QPointF middlePoint( x, y );

//rotate both arrow points
QPointF p1 = QPointF( -arrowHeadWidth / 2.0, arrowHeadWidth );
QPointF p2 = QPointF( arrowHeadWidth / 2.0, arrowHeadWidth );
Expand Down Expand Up @@ -433,6 +442,10 @@ void QgsLayoutItemPolyline::updateBoundingRect()
QRectF br = rect();

double margin = std::max( mMaxSymbolBleed, computeMarkerMargin() );
if ( mEndMarker == ArrowHead )
{
margin += 0.5 * mArrowHeadWidth;
}
br.adjust( -margin, -margin, margin, margin );
mCurrentRectangle = br;

Expand Down
11 changes: 11 additions & 0 deletions tests/src/python/test_qgslayoutpolyline.py
Expand Up @@ -146,6 +146,17 @@ def testSelectedNode(self):
myTestResult, myMessage = checker.testLayout()
assert myTestResult, myMessage

def testEndArrow(self):
self.polyline.setEndMarker(QgsLayoutItemPolyline.ArrowHead)
self.polyline.setArrowHeadWidth(30.0)

checker = QgsLayoutChecker('composerpolyline_endArrow', self.layout)
checker.setControlPathPrefix("composer_polyline")
myTestResult, myMessage = checker.testLayout()
assert myTestResult, myMessage

self.polyline.setEndMarker(QgsLayoutItemPolyline.NoMarker)

def testRemoveNode(self):
"""Test removeNode method"""

Expand Down
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 4eaae77

Please sign in to comment.