Skip to content

Commit

Permalink
Merge pull request #1734 from strk/zoom-by-wheel-glitch2
Browse files Browse the repository at this point in the history
Fix zoomwheel regression (temporary hack)
  • Loading branch information
wonder-sk committed Dec 21, 2014
2 parents f3bcaf4 + 0e4b7e6 commit 1d9eb8f
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 38 deletions.
8 changes: 7 additions & 1 deletion src/gui/qgsmapcanvas.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -723,7 +723,13 @@ void QgsMapCanvas::rendererJobFinished()

p.end();

QgsRectangle rect = mSettings.visibleExtent();
// This is an hack to pass QgsMapCanvasItem::setRect what it
// expects (encoding of position and size of the item)
const QgsMapToPixel& m2p = mSettings.mapToPixel();
QgsPoint topLeft = m2p.toMapPoint(0,0);
double res = m2p.mapUnitsPerPixel();
QgsRectangle rect(topLeft.x(), topLeft.y(), topLeft.x() + img.width()*res, topLeft.y() - img.height()*res);

mMap->setContent( img, rect );
}

Expand Down
25 changes: 7 additions & 18 deletions src/gui/qgsmapcanvasitem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,21 +67,6 @@ QPointF QgsMapCanvasItem::toCanvasCoordinates( const QgsPoint& point )
return QPointF( x, y ) + mPanningOffset;
}

// private
QRectF QgsMapCanvasItem::toCanvasCoordinates( const QRectF& rect )
{
QPointF tl( toCanvasCoordinates( rect.topLeft() ) );
QPointF bl( toCanvasCoordinates( rect.bottomLeft() ) );
QPointF br( toCanvasCoordinates( rect.bottomRight() ) );
QPointF tr( toCanvasCoordinates( rect.topRight() ) );
double xmin = std::min( tl.x(), std::min( bl.x(), std::min( br.x(), tr.x() ) ) );
double ymin = std::min( tl.y(), std::min( bl.y(), std::min( br.y(), tr.y() ) ) );
double xmax = std::max( tl.x(), std::max( bl.x(), std::max( br.x(), tr.x() ) ) );
double ymax = std::max( tl.y(), std::max( bl.y(), std::max( br.y(), tr.y() ) ) );
return QRectF( QPointF( xmin, ymin ), QPointF( xmax, ymax ) );
}


QgsRectangle QgsMapCanvasItem::rect() const
{
return mRect;
Expand All @@ -96,13 +81,17 @@ void QgsMapCanvasItem::setRect( const QgsRectangle& rect )
QRectF r; // empty rect by default
if ( !mRect.isEmpty() )
{
r = toCanvasCoordinates( mRect.toRectF() );
r = r.normalized();
// rect encodes origin of the item (xMin,yMax from map to canvas units)
// and size (rect size / map units per pixel)
r.setTopLeft( toCanvasCoordinates( QPointF(mRect.xMinimum(), mRect.yMaximum()) ) );
const QgsMapToPixel* m2p = mMapCanvas->getCoordinateTransform();
double res = m2p->mapUnitsPerPixel();
r.setSize( QSizeF(mRect.width()/res, mRect.height()/res) );
}

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

// QgsDebugMsg(QString("[%1,%2]-[%3x%4]").arg((int) r.left()).arg((int) r.top()).arg((int) r.width()).arg((int) r.height()));
Expand Down
17 changes: 9 additions & 8 deletions src/gui/qgsmapcanvasitem.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,23 +82,24 @@ class GUI_EXPORT QgsMapCanvasItem : public QGraphicsItem
//! pointer to map canvas
QgsMapCanvas* mMapCanvas;

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

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

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

private:

//! transformation from map coordinates to screen coordinates
//! if rotation is set it is taken in consideration so that
//! the returned rectangle is the bounding box of the rotated input
QRectF toCanvasCoordinates( const QRectF& rect );

};


Expand Down
15 changes: 4 additions & 11 deletions src/gui/qgsmapcanvasmap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,19 +50,12 @@ void QgsMapCanvasMap::paint( QPainter* painter )
if ( mImage.size() != QSize( w, h ) )
{
QgsDebugMsg( QString( "map paint DIFFERENT SIZE: img %1,%2 item %3,%4" ).arg( mImage.width() ).arg( mImage.height() ).arg( w ).arg( h ) );
int tX = ( w - mImage.width() ) / 2.0;
int tY = ( h - mImage.height() ) / 2.0;
int fX = 0;
int fY = 0;
int fW = w;
int fH = h;
painter->drawImage( tX, tY, mImage, fX, fY, fW, fH );
}
else
{
painter->drawImage( QRect( 0, 0, w, h ), mImage );
// This happens on zoom events when ::paint is called before
// the renderer has completed
}

painter->drawImage( QRect( 0, 0, w, h ), mImage );

// For debugging:
#if 0
QRectF br = boundingRect();
Expand Down

0 comments on commit 1d9eb8f

Please sign in to comment.