diff --git a/src/gui/layertree/qgslayertreeview.cpp b/src/gui/layertree/qgslayertreeview.cpp index c39ab91eede3..65db2a91268c 100644 --- a/src/gui/layertree/qgslayertreeview.cpp +++ b/src/gui/layertree/qgslayertreeview.cpp @@ -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 @@ -421,11 +424,14 @@ 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; @@ -433,16 +439,22 @@ QgsLayerTreeGroup *QgsLayerTreeView::currentGroupNode() const QgsLayerTreeModelLegendNode *QgsLayerTreeView::currentLegendNode() const { - return index2legendNode( selectionModel()->currentIndex() ); + if ( QItemSelectionModel *selectModel = selectionModel() ) + return index2legendNode( selectModel->currentIndex() ); + else + return nullptr; } QList 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 ); } @@ -476,14 +488,18 @@ QList QgsLayerTreeView::selectedLayers() const QList QgsLayerTreeView::selectedLegendNodes() const { QList 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 ); + } } } @@ -493,10 +509,15 @@ QList QgsLayerTreeView::selectedLegendNodes() con QList 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(); const QList nodes = layerTreeModel()->indexes2nodes( mapped, false ); const QSet layersSet = QgsLayerTreeUtils::collectMapLayersRecursive( nodes ); diff --git a/tests/src/python/test_qgslayertreeview.py b/tests/src/python/test_qgslayertreeview.py index 8d02dc56d710..e1aea15d7a51 100644 --- a/tests/src/python/test_qgslayertreeview.py +++ b/tests/src/python/test_qgslayertreeview.py @@ -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)