Skip to content

Commit

Permalink
Consider rotation in rubberband placement
Browse files Browse the repository at this point in the history
Fixes artifacts on zoom by rectangle (#11910) and placement of
measure tool rubberband.
  • Loading branch information
Sandro Santilli committed Jan 3, 2015
1 parent 888a9f0 commit d726959
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 28 deletions.
64 changes: 36 additions & 28 deletions src/gui/qgsrubberband.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -425,13 +425,15 @@ void QgsRubberBand::setToCanvasRectangle( const QRect& rect )

const QgsMapToPixel* transform = mMapCanvas->getCoordinateTransform();
QgsPoint ll = transform->toMapCoordinates( rect.left(), rect.bottom() );
QgsPoint lr = transform->toMapCoordinates( rect.right(), rect.bottom() );
QgsPoint ul = transform->toMapCoordinates( rect.left(), rect.top() );
QgsPoint ur = transform->toMapCoordinates( rect.right(), rect.top() );

reset( QGis::Polygon );
addPoint( ll, false );
addPoint( QgsPoint( ur.x(), ll.y() ), false );
addPoint( lr, false );
addPoint( ur, false );
addPoint( QgsPoint( ll.x(), ur.y() ), true );
addPoint( ul, true );
}

/*!
Expand Down Expand Up @@ -518,39 +520,45 @@ void QgsRubberBand::paint( QPainter* p )

void QgsRubberBand::updateRect()
{
if ( mPoints.size() > 0 )
if ( mPoints.empty() )
{
//initial point
QList<QgsPoint>::const_iterator it = mPoints.at( 0 ).constBegin();
if ( it == mPoints.at( 0 ).constEnd() )
{
return;
}
setRect( QgsRectangle() );
setVisible( false );
return;
}

qreal scale = mMapCanvas->mapUnitsPerPixel();
qreal s = ( mIconSize - 1 ) / 2 * scale;
qreal p = mPen.width() * scale;
const QgsMapToPixel& m2p = *( mMapCanvas->getCoordinateTransform() );

QgsRectangle r( it->x() + mTranslationOffsetX - s - p, it->y() + mTranslationOffsetY - s - p,
it->x() + mTranslationOffsetX + s + p, it->y() + mTranslationOffsetY + s + p );
qreal w = ( mIconSize - 1 ) / 2 + mPen.width();

for ( int i = 0; i < mPoints.size(); ++i )
QgsRectangle r;
for ( int i = 0; i < mPoints.size(); ++i )
{
QList<QgsPoint>::const_iterator it = mPoints.at( i ).constBegin(),
itE = mPoints.at( i ).constEnd();
int j = 0;
for ( ; it != itE; ++it )
{
QList<QgsPoint>::const_iterator it = mPoints.at( i ).constBegin();
for ( ; it != mPoints.at( i ).constEnd(); ++it )
{
QgsRectangle rect = QgsRectangle( it->x() + mTranslationOffsetX - s - p, it->y() + mTranslationOffsetY - s - p,
it->x() + mTranslationOffsetX + s + p, it->y() + mTranslationOffsetY + s + p );
r.combineExtentWith( &rect );
}
QgsPoint p( it->x() + mTranslationOffsetX, it->y() + mTranslationOffsetY );
p = m2p.transform( p );
QgsRectangle rect( p.x() - w, p.y() - w, p.x() + w, p.y() + w );
r.combineExtentWith( &rect );
}
setRect( r );
}
else
{
setRect( QgsRectangle() );
}
setVisible( mPoints.size() > 0 );

// This is an hack to pass QgsMapCanvasItem::setRect what it
// expects (encoding of position and size of the item)
QgsPoint topLeft = m2p.toMapPoint( r.xMinimum(), r.yMinimum() );
double res = m2p.mapUnitsPerPixel();
QgsRectangle rect( topLeft.x(), topLeft.y(), topLeft.x() + r.width()*res, topLeft.y() - r.height()*res );

setRect( rect );
setVisible( true );
}

void QgsRubberBand::updatePosition( )
{
// nothing to do here...
}

void QgsRubberBand::setTranslationOffset( double dx, double dy )
Expand Down
2 changes: 2 additions & 0 deletions src/gui/qgsrubberband.h
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,8 @@ class GUI_EXPORT QgsRubberBand: public QgsMapCanvasItem
*/
QgsGeometry* asGeometry();

virtual void updatePosition();

protected:
virtual void paint( QPainter* p );

Expand Down

0 comments on commit d726959

Please sign in to comment.