Skip to content

Commit ac83093

Browse files
committed
Port zoom actions from composer to layout designer
1 parent cf488d3 commit ac83093

File tree

5 files changed

+209
-1
lines changed

5 files changed

+209
-1
lines changed

python/gui/layout/qgslayoutview.sip

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,45 @@ class QgsLayoutView: QGraphicsView
7373
of the scale applied. The ``scale`` parameter specifies the zoom factor to scale the view by.
7474
%End
7575

76+
void setZoomLevel( double level );
77+
%Docstring
78+
Sets the zoom ``level`` for the view, where a zoom level of 1.0 corresponds to 100%.
79+
%End
80+
81+
public slots:
82+
83+
void zoomFull();
84+
%Docstring
85+
Zooms the view to the full extent of the layout.
86+
.. seealso:: zoomIn()
87+
.. seealso:: zoomOut()
88+
.. seealso:: zoomActual()
89+
%End
90+
91+
void zoomIn();
92+
%Docstring
93+
Zooms in to the view by a preset amount.
94+
.. seealso:: zoomFull()
95+
.. seealso:: zoomOut()
96+
.. seealso:: zoomActual()
97+
%End
98+
99+
void zoomOut();
100+
%Docstring
101+
Zooms out of the view by a preset amount.
102+
.. seealso:: zoomFull()
103+
.. seealso:: zoomIn()
104+
.. seealso:: zoomActual()
105+
%End
106+
107+
void zoomActual();
108+
%Docstring
109+
Zooms to the actual size of the layout.
110+
.. seealso:: zoomFull()
111+
.. seealso:: zoomIn()
112+
.. seealso:: zoomOut()
113+
%End
114+
76115
signals:
77116

78117
void layoutSet( QgsLayout *layout );

src/app/layout/qgslayoutdesignerdialog.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#include "qgslayoutviewtoolselect.h"
2929
#include "qgsgui.h"
3030
#include "qgslayoutitemguiregistry.h"
31+
#include <QShortcut>
3132

3233
QgsAppLayoutDesignerInterface::QgsAppLayoutDesignerInterface( QgsLayoutDesignerDialog *dialog )
3334
: QgsLayoutDesignerInterface( dialog )
@@ -112,6 +113,15 @@ QgsLayoutDesignerDialog::QgsLayoutDesignerDialog( QWidget *parent, Qt::WindowFla
112113
mToolsActionGroup->addAction( mActionSelectMoveItem );
113114
connect( mActionSelectMoveItem, &QAction::triggered, mSelectTool, [ = ] { mView->setTool( mSelectTool ); } );
114115

116+
//Ctrl+= should also trigger zoom in
117+
QShortcut *ctrlEquals = new QShortcut( QKeySequence( QStringLiteral( "Ctrl+=" ) ), this );
118+
connect( ctrlEquals, &QShortcut::activated, mActionZoomIn, &QAction::trigger );
119+
120+
connect( mActionZoomIn, &QAction::triggered, mView, &QgsLayoutView::zoomIn );
121+
connect( mActionZoomOut, &QAction::triggered, mView, &QgsLayoutView::zoomOut );
122+
connect( mActionZoomAll, &QAction::triggered, mView, &QgsLayoutView::zoomFull );
123+
connect( mActionZoomActual, &QAction::triggered, mView, &QgsLayoutView::zoomActual );
124+
115125
mView->setTool( mSelectTool );
116126
mView->setFocus();
117127

src/gui/layout/qgslayoutview.cpp

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,9 @@
2424
#include "qgslayoutviewtooltemporarymousepan.h"
2525
#include "qgssettings.h"
2626
#include "qgsrectangle.h"
27+
#include "qgsapplication.h"
2728
#include <memory>
29+
#include <QDesktopWidget>
2830

2931
#define MIN_VIEW_SCALE 0.05
3032
#define MAX_VIEW_SCALE 1000.0
@@ -95,6 +97,38 @@ void QgsLayoutView::scaleSafe( double scale )
9597
setTransform( QTransform::fromScale( scale, scale ) );
9698
}
9799

