Skip to content

Commit

Permalink
Improve logic in QgsMapCanvas::nextZoomLevel to avoid unexpected zoom…
Browse files Browse the repository at this point in the history
… jumps
  • Loading branch information
manisandro committed Oct 26, 2022
1 parent 4d95af5 commit 26c7c01
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 5 deletions.
21 changes: 19 additions & 2 deletions src/gui/qgsmapcanvas.cpp
Expand Up @@ -3528,8 +3528,9 @@ int QgsMapCanvas::nextZoomLevel( const QList<double> &resolutions, bool zoomIn )
{
int resolutionLevel = -1;
double currentResolution = mapUnitsPerPixel();
int nResolutions = resolutions.size();

for ( int i = 0, n = resolutions.size(); i < n; ++i )
for ( int i = 0; i < nResolutions; ++i )
{
if ( qgsDoubleNear( resolutions[i], currentResolution, 0.0001 ) )
{
Expand All @@ -3541,8 +3542,24 @@ int QgsMapCanvas::nextZoomLevel( const QList<double> &resolutions, bool zoomIn )
resolutionLevel = zoomIn ? ( i - 1 ) : i;
break;
}
resolutionLevel = zoomIn ? i : i + 1;
}
return ( resolutionLevel < 0 || resolutionLevel >= resolutions.size() ) ? -1 : resolutionLevel;

if ( resolutionLevel < 0 || resolutionLevel >= nResolutions )
{
return -1;
}
if ( zoomIn && resolutionLevel == nResolutions - 1 && resolutions[nResolutions - 1] < currentResolution / mWheelZoomFactor )
{
// Avoid jumping straight to last resolution when zoomed far out and zooming in
return -1;
}
if ( !zoomIn && resolutionLevel == 0 && resolutions[0] > mWheelZoomFactor * currentResolution )
{
// Avoid jumping straight to first resolution when zoomed far in and zooming out
return -1;
}
return resolutionLevel;
}

double QgsMapCanvas::zoomInFactor() const
Expand Down
42 changes: 39 additions & 3 deletions tests/src/gui/testqgsmapcanvas.cpp
Expand Up @@ -538,19 +538,55 @@ void TestQgsMapCanvas::testZoomResolutions()
{
mCanvas->setExtent( QgsRectangle( 0, 0, 10, 10 ) );
const double resolution = mCanvas->mapSettings().mapUnitsPerPixel();
const double wheelFactor = 2.0;
mCanvas->setWheelFactor( wheelFactor );

const double nextResolution = qCeil( resolution ) + 1;
QList<double> resolutions = QList<double>() << nextResolution << ( 2.5 * nextResolution ) << ( 3.6 * nextResolution ) << ( 4.7 * nextResolution );
mCanvas->setZoomResolutions( resolutions );

// From first to last resolution in list
// Ensure we are at first resolution
while ( mCanvas->mapSettings().mapUnitsPerPixel() < resolutions[0] )
{
mCanvas->zoomOut();
}
int nResolutions = resolutions.size();
for ( int i = 1; i < nResolutions; ++i )
{
mCanvas->zoomOut();
QGSCOMPARENEAR( mCanvas->mapSettings().mapUnitsPerPixel(), resolutions[i], 0.0001 );
}

// beyond last resolution
mCanvas->zoomOut();
QGSCOMPARENEAR( mCanvas->mapSettings().mapUnitsPerPixel(), resolutions[0], 0.0001 );
QGSCOMPARENEAR( mCanvas->mapSettings().mapUnitsPerPixel(), wheelFactor * resolutions.last(), 0.0001 );

mCanvas->zoomOut();
QGSCOMPARENEAR( mCanvas->mapSettings().mapUnitsPerPixel(), resolutions[1], 0.0001 );
QGSCOMPARENEAR( mCanvas->mapSettings().mapUnitsPerPixel(), wheelFactor * wheelFactor * resolutions.last(), 0.0001 );

mCanvas->zoomIn();
QGSCOMPARENEAR( mCanvas->mapSettings().mapUnitsPerPixel(), wheelFactor * resolutions.last(), 0.0001 );

// From last to first resolution in list
for ( int i = nResolutions - 1; i >= 0; --i )
{
mCanvas->zoomIn();
QGSCOMPARENEAR( mCanvas->mapSettings().mapUnitsPerPixel(), resolutions[i], 0.0001 );
}

// before first resolution
mCanvas->zoomIn();
QGSCOMPARENEAR( mCanvas->mapSettings().mapUnitsPerPixel(), resolutions.first() / wheelFactor, 0.0001 );

mCanvas->zoomIn();
QGSCOMPARENEAR( mCanvas->mapSettings().mapUnitsPerPixel(), resolutions[0], 0.0001 );
QGSCOMPARENEAR( mCanvas->mapSettings().mapUnitsPerPixel(), resolutions.first() / ( wheelFactor * wheelFactor ), 0.0001 );

mCanvas->zoomOut();
QGSCOMPARENEAR( mCanvas->mapSettings().mapUnitsPerPixel(), resolutions.first() / wheelFactor, 0.0001 );

mCanvas->zoomOut();
QGSCOMPARENEAR( mCanvas->mapSettings().mapUnitsPerPixel(), resolutions.first(), 0.0001 );

QCOMPARE( mCanvas->zoomResolutions(), resolutions );
}
Expand Down

0 comments on commit 26c7c01

Please sign in to comment.