Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(layertreeview): add checks on selectedModel to avoid segfault #59337

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 41 additions & 20 deletions src/gui/layertree/qgslayertreeview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,10 @@ QgsMapLayer *QgsLayerTreeView::layerForIndex( const QModelIndex &index ) const

QgsLayerTreeNode *QgsLayerTreeView::currentNode() const
{
return index2node( selectionModel()->currentIndex() );
if ( QItemSelectionModel *selectModel = selectionModel() )
return index2node( selectModel->currentIndex() );
else
return nullptr;
}

QgsLayerTreeGroup *QgsLayerTreeView::currentGroupNode() const
Expand All @@ -421,28 +424,37 @@ QgsLayerTreeGroup *QgsLayerTreeView::currentGroupNode() const
return QgsLayerTree::toGroup( parent );
}

if ( QgsLayerTreeModelLegendNode *legendNode = index2legendNode( selectionModel()->currentIndex() ) )
if ( QItemSelectionModel *selectModel = selectionModel() )
{
QgsLayerTreeLayer *parent = legendNode->layerNode();
if ( QgsLayerTree::isGroup( parent->parent() ) )
return QgsLayerTree::toGroup( parent->parent() );
if ( QgsLayerTreeModelLegendNode *legendNode = index2legendNode( selectModel->currentIndex() ) )
{
QgsLayerTreeLayer *parent = legendNode->layerNode();
if ( QgsLayerTree::isGroup( parent->parent() ) )
return QgsLayerTree::toGroup( parent->parent() );
}
}

return nullptr;
}

QgsLayerTreeModelLegendNode *QgsLayerTreeView::currentLegendNode() const
{
return index2legendNode( selectionModel()->currentIndex() );
if ( QItemSelectionModel *selectModel = selectionModel() )
return index2legendNode( selectModel->currentIndex() );
else
return nullptr;
}

QList<QgsLayerTreeNode *> QgsLayerTreeView::selectedNodes( bool skipInternal ) const
{
QModelIndexList mapped;
const QModelIndexList selected = selectionModel()->selectedIndexes();
mapped.reserve( selected.size() );
for ( const QModelIndex &index : selected )
mapped << mProxyModel->mapToSource( index );
if ( QItemSelectionModel *selectModel = selectionModel() )
{
const QModelIndexList selected = selectModel->selectedIndexes();
mapped.reserve( selected.size() );
for ( const QModelIndex &index : selected )
mapped << mProxyModel->mapToSource( index );
}

return layerTreeModel()->indexes2nodes( mapped, skipInternal );
}
Expand Down Expand Up @@ -476,14 +488,18 @@ QList<QgsMapLayer *> QgsLayerTreeView::selectedLayers() const
QList<QgsLayerTreeModelLegendNode *> QgsLayerTreeView::selectedLegendNodes() const
{
QList<QgsLayerTreeModelLegendNode *> res;
const QModelIndexList selected = selectionModel()->selectedIndexes();
res.reserve( selected.size() );
for ( const QModelIndex &index : selected )

if ( QItemSelectionModel *selectModel = selectionModel() )
{
const QModelIndex &modelIndex = mProxyModel->mapToSource( index );
if ( QgsLayerTreeModelLegendNode *node = layerTreeModel()->index2legendNode( modelIndex ) )
const QModelIndexList selected = selectModel->selectedIndexes();
res.reserve( selected.size() );
for ( const QModelIndex &index : selected )
{
res.push_back( node );
const QModelIndex &modelIndex = mProxyModel->mapToSource( index );
if ( QgsLayerTreeModelLegendNode *node = layerTreeModel()->index2legendNode( modelIndex ) )
{
res.push_back( node );
}
}
}

Expand All @@ -493,10 +509,15 @@ QList<QgsLayerTreeModelLegendNode *> QgsLayerTreeView::selectedLegendNodes() con
QList<QgsMapLayer *> QgsLayerTreeView::selectedLayersRecursive() const
{
QModelIndexList mapped;
const QModelIndexList selected = selectionModel()->selectedIndexes();
mapped.reserve( selected.size() );
for ( const QModelIndex &index : selected )
mapped << mProxyModel->mapToSource( index );
if ( QItemSelectionModel *selectModel = selectionModel() )
{
const QModelIndexList selected = selectModel->selectedIndexes();
mapped.reserve( selected.size() );
for ( const QModelIndex &index : selected )
mapped << mProxyModel->mapToSource( index );
}
else
return QList<QgsMapLayer *>();

const QList<QgsLayerTreeNode *> nodes = layerTreeModel()->indexes2nodes( mapped, false );
const QSet<QgsMapLayer *> layersSet = QgsLayerTreeUtils::collectMapLayersRecursive( nodes );
Expand Down
10 changes: 10 additions & 0 deletions tests/src/python/test_qgslayertreeview.py
Original file line number Diff line number Diff line change
Expand Up @@ -705,6 +705,16 @@ def test_selected_legend_nodes(self):
model = QgsLayerTreeModel(root)
layer_tree_layer = root.addLayer(layer)
view = QgsLayerTreeView()

self.assertEqual(view.selectedNodes(), [])
self.assertEqual(view.selectedLegendNodes(), [])
self.assertEqual(view.selectedLayersRecursive(), [])
self.assertEqual(view.selectedLayerNodes(), [])
# self.assertEqual(view.selectedLayers(), [])
# self.assertIsNone(view.currentNode())
# self.assertIsNone(view.currentLegendNode())
# self.assertIsNone(view.currentGroupNode())

view.setModel(model)

legend_nodes = model.layerLegendNodes(layer_tree_layer)
Expand Down
Loading