100+
void QgsLayoutView::setZoomLevel( double level )
101+
{
102+
double dpi = QgsApplication::desktop()->logicalDpiX();
103+
//monitor dpi is not always correct - so make sure the value is sane
104+
if ( ( dpi < 60 ) || ( dpi > 1200 ) )
105+
dpi = 72;
106+
107+
//desired pixel width for 1mm on screen
108+
double scale = qBound( MIN_VIEW_SCALE, level * dpi / 25.4, MAX_VIEW_SCALE );
109+
setTransform( QTransform::fromScale( scale, scale ) );
110+
}
111+
112+
void QgsLayoutView::zoomFull()
113+
{
114+
fitInView( scene()->sceneRect(), Qt::KeepAspectRatio );
115+
}
116+
117+
void QgsLayoutView::zoomIn()
118+
{
119+
scaleSafe( 2 );
120+
}
121+
122+
void QgsLayoutView::zoomOut()
123+
{
124+
scaleSafe( 0.5 );
125+
}
126+
127+
void QgsLayoutView::zoomActual()
128+
{
129+
setZoomLevel( 1.0 );
130+
}
131+
98132
void QgsLayoutView::mousePressEvent( QMouseEvent *event )
99133
{
100134
if ( mTool )

src/gui/layout/qgslayoutview.h

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,45 @@ class GUI_EXPORT QgsLayoutView: public QGraphicsView
9494
*/
9595
void scaleSafe( double scale );
9696

97+
/**
98+
* Sets the zoom \a level for the view, where a zoom level of 1.0 corresponds to 100%.
99+
*/
100+
void setZoomLevel( double level );
101+
102+
public slots:
103+
104+
/**
105+
* Zooms the view to the full extent of the layout.
106+
* \see zoomIn()
107+
* \see zoomOut()
108+
* \see zoomActual()
109+
*/
110+
void zoomFull();
111+
112+
/**
113+
* Zooms in to the view by a preset amount.
114+
* \see zoomFull()
115+
* \see zoomOut()
116+
* \see zoomActual()
117+
*/
118+
void zoomIn();
119+
120+
/**
121+
* Zooms out of the view by a preset amount.
122+
* \see zoomFull()
123+
* \see zoomIn()
124+
* \see zoomActual()
125+
*/
126+
void zoomOut();
127+
128+
/**
129+
* Zooms to the actual size of the layout.
130+
* \see zoomFull()
131+
* \see zoomIn()
132+
* \see zoomOut()
133+
*/
134+
void zoomActual();
135+
97136
signals:
98137

99138
/**

src/ui/layout/qgslayoutdesignerbase.ui

Lines changed: 87 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@
8383
<x>0</x>
8484
<y>0</y>
8585
<width>1083</width>
86-
<height>42</height>
86+
<height>25</height>
8787
</rect>
8888
</property>
8989
<widget class="QMenu" name="mLayoutMenu">
@@ -97,9 +97,34 @@
9797
<string>&amp;Items</string>
9898
</property>
9999
</widget>
100+
<widget class="QMenu" name="mMenuView">
101+
<property name="title">
102+
<string>View</string>
103+
</property>
104+
<addaction name="mActionZoomIn"/>
105+
<addaction name="mActionZoomOut"/>
106+
<addaction name="mActionZoomActual"/>
107+
<addaction name="mActionZoomAll"/>
108+
</widget>
100109
<addaction name="mLayoutMenu"/>
110+
<addaction name="mMenuView"/>
101111
<addaction name="mItemMenu"/>
102112
</widget>
113+
<widget class="QToolBar" name="mNavigationToolbar">
114+
<property name="windowTitle">
115+
<string>toolBar</string>
116+
</property>
117+
<attribute name="toolBarArea">
118+
<enum>TopToolBarArea</enum>
119+
</attribute>
120+
<attribute name="toolBarBreak">
121+
<bool>false</bool>
122+
</attribute>
123+
<addaction name="mActionZoomIn"/>
124+
<addaction name="mActionZoomOut"/>
125+
<addaction name="mActionZoomActual"/>
126+
<addaction name="mActionZoomAll"/>
127+
</widget>
103128
<action name="mActionClose">
104129
<property name="text">
105130
<string>&amp;Close</string>
@@ -162,6 +187,66 @@
162187
<string>V</string>
163188
</property>
164189
</action>
190+
<action name="mActionZoomAll">
191+
<property name="icon">
192+
<iconset resource="../../../images/images.qrc">
193+
<normaloff>:/images/themes/default/mActionZoomFullExtent.svg</normaloff>:/images/themes/default/mActionZoomFullExtent.svg</iconset>
194+
</property>
195+
<property name="text">
196+
<string>Zoom &amp;Full</string>
197+
</property>
198+
<property name="toolTip">
199+
<string>Zoom full</string>
200+
</property>
201+
<property name="shortcut">
202+
<string>Ctrl+0</string>
203+
</property>
204+
</action>
205+
<action name="mActionZoomIn">
206+
<property name="icon">
207+
<iconset resource="../../../images/images.qrc">
208+
<normaloff>:/images/themes/default/mActionZoomIn.svg</normaloff>:/images/themes/default/mActionZoomIn.svg</iconset>
209+
</property>
210+
<property name="text">
211+
<string>Zoom &amp;In</string>
212+
</property>
213+
<property name="toolTip">
214+
<string>Zoom in</string>
215+
</property>
216+
<property name="shortcut">
217+
<string>Ctrl++</string>
218+
</property>
219+
</action>
220+
<action name="mActionZoomOut">
221+
<property name="icon">
222+
<iconset resource="../../../images/images.qrc">
223+
<normaloff>:/images/themes/default/mActionZoomOut.svg</normaloff>:/images/themes/default/mActionZoomOut.svg</iconset>
224+
</property>
225+
<property name="text">
226+
<string>Zoom &amp;Out</string>
227+
</property>
228+
<property name="toolTip">
229+
<string>Zoom out</string>
230+
</property>
231+
<property name="shortcut">
232+
<string>Ctrl+-</string>
233+
</property>
234+
</action>
235+
<action name="mActionZoomActual">
236+
<property name="icon">
237+
<iconset resource="../../../images/images.qrc">
238+
<normaloff>:/images/themes/default/mActionZoomActual.svg</normaloff>:/images/themes/default/mActionZoomActual.svg</iconset>
239+
</property>
240+
<property name="text">
241+
<string>Zoom to &amp;100%</string>
242+
</property>
243+
<property name="toolTip">
244+
<string>Zoom to 100%</string>
245+
</property>
246+
<property name="shortcut">
247+
<string>Ctrl+1</string>
248+
</property>
249+
</action>
165250
</widget>
166251
<resources>
167252
<include location="../../../images/images.qrc"/>
@@ -189,6 +274,7 @@
189274
<include location="../../../images/images.qrc"/>
190275
<include location="../../../images/images.qrc"/>
191276
<include location="../../../images/images.qrc"/>
277+
<include location="../../../images/images.qrc"/>
192278
</resources>
193279
<connections/>
194280
</ui>

0 commit comments

Comments
 (0)