@@ -425,13 +425,15 @@ void QgsRubberBand::setToCanvasRectangle( const QRect& rect )
425
425
426
426
const QgsMapToPixel* transform = mMapCanvas ->getCoordinateTransform ();
427
427
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 () );
428
430
QgsPoint ur = transform->toMapCoordinates ( rect.right (), rect.top () );
429
431
430
432
reset ( QGis::Polygon );
431
433
addPoint ( ll, false );
432
- addPoint ( QgsPoint ( ur. x (), ll. y () ) , false );
434
+ addPoint ( lr , false );
433
435
addPoint ( ur, false );
434
- addPoint ( QgsPoint ( ll. x (), ur. y () ) , true );
436
+ addPoint ( ul , true );
435
437
}
436
438
437
439
/* !
@@ -518,39 +520,45 @@ void QgsRubberBand::paint( QPainter* p )
518
520
519
521
void QgsRubberBand::updateRect ()
520
522
{
521
- if ( mPoints .size () > 0 )
523
+ if ( mPoints .empty () )
522
524
{
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
+ }
529
529
530
- qreal scale = mMapCanvas ->mapUnitsPerPixel ();
531
- qreal s = ( mIconSize - 1 ) / 2 * scale;
532
- qreal p = mPen .width () * scale;
530
+ const QgsMapToPixel& m2p = *( mMapCanvas ->getCoordinateTransform () );
533
531
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 ();
536
533
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 )
538
541
{
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 );
546
546
}
547
- setRect ( r );
548
547
}
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...
554
562
}
555
563
556
564
void QgsRubberBand::setTranslationOffset ( double dx, double dy )
0 commit comments