Fix layer tree expanded state when used expand/collapse all (fixes #15691)

(cherry picked from commit de85fdd6e8fa3d4f38196376aabccce317cbf341)
This commit is contained in:
Martin Dobias 2016-10-14 11:17:36 +08:00
parent 24fbe1a080
commit a3186f80bc
4 changed files with 64 additions and 2 deletions

View File

@ -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 );

View File

@ -3104,11 +3104,11 @@ void QgisApp::initLayerTreeView()
QAction* actionExpandAll = new QAction( tr( "Expand All" ), this );
actionExpandAll->setIcon( QgsApplication::getThemeIcon( QStringLiteral( "/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( QStringLiteral( "/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 );

View File

@ -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();
}

View File

@ -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 );