Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Fix unreported crash when removing items from layout legend
Master only
  • Loading branch information
nyalldawson committed Nov 21, 2020
1 parent 41dc29a commit cfc5b37
Showing 1 changed file with 13 additions and 11 deletions.
24 changes: 13 additions & 11 deletions src/gui/layout/qgslayoutlegendwidget.cpp
Expand Up @@ -939,16 +939,16 @@ void QgsLayoutLegendWidget::mRemoveToolButton_clicked()

mLegend->beginCommand( tr( "Remove Legend Item" ) );

QList<QPersistentModelIndex> indexes;
const auto constSelectedIndexes = selectionModel->selectedIndexes();
for ( const QModelIndex &index : constSelectedIndexes )
indexes << index;
QList<QPersistentModelIndex> proxyIndexes;
const QModelIndexList viewSelection = selectionModel->selectedIndexes();
for ( const QModelIndex &index : viewSelection )
proxyIndexes << index;

// first try to remove legend nodes
QHash<QgsLayerTreeLayer *, QList<int> > nodesWithRemoval;
for ( const QPersistentModelIndex &index : qgis::as_const( indexes ) )
for ( const QPersistentModelIndex &proxyIndex : qgis::as_const( proxyIndexes ) )
{
if ( QgsLayerTreeModelLegendNode *legendNode = mItemTreeView->index2legendNode( index ) )
if ( QgsLayerTreeModelLegendNode *legendNode = mItemTreeView->index2legendNode( proxyIndex ) )
{
QgsLayerTreeLayer *nodeLayer = legendNode->layerNode();
nodesWithRemoval[nodeLayer].append( _unfilteredLegendNodeIndex( legendNode ) );
Expand All @@ -960,8 +960,7 @@ void QgsLayoutLegendWidget::mRemoveToolButton_clicked()
std::sort( toDelete.begin(), toDelete.end(), std::greater<int>() );
QList<int> order = QgsMapLayerLegendUtils::legendNodeOrder( it.key() );

const auto constToDelete = toDelete;
for ( int i : constToDelete )
for ( int i : qgis::as_const( toDelete ) )
{
if ( i >= 0 && i < order.count() )
order.removeAt( i );
Expand All @@ -972,10 +971,13 @@ void QgsLayoutLegendWidget::mRemoveToolButton_clicked()
}

// then remove layer tree nodes
for ( const QPersistentModelIndex &index : qgis::as_const( indexes ) )
for ( const QPersistentModelIndex &proxyIndex : qgis::as_const( proxyIndexes ) )
{
if ( index.isValid() && mItemTreeView->index2node( index ) )
mLegend->model()->removeRow( index.row(), index.parent() );
if ( proxyIndex.isValid() && mItemTreeView->index2node( proxyIndex ) )
{
const QModelIndex sourceIndex = mItemTreeView->proxyModel()->mapToSource( proxyIndex );
mLegend->model()->removeRow( sourceIndex.row(), sourceIndex.parent() );
}
}

mLegend->updateLegend();
Expand Down

0 comments on commit cfc5b37

Please sign in to comment.