Skip to content
Permalink
Browse files

Fix layer tree expanded state when used expand/collapse all (fixes #1…

  • Loading branch information
wonder-sk committed Oct 14, 2016
1 parent f124107 commit de85fdd6e8fa3d4f38196376aabccce317cbf341
@@ -72,6 +72,14 @@ class QgsLayerTreeView : QTreeView
//! Force refresh of layer symbology. Normally not needed as the changes of layer's renderer are monitored by the model
void refreshLayerSymbology( const QString& layerId );

//! Enhancement of QTreeView::expandAll() that also records expanded state in layer tree nodes
//! @note added in QGIS 2.18
void expandAllNodes();

//! Enhancement of QTreeView::collapseAll() that also records expanded state in layer tree nodes
//! @note added in QGIS 2.18
void collapseAllNodes();

signals:
//! Emitted when a current layer is changed
void currentLayerChanged( QgsMapLayer* layer );
@@ -3037,11 +3037,11 @@ void QgisApp::initLayerTreeView()
QAction* actionExpandAll = new QAction( tr( "Expand All" ), this );
actionExpandAll->setIcon( QgsApplication::getThemeIcon( "/mActionExpandTree.svg" ) );
actionExpandAll->setToolTip( tr( "Expand All" ) );
connect( actionExpandAll, SIGNAL( triggered( bool ) ), mLayerTreeView, SLOT( expandAll() ) );
connect( actionExpandAll, SIGNAL( triggered( bool ) ), mLayerTreeView, SLOT( expandAllNodes() ) );
QAction* actionCollapseAll = new QAction( tr( "Collapse All" ), this );
actionCollapseAll->setIcon( QgsApplication::getThemeIcon( "/mActionCollapseTree.svg" ) );
actionCollapseAll->setToolTip( tr( "Collapse All" ) );
connect( actionCollapseAll, SIGNAL( triggered( bool ) ), mLayerTreeView, SLOT( collapseAll() ) );
connect( actionCollapseAll, SIGNAL( triggered( bool ) ), mLayerTreeView, SLOT( collapseAllNodes() ) );

QWidget* spacer = new QWidget();
spacer->setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Expanding );
@@ -336,3 +336,49 @@ void QgsLayerTreeView::refreshLayerSymbology( const QString& layerId )
if ( nodeLayer )
layerTreeModel()->refreshLayerLegend( nodeLayer );
}


static void _expandAllLegendNodes( QgsLayerTreeLayer* nodeLayer, bool expanded, QgsLayerTreeModel* model )
{
// for layers we also need to find out with legend nodes contain some children and make them expanded/collapsed
// if we are collapsing, we just write out an empty list
QStringList lst;
if ( expanded )
{
Q_FOREACH ( QgsLayerTreeModelLegendNode* legendNode, model->layerLegendNodes( nodeLayer ) )
{
QString parentKey = legendNode->data( QgsLayerTreeModelLegendNode::ParentRuleKeyRole ).toString();
if ( !parentKey.isEmpty() && !lst.contains( parentKey ) )
lst << parentKey;
}
}
nodeLayer->setCustomProperty( "expandedLegendNodes", lst );
}


static void _expandAllNodes( QgsLayerTreeGroup* parent, bool expanded, QgsLayerTreeModel* model )
{
Q_FOREACH ( QgsLayerTreeNode* node, parent->children() )
{
node->setExpanded( expanded );
if ( QgsLayerTree::isGroup( node ) )
_expandAllNodes( QgsLayerTree::toGroup( node ), expanded, model );
else if ( QgsLayerTree::isLayer( node ) )
_expandAllLegendNodes( QgsLayerTree::toLayer( node ), expanded, model );
}
}


void QgsLayerTreeView::expandAllNodes()
{
// unfortunately expandAll() does not emit expanded() signals
_expandAllNodes( layerTreeModel()->rootGroup(), true, layerTreeModel() );
expandAll();
}

void QgsLayerTreeView::collapseAllNodes()
{
// unfortunately collapseAll() does not emit collapsed() signals
_expandAllNodes( layerTreeModel()->rootGroup(), false, layerTreeModel() );
collapseAll();
}
@@ -92,6 +92,14 @@ class GUI_EXPORT QgsLayerTreeView : public QTreeView
//! Force refresh of layer symbology. Normally not needed as the changes of layer's renderer are monitored by the model
void refreshLayerSymbology( const QString& layerId );

//! Enhancement of QTreeView::expandAll() that also records expanded state in layer tree nodes
//! @note added in QGIS 2.18
void expandAllNodes();

//! Enhancement of QTreeView::collapseAll() that also records expanded state in layer tree nodes
//! @note added in QGIS 2.18
void collapseAllNodes();

signals:
//! Emitted when a current layer is changed
void currentLayerChanged( QgsMapLayer* layer );

0 comments on commit de85fdd

Please sign in to comment.
You can’t perform that action at this time.