Skip to content
Permalink
Browse files

If no project layer order set, use layer tree layer order

for QgsMapThemeCollection::masterVisibleLayers()
  • Loading branch information
nyalldawson committed Mar 13, 2017
1 parent 0d0a81b commit 2cba52846eb307460180efb4fff18aff9de10b89
@@ -12,11 +12,8 @@
*/
namespace QgsLayerTree
{
//! Check whether the node is a valid group node
bool isGroup( QgsLayerTreeNode* node );

//! Check whether the node is a valid layer node
bool isLayer( QgsLayerTreeNode* node );
bool isGroup( QgsLayerTreeNode *node );
bool isLayer( const QgsLayerTreeNode *node );

//! Cast node to a group. No type checking is done - use isGroup() to find out whether this operation is legal.
// PYTHON: automatic cast
@@ -70,7 +70,7 @@ class QgsLayerTreeNode : QObject
~QgsLayerTreeNode();

//! Find out about type of the node. It is usually shorter to use convenience functions from QgsLayerTree namespace for that
NodeType nodeType();
NodeType nodeType() const;
//! Get pointer to the parent. If parent is a null pointer, the node is a root node
QgsLayerTreeNode *parent();
//! Get list of children of the node. Children are owned by the parent
@@ -132,8 +132,7 @@ class QgsLayerTreeNode : QObject
//! Return whether this node is unchecked and all its children.
//! @note added in 3.0
bool isItemVisibilityUncheckedRecursive() const;

//! Return whether the node should be shown as expanded or collapsed in GUI
QList< QgsMapLayer * > checkedLayers() const;
bool isExpanded() const;
//! Set whether the node should be shown as expanded or collapsed in GUI
void setExpanded( bool expanded );
@@ -37,7 +37,7 @@ namespace QgsLayerTree
}

//! Check whether the node is a valid layer node
inline bool isLayer( QgsLayerTreeNode *node )
inline bool isLayer( const QgsLayerTreeNode *node )
{
return node && node->nodeType() == QgsLayerTreeNode::NodeLayer;
}
@@ -54,6 +54,12 @@ namespace QgsLayerTree
return static_cast<QgsLayerTreeLayer *>( node );
}

//! Cast node to a layer. No type checking is done - use isLayer() to find out whether this operation is legal.
inline const QgsLayerTreeLayer *toLayer( const QgsLayerTreeNode *node )
{
return static_cast< const QgsLayerTreeLayer *>( node );
}

}

#endif // QGSLAYERTREE_H
@@ -126,6 +126,26 @@ bool QgsLayerTreeNode::isItemVisibilityUncheckedRecursive() const
return true;
}

void fetchCheckedLayers( const QgsLayerTreeNode *node, QList<QgsMapLayer *> &layers )
{
if ( QgsLayerTree::isLayer( node ) )
{
const QgsLayerTreeLayer *nodeLayer = QgsLayerTree::toLayer( node );
if ( nodeLayer->isVisible() )
layers << nodeLayer->layer();
}

Q_FOREACH ( QgsLayerTreeNode *child, node->children() )
fetchCheckedLayers( child, layers );
}

QList<QgsMapLayer *> QgsLayerTreeNode::checkedLayers() const
{
QList<QgsMapLayer *> layers;
fetchCheckedLayers( this, layers );
return layers;
}

void QgsLayerTreeNode::setExpanded( bool expanded )
{
if ( mExpanded == expanded )
@@ -24,6 +24,7 @@
class QDomElement;

class QgsProject;
class QgsMapLayer;

/** \ingroup core
* This class is a base class for nodes in a layer tree.
@@ -80,7 +81,7 @@ class CORE_EXPORT QgsLayerTreeNode : public QObject
~QgsLayerTreeNode();

//! Find out about type of the node. It is usually shorter to use convenience functions from QgsLayerTree namespace for that
NodeType nodeType() { return mNodeType; }
NodeType nodeType() const { return mNodeType; }
//! Get pointer to the parent. If parent is a null pointer, the node is a root node
QgsLayerTreeNode *parent() { return mParent; }
//! Get list of children of the node. Children are owned by the parent
@@ -145,6 +146,13 @@ class CORE_EXPORT QgsLayerTreeNode : public QObject
//! @note added in 3.0
bool isItemVisibilityUncheckedRecursive() const;

/**
* Returns a list of any checked layers which belong to this node or its
* children.
* @note added in QGIS 3.0
*/
QList< QgsMapLayer * > checkedLayers() const;

//! Return whether the node should be shown as expanded or collapsed in GUI
bool isExpanded() const;
//! Set whether the node should be shown as expanded or collapsed in GUI
@@ -185,17 +185,25 @@ QList<QgsMapLayer *> QgsMapThemeCollection::masterLayerOrder() const

QList<QgsMapLayer *> QgsMapThemeCollection::masterVisibleLayers() const
{
QList< QgsMapLayer * > visible;
Q_FOREACH ( QgsMapLayer *layer, masterLayerOrder() )
QList< QgsMapLayer *> allLayers = masterLayerOrder();
QList< QgsMapLayer * > visibleLayers = mProject->layerTreeRoot()->checkedLayers();

if ( allLayers.isEmpty() )
{
// no project layer order set
return visibleLayers;
}

else
{
QgsLayerTreeLayer *nodeLayer = mProject->layerTreeRoot()->findLayer( layer );
if ( nodeLayer )
QList< QgsMapLayer * > orderedVisibleLayers;
Q_FOREACH ( QgsMapLayer *layer, allLayers )
{
if ( nodeLayer->isVisible() )
visible << layer;
if ( visibleLayers.contains( layer ) )
orderedVisibleLayers << layer;
}
return orderedVisibleLayers;
}
return visible;
}


@@ -204,6 +204,11 @@ def testMasterVisibleLayers(self):
prj.setLayerOrder([layer, layer2, layer3])
self.assertEqual(prj.mapThemeCollection().masterVisibleLayers(), [layer, layer3])

# test with no project layer order set, should respect tree order
prj.setLayerOrder([])
self.assertEqual(prj.mapThemeCollection().masterVisibleLayers(), [layer, layer3])
layer_node.setItemVisibilityChecked(True)
self.assertEqual(prj.mapThemeCollection().masterVisibleLayers(), [layer, layer2, layer3])

if __name__ == '__main__':
unittest.main()

0 comments on commit 2cba528

Please sign in to comment.
You can’t perform that action at this time.