Skip to content

Commit aacb1bd

Browse files
committedJan 22, 2015
Save and restore expanded state of legend nodes in tree hierarchy
1 parent 3d539c8 commit aacb1bd

File tree

3 files changed

+39
-6
lines changed

3 files changed

+39
-6
lines changed
 

‎python/core/layertree/qgslayertreenode.sip

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@
3131
* - "showFeatureCount" - whether to show feature counts in layer tree (vector only)
3232
* - "embedded" - whether the node comes from an external project
3333
* - "embedded_project" - path to the external project (embedded root node only)
34+
* - "legend/..." - properties for legend appearance customization
35+
* - "expandedLegendNodes" - list of layer's legend nodes' rules in expanded state
3436
*
3537
* @see also QgsLayerTree, QgsLayerTreeLayer, QgsLayerTreeGroup
3638
* @note added in 2.4

‎src/core/layertree/qgslayertreenode.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ class QDomElement;
5656
* - "showFeatureCount" - whether to show feature counts in layer tree (vector only)
5757
* - "embedded" - whether the node comes from an external project
5858
* - "embedded_project" - path to the external project (embedded root node only)
59+
* - "legend/..." - properties for legend appearance customization
60+
* - "expandedLegendNodes" - list of layer's legend nodes' rules in expanded state
5961
*
6062
* @see also QgsLayerTree, QgsLayerTreeLayer, QgsLayerTreeGroup
6163
* @note added in 2.4

‎src/gui/layertree/qgslayertreeview.cpp

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,20 @@ void QgsLayerTreeView::modelRowsInserted( QModelIndex index, int start, int end
129129
return;
130130

131131
if ( QgsLayerTree::isLayer( parentNode ) )
132-
return; // layers have only symbology nodes (no expanded/collapsed handling)
132+
{
133+
// if ShowLegendAsTree flag is enabled in model, we may need to expand some legend nodes
134+
QStringList expandedNodeKeys = parentNode->customProperty( "expandedLegendNodes" ).toStringList();
135+
if ( expandedNodeKeys.isEmpty() )
136+
return;
137+
138+
foreach ( QgsLayerTreeModelLegendNode* legendNode, layerTreeModel()->layerLegendNodes( QgsLayerTree::toLayer( parentNode ) ) )
139+
{
140+
QString ruleKey = legendNode->data( QgsLayerTreeModelLegendNode::RuleKeyRole ).toString();
141+
if ( expandedNodeKeys.contains( ruleKey ) )
142+
setExpanded( layerTreeModel()->legendNode2index( legendNode ), true );
143+
}
144+
return;
145+
}
133146

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

150163
void QgsLayerTreeView::updateExpandedStateToNode( QModelIndex index )
151164
{
152-
QgsLayerTreeNode* node = layerTreeModel()->index2node( index );
153-
if ( !node )
154-
return;
155-
156-
node->setExpanded( isExpanded( index ) );
165+
if ( QgsLayerTreeNode* node = layerTreeModel()->index2node( index ) )
166+
{
167+
node->setExpanded( isExpanded( index ) );
168+
}
169+
else if ( QgsLayerTreeModelLegendNode* node = layerTreeModel()->index2legendNode( index ) )
170+
{
171+
QString ruleKey = node->data( QgsLayerTreeModelLegendNode::RuleKeyRole ).toString();
172+
QStringList lst = node->layerNode()->customProperty( "expandedLegendNodes" ).toStringList();
173+
bool expanded = isExpanded( index );
174+
bool isInList = lst.contains( ruleKey );
175+
if ( expanded && !isInList )
176+
{
177+
lst.append( ruleKey );
178+
node->layerNode()->setCustomProperty( "expandedLegendNodes", lst );
179+
}
180+
else if ( !expanded && isInList )
181+
{
182+
lst.removeAll( ruleKey );
183+
node->layerNode()->setCustomProperty( "expandedLegendNodes", lst );
184+
}
185+
}
157186
}
158187

159188
void QgsLayerTreeView::onCurrentChanged()

0 commit comments

Comments
 (0)
Please sign in to comment.