Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
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 de85fdd
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 2 deletions.
8 changes: 8 additions & 0 deletions python/gui/layertree/qgslayertreeview.sip
Expand Up @@ -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 );
Expand Down
4 changes: 2 additions & 2 deletions src/app/qgisapp.cpp
Expand Up @@ -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 );
Expand Down
46 changes: 46 additions & 0 deletions src/gui/layertree/qgslayertreeview.cpp
Expand Up @@ -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();
}
8 changes: 8 additions & 0 deletions src/gui/layertree/qgslayertreeview.h
Expand Up @@ -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 );
Expand Down

0 comments on commit de85fdd

Please sign in to comment.