Skip to content

Commit

Permalink
Protect layout view against null layouts
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Jan 5, 2018
1 parent aef0432 commit dcf364f
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/gui/layout/qgslayoutruler.cpp
Expand Up @@ -365,6 +365,9 @@ QPoint QgsLayoutRuler::convertLayoutPointToLocal( QPointF layoutPoint ) const

QgsLayoutGuide *QgsLayoutRuler::guideAtPoint( QPoint localPoint ) const
{
if ( !mView->currentLayout() )
return nullptr;

QPointF layoutPoint = convertLocalPointToLayout( localPoint );
QList< QgsLayoutItemPage * > visiblePages = mView->visiblePages();
QList< QgsLayoutGuide * > guides = mView->currentLayout()->guides().guides( mOrientation == Qt::Horizontal ? Qt::Vertical : Qt::Horizontal );
Expand Down
78 changes: 78 additions & 0 deletions src/gui/layout/qgslayoutview.cpp
Expand Up @@ -190,6 +190,9 @@ void QgsLayoutView::scaleSafe( double scale )

void QgsLayoutView::setZoomLevel( double level )
{
if ( !currentLayout() )
return;

if ( currentLayout()->units() == QgsUnitTypes::LayoutPixels )
{
setTransform( QTransform::fromScale( level, level ) );
Expand Down Expand Up @@ -250,6 +253,9 @@ QgsLayoutViewMenuProvider *QgsLayoutView::menuProvider() const

QList<QgsLayoutItemPage *> QgsLayoutView::visiblePages() const
{
if ( !currentLayout() )
return QList< QgsLayoutItemPage *>();

//get current visible part of scene
QRect viewportRect( 0, 0, viewport()->width(), viewport()->height() );
QRectF visibleRect = mapToScene( viewportRect ).boundingRect();
Expand All @@ -258,6 +264,9 @@ QList<QgsLayoutItemPage *> QgsLayoutView::visiblePages() const

QList<int> QgsLayoutView::visiblePageNumbers() const
{
if ( !currentLayout() )
return QList< int >();

//get current visible part of scene
QRect viewportRect( 0, 0, viewport()->width(), viewport()->height() );
QRectF visibleRect = mapToScene( viewportRect ).boundingRect();
Expand All @@ -266,18 +275,27 @@ QList<int> QgsLayoutView::visiblePageNumbers() const

void QgsLayoutView::alignSelectedItems( QgsLayoutAligner::Alignment alignment )
{
if ( !currentLayout() )
return;

const QList<QgsLayoutItem *> selectedItems = currentLayout()->selectedLayoutItems();
QgsLayoutAligner::alignItems( currentLayout(), selectedItems, alignment );
}

void QgsLayoutView::distributeSelectedItems( QgsLayoutAligner::Distribution distribution )
{
if ( !currentLayout() )
return;

const QList<QgsLayoutItem *> selectedItems = currentLayout()->selectedLayoutItems();
QgsLayoutAligner::distributeItems( currentLayout(), selectedItems, distribution );
}

void QgsLayoutView::resizeSelectedItems( QgsLayoutAligner::Resize resize )
{
if ( !currentLayout() )
return;

const QList<QgsLayoutItem *> selectedItems = currentLayout()->selectedLayoutItems();
QgsLayoutAligner::resizeItems( currentLayout(), selectedItems, resize );
}
Expand All @@ -289,6 +307,9 @@ void QgsLayoutView::copySelectedItems( QgsLayoutView::ClipboardOperation operati

void QgsLayoutView::copyItems( const QList<QgsLayoutItem *> &items, QgsLayoutView::ClipboardOperation operation )
{
if ( !currentLayout() )
return;

QgsReadWriteContext context;
QDomDocument doc;
QDomElement documentElement = doc.createElement( QStringLiteral( "LayoutItemClipboard" ) );
Expand Down Expand Up @@ -335,6 +356,9 @@ void QgsLayoutView::copyItems( const QList<QgsLayoutItem *> &items, QgsLayoutVie

QList< QgsLayoutItem * > QgsLayoutView::pasteItems( QgsLayoutView::PasteMode mode )
{
if ( !currentLayout() )
return QList< QgsLayoutItem * >();

QList< QgsLayoutItem * > pastedItems;
QDomDocument doc;
QClipboard *clipboard = QApplication::clipboard();
Expand Down Expand Up @@ -373,6 +397,9 @@ QList< QgsLayoutItem * > QgsLayoutView::pasteItems( QgsLayoutView::PasteMode mod

QList<QgsLayoutItem *> QgsLayoutView::pasteItems( QPointF layoutPoint )
{
if ( !currentLayout() )
return QList<QgsLayoutItem *>();

QList< QgsLayoutItem * > pastedItems;
QDomDocument doc;
QClipboard *clipboard = QApplication::clipboard();
Expand Down Expand Up @@ -455,13 +482,19 @@ void QgsLayoutView::setPaintingEnabled( bool enabled )

void QgsLayoutView::zoomFull()
{
if ( !scene() )
return;

fitInView( scene()->sceneRect(), Qt::KeepAspectRatio );
viewChanged();
emit zoomLevelChanged();
}

void QgsLayoutView::zoomWidth()
{
if ( !scene() )
return;

//get current visible part of scene
QRect viewportRect( 0, 0, viewport()->width(), viewport()->height() );
QRectF visibleRect = mapToScene( viewportRect ).boundingRect();
Expand Down Expand Up @@ -623,6 +656,9 @@ void QgsLayoutView::selectNextItemBelow()

void QgsLayoutView::raiseSelectedItems()
{
if ( !currentLayout() )
return;

const QList<QgsLayoutItem *> selectedItems = currentLayout()->selectedLayoutItems();
bool itemsRaised = false;
for ( QgsLayoutItem *item : selectedItems )
Expand All @@ -643,6 +679,9 @@ void QgsLayoutView::raiseSelectedItems()

void QgsLayoutView::lowerSelectedItems()
{
if ( !currentLayout() )
return;

const QList<QgsLayoutItem *> selectedItems = currentLayout()->selectedLayoutItems();
bool itemsLowered = false;
for ( QgsLayoutItem *item : selectedItems )
Expand All @@ -663,6 +702,9 @@ void QgsLayoutView::lowerSelectedItems()

void QgsLayoutView::moveSelectedItemsToTop()
{
if ( !currentLayout() )
return;

const QList<QgsLayoutItem *> selectedItems = currentLayout()->selectedLayoutItems();
bool itemsRaised = false;
for ( QgsLayoutItem *item : selectedItems )
Expand All @@ -683,6 +725,9 @@ void QgsLayoutView::moveSelectedItemsToTop()

void QgsLayoutView::moveSelectedItemsToBottom()
{
if ( !currentLayout() )
return;

const QList<QgsLayoutItem *> selectedItems = currentLayout()->selectedLayoutItems();
bool itemsLowered = false;
for ( QgsLayoutItem *item : selectedItems )
Expand All @@ -703,6 +748,9 @@ void QgsLayoutView::moveSelectedItemsToBottom()

void QgsLayoutView::lockSelectedItems()
{
if ( !currentLayout() )
return;

currentLayout()->undoStack()->beginMacro( tr( "Lock Items" ) );
const QList<QgsLayoutItem *> selectionList = currentLayout()->selectedLayoutItems();
for ( QgsLayoutItem *item : selectionList )
Expand All @@ -716,6 +764,9 @@ void QgsLayoutView::lockSelectedItems()

void QgsLayoutView::unlockAllItems()
{
if ( !currentLayout() )
return;

//unlock all items in layout
currentLayout()->undoStack()->beginMacro( tr( "Unlock Items" ) );

Expand Down Expand Up @@ -743,11 +794,17 @@ void QgsLayoutView::unlockAllItems()

void QgsLayoutView::deleteSelectedItems()
{
if ( !currentLayout() )
return;

deleteItems( currentLayout()->selectedLayoutItems() );
}

void QgsLayoutView::deleteItems( const QList<QgsLayoutItem *> &items )
{
if ( !currentLayout() )
return;

if ( items.empty() )
return;

Expand Down Expand Up @@ -817,6 +874,9 @@ void QgsLayoutView::ungroupSelectedItems()

void QgsLayoutView::mousePressEvent( QMouseEvent *event )
{
if ( !currentLayout() )
return;

if ( mSnapMarker )
mSnapMarker->setVisible( false );

Expand Down Expand Up @@ -853,6 +913,9 @@ void QgsLayoutView::mousePressEvent( QMouseEvent *event )

void QgsLayoutView::mouseReleaseEvent( QMouseEvent *event )
{
if ( !currentLayout() )
return;

if ( mTool )
{
std::unique_ptr<QgsLayoutViewMouseEvent> me( new QgsLayoutViewMouseEvent( this, event, mTool->flags() & QgsLayoutViewTool::FlagSnaps ) );
Expand All @@ -866,6 +929,9 @@ void QgsLayoutView::mouseReleaseEvent( QMouseEvent *event )

void QgsLayoutView::mouseMoveEvent( QMouseEvent *event )
{
if ( !currentLayout() )
return;

mMouseCurrentXY = event->pos();

QPointF cursorPos = mapToScene( mMouseCurrentXY );
Expand Down Expand Up @@ -906,6 +972,9 @@ void QgsLayoutView::mouseMoveEvent( QMouseEvent *event )

void QgsLayoutView::mouseDoubleClickEvent( QMouseEvent *event )
{
if ( !currentLayout() )
return;

if ( mTool )
{
std::unique_ptr<QgsLayoutViewMouseEvent> me( new QgsLayoutViewMouseEvent( this, event, mTool->flags() & QgsLayoutViewTool::FlagSnaps ) );
Expand All @@ -919,6 +988,9 @@ void QgsLayoutView::mouseDoubleClickEvent( QMouseEvent *event )

void QgsLayoutView::wheelEvent( QWheelEvent *event )
{
if ( !currentLayout() )
return;

if ( mTool )
{
mTool->wheelEvent( event );
Expand All @@ -933,6 +1005,9 @@ void QgsLayoutView::wheelEvent( QWheelEvent *event )

void QgsLayoutView::keyPressEvent( QKeyEvent *event )
{
if ( !currentLayout() )
return;

if ( mTool )
{
mTool->keyPressEvent( event );
Expand Down Expand Up @@ -979,6 +1054,9 @@ void QgsLayoutView::keyPressEvent( QKeyEvent *event )

void QgsLayoutView::keyReleaseEvent( QKeyEvent *event )
{
if ( !currentLayout() )
return;

if ( mTool )
{
mTool->keyReleaseEvent( event );
Expand Down

0 comments on commit dcf364f

Please sign in to comment.