Skip to content
Permalink
Browse files

[FEATURE] ctrl+wheel on canvas results in smaller zoom

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 5377952a1a38b18741378c0d98bcf645036cbb22
Showing with 46 additions and 6 deletions.
  1. +7 −4 src/gui/qgsmapcanvas.cpp
  2. +2 −0 src/gui/qgsmapcanvas.h
  3. +1 −1 src/gui/qgsmaptool.cpp
  4. +36 −1 tests/src/gui/testqgsmapcanvas.cpp
@@ -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();
@@ -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

@@ -164,7 +164,7 @@ void QgsMapTool::canvasReleaseEvent( QgsMapMouseEvent* e )

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

void QgsMapTool::keyPressEvent( QKeyEvent *e )
@@ -38,7 +38,7 @@ class TestQgsMapCanvas : public QObject
Q_OBJECT
public:
TestQgsMapCanvas()
: mCanvas( 0 )
: mCanvas( nullptr )
{}

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

private:
QgsMapCanvas* mCanvas;
@@ -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.
You can’t perform that action at this time.