Skip to content

Commit

Permalink
[FEATURE][map overview] Support zooming in/out by scrolling mouse whe…
Browse files Browse the repository at this point in the history
…el over map overview panel
  • Loading branch information
nirvn committed Dec 10, 2020
1 parent d78665d commit e1933c8
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 2 deletions.
10 changes: 10 additions & 0 deletions python/gui/auto_generated/qgsmapcanvas.sip.in
Expand Up @@ -961,6 +961,16 @@ Set a list of resolutions (map units per pixel) to which to "snap to" when zoomi
:param resolutions: A list of resolutions

.. versionadded:: 3.12
%End

double zoomInFactor() const;
%Docstring
Returns the zoom in factor.
%End

double zoomOutFactor() const;
%Docstring
Returns the zoom in factor.
%End

const QList<double> &zoomResolutions() const;
Expand Down
6 changes: 6 additions & 0 deletions python/gui/auto_generated/qgsmapoverviewcanvas.sip.in
Expand Up @@ -104,6 +104,12 @@ Overridden mouse press event

virtual void mouseReleaseEvent( QMouseEvent *e );

%Docstring
Overridden mouse release event
%End

virtual void wheelEvent( QWheelEvent *e );

%Docstring
Overridden mouse release event
%End
Expand Down
12 changes: 10 additions & 2 deletions src/gui/qgsmapcanvas.h
Expand Up @@ -872,6 +872,16 @@ class GUI_EXPORT QgsMapCanvas : public QGraphicsView
*/
void setZoomResolutions( const QList<double> &resolutions ) { mZoomResolutions = resolutions; }

/**
* Returns the zoom in factor.
*/
double zoomInFactor() const;

/**
* Returns the zoom in factor.
*/
double zoomOutFactor() const;

/**
* \returns List of resolutions to which to "snap to" when zooming the map
* \see setZoomResolutions()
Expand Down Expand Up @@ -1329,8 +1339,6 @@ class GUI_EXPORT QgsMapCanvas : public QGraphicsView
bool panOperationInProgress();

int nextZoomLevel( const QList<double> &resolutions, bool zoomIn = true ) const;
double zoomInFactor() const;
double zoomOutFactor() const;

/**
* Make sure to remove any rendered images of temporal-enabled layers from cache (does nothing if cache is not enabled)
Expand Down
22 changes: 22 additions & 0 deletions src/gui/qgsmapoverviewcanvas.cpp
Expand Up @@ -145,6 +145,28 @@ void QgsMapOverviewCanvas::mouseReleaseEvent( QMouseEvent *e )
}


void QgsMapOverviewCanvas::wheelEvent( QWheelEvent *e )
{
double zoomFactor = e->angleDelta().y() > 0 ? 1. / mMapCanvas->zoomInFactor() : mMapCanvas->zoomOutFactor();

// "Normal" mouse have an angle delta of 120, precision mouses provide data faster, in smaller steps
zoomFactor = 1.0 + ( zoomFactor - 1.0 ) / 120.0 * std::fabs( e->angleDelta().y() );

if ( e->modifiers() & Qt::ControlModifier )
{
//holding ctrl while wheel zooming results in a finer zoom
zoomFactor = 1.0 + ( zoomFactor - 1.0 ) / 20.0;
}

double signedWheelFactor = e->angleDelta().y() > 0 ? 1 / zoomFactor : zoomFactor;

const QgsMapToPixel &cXf = mSettings.mapToPixel();
QgsPointXY center = cXf.toMapCoordinates( e->pos() );

updatePanningWidget( e->pos() );
mMapCanvas->zoomByFactor( signedWheelFactor, &center );
}

void QgsMapOverviewCanvas::mouseMoveEvent( QMouseEvent *e )
{
// move with panning widget if tracking cursor
Expand Down
3 changes: 3 additions & 0 deletions src/gui/qgsmapoverviewcanvas.h
Expand Up @@ -95,6 +95,9 @@ class GUI_EXPORT QgsMapOverviewCanvas : public QWidget
//! Overridden mouse release event
void mouseReleaseEvent( QMouseEvent *e ) override;

//! Overridden mouse release event
void wheelEvent( QWheelEvent *e ) override;

//! called when panning to reflect mouse movement
void updatePanningWidget( QPoint pos );

Expand Down

0 comments on commit e1933c8

Please sign in to comment.