Skip to content

Commit

Permalink
[FEATURE] ctrl+wheel on canvas results in smaller zoom
Browse files Browse the repository at this point in the history
Holding down ctrl while using the mouse wheel to zoom in or
out now results in a finer zoom. This behaviour brings canvas
into line with composer.
  • Loading branch information
nyalldawson committed May 29, 2016
1 parent aa53cfe commit 5377952
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 6 deletions.
11 changes: 7 additions & 4 deletions src/gui/qgsmapcanvas.cpp
Expand Up @@ -1488,15 +1488,18 @@ void QgsMapCanvas::wheelEvent( QWheelEvent *e )
if ( mMapTool )
{
mMapTool->wheelEvent( e );
if ( e->isAccepted() )
return;
}

if ( QgsApplication::keyboardModifiers() )
double zoomFactor = mWheelZoomFactor;
if ( e->modifiers() & Qt::ControlModifier )
{
// leave the wheel for map tools if any modifier pressed
return;
//holding ctrl while wheel zooming results in a finer zoom
zoomFactor = 1.0 + ( zoomFactor - 1.0 ) / 20.0;
}

double signedWheelFactor = e->delta() > 0 ? 1 / mWheelZoomFactor : mWheelZoomFactor;
double signedWheelFactor = e->delta() > 0 ? 1 / zoomFactor : zoomFactor;

// zoom map to mouse cursor by scaling
QgsPoint oldCenter = center();
Expand Down
2 changes: 2 additions & 0 deletions src/gui/qgsmapcanvas.h
Expand Up @@ -789,6 +789,8 @@ class GUI_EXPORT QgsMapCanvas : public QGraphicsView
//! @note added in 2.16
void updateMapSize();

friend class TestQgsMapCanvas;

}; // class QgsMapCanvas
Q_NOWARN_DEPRECATED_POP

Expand Down
2 changes: 1 addition & 1 deletion src/gui/qgsmaptool.cpp
Expand Up @@ -164,7 +164,7 @@ void QgsMapTool::canvasReleaseEvent( QgsMapMouseEvent* e )

void QgsMapTool::wheelEvent( QWheelEvent *e )
{
Q_UNUSED( e );
e->ignore();
}

void QgsMapTool::keyPressEvent( QKeyEvent *e )
Expand Down
37 changes: 36 additions & 1 deletion tests/src/gui/testqgsmapcanvas.cpp
Expand Up @@ -38,7 +38,7 @@ class TestQgsMapCanvas : public QObject
Q_OBJECT
public:
TestQgsMapCanvas()
: mCanvas( 0 )
: mCanvas( nullptr )
{}

private slots:
Expand All @@ -50,6 +50,7 @@ class TestQgsMapCanvas : public QObject
void testMagnification();
void testMagnificationExtent();
void testMagnificationScale();
void testZoomByWheel();

private:
QgsMapCanvas* mCanvas;
Expand Down Expand Up @@ -360,5 +361,39 @@ void TestQgsMapCanvas::testMagnificationScale()
QCOMPARE( initialScale, mCanvas->scale() );
}

void TestQgsMapCanvas::testZoomByWheel()
{
mCanvas->setExtent( QgsRectangle( 0, 0, 10, 10 ) );
QgsRectangle initialExtent = mCanvas->extent();
double originalWidth = initialExtent.width();
double originalHeight = initialExtent.height();

mCanvas->setWheelFactor( 2 );

//test zoom out
QWheelEvent e( QPoint( 0, 0 ), -1, Qt::NoButton, Qt::NoModifier );
mCanvas->wheelEvent( &e );
QVERIFY( qgsDoubleNear( mCanvas->extent().width(), originalWidth * 2.0, 0.1 ) );
QVERIFY( qgsDoubleNear( mCanvas->extent().height(), originalHeight * 2.0, 0.1 ) );

//test zoom in
e = QWheelEvent( QPoint( 0, 0 ), 1, Qt::NoButton, Qt::NoModifier );
mCanvas->wheelEvent( &e );
QVERIFY( qgsDoubleNear( mCanvas->extent().width(), originalWidth, 0.1 ) );
QVERIFY( qgsDoubleNear( mCanvas->extent().height(), originalHeight, 0.1 ) );

// test zoom out with ctrl
e = QWheelEvent( QPoint( 0, 0 ), -1, Qt::NoButton, Qt::ControlModifier );
mCanvas->wheelEvent( &e );
QVERIFY( qgsDoubleNear( mCanvas->extent().width(), 1.05 * originalWidth, 0.1 ) );
QVERIFY( qgsDoubleNear( mCanvas->extent().height(), 1.05 * originalHeight, 0.1 ) );

//test zoom in with ctrl
e = QWheelEvent( QPoint( 0, 0 ), 1, Qt::NoButton, Qt::ControlModifier );
mCanvas->wheelEvent( &e );
QVERIFY( qgsDoubleNear( mCanvas->extent().width(), originalWidth, 0.1 ) );
QVERIFY( qgsDoubleNear( mCanvas->extent().height(), originalHeight, 0.1 ) );
}

QTEST_MAIN( TestQgsMapCanvas )
#include "testqgsmapcanvas.moc"

0 comments on commit 5377952

Please sign in to comment.