Skip to content

Commit

Permalink
Fix incorrect draw stacking when drawing rubber bands with
Browse files Browse the repository at this point in the history
secondary lines enabled
  • Loading branch information
nyalldawson committed Oct 1, 2017
1 parent df71901 commit 4374c6e
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 22 deletions.
2 changes: 1 addition & 1 deletion python/gui/qgsrubberband.sip
Expand Up @@ -275,7 +275,7 @@ for tracking the mouse while drawing polylines or polygons.
\param p The QPainter object
%End

void drawShape( QPainter *p, QVector<QPointF> &pts );
void drawShape( QPainter *p, const QVector<QPointF> &pts );
%Docstring
Draws shape of the rubber band.
\param p The QPainter object
Expand Down
52 changes: 32 additions & 20 deletions src/gui/qgsrubberband.cpp
Expand Up @@ -405,35 +405,47 @@ void QgsRubberBand::setToCanvasRectangle( QRect rect )

void QgsRubberBand::paint( QPainter *p )
{
if ( !mPoints.isEmpty() )
if ( mPoints.isEmpty() )
return;

QVector< QVector<QPointF> > shapes;
for ( const QList<QgsPointXY> &line : qgsAsConst( mPoints ) )
{
Q_FOREACH ( const QList<QgsPointXY> &line, mPoints )
QVector<QPointF> pts;
for ( const QgsPointXY &pt : line )
{
QVector<QPointF> pts;
Q_FOREACH ( const QgsPointXY &pt, line )
{
const QPointF cur = toCanvasCoordinates( QgsPointXY( pt.x() + mTranslationOffsetX, pt.y() + mTranslationOffsetY ) ) - pos();
if ( pts.empty() || std::abs( pts.back().x() - cur.x() ) > 1 || std::abs( pts.back().y() - cur.y() ) > 1 )
pts.append( cur );
}

if ( mSecondaryPen.color().isValid() )
{
mSecondaryPen.setWidth( mPen.width() + 2 );

p->setBrush( Qt::NoBrush );
p->setPen( mSecondaryPen );
drawShape( p, pts );
}
const QPointF cur = toCanvasCoordinates( QgsPointXY( pt.x() + mTranslationOffsetX, pt.y() + mTranslationOffsetY ) ) - pos();
if ( pts.empty() || std::abs( pts.back().x() - cur.x() ) > 1 || std::abs( pts.back().y() - cur.y() ) > 1 )
pts.append( cur );
}
shapes << pts;
}

int iterations = mSecondaryPen.color().isValid() ? 2 : 1;
for ( int i = 0; i < iterations; ++i )
{
if ( i == 0 && iterations > 1 )
{
// first iteration with multi-pen painting, so use secondary pen
mSecondaryPen.setWidth( mPen.width() + 2 );
p->setBrush( Qt::NoBrush );
p->setPen( mSecondaryPen );
}
else
{
// "top" layer, use primary pen/brush
p->setBrush( mBrush );
p->setPen( mPen );
drawShape( p, pts );
}

for ( const QVector<QPointF> &shape : qgsAsConst( shapes ) )
{
drawShape( p, shape );
}
}
}

void QgsRubberBand::drawShape( QPainter *p, QVector<QPointF> &pts )
void QgsRubberBand::drawShape( QPainter *p, const QVector<QPointF> &pts )
{
switch ( mGeometryType )
{
Expand Down
2 changes: 1 addition & 1 deletion src/gui/qgsrubberband.h
Expand Up @@ -310,7 +310,7 @@ class GUI_EXPORT QgsRubberBand: public QgsMapCanvasItem
* \param p The QPainter object
* \param pts A list of points used to draw the shape
*/
void drawShape( QPainter *p, QVector<QPointF> &pts );
void drawShape( QPainter *p, const QVector<QPointF> &pts );

//! Recalculates needed rectangle
void updateRect();
Expand Down

0 comments on commit 4374c6e

Please sign in to comment.