Skip to content

Commit 5377952

Browse files
committed
[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.
1 parent aa53cfe commit 5377952

File tree

4 files changed

+46
-6
lines changed

4 files changed

+46
-6
lines changed

src/gui/qgsmapcanvas.cpp

+7-4
Original file line numberDiff line numberDiff line change
@@ -1488,15 +1488,18 @@ void QgsMapCanvas::wheelEvent( QWheelEvent *e )
14881488
if ( mMapTool )
14891489
{
14901490
mMapTool->wheelEvent( e );
1491+
if ( e->isAccepted() )
1492+
return;
14911493
}
14921494

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

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

15011504
// zoom map to mouse cursor by scaling
15021505
QgsPoint oldCenter = center();

src/gui/qgsmapcanvas.h

+2
Original file line numberDiff line numberDiff line change
@@ -789,6 +789,8 @@ class GUI_EXPORT QgsMapCanvas : public QGraphicsView
789789
//! @note added in 2.16
790790
void updateMapSize();
791791

792+
friend class TestQgsMapCanvas;
793+
792794
}; // class QgsMapCanvas
793795
Q_NOWARN_DEPRECATED_POP
794796

src/gui/qgsmaptool.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ void QgsMapTool::canvasReleaseEvent( QgsMapMouseEvent* e )
164164

165165
void QgsMapTool::wheelEvent( QWheelEvent *e )
166166
{
167-
Q_UNUSED( e );
167+
e->ignore();
168168
}
169169

170170
void QgsMapTool::keyPressEvent( QKeyEvent *e )

tests/src/gui/testqgsmapcanvas.cpp

+36-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ class TestQgsMapCanvas : public QObject
3838
Q_OBJECT
3939
public:
4040
TestQgsMapCanvas()
41-
: mCanvas( 0 )
41+
: mCanvas( nullptr )
4242
{}
4343

4444
private slots:
@@ -50,6 +50,7 @@ class TestQgsMapCanvas : public QObject
5050
void testMagnification();
5151
void testMagnificationExtent();
5252
void testMagnificationScale();
53+
void testZoomByWheel();
5354

5455
private:
5556
QgsMapCanvas* mCanvas;
@@ -360,5 +361,39 @@ void TestQgsMapCanvas::testMagnificationScale()
360361
QCOMPARE( initialScale, mCanvas->scale() );
361362
}
362363

364+
void TestQgsMapCanvas::testZoomByWheel()
365+
{
366+
mCanvas->setExtent( QgsRectangle( 0, 0, 10, 10 ) );
367+
QgsRectangle initialExtent = mCanvas->extent();
368+
double originalWidth = initialExtent.width();
369+
double originalHeight = initialExtent.height();
370+
371+
mCanvas->setWheelFactor( 2 );
372+
373+
//test zoom out
374+
QWheelEvent e( QPoint( 0, 0 ), -1, Qt::NoButton, Qt::NoModifier );
375+
mCanvas->wheelEvent( &e );
376+
QVERIFY( qgsDoubleNear( mCanvas->extent().width(), originalWidth * 2.0, 0.1 ) );
377+
QVERIFY( qgsDoubleNear( mCanvas->extent().height(), originalHeight * 2.0, 0.1 ) );
378+
379+
//test zoom in
380+
e = QWheelEvent( QPoint( 0, 0 ), 1, Qt::NoButton, Qt::NoModifier );
381+
mCanvas->wheelEvent( &e );
382+
QVERIFY( qgsDoubleNear( mCanvas->extent().width(), originalWidth, 0.1 ) );
383+
QVERIFY( qgsDoubleNear( mCanvas->extent().height(), originalHeight, 0.1 ) );
384+
385+
// test zoom out with ctrl
386+
e = QWheelEvent( QPoint( 0, 0 ), -1, Qt::NoButton, Qt::ControlModifier );
387+
mCanvas->wheelEvent( &e );
388+
QVERIFY( qgsDoubleNear( mCanvas->extent().width(), 1.05 * originalWidth, 0.1 ) );
389+
QVERIFY( qgsDoubleNear( mCanvas->extent().height(), 1.05 * originalHeight, 0.1 ) );
390+
391+
//test zoom in with ctrl
392+
e = QWheelEvent( QPoint( 0, 0 ), 1, Qt::NoButton, Qt::ControlModifier );
393+
mCanvas->wheelEvent( &e );
394+
QVERIFY( qgsDoubleNear( mCanvas->extent().width(), originalWidth, 0.1 ) );
395+
QVERIFY( qgsDoubleNear( mCanvas->extent().height(), originalHeight, 0.1 ) );
396+
}
397+
363398
QTEST_MAIN( TestQgsMapCanvas )
364399
#include "testqgsmapcanvas.moc"

0 commit comments

Comments
 (0)