Skip to content

Commit 1d9eb8f

Browse files
committedDec 21, 2014
Merge pull request #1734 from strk/zoom-by-wheel-glitch2
Fix zoomwheel regression (temporary hack)
2 parents f3bcaf4 + 0e4b7e6 commit 1d9eb8f

File tree

4 files changed

+27
-38
lines changed

4 files changed

+27
-38
lines changed
 

‎src/gui/qgsmapcanvas.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -723,7 +723,13 @@ void QgsMapCanvas::rendererJobFinished()
723723

724724
p.end();
725725

726-
QgsRectangle rect = mSettings.visibleExtent();
726+
// This is an hack to pass QgsMapCanvasItem::setRect what it
727+
// expects (encoding of position and size of the item)
728+
const QgsMapToPixel& m2p = mSettings.mapToPixel();
729+
QgsPoint topLeft = m2p.toMapPoint(0,0);
730+
double res = m2p.mapUnitsPerPixel();
731+
QgsRectangle rect(topLeft.x(), topLeft.y(), topLeft.x() + img.width()*res, topLeft.y() - img.height()*res);
732+
727733
mMap->setContent( img, rect );
728734
}
729735

‎src/gui/qgsmapcanvasitem.cpp

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -67,21 +67,6 @@ QPointF QgsMapCanvasItem::toCanvasCoordinates( const QgsPoint& point )
6767
return QPointF( x, y ) + mPanningOffset;
6868
}
6969

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-
8570
QgsRectangle QgsMapCanvasItem::rect() const
8671
{
8772
return mRect;
@@ -96,13 +81,17 @@ void QgsMapCanvasItem::setRect( const QgsRectangle& rect )
9681
QRectF r; // empty rect by default
9782
if ( !mRect.isEmpty() )
9883
{
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) );
10190
}
10291

10392
// set position in canvas where the item will have coordinate (0,0)
10493
prepareGeometryChange();
105-
setPos( r.topLeft() ); // TODO: compute from (0,0) using toMapCoordinates ?
94+
setPos( r.topLeft() );
10695
mItemSize = QSizeF( r.width() + 2, r.height() + 2 );
10796

10897
// QgsDebugMsg(QString("[%1,%2]-[%3x%4]").arg((int) r.left()).arg((int) r.top()).arg((int) r.width()).arg((int) r.height()));

‎src/gui/qgsmapcanvasitem.h

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -82,23 +82,24 @@ class GUI_EXPORT QgsMapCanvasItem : public QGraphicsItem
8282
//! pointer to map canvas
8383
QgsMapCanvas* mMapCanvas;
8484

85-
//! canvas item rectangle (in map coordinates)
85+
//! cached canvas item rectangle in map coordinates
86+
//! encodes position (xmin,ymax) and size (width/height)
87+
//! used to re-position and re-size the item on zoom/pan
88+
//! while waiting for the renderer to complete.
89+
//!
90+
//! NOTE: does not include rotation information, so cannot
91+
//! be used to correctly present pre-rendered map
92+
//! on rotation change
8693
QgsRectangle mRect;
8794

8895
//! offset from normal position due current panning operation,
8996
//! used when converting map coordinates to move map canvas items
97+
//! @deprecated since v2.4
9098
QPoint mPanningOffset;
9199

92100
//! cached size of the item (to return in boundingRect())
93101
QSizeF mItemSize;
94102

95-
private:
96-
97-
//! transformation from map coordinates to screen coordinates
98-
//! if rotation is set it is taken in consideration so that
99-
//! the returned rectangle is the bounding box of the rotated input
100-
QRectF toCanvasCoordinates( const QRectF& rect );
101-
102103
};
103104

104105

‎src/gui/qgsmapcanvasmap.cpp

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -50,19 +50,12 @@ void QgsMapCanvasMap::paint( QPainter* painter )
5050
if ( mImage.size() != QSize( w, h ) )
5151
{
5252
QgsDebugMsg( QString( "map paint DIFFERENT SIZE: img %1,%2 item %3,%4" ).arg( mImage.width() ).arg( mImage.height() ).arg( w ).arg( h ) );
53-
int tX = ( w - mImage.width() ) / 2.0;
54-
int tY = ( h - mImage.height() ) / 2.0;
55-
int fX = 0;
56-
int fY = 0;
57-
int fW = w;
58-
int fH = h;
59-
painter->drawImage( tX, tY, mImage, fX, fY, fW, fH );
60-
}
61-
else
62-
{
63-
painter->drawImage( QRect( 0, 0, w, h ), mImage );
53+
// This happens on zoom events when ::paint is called before
54+
// the renderer has completed
6455
}
6556

57+
painter->drawImage( QRect( 0, 0, w, h ), mImage );
58+
6659
// For debugging:
6760
#if 0
6861
QRectF br = boundingRect();

0 commit comments

Comments
 (0)
Please sign in to comment.