Skip to content

Commit

Permalink
[layertree] Fix #10449 (auto-collapse layer if symbology has >= 10 it…
Browse files Browse the repository at this point in the history
…ems)
  • Loading branch information
wonder-sk committed Jun 17, 2014
1 parent 378de25 commit eb6a45c
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 2 deletions.
5 changes: 5 additions & 0 deletions python/gui/layertree/qgslayertreemodel.sip
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,11 @@ class QgsLayerTreeModel : QAbstractItemModel
//! Get font for a particular type of layer tree node. nodeType should come from QgsLayerTreeNode::NodeType enumeration
QFont layerTreeNodeFont( int nodeType ) const;

//! Set at what number of symbology nodes the layer node should be collapsed. Setting -1 disables the auto-collapse (default).
void setAutoCollapseSymbologyNodes( int nodeCount );
//! Return at what number of symbology nodes the layer node should be collapsed. -1 means no auto-collapse (default).
int autoCollapseSymbologyNodes() const;

signals:

};
1 change: 1 addition & 0 deletions src/app/qgisapp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2300,6 +2300,7 @@ void QgisApp::initLayerTreeView()
model->setFlag( QgsLayerTreeModel::AllowNodeReorder );
model->setFlag( QgsLayerTreeModel::AllowNodeRename );
model->setFlag( QgsLayerTreeModel::AllowNodeChangeVisibility );
model->setAutoCollapseSymbologyNodes( 10 );

mLayerTreeView->setModel( model );
mLayerTreeView->setMenuProvider( new QgsAppLayerTreeViewMenuProvider( mLayerTreeView, mMapCanvas ) );
Expand Down
5 changes: 4 additions & 1 deletion src/core/layertree/qgslayertreegroup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -214,15 +214,18 @@ void QgsLayerTreeGroup::writeXML( QDomElement& parentElement )

void QgsLayerTreeGroup::readChildrenFromXML( QDomElement& element )
{
QList<QgsLayerTreeNode*> nodes;
QDomElement childElem = element.firstChildElement();
while ( !childElem.isNull() )
{
QgsLayerTreeNode* newNode = QgsLayerTreeNode::readXML( childElem );
if ( newNode )
addChildNode( newNode );
nodes << newNode;

childElem = childElem.nextSiblingElement();
}

insertChildNodes( -1, nodes );
}

QString QgsLayerTreeGroup::dump() const
Expand Down
3 changes: 3 additions & 0 deletions src/core/layertree/qgslayertreenode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,9 @@ void QgsLayerTreeNode::writeCommonXML( QDomElement& element )

void QgsLayerTreeNode::insertChildrenPrivate( int index, QList<QgsLayerTreeNode*> nodes )
{
if ( nodes.count() == 0 )
return;

foreach ( QgsLayerTreeNode* node, nodes )
{
Q_ASSERT( node->mParent == 0 );
Expand Down
1 change: 1 addition & 0 deletions src/core/layertree/qgslayertreenode.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ class QDomElement;
* file. The storage is not efficient for large amount of data.
*
* Custom properties that have already been used within QGIS:
* - "loading" - whether the project is being currently loaded (root node only)
* - "overview" - whether to show a layer in overview
* - "showFeatureCount" - whether to show feature counts in layer tree (vector only)
* - "embedded" - whether the node comes from an external project
Expand Down
4 changes: 4 additions & 0 deletions src/core/qgsproject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -903,6 +903,8 @@ bool QgsProject::read()

// read the layer tree from project file

mRootGroup->setCustomProperty( "loading", 1 );

QDomElement layerTreeElem = doc->documentElement().firstChildElement( "layer-tree-group" );
if ( !layerTreeElem.isNull() )
{
Expand Down Expand Up @@ -945,6 +947,8 @@ bool QgsProject::read()
// make sure the are just valid layers
QgsLayerTreeUtils::removeInvalidLayers( mRootGroup );

mRootGroup->removeCustomProperty( "loading" );

// read the project: used by map canvas and legend
emit readProject( *doc );

Expand Down
16 changes: 15 additions & 1 deletion src/gui/layertree/qgslayertreemodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ QgsLayerTreeModel::QgsLayerTreeModel( QgsLayerTreeGroup* rootNode, QObject *pare
: QAbstractItemModel( parent )
, mRootNode( rootNode )
, mFlags( ShowSymbology )
, mAutoCollapseSymNodesCount( -1 )
{
Q_ASSERT( mRootNode );

Expand Down Expand Up @@ -429,11 +430,17 @@ void QgsLayerTreeModel::refreshLayerSymbology( QgsLayerTreeLayer* nodeLayer )
emit dataChanged( idx, idx );

// update children
beginRemoveRows( idx, 0, rowCount( idx ) - 1 );
int oldNodeCount = rowCount( idx );
beginRemoveRows( idx, 0, oldNodeCount - 1 );
removeSymbologyFromLayer( nodeLayer );
endRemoveRows();

addSymbologyToLayer( nodeLayer );
int newNodeCount = rowCount( idx );

// automatic collapse of symbology nodes - useful if a layer has many symbology nodes
if ( mAutoCollapseSymNodesCount != -1 && oldNodeCount != newNodeCount && newNodeCount >= mAutoCollapseSymNodesCount )
nodeLayer->setExpanded( false );
}

QModelIndex QgsLayerTreeModel::currentIndex() const
Expand Down Expand Up @@ -732,6 +739,13 @@ void QgsLayerTreeModel::connectToLayer( QgsLayerTreeLayer* nodeLayer )
if ( testFlag( ShowSymbology ) )
{
addSymbologyToLayer( nodeLayer );

// automatic collapse of symbology nodes - useful if a layer has many symbology nodes
if ( !mRootNode->customProperty( "loading" ).toBool() )
{
if ( mAutoCollapseSymNodesCount != -1 && rowCount( node2index( nodeLayer ) ) >= mAutoCollapseSymNodesCount )
nodeLayer->setExpanded( false );
}
}

QgsMapLayer* layer = nodeLayer->layer();
Expand Down
7 changes: 7 additions & 0 deletions src/gui/layertree/qgslayertreemodel.h
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,11 @@ class GUI_EXPORT QgsLayerTreeModel : public QAbstractItemModel
//! Get font for a particular type of layer tree node. nodeType should come from QgsLayerTreeNode::NodeType enumeration
QFont layerTreeNodeFont( int nodeType ) const;

//! Set at what number of symbology nodes the layer node should be collapsed. Setting -1 disables the auto-collapse (default).
void setAutoCollapseSymbologyNodes( int nodeCount ) { mAutoCollapseSymNodesCount = nodeCount; }
//! Return at what number of symbology nodes the layer node should be collapsed. -1 means no auto-collapse (default).
int autoCollapseSymbologyNodes() const { return mAutoCollapseSymNodesCount; }

signals:

protected slots:
Expand Down Expand Up @@ -180,6 +185,8 @@ class GUI_EXPORT QgsLayerTreeModel : public QAbstractItemModel
QMap<QgsLayerTreeLayer*, QList<QgsLayerTreeModelSymbologyNode*> > mSymbologyNodes;
//! Current index - will be underlined
QPersistentModelIndex mCurrentIndex;
//! Minimal number of nodes when symbology should be automatically collapsed. -1 = disabled
int mAutoCollapseSymNodesCount;

QFont mFontLayer;
QFont mFontGroup;
Expand Down

0 comments on commit eb6a45c

Please sign in to comment.