Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Add some shortcuts to zoom to layout/layout width in status bar combobox
  • Loading branch information
nyalldawson committed Jul 11, 2017
1 parent e27a32d commit 02d0a04
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 37 deletions.
5 changes: 0 additions & 5 deletions python/gui/layout/qgslayoutdesignerinterface.sip
Expand Up @@ -55,11 +55,6 @@ class QgsLayoutDesignerInterface: QObject
Closes the layout designer.
%End

virtual void zoomFull() = 0;
%Docstring
Zooms to full extent of layout.
%End

};

/************************************************************************
Expand Down
8 changes: 8 additions & 0 deletions python/gui/layout/qgslayoutview.sip
Expand Up @@ -86,6 +86,14 @@ class QgsLayoutView: QGraphicsView
.. seealso:: zoomIn()
.. seealso:: zoomOut()
.. seealso:: zoomActual()
%End

void zoomWidth();
%Docstring
Zooms the view to the full width of the layout.
.. seealso:: zoomIn()
.. seealso:: zoomOut()
.. seealso:: zoomActual()
%End

void zoomIn();
Expand Down
45 changes: 24 additions & 21 deletions src/app/layout/qgslayoutdesignerdialog.cpp
Expand Up @@ -35,6 +35,8 @@

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

QgsAppLayoutDesignerInterface::QgsAppLayoutDesignerInterface( QgsLayoutDesignerDialog *dialog )
: QgsLayoutDesignerInterface( dialog )
Expand All @@ -56,11 +58,6 @@ void QgsAppLayoutDesignerInterface::close()
mDesigner->close();
}

void QgsAppLayoutDesignerInterface::zoomFull()
{
mDesigner->zoomFull();
}


QgsLayoutDesignerDialog::QgsLayoutDesignerDialog( QWidget *parent, Qt::WindowFlags flags )
: QMainWindow( parent, flags )
Expand Down Expand Up @@ -145,9 +142,11 @@ QgsLayoutDesignerDialog::QgsLayoutDesignerDialog( QWidget *parent, Qt::WindowFla

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

mStatusBar->addPermanentWidget( mStatusZoomCombo );
Expand Down Expand Up @@ -192,7 +191,7 @@ void QgsLayoutDesignerDialog::open()
{
show();
activate();
zoomFull(); // zoomFull() does not work properly until we have called show()
mView->zoomFull(); // zoomFull() does not work properly until we have called show()

#if 0 // TODO

Expand All @@ -219,14 +218,6 @@ void QgsLayoutDesignerDialog::activate()
#endif
}

void QgsLayoutDesignerDialog::zoomFull()
{
if ( mView )
{
mView->fitInView( mLayout->sceneRect(), Qt::KeepAspectRatio );
}
}

void QgsLayoutDesignerDialog::closeEvent( QCloseEvent * )
{
emit aboutToClose();
Expand All @@ -253,12 +244,24 @@ void QgsLayoutDesignerDialog::itemTypeAdded( int type )

void QgsLayoutDesignerDialog::statusZoomCombo_currentIndexChanged( int index )
{
double selectedZoom = sStatusZoomLevelsList.at( sStatusZoomLevelsList.count() - index - 1 );
if ( mView )
QVariant data = mStatusZoomCombo->itemData( index );
if ( data.toInt() == FIT_LAYOUT )
{
mView->zoomFull();
}
else if ( data.toInt() == FIT_LAYOUT_WIDTH )
{
mView->zoomWidth();
}
else
{
mView->setZoomLevel( selectedZoom );
//update zoom combobox text for correct format (one decimal place, trailing % sign)
whileBlocking( mStatusZoomCombo )->lineEdit()->setText( tr( "%1%" ).arg( selectedZoom * 100.0, 0, 'f', 1 ) );
double selectedZoom = data.toDouble();
if ( mView )
{
mView->setZoomLevel( selectedZoom );
//update zoom combobox text for correct format (one decimal place, trailing % sign)
whileBlocking( mStatusZoomCombo )->lineEdit()->setText( tr( "%1%" ).arg( selectedZoom * 100.0, 0, 'f', 1 ) );
}
}
}

Expand Down
6 changes: 0 additions & 6 deletions src/app/layout/qgslayoutdesignerdialog.h
Expand Up @@ -40,7 +40,6 @@ class QgsAppLayoutDesignerInterface : public QgsLayoutDesignerInterface
public slots:

void close() override;
void zoomFull() override;

private:

Expand Down Expand Up @@ -99,11 +98,6 @@ class QgsLayoutDesignerDialog: public QMainWindow, private Ui::QgsLayoutDesigner
*/
void activate();

/**
* Zooms to show full layout.
*/
void zoomFull();

signals:

/**
Expand Down
5 changes: 0 additions & 5 deletions src/gui/layout/qgslayoutdesignerinterface.h
Expand Up @@ -68,11 +68,6 @@ class GUI_EXPORT QgsLayoutDesignerInterface: public QObject
*/
virtual void close() = 0;

/**
* Zooms to full extent of layout.
*/
virtual void zoomFull() = 0;

};

#endif // QGSLAYOUTDESIGNERINTERFACE_H
23 changes: 23 additions & 0 deletions src/gui/layout/qgslayoutview.cpp
Expand Up @@ -95,6 +95,7 @@ void QgsLayoutView::scaleSafe( double scale )
scale *= currentScale;
scale = qBound( MIN_VIEW_SCALE, scale, MAX_VIEW_SCALE );
setTransform( QTransform::fromScale( scale, scale ) );
emit zoomLevelChanged();
}

void QgsLayoutView::setZoomLevel( double level )
Expand All @@ -113,6 +114,28 @@ void QgsLayoutView::setZoomLevel( double level )
void QgsLayoutView::zoomFull()
{
fitInView( scene()->sceneRect(), Qt::KeepAspectRatio );
emit zoomLevelChanged();
}

void QgsLayoutView::zoomWidth()
{
//get current visible part of scene
QRect viewportRect( 0, 0, viewport()->width(), viewport()->height() );
QRectF visibleRect = mapToScene( viewportRect ).boundingRect();

double verticalCenter = ( visibleRect.top() + visibleRect.bottom() ) / 2.0;
// expand out visible rect to include left/right edges of scene
// centered on current visible vertical center
// note that we can't have a 0 height rect - fitInView doesn't handle that
// so we just set a very small height instead.
const double tinyHeight = 0.01;
QRectF targetRect( scene()->sceneRect().left(),
verticalCenter - tinyHeight,
scene()->sceneRect().width(),
tinyHeight * 2 );

fitInView( targetRect, Qt::KeepAspectRatio );
emit zoomLevelChanged();
}

void QgsLayoutView::zoomIn()
Expand Down
8 changes: 8 additions & 0 deletions src/gui/layout/qgslayoutview.h
Expand Up @@ -109,6 +109,14 @@ class GUI_EXPORT QgsLayoutView: public QGraphicsView
*/
void zoomFull();

/**
* Zooms the view to the full width of the layout.
* \see zoomIn()
* \see zoomOut()
* \see zoomActual()
*/
void zoomWidth();

/**
* Zooms in to the view by a preset amount.
* \see zoomFull()
Expand Down

0 comments on commit 02d0a04

Please sign in to comment.