Skip to content

Commit d726959

Browse files
author
Sandro Santilli
committedJan 3, 2015
Consider rotation in rubberband placement
Fixes artifacts on zoom by rectangle (#11910) and placement of measure tool rubberband.
1 parent 888a9f0 commit d726959

File tree

2 files changed

+38
-28
lines changed

2 files changed

+38
-28
lines changed
 

‎src/gui/qgsrubberband.cpp

Lines changed: 36 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -425,13 +425,15 @@ void QgsRubberBand::setToCanvasRectangle( const QRect& rect )
425425

426426
const QgsMapToPixel* transform = mMapCanvas->getCoordinateTransform();
427427
QgsPoint ll = transform->toMapCoordinates( rect.left(), rect.bottom() );
428+
QgsPoint lr = transform->toMapCoordinates( rect.right(), rect.bottom() );
429+
QgsPoint ul = transform->toMapCoordinates( rect.left(), rect.top() );
428430
QgsPoint ur = transform->toMapCoordinates( rect.right(), rect.top() );
429431

430432
reset( QGis::Polygon );
431433
addPoint( ll, false );
432-
addPoint( QgsPoint( ur.x(), ll.y() ), false );
434+
addPoint( lr, false );
433435
addPoint( ur, false );
434-
addPoint( QgsPoint( ll.x(), ur.y() ), true );
436+
addPoint( ul, true );
435437
}
436438

437439
/*!
@@ -518,39 +520,45 @@ void QgsRubberBand::paint( QPainter* p )
518520

519521
void QgsRubberBand::updateRect()
520522
{
521-
if ( mPoints.size() > 0 )
523+
if ( mPoints.empty() )
522524
{
523-
//initial point
524-
QList<QgsPoint>::const_iterator it = mPoints.at( 0 ).constBegin();
525-
if ( it == mPoints.at( 0 ).constEnd() )
526-
{
527-
return;
528-
}
525+
setRect( QgsRectangle() );
526+
setVisible( false );
527+
return;
528+
}
529529

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

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

537-
for ( int i = 0; i < mPoints.size(); ++i )
534+
QgsRectangle r;
535+
for ( int i = 0; i < mPoints.size(); ++i )
536+
{
537+
QList<QgsPoint>::const_iterator it = mPoints.at( i ).constBegin(),
538+
itE = mPoints.at( i ).constEnd();
539+
int j = 0;
540+
for ( ; it != itE; ++it )
538541
{
539-
QList<QgsPoint>::const_iterator it = mPoints.at( i ).constBegin();
540-
for ( ; it != mPoints.at( i ).constEnd(); ++it )
541-
{
542-
QgsRectangle rect = QgsRectangle( it->x() + mTranslationOffsetX - s - p, it->y() + mTranslationOffsetY - s - p,
543-
it->x() + mTranslationOffsetX + s + p, it->y() + mTranslationOffsetY + s + p );
544-
r.combineExtentWith( &rect );
545-
}
542+
QgsPoint p( it->x() + mTranslationOffsetX, it->y() + mTranslationOffsetY );
543+
p = m2p.transform( p );
544+
QgsRectangle rect( p.x() - w, p.y() - w, p.x() + w, p.y() + w );
545+
r.combineExtentWith( &rect );
546546
}
547-
setRect( r );
548547
}
549-
else
550-
{
551-
setRect( QgsRectangle() );
552-
}
553-
setVisible( mPoints.size() > 0 );
548+
549+
// This is an hack to pass QgsMapCanvasItem::setRect what it
550+
// expects (encoding of position and size of the item)
551+
QgsPoint topLeft = m2p.toMapPoint( r.xMinimum(), r.yMinimum() );
552+
double res = m2p.mapUnitsPerPixel();
553+
QgsRectangle rect( topLeft.x(), topLeft.y(), topLeft.x() + r.width()*res, topLeft.y() - r.height()*res );
554+
555+
setRect( rect );
556+
setVisible( true );
557+
}
558+
559+
void QgsRubberBand::updatePosition( )
560+
{
561+
// nothing to do here...
554562
}
555563

556564
void QgsRubberBand::setTranslationOffset( double dx, double dy )

‎src/gui/qgsrubberband.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,8 @@ class GUI_EXPORT QgsRubberBand: public QgsMapCanvasItem
242242
*/
243243
QgsGeometry* asGeometry();
244244

245+
virtual void updatePosition();
246+
245247
protected:
246248
virtual void paint( QPainter* p );
247249

0 commit comments

Comments
 (0)
Please sign in to comment.