Skip to content

Commit

Permalink
Save and restore expanded state of legend nodes in tree hierarchy
Browse files Browse the repository at this point in the history
  • Loading branch information
wonder-sk committed Jan 22, 2015
1 parent 3d539c8 commit aacb1bd
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 6 deletions.
2 changes: 2 additions & 0 deletions python/core/layertree/qgslayertreenode.sip
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
* - "showFeatureCount" - whether to show feature counts in layer tree (vector only)
* - "embedded" - whether the node comes from an external project
* - "embedded_project" - path to the external project (embedded root node only)
* - "legend/..." - properties for legend appearance customization
* - "expandedLegendNodes" - list of layer's legend nodes' rules in expanded state
*
* @see also QgsLayerTree, QgsLayerTreeLayer, QgsLayerTreeGroup
* @note added in 2.4
Expand Down
2 changes: 2 additions & 0 deletions src/core/layertree/qgslayertreenode.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ class QDomElement;
* - "showFeatureCount" - whether to show feature counts in layer tree (vector only)
* - "embedded" - whether the node comes from an external project
* - "embedded_project" - path to the external project (embedded root node only)
* - "legend/..." - properties for legend appearance customization
* - "expandedLegendNodes" - list of layer's legend nodes' rules in expanded state
*
* @see also QgsLayerTree, QgsLayerTreeLayer, QgsLayerTreeGroup
* @note added in 2.4
Expand Down
41 changes: 35 additions & 6 deletions src/gui/layertree/qgslayertreeview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,20 @@ void QgsLayerTreeView::modelRowsInserted( QModelIndex index, int start, int end
return;

if ( QgsLayerTree::isLayer( parentNode ) )
return; // layers have only symbology nodes (no expanded/collapsed handling)
{
// if ShowLegendAsTree flag is enabled in model, we may need to expand some legend nodes
QStringList expandedNodeKeys = parentNode->customProperty( "expandedLegendNodes" ).toStringList();
if ( expandedNodeKeys.isEmpty() )
return;

foreach ( QgsLayerTreeModelLegendNode* legendNode, layerTreeModel()->layerLegendNodes( QgsLayerTree::toLayer( parentNode ) ) )
{
QString ruleKey = legendNode->data( QgsLayerTreeModelLegendNode::RuleKeyRole ).toString();
if ( expandedNodeKeys.contains( ruleKey ) )
setExpanded( layerTreeModel()->legendNode2index( legendNode ), true );
}
return;
}

QList<QgsLayerTreeNode*> children = parentNode->children();
for ( int i = start; i <= end; ++i )
Expand All @@ -149,11 +162,27 @@ void QgsLayerTreeView::modelRowsRemoved()

void QgsLayerTreeView::updateExpandedStateToNode( QModelIndex index )
{
QgsLayerTreeNode* node = layerTreeModel()->index2node( index );
if ( !node )
return;

node->setExpanded( isExpanded( index ) );
if ( QgsLayerTreeNode* node = layerTreeModel()->index2node( index ) )
{
node->setExpanded( isExpanded( index ) );
}
else if ( QgsLayerTreeModelLegendNode* node = layerTreeModel()->index2legendNode( index ) )
{
QString ruleKey = node->data( QgsLayerTreeModelLegendNode::RuleKeyRole ).toString();
QStringList lst = node->layerNode()->customProperty( "expandedLegendNodes" ).toStringList();
bool expanded = isExpanded( index );
bool isInList = lst.contains( ruleKey );
if ( expanded && !isInList )
{
lst.append( ruleKey );
node->layerNode()->setCustomProperty( "expandedLegendNodes", lst );
}
else if ( !expanded && isInList )
{
lst.removeAll( ruleKey );
node->layerNode()->setCustomProperty( "expandedLegendNodes", lst );
}
}
}

void QgsLayerTreeView::onCurrentChanged()
Expand Down

0 comments on commit aacb1bd

Please sign in to comment.