[composer] Prevent zooming out/in too far

Would cause issues when scale became 0 and it was impossible
to further interact with the composer.
This commit is contained in:
Nyall Dawson 2016-05-29 22:25:34 +10:00
parent f9a10e4a99
commit aa53cfe387
6 changed files with 96 additions and 5 deletions

View File

@ -112,6 +112,13 @@ class QgsComposerView : QGraphicsView
/** Set zoom level, where a zoom level of 1.0 corresponds to 100%*/
void setZoomLevel( double zoomLevel );
/** Scales the view in a safe way, by limiting the acceptable range
* of the scale applied.
* @param scale factor to scale view by
* @note added in QGIS 2.16
*/
void scaleSafe( double scale );
/** Sets whether a preview effect should be used to alter the view's appearance
* @param enabled Set to true to enable the preview effect on the view
* @note added in 2.3

View File

@ -1250,7 +1250,7 @@ void QgsComposer::on_mActionZoomAll_triggered()
void QgsComposer::on_mActionZoomIn_triggered()
{
mView->scale( 2, 2 );
mView->scaleSafe( 2 );
mView->updateRulers();
mView->update();
emit zoomLevelChanged();
@ -1258,7 +1258,7 @@ void QgsComposer::on_mActionZoomIn_triggered()
void QgsComposer::on_mActionZoomOut_triggered()
{
mView->scale( .5, .5 );
mView->scaleSafe( 0.5 );
mView->updateRulers();
mView->update();
emit zoomLevelChanged();

View File

@ -48,6 +48,9 @@
#include "qgscursors.h"
#include "qgscomposerutils.h"
#define MIN_VIEW_SCALE 0.05
#define MAX_VIEW_SCALE 1000.0
QgsComposerView::QgsComposerView( QWidget* parent, const char* name, const Qt::WindowFlags& f )
: QGraphicsView( parent )
, mCurrentTool( Select )
@ -2060,11 +2063,11 @@ void QgsComposerView::wheelZoom( QWheelEvent * event )
//zoom composition
if ( zoomIn )
{
scale( zoomFactor, zoomFactor );
scaleSafe( zoomFactor );
}
else
{
scale( 1 / zoomFactor, 1 / zoomFactor );
scaleSafe( 1 / zoomFactor );
}
//update composition for new zoom
@ -2092,7 +2095,7 @@ void QgsComposerView::setZoomLevel( double zoomLevel )
dpi = 72;
//desired pixel width for 1mm on screen
double scale = zoomLevel * dpi / 25.4;
double scale = qBound( MIN_VIEW_SCALE, zoomLevel * dpi / 25.4, MAX_VIEW_SCALE );
setTransform( QTransform::fromScale( scale, scale ) );
updateRulers();
@ -2100,6 +2103,14 @@ void QgsComposerView::setZoomLevel( double zoomLevel )
emit zoomLevelChanged();
}
void QgsComposerView::scaleSafe( double scale )
{
double currentScale = transform().m11();
scale *= currentScale;
scale = qBound( MIN_VIEW_SCALE, scale, MAX_VIEW_SCALE );
setTransform( QTransform::fromScale( scale, scale ) );
}
void QgsComposerView::setPreviewModeEnabled( bool enabled )
{
if ( !mPreviewEffect )

View File

@ -148,6 +148,13 @@ class GUI_EXPORT QgsComposerView: public QGraphicsView
/** Set zoom level, where a zoom level of 1.0 corresponds to 100%*/
void setZoomLevel( double zoomLevel );
/** Scales the view in a safe way, by limiting the acceptable range
* of the scale applied.
* @param scale factor to scale view by
* @note added in QGIS 2.16
*/
void scaleSafe( double scale );
/** Sets whether a preview effect should be used to alter the view's appearance
* @param enabled Set to true to enable the preview effect on the view
* @note added in 2.3

View File

@ -29,6 +29,7 @@ ADD_PYTHON_TEST(PyQgsComposerPicture test_qgscomposerpicture.py)
ADD_PYTHON_TEST(PyQgsComposerShapes test_qgscomposershapes.py)
ADD_PYTHON_TEST(PyQgsComposerPolygon test_qgscomposerpolygon.py)
ADD_PYTHON_TEST(PyQgsComposerPolyline test_qgscomposerpolyline.py)
ADD_PYTHON_TEST(PyQgsComposerView test_qgscomposerview.py)
ADD_PYTHON_TEST(PyQgsComposition test_qgscomposition.py)
ADD_PYTHON_TEST(PyQgsConditionalStyle test_qgsconditionalstyle.py)
ADD_PYTHON_TEST(PyQgsCoordinateTransform test_qgscoordinatetransform.py)

View File

@ -0,0 +1,65 @@
# -*- coding: utf-8 -*-
"""QGIS Unit tests for QgsComposerView.
.. note:: This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
"""
__author__ = 'Nyall Dawson'
__date__ = '29/05/2016'
__copyright__ = 'Copyright 2016, The QGIS Project'
# This will get replaced with a git SHA1 when you do a git archive
__revision__ = '$Format:%H$'
import qgis # NOQA
import os
from qgis.gui import (QgsComposerView)
from qgis.PyQt.QtCore import QRectF
from qgis.PyQt.QtGui import QTransform
from qgis.testing import start_app, unittest
start_app()
class TestQgsComposerView(unittest.TestCase):
def testScaleSafe(self):
""" test scaleSafe method """
view = QgsComposerView()
view.fitInView(QRectF(0, 0, 10, 10))
scale = view.transform().m11()
view.scaleSafe(2)
self.assertAlmostEqual(view.transform().m11(), 2)
view.scaleSafe(4)
self.assertAlmostEqual(view.transform().m11(), 8)
# try to zoom in heaps
view.scaleSafe(99999999)
# assume we have hit the limit
scale = view.transform().m11()
view.scaleSafe(2)
self.assertAlmostEqual(view.transform().m11(), scale)
view.setTransform(QTransform.fromScale(1, 1))
self.assertAlmostEqual(view.transform().m11(), 1)
# test zooming out
view.scaleSafe(0.5)
self.assertAlmostEqual(view.transform().m11(), 0.5)
view.scaleSafe(0.1)
self.assertAlmostEqual(view.transform().m11(), 0.05)
# try zooming out heaps
view.scaleSafe(0.000000001)
# assume we have hit the limit
scale = view.transform().m11()
view.scaleSafe(0.5)
self.assertAlmostEqual(view.transform().m11(), scale)
if __name__ == '__main__':
unittest.main()