@@ -67,21 +67,6 @@ QPointF QgsMapCanvasItem::toCanvasCoordinates( const QgsPoint& point )
67
67
return QPointF ( x, y ) + mPanningOffset ;
68
68
}
69
69
70
- // private
71
- QRectF QgsMapCanvasItem::toCanvasCoordinates ( const QRectF& rect )
72
- {
73
- QPointF tl ( toCanvasCoordinates ( rect.topLeft () ) );
74
- QPointF bl ( toCanvasCoordinates ( rect.bottomLeft () ) );
75
- QPointF br ( toCanvasCoordinates ( rect.bottomRight () ) );
76
- QPointF tr ( toCanvasCoordinates ( rect.topRight () ) );
77
- double xmin = std::min ( tl.x (), std::min ( bl.x (), std::min ( br.x (), tr.x () ) ) );
78
- double ymin = std::min ( tl.y (), std::min ( bl.y (), std::min ( br.y (), tr.y () ) ) );
79
- double xmax = std::max ( tl.x (), std::max ( bl.x (), std::max ( br.x (), tr.x () ) ) );
80
- double ymax = std::max ( tl.y (), std::max ( bl.y (), std::max ( br.y (), tr.y () ) ) );
81
- return QRectF ( QPointF ( xmin, ymin ), QPointF ( xmax, ymax ) );
82
- }
83
-
84
-
85
70
QgsRectangle QgsMapCanvasItem::rect () const
86
71
{
87
72
return mRect ;
@@ -96,13 +81,17 @@ void QgsMapCanvasItem::setRect( const QgsRectangle& rect )
96
81
QRectF r; // empty rect by default
97
82
if ( !mRect .isEmpty () )
98
83
{
99
- r = toCanvasCoordinates ( mRect .toRectF () );
100
- r = r.normalized ();
84
+ // rect encodes origin of the item (xMin,yMax from map to canvas units)
85
+ // and size (rect size / map units per pixel)
86
+ r.setTopLeft ( toCanvasCoordinates ( QPointF (mRect .xMinimum (), mRect .yMaximum ()) ) );
87
+ const QgsMapToPixel* m2p = mMapCanvas ->getCoordinateTransform ();
88
+ double res = m2p->mapUnitsPerPixel ();
89
+ r.setSize ( QSizeF (mRect .width ()/res, mRect .height ()/res) );
101
90
}
102
91
103
92
// set position in canvas where the item will have coordinate (0,0)
104
93
prepareGeometryChange ();
105
- setPos ( r.topLeft () ); // TODO: compute from (0,0) using toMapCoordinates ?
94
+ setPos ( r.topLeft () );
106
95
mItemSize = QSizeF ( r.width () + 2 , r.height () + 2 );
107
96
108
97
// QgsDebugMsg(QString("[%1,%2]-[%3x%4]").arg((int) r.left()).arg((int) r.top()).arg((int) r.width()).arg((int) r.height()));
0 commit comments