void QgsLayerTreeView::updateExpandedStateFromNode( QgsLayerTreeNode* node ) { QModelIndex idx = layerTreeModel()->node2index( node ); setExpanded( idx, node->isExpanded() ); foreach ( QgsLayerTreeNode* child, node->children() ) updateExpandedStateFromNode( child ); }
void QgsLayerTreeView::setModel( QAbstractItemModel* model ) { if ( !qobject_cast<QgsLayerTreeModel*>( model ) ) return; connect( model, SIGNAL( rowsInserted( QModelIndex, int, int ) ), this, SLOT( modelRowsInserted( QModelIndex, int, int ) ) ); connect( model, SIGNAL( rowsRemoved( QModelIndex, int, int ) ), this, SLOT( modelRowsRemoved() ) ); QTreeView::setModel( model ); connect( layerTreeModel()->rootGroup(), SIGNAL( expandedChanged( QgsLayerTreeNode*, bool ) ), this, SLOT( onExpandedChanged( QgsLayerTreeNode*, bool ) ) ); connect( selectionModel(), SIGNAL( currentChanged( QModelIndex, QModelIndex ) ), this, SLOT( onCurrentChanged() ) ); connect( layerTreeModel(), SIGNAL( modelReset() ), this, SLOT( onModelReset() ) ); updateExpandedStateFromNode( layerTreeModel()->rootGroup() ); }
void QgsLayerTreeView::modelRowsInserted( QModelIndex index, int start, int end ) { QgsLayerTreeNode* parentNode = layerTreeModel()->index2node( index ); if ( !parentNode ) return; if ( QgsLayerTree::isLayer( parentNode ) ) return; // layers have only symbology nodes (no expanded/collapsed handling) QList<QgsLayerTreeNode*> children = parentNode->children(); for ( int i = start; i <= end; ++i ) { updateExpandedStateFromNode( children[i] ); } // make sure we still have correct current layer onCurrentChanged(); }
void QgsLayerTreeView::onModelReset() { updateExpandedStateFromNode( layerTreeModel()->rootGroup() ); }