Skip to content

Commit 02d0a04

Browse files
committedJul 11, 2017
Add some shortcuts to zoom to layout/layout width in status bar combobox
1 parent e27a32d commit 02d0a04

File tree

7 files changed

+63
-37
lines changed

7 files changed

+63
-37
lines changed
 

‎python/gui/layout/qgslayoutdesignerinterface.sip

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,6 @@ class QgsLayoutDesignerInterface: QObject
5555
Closes the layout designer.
5656
%End
5757

58-
virtual void zoomFull() = 0;
59-
%Docstring
60-
Zooms to full extent of layout.
61-
%End
62-
6358
};
6459

6560
/************************************************************************

‎python/gui/layout/qgslayoutview.sip

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,14 @@ class QgsLayoutView: QGraphicsView
8686
.. seealso:: zoomIn()
8787
.. seealso:: zoomOut()
8888
.. seealso:: zoomActual()
89+
%End
90+
91+
void zoomWidth();
92+
%Docstring
93+
Zooms the view to the full width of the layout.
94+
.. seealso:: zoomIn()
95+
.. seealso:: zoomOut()
96+
.. seealso:: zoomActual()
8997
%End
9098

9199
void zoomIn();

‎src/app/layout/qgslayoutdesignerdialog.cpp

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@
3535

3636
//add some nice zoom levels for zoom comboboxes
3737
QList<double> QgsLayoutDesignerDialog::sStatusZoomLevelsList { 0.125, 0.25, 0.5, 1.0, 2.0, 4.0, 8.0};
38+
#define FIT_LAYOUT -101
39+
#define FIT_LAYOUT_WIDTH -102
3840

3941
QgsAppLayoutDesignerInterface::QgsAppLayoutDesignerInterface( QgsLayoutDesignerDialog *dialog )
4042
: QgsLayoutDesignerInterface( dialog )
@@ -56,11 +58,6 @@ void QgsAppLayoutDesignerInterface::close()
5658
mDesigner->close();
5759
}
5860

59-
void QgsAppLayoutDesignerInterface::zoomFull()
60-
{
61-
mDesigner->zoomFull();
62-
}
63-
6461

6562
QgsLayoutDesignerDialog::QgsLayoutDesignerDialog( QWidget *parent, Qt::WindowFlags flags )
6663
: QMainWindow( parent, flags )
@@ -145,9 +142,11 @@ QgsLayoutDesignerDialog::QgsLayoutDesignerDialog( QWidget *parent, Qt::WindowFla
145142

146143
Q_FOREACH ( double level, sStatusZoomLevelsList )
147144
{
148-
mStatusZoomCombo->insertItem( 0, tr( "%1%" ).arg( level * 100.0, 0, 'f', 1 ) );
145+
mStatusZoomCombo->insertItem( 0, tr( "%1%" ).arg( level * 100.0, 0, 'f', 1 ), level );
149146
}
150-
connect( mStatusZoomCombo, static_cast<void ( QComboBox::* )( int )>( &QComboBox::currentIndexChanged ), this, &QgsLayoutDesignerDialog::statusZoomCombo_currentIndexChanged );
147+
mStatusZoomCombo->insertItem( 0, tr( "Fit Layout" ), FIT_LAYOUT );
148+
mStatusZoomCombo->insertItem( 0, tr( "Fit Layout Width" ), FIT_LAYOUT_WIDTH );
149+
connect( mStatusZoomCombo, static_cast<void ( QComboBox::* )( int )>( &QComboBox::activated ), this, &QgsLayoutDesignerDialog::statusZoomCombo_currentIndexChanged );
151150
connect( mStatusZoomCombo->lineEdit(), &QLineEdit::returnPressed, this, &QgsLayoutDesignerDialog::statusZoomCombo_zoomEntered );
152151

153152
mStatusBar->addPermanentWidget( mStatusZoomCombo );
@@ -192,7 +191,7 @@ void QgsLayoutDesignerDialog::open()
192191
{
193192
show();
194193
activate();
195-
zoomFull(); // zoomFull() does not work properly until we have called show()
194+
mView->zoomFull(); // zoomFull() does not work properly until we have called show()
196195

197196
#if 0 // TODO
198197

@@ -219,14 +218,6 @@ void QgsLayoutDesignerDialog::activate()
219218
#endif
220219
}
221220

222-
void QgsLayoutDesignerDialog::zoomFull()
223-
{
224-
if ( mView )
225-
{
226-
mView->fitInView( mLayout->sceneRect(), Qt::KeepAspectRatio );
227-
}
228-
}
229-
230221
void QgsLayoutDesignerDialog::closeEvent( QCloseEvent * )
231222
{
232223
emit aboutToClose();
@@ -253,12 +244,24 @@ void QgsLayoutDesignerDialog::itemTypeAdded( int type )
253244

254245
void QgsLayoutDesignerDialog::statusZoomCombo_currentIndexChanged( int index )
255246
{
256-
double selectedZoom = sStatusZoomLevelsList.at( sStatusZoomLevelsList.count() - index - 1 );
257-
if ( mView )
247+
QVariant data = mStatusZoomCombo->itemData( index );
248+
if ( data.toInt() == FIT_LAYOUT )
249+
{
250+
mView->zoomFull();
251+
}
252+
else if ( data.toInt() == FIT_LAYOUT_WIDTH )
253+
{
254+
mView->zoomWidth();
255+
}
256+
else
258257
{
259-
mView->setZoomLevel( selectedZoom );
260-
//update zoom combobox text for correct format (one decimal place, trailing % sign)
261-
whileBlocking( mStatusZoomCombo )->lineEdit()->setText( tr( "%1%" ).arg( selectedZoom * 100.0, 0, 'f', 1 ) );
258+
double selectedZoom = data.toDouble();
259+
if ( mView )
260+
{
261+
mView->setZoomLevel( selectedZoom );
262+
//update zoom combobox text for correct format (one decimal place, trailing % sign)
263+
whileBlocking( mStatusZoomCombo )->lineEdit()->setText( tr( "%1%" ).arg( selectedZoom * 100.0, 0, 'f', 1 ) );
264+
}
262265
}
263266
}
264267

‎src/app/layout/qgslayoutdesignerdialog.h

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ class QgsAppLayoutDesignerInterface : public QgsLayoutDesignerInterface
4040
public slots:
4141

4242
void close() override;
43-
void zoomFull() override;
4443

4544
private:
4645

@@ -99,11 +98,6 @@ class QgsLayoutDesignerDialog: public QMainWindow, private Ui::QgsLayoutDesigner
9998
*/
10099
void activate();
101100

