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

for QgsMapThemeCollection::masterVisibleLayers()
This commit is contained in:
Nyall Dawson 2017-03-13 14:42:22 +10:00
parent 0d0a81b00d
commit 2cba52846e
7 changed files with 62 additions and 19 deletions

View File

@ -12,11 +12,8 @@
*/ */
namespace QgsLayerTree namespace QgsLayerTree
{ {
//! Check whether the node is a valid group node bool isGroup( QgsLayerTreeNode *node );
bool isGroup( QgsLayerTreeNode* node ); bool isLayer( const QgsLayerTreeNode *node );
//! Check whether the node is a valid layer node
bool isLayer( QgsLayerTreeNode* node );
//! Cast node to a group. No type checking is done - use isGroup() to find out whether this operation is legal. //! Cast node to a group. No type checking is done - use isGroup() to find out whether this operation is legal.
// PYTHON: automatic cast // PYTHON: automatic cast

View File

@ -70,7 +70,7 @@ class QgsLayerTreeNode : QObject
~QgsLayerTreeNode(); ~QgsLayerTreeNode();
//! Find out about type of the node. It is usually shorter to use convenience functions from QgsLayerTree namespace for that //! 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 //! Get pointer to the parent. If parent is a null pointer, the node is a root node
QgsLayerTreeNode *parent(); QgsLayerTreeNode *parent();
//! Get list of children of the node. Children are owned by the 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. //! Return whether this node is unchecked and all its children.
//! @note added in 3.0 //! @note added in 3.0
bool isItemVisibilityUncheckedRecursive() const; bool isItemVisibilityUncheckedRecursive() const;
QList< QgsMapLayer * > checkedLayers() const;
//! Return whether the node should be shown as expanded or collapsed in GUI
bool isExpanded() const; bool isExpanded() const;
//! Set whether the node should be shown as expanded or collapsed in GUI //! Set whether the node should be shown as expanded or collapsed in GUI
void setExpanded( bool expanded ); void setExpanded( bool expanded );

View File

@ -37,7 +37,7 @@ namespace QgsLayerTree
} }
//! Check whether the node is a valid layer node //! 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; return node && node->nodeType() == QgsLayerTreeNode::NodeLayer;
} }
@ -54,6 +54,12 @@ namespace QgsLayerTree
return static_cast<QgsLayerTreeLayer *>( node ); 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 #endif // QGSLAYERTREE_H

View File

@ -126,6 +126,26 @@ bool QgsLayerTreeNode::isItemVisibilityUncheckedRecursive() const
return true; 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 ) void QgsLayerTreeNode::setExpanded( bool expanded )
{ {
if ( mExpanded == expanded ) if ( mExpanded == expanded )

View File

@ -24,6 +24,7 @@
class QDomElement; class QDomElement;
class QgsProject; class QgsProject;
class QgsMapLayer;
/** \ingroup core /** \ingroup core
* This class is a base class for nodes in a layer tree. * This class is a base class for nodes in a layer tree.
@ -80,7 +81,7 @@ class CORE_EXPORT QgsLayerTreeNode : public QObject
~QgsLayerTreeNode(); ~QgsLayerTreeNode();
//! Find out about type of the node. It is usually shorter to use convenience functions from QgsLayerTree namespace for that //! 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 //! Get pointer to the parent. If parent is a null pointer, the node is a root node
QgsLayerTreeNode *parent() { return mParent; } QgsLayerTreeNode *parent() { return mParent; }
//! Get list of children of the node. Children are owned by the parent //! 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 //! @note added in 3.0
bool isItemVisibilityUncheckedRecursive() const; 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 //! Return whether the node should be shown as expanded or collapsed in GUI
bool isExpanded() const; bool isExpanded() const;
//! Set whether the node should be shown as expanded or collapsed in GUI //! Set whether the node should be shown as expanded or collapsed in GUI

View File

@ -185,17 +185,25 @@ QList<QgsMapLayer *> QgsMapThemeCollection::masterLayerOrder() const
QList<QgsMapLayer *> QgsMapThemeCollection::masterVisibleLayers() const QList<QgsMapLayer *> QgsMapThemeCollection::masterVisibleLayers() const
{ {
QList< QgsMapLayer * > visible; QList< QgsMapLayer *> allLayers = masterLayerOrder();
Q_FOREACH ( QgsMapLayer *layer, masterLayerOrder() ) QList< QgsMapLayer * > visibleLayers = mProject->layerTreeRoot()->checkedLayers();
if ( allLayers.isEmpty() )
{ {
QgsLayerTreeLayer *nodeLayer = mProject->layerTreeRoot()->findLayer( layer ); // no project layer order set
if ( nodeLayer ) return visibleLayers;
}
else
{ {
if ( nodeLayer->isVisible() ) QList< QgsMapLayer * > orderedVisibleLayers;
visible << layer; Q_FOREACH ( QgsMapLayer *layer, allLayers )
{
if ( visibleLayers.contains( layer ) )
orderedVisibleLayers << layer;
} }
return orderedVisibleLayers;
} }
return visible;
} }

View File

@ -204,6 +204,11 @@ class TestQgsMapThemeCollection(unittest.TestCase):
prj.setLayerOrder([layer, layer2, layer3]) prj.setLayerOrder([layer, layer2, layer3])
self.assertEqual(prj.mapThemeCollection().masterVisibleLayers(), [layer, 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__': if __name__ == '__main__':
unittest.main() unittest.main()