Skip to content

Commit 4374c6e

Browse files
committedOct 1, 2017
Fix incorrect draw stacking when drawing rubber bands with
secondary lines enabled
1 parent df71901 commit 4374c6e

File tree

3 files changed

+34
-22
lines changed

3 files changed

+34
-22
lines changed
 

‎python/gui/qgsrubberband.sip

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,7 @@ for tracking the mouse while drawing polylines or polygons.
275275
\param p The QPainter object
276276
%End
277277

278-
void drawShape( QPainter *p, QVector<QPointF> &pts );
278+
void drawShape( QPainter *p, const QVector<QPointF> &pts );
279279
%Docstring
280280
Draws shape of the rubber band.
281281
\param p The QPainter object

‎src/gui/qgsrubberband.cpp

Lines changed: 32 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -405,35 +405,47 @@ void QgsRubberBand::setToCanvasRectangle( QRect rect )
405405

406406
void QgsRubberBand::paint( QPainter *p )
407407
{
408-
if ( !mPoints.isEmpty() )
408+
if ( mPoints.isEmpty() )
409+
return;
410+
411+
QVector< QVector<QPointF> > shapes;
412+
for ( const QList<QgsPointXY> &line : qgsAsConst( mPoints ) )
409413
{
410-
Q_FOREACH ( const QList<QgsPointXY> &line, mPoints )
414+
QVector<QPointF> pts;
415+
for ( const QgsPointXY &pt : line )
411416
{
412-
QVector<QPointF> pts;
413-
Q_FOREACH ( const QgsPointXY &pt, line )
414-
{
415-
const QPointF cur = toCanvasCoordinates( QgsPointXY( pt.x() + mTranslationOffsetX, pt.y() + mTranslationOffsetY ) ) - pos();
416-
if ( pts.empty() || std::abs( pts.back().x() - cur.x() ) > 1 || std::abs( pts.back().y() - cur.y() ) > 1 )
417-
pts.append( cur );
418-
}
419-
420-
if ( mSecondaryPen.color().isValid() )
421-
{
422-
mSecondaryPen.setWidth( mPen.width() + 2 );
423-
424-
p->setBrush( Qt::NoBrush );
425-
p->setPen( mSecondaryPen );
426-
drawShape( p, pts );
427-
}
417+
const QPointF cur = toCanvasCoordinates( QgsPointXY( pt.x() + mTranslationOffsetX, pt.y() + mTranslationOffsetY ) ) - pos();
418+
if ( pts.empty() || std::abs( pts.back().x() - cur.x() ) > 1 || std::abs( pts.back().y() - cur.y() ) > 1 )
419+
pts.append( cur );
420+
}
421+
shapes << pts;
422+
}
428423

424+
int iterations = mSecondaryPen.color().isValid() ? 2 : 1;
425+
for ( int i = 0; i < iterations; ++i )
426+
{
427+
if ( i == 0 && iterations > 1 )
428+
{
429+
// first iteration with multi-pen painting, so use secondary pen
430+
mSecondaryPen.setWidth( mPen.width() + 2 );
431+
p->setBrush( Qt::NoBrush );
432+
p->setPen( mSecondaryPen );
433+
}
434+
else
435+
{
436+
// "top" layer, use primary pen/brush
429437
p->setBrush( mBrush );
430438
p->setPen( mPen );
431-
drawShape( p, pts );
439+
}
440+
441+
for ( const QVector<QPointF> &shape : qgsAsConst( shapes ) )
442+
{
443+
drawShape( p, shape );
432444
}
433445
}
434446
}
435447

436-
void QgsRubberBand::drawShape( QPainter *p, QVector<QPointF> &pts )
448+
void QgsRubberBand::drawShape( QPainter *p, const QVector<QPointF> &pts )
437449
{
438450
switch ( mGeometryType )
439451
{

‎src/gui/qgsrubberband.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,7 @@ class GUI_EXPORT QgsRubberBand: public QgsMapCanvasItem
310310
* \param p The QPainter object
311311
* \param pts A list of points used to draw the shape
312312
*/
313-
void drawShape( QPainter *p, QVector<QPointF> &pts );
313+
void drawShape( QPainter *p, const QVector<QPointF> &pts );
314314

315315
//! Recalculates needed rectangle
316316
void updateRect();

0 commit comments

Comments
 (0)
Please sign in to comment.