102-
/**
103-
* Zooms to show full layout.
104-
*/
105-
void zoomFull();
106-
107101
signals:
108102

109103
/**

‎src/gui/layout/qgslayoutdesignerinterface.h

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -68,11 +68,6 @@ class GUI_EXPORT QgsLayoutDesignerInterface: public QObject
6868
*/
6969
virtual void close() = 0;
7070

71-
/**
72-
* Zooms to full extent of layout.
73-
*/
74-
virtual void zoomFull() = 0;
75-
7671
};
7772

7873
#endif // QGSLAYOUTDESIGNERINTERFACE_H

‎src/gui/layout/qgslayoutview.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ void QgsLayoutView::scaleSafe( double scale )
9595
scale *= currentScale;
9696
scale = qBound( MIN_VIEW_SCALE, scale, MAX_VIEW_SCALE );
9797
setTransform( QTransform::fromScale( scale, scale ) );
98+
emit zoomLevelChanged();
9899
}
99100

100101
void QgsLayoutView::setZoomLevel( double level )
@@ -113,6 +114,28 @@ void QgsLayoutView::setZoomLevel( double level )
113114
void QgsLayoutView::zoomFull()
114115
{
115116
fitInView( scene()->sceneRect(), Qt::KeepAspectRatio );
117+
emit zoomLevelChanged();
118+
}
119+
120+
void QgsLayoutView::zoomWidth()
121+
{
122+
//get current visible part of scene
123+
QRect viewportRect( 0, 0, viewport()->width(), viewport()->height() );
124+
QRectF visibleRect = mapToScene( viewportRect ).boundingRect();
125+
126+
double verticalCenter = ( visibleRect.top() + visibleRect.bottom() ) / 2.0;
127+
// expand out visible rect to include left/right edges of scene
128+
// centered on current visible vertical center
129+
// note that we can't have a 0 height rect - fitInView doesn't handle that
130+
// so we just set a very small height instead.
131+
const double tinyHeight = 0.01;
132+
QRectF targetRect( scene()->sceneRect().left(),
133+
verticalCenter - tinyHeight,
134+
scene()->sceneRect().width(),
135+
tinyHeight * 2 );
136+
137+
fitInView( targetRect, Qt::KeepAspectRatio );
138+
emit zoomLevelChanged();
116139
}
117140

118141
void QgsLayoutView::zoomIn()

‎src/gui/layout/qgslayoutview.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,14 @@ class GUI_EXPORT QgsLayoutView: public QGraphicsView
109109
*/
110110
void zoomFull();
111111

112+
/**
113+
* Zooms the view to the full width of the layout.
114+
* \see zoomIn()
115+
* \see zoomOut()
116+
* \see zoomActual()
117+
*/
118+
void zoomWidth();
119+
112120
/**
113121
* Zooms in to the view by a preset amount.
114122
* \see zoomFull()

0 commit comments

Comments
 (0)
Please sign in to comment.