Skip to content

Commit 73d69f2

Browse files
committed
Merge pull request #1362 from manisandro/composer_arrow_fill_bb
Make composer arrow, with and without markers, better fill the bounding box area
2 parents b385467 + 9d838c9 commit 73d69f2

File tree

1 file changed

+11
-23
lines changed

1 file changed

+11
-23
lines changed

src/core/composer/qgscomposerarrow.cpp

+11-23
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include "qgscomposition.h"
2020
#include <QPainter>
2121
#include <QSvgRenderer>
22+
#include <QVector2D>
2223

2324
#include <cmath>
2425

@@ -76,7 +77,6 @@ void QgsComposerArrow::paint( QPainter* painter, const QStyleOptionGraphicsItem
7677

7778
//draw arrow
7879
QPen arrowPen = mPen;
79-
arrowPen.setCapStyle( Qt::FlatCap );
8080
arrowPen.setColor( mArrowColor );
8181
painter->setPen( arrowPen );
8282
painter->setBrush( QBrush( mArrowColor ) );
@@ -134,7 +134,9 @@ void QgsComposerArrow::drawHardcodedMarker( QPainter *p, MarkerType type )
134134
QBrush arrowBrush = p->brush();
135135
arrowBrush.setColor( mArrowColor );
136136
p->setBrush( arrowBrush );
137-
drawArrowHead( p, mStopPoint.x() - pos().x(), mStopPoint.y() - pos().y(), angle( mStartPoint, mStopPoint ), mArrowHeadWidth );
137+
QVector2D dir = QVector2D( mStopPoint - mStartPoint ).normalized();
138+
QPointF stop = mStopPoint + ( dir * 0.5 * mArrowHeadWidth ).toPointF();
139+
drawArrowHead( p, stop.x() - pos().x(), stop.y() - pos().y(), angle( mStartPoint, stop ), mArrowHeadWidth );
138140
}
139141

140142
void QgsComposerArrow::drawSVGMarker( QPainter* p, MarkerType type, const QString &markerPath )
@@ -199,27 +201,11 @@ void QgsComposerArrow::drawSVGMarker( QPainter* p, MarkerType type, const QStrin
199201
}
200202
}
201203

202-
//rotate image fix point for backtransform
203-
QPointF fixPoint;
204-
if ( type == StartMarker )
205-
{
206-
fixPoint.setX( 0 ); fixPoint.setY( arrowHeadHeight / 2.0 );
207-
}
208-
else
209-
{
210-
fixPoint.setX( 0 ); fixPoint.setY( -arrowHeadHeight / 2.0 );
211-
}
212-
QPointF rotatedFixPoint;
213-
double angleRad = ang / 180 * M_PI;
214-
rotatedFixPoint.setX( fixPoint.x() * cos( angleRad ) + fixPoint.y() * -sin( angleRad ) );
215-
rotatedFixPoint.setY( fixPoint.x() * sin( angleRad ) + fixPoint.y() * cos( angleRad ) );
216-
217-
218204
QPainter imagePainter( &markerImage );
219205
r.render( &imagePainter );
220206

221207
p->save();
222-
p->translate( canvasPoint.x() - rotatedFixPoint.x() , canvasPoint.y() - rotatedFixPoint.y() );
208+
p->translate( canvasPoint.x() , canvasPoint.y() );
223209
p->rotate( ang );
224210
p->translate( -mArrowHeadWidth / 2.0, -arrowHeadHeight / 2.0 );
225211

@@ -280,16 +266,18 @@ double QgsComposerArrow::computeMarkerMargin() const
280266
double margin = 0;
281267
if ( mMarkerMode == DefaultMarker )
282268
{
283-
margin = mPen.widthF() / 2.0 + mArrowHeadWidth / 2.0;
269+
margin = mPen.widthF() / std::sqrt( 2.0 ) + mArrowHeadWidth / 2.0;
284270
}
285271
else if ( mMarkerMode == NoMarker )
286272
{
287-
margin = mPen.widthF() / 2.0;
273+
margin = mPen.widthF() / std::sqrt( 2.0 );
288274
}
289275
else if ( mMarkerMode == SVGMarker )
290276
{
291-
double maxArrowHeight = qMax( mStartArrowHeadHeight, mStopArrowHeadHeight );
292-
margin = mPen.widthF() / 2 + qMax( mArrowHeadWidth / 2.0, maxArrowHeight / 2.0 );
277+
double startMarkerMargin = std::sqrt( 0.25 * ( mStartArrowHeadHeight * mStartArrowHeadHeight + mArrowHeadWidth * mArrowHeadWidth ) );
278+
double stopMarkerMargin = std::sqrt( 0.25 * ( mStopArrowHeadHeight * mStopArrowHeadHeight + mArrowHeadWidth * mArrowHeadWidth ) );
279+
double markerMargin = qMax( startMarkerMargin, stopMarkerMargin );
280+
margin = qMax( mPen.widthF() / std::sqrt( 2.0 ), markerMargin );
293281
}
294282
return margin;
295283
}

0 commit comments

Comments
 (0)