diff --git a/python/gui/layout/qgslayoutview.sip b/python/gui/layout/qgslayoutview.sip index b9b3629c095..389cce1927d 100644 --- a/python/gui/layout/qgslayoutview.sip +++ b/python/gui/layout/qgslayoutview.sip @@ -73,6 +73,45 @@ class QgsLayoutView: QGraphicsView of the scale applied. The ``scale`` parameter specifies the zoom factor to scale the view by. %End + void setZoomLevel( double level ); +%Docstring + Sets the zoom ``level`` for the view, where a zoom level of 1.0 corresponds to 100%. +%End + + public slots: + + void zoomFull(); +%Docstring + Zooms the view to the full extent of the layout. +.. seealso:: zoomIn() +.. seealso:: zoomOut() +.. seealso:: zoomActual() +%End + + void zoomIn(); +%Docstring + Zooms in to the view by a preset amount. +.. seealso:: zoomFull() +.. seealso:: zoomOut() +.. seealso:: zoomActual() +%End + + void zoomOut(); +%Docstring + Zooms out of the view by a preset amount. +.. seealso:: zoomFull() +.. seealso:: zoomIn() +.. seealso:: zoomActual() +%End + + void zoomActual(); +%Docstring + Zooms to the actual size of the layout. +.. seealso:: zoomFull() +.. seealso:: zoomIn() +.. seealso:: zoomOut() +%End + signals: void layoutSet( QgsLayout *layout ); diff --git a/src/app/layout/qgslayoutdesignerdialog.cpp b/src/app/layout/qgslayoutdesignerdialog.cpp index 2ec0ea12cdf..34aba612011 100644 --- a/src/app/layout/qgslayoutdesignerdialog.cpp +++ b/src/app/layout/qgslayoutdesignerdialog.cpp @@ -28,6 +28,7 @@ #include "qgslayoutviewtoolselect.h" #include "qgsgui.h" #include "qgslayoutitemguiregistry.h" +#include QgsAppLayoutDesignerInterface::QgsAppLayoutDesignerInterface( QgsLayoutDesignerDialog *dialog ) : QgsLayoutDesignerInterface( dialog ) @@ -112,6 +113,15 @@ QgsLayoutDesignerDialog::QgsLayoutDesignerDialog( QWidget *parent, Qt::WindowFla mToolsActionGroup->addAction( mActionSelectMoveItem ); connect( mActionSelectMoveItem, &QAction::triggered, mSelectTool, [ = ] { mView->setTool( mSelectTool ); } ); + //Ctrl+= should also trigger zoom in + QShortcut *ctrlEquals = new QShortcut( QKeySequence( QStringLiteral( "Ctrl+=" ) ), this ); + connect( ctrlEquals, &QShortcut::activated, mActionZoomIn, &QAction::trigger ); + + connect( mActionZoomIn, &QAction::triggered, mView, &QgsLayoutView::zoomIn ); + connect( mActionZoomOut, &QAction::triggered, mView, &QgsLayoutView::zoomOut ); + connect( mActionZoomAll, &QAction::triggered, mView, &QgsLayoutView::zoomFull ); + connect( mActionZoomActual, &QAction::triggered, mView, &QgsLayoutView::zoomActual ); + mView->setTool( mSelectTool ); mView->setFocus(); diff --git a/src/gui/layout/qgslayoutview.cpp b/src/gui/layout/qgslayoutview.cpp index 3b570e66af9..174f8587c97 100644 --- a/src/gui/layout/qgslayoutview.cpp +++ b/src/gui/layout/qgslayoutview.cpp @@ -24,7 +24,9 @@ #include "qgslayoutviewtooltemporarymousepan.h" #include "qgssettings.h" #include "qgsrectangle.h" +#include "qgsapplication.h" #include +#include #define MIN_VIEW_SCALE 0.05 #define MAX_VIEW_SCALE 1000.0 @@ -95,6 +97,38 @@ void QgsLayoutView::scaleSafe( double scale ) setTransform( QTransform::fromScale( scale, scale ) ); } +void QgsLayoutView::setZoomLevel( double level ) +{ + double dpi = QgsApplication::desktop()->logicalDpiX(); + //monitor dpi is not always correct - so make sure the value is sane + if ( ( dpi < 60 ) || ( dpi > 1200 ) ) + dpi = 72; + + //desired pixel width for 1mm on screen + double scale = qBound( MIN_VIEW_SCALE, level * dpi / 25.4, MAX_VIEW_SCALE ); + setTransform( QTransform::fromScale( scale, scale ) ); +} + +void QgsLayoutView::zoomFull() +{ + fitInView( scene()->sceneRect(), Qt::KeepAspectRatio ); +} + +void QgsLayoutView::zoomIn() +{ + scaleSafe( 2 ); +} + +void QgsLayoutView::zoomOut() +{ + scaleSafe( 0.5 ); +} + +void QgsLayoutView::zoomActual() +{ + setZoomLevel( 1.0 ); +} + void QgsLayoutView::mousePressEvent( QMouseEvent *event ) { if ( mTool ) diff --git a/src/gui/layout/qgslayoutview.h b/src/gui/layout/qgslayoutview.h index ea42409dbc1..df4470cb62c 100644 --- a/src/gui/layout/qgslayoutview.h +++ b/src/gui/layout/qgslayoutview.h @@ -94,6 +94,45 @@ class GUI_EXPORT QgsLayoutView: public QGraphicsView */ void scaleSafe( double scale ); + /** + * Sets the zoom \a level for the view, where a zoom level of 1.0 corresponds to 100%. + */ + void setZoomLevel( double level ); + + public slots: + + /** + * Zooms the view to the full extent of the layout. + * \see zoomIn() + * \see zoomOut() + * \see zoomActual() + */ + void zoomFull(); + + /** + * Zooms in to the view by a preset amount. + * \see zoomFull() + * \see zoomOut() + * \see zoomActual() + */ + void zoomIn(); + + /** + * Zooms out of the view by a preset amount. + * \see zoomFull() + * \see zoomIn() + * \see zoomActual() + */ + void zoomOut(); + + /** + * Zooms to the actual size of the layout. + * \see zoomFull() + * \see zoomIn() + * \see zoomOut() + */ + void zoomActual(); + signals: /** diff --git a/src/ui/layout/qgslayoutdesignerbase.ui b/src/ui/layout/qgslayoutdesignerbase.ui index 34eb498c93b..fa5854e5361 100644 --- a/src/ui/layout/qgslayoutdesignerbase.ui +++ b/src/ui/layout/qgslayoutdesignerbase.ui @@ -83,7 +83,7 @@ 0 0 1083 - 42 + 25 @@ -97,9 +97,34 @@ &Items + + + View + + + + + + + + + + toolBar + + + TopToolBarArea + + + false + + + + + + &Close @@ -162,6 +187,66 @@ V + + + + :/images/themes/default/mActionZoomFullExtent.svg:/images/themes/default/mActionZoomFullExtent.svg + + + Zoom &Full + + + Zoom full + + + Ctrl+0 + + + + + + :/images/themes/default/mActionZoomIn.svg:/images/themes/default/mActionZoomIn.svg + + + Zoom &In + + + Zoom in + + + Ctrl++ + + + + + + :/images/themes/default/mActionZoomOut.svg:/images/themes/default/mActionZoomOut.svg + + + Zoom &Out + + + Zoom out + + + Ctrl+- + + + + + + :/images/themes/default/mActionZoomActual.svg:/images/themes/default/mActionZoomActual.svg + + + Zoom to &100% + + + Zoom to 100% + + + Ctrl+1 + + @@ -189,6 +274,7 @@ +