17#include "moc_qgslayertree.cpp"
28 , mCustomLayerOrder( other.mCustomLayerOrder )
29 , mHasCustomLayerOrder( other.mHasCustomLayerOrder )
34void QgsLayerTree::init()
42 return _qgis_listQPointerToRaw( mCustomLayerOrder );
49 if ( newOrder == mCustomLayerOrder )
52 mCustomLayerOrder = newOrder;
55 if ( mHasCustomLayerOrder )
61 QList<QgsMapLayer *> layers;
74 layers.append( layer );
82 if ( mHasCustomLayerOrder )
94 return mHasCustomLayerOrder;
121 QDomDocument doc = parentElement.ownerDocument();
122 QDomElement elem = doc.createElement( QStringLiteral(
"layer-tree-group" ) );
127 node->writeXml( elem, context );
129 QDomElement customOrderElem = doc.createElement( QStringLiteral(
"custom-order" ) );
130 customOrderElem.setAttribute( QStringLiteral(
"enabled" ), mHasCustomLayerOrder ? 1 : 0 );
131 elem.appendChild( customOrderElem );
133 for (
QgsMapLayer *layer : std::as_const( mCustomLayerOrder ) )
139 QDomElement layerElem = doc.createElement( QStringLiteral(
"item" ) );
140 layerElem.appendChild( doc.createTextNode( layer->id() ) );
141 customOrderElem.appendChild( layerElem );
144 elem.appendChild( customOrderElem );
146 parentElement.appendChild( elem );
161void QgsLayerTree::nodeAddedChildren(
QgsLayerTreeNode *node,
int indexFrom,
int indexTo )
166 QList<QgsMapLayer *> layers;
169 for (
int i = indexFrom; i <= indexTo; ++i )
180 layers << nodeL->layer();
184 for (
QgsMapLayer *layer : std::as_const( layers ) )
186 if ( !mCustomLayerOrder.contains( layer ) && layer )
187 mCustomLayerOrder.append( layer );
194void QgsLayerTree::nodeRemovedChildren()
197 auto layer = layers.begin();
199 while ( layer != layers.end() )
202 layer = layers.erase( layer );
213 const bool emitSignal = _qgis_listRawToQPointer( layers ) == mCustomLayerOrder;
222void QgsLayerTree::addMissingLayers()
224 bool changed =
false;
226 const QList< QgsLayerTreeLayer * > layers =
findLayers();
227 for (
const auto layer : layers )
229 if ( !mCustomLayerOrder.contains( layer->layer() ) &&
230 layer->layer() && layer->layer()->isSpatial() )
232 mCustomLayerOrder.append( layer->layer() );
240 if ( mHasCustomLayerOrder )
249 QDomElement customOrderElem = elem.firstChildElement( QStringLiteral(
"custom-order" ) );
250 if ( !customOrderElem.isNull() )
254 QDomElement itemElem = customOrderElem.firstChildElement( QStringLiteral(
"item" ) );
255 while ( !itemElem.isNull() )
257 order.append( itemElem.text() );
258 itemElem = itemElem.nextSiblingElement( QStringLiteral(
"item" ) );
Layer tree group node serves as a container for layers and further groups.
QList< QgsMapLayer * > layerOrderRespectingGroupLayers() const
Returns an ordered list of map layers in the group, ignoring any layers which are child layers of Qgs...
void removeAllChildren()
Remove all child nodes.
void readChildrenFromXml(QDomElement &element, const QgsReadWriteContext &context)
Read children from XML and append them to the group.
QList< QgsLayerTreeLayer * > findLayers() const
Find all layer nodes.
QgsLayerTreeLayer * findLayer(QgsMapLayer *layer) const
Find layer node representing the map layer.
Layer tree node points to a map layer.
QgsMapLayer * layer() const
Returns the map layer associated with this node.
This class is a base class for nodes in a layer tree.
void readCommonXml(QDomElement &element)
Read common XML elements.
void removedChildren(QgsLayerTreeNode *node, int indexFrom, int indexTo)
Emitted when one or more nodes has been removed from a node within the tree.
QList< QgsLayerTreeNode * > children()
Gets list of children of the node. Children are owned by the parent.
void writeCommonXml(QDomElement &element)
Write common XML elements.
void addedChildren(QgsLayerTreeNode *node, int indexFrom, int indexTo)
Emitted when one or more nodes have been added to a node within the tree.
QList< QgsLayerTreeNode * > mChildren
list of children - node is responsible for their deletion
Namespace with helper functions for layer tree operations.
bool hasCustomLayerOrder() const
Determines if the layer order should be derived from the layer tree or if a custom override order sha...
void customLayerOrderChanged()
Emitted when the custom layer order has changed.
void readLayerOrderFromXml(const QDomElement &doc)
Load the layer order from an XML element.
static QgsLayerTreeLayer * toLayer(QgsLayerTreeNode *node)
Cast node to a layer.
void writeXml(QDomElement &parentElement, const QgsReadWriteContext &context) override
Write layer tree to XML.
QList< QgsMapLayer * > layerOrder() const
The order in which layers will be rendered on the canvas.
QgsLayerTree()
Create a new empty layer tree.
void hasCustomLayerOrderChanged(bool hasCustomLayerOrder)
Emitted when the hasCustomLayerOrder flag changes.
void clear()
Clear any information from this layer tree.
static bool isLayer(const QgsLayerTreeNode *node)
Check whether the node is a valid layer node.
void setHasCustomLayerOrder(bool hasCustomLayerOrder)
Determines if the layer order should be derived from the layer tree or if a custom override order sha...
static bool isGroup(QgsLayerTreeNode *node)
Check whether the node is a valid group node.
static QgsLayerTree * readXml(QDomElement &element, const QgsReadWriteContext &context)
Load the layer tree from an XML element.
void setCustomLayerOrder(const QList< QgsMapLayer * > &customLayerOrder)
The order in which layers will be rendered on the canvas.
static QgsLayerTreeGroup * toGroup(QgsLayerTreeNode *node)
Cast node to a group.
void layerOrderChanged()
Emitted when the layer order has changed.
QList< QgsMapLayer * > customLayerOrder() const
The order in which layers will be rendered on the canvas.
QgsLayerTree * clone() const override
Create a copy of the node. Returns new instance.
Base class for all map layer types.
virtual bool isSpatial() const
Returns true if the layer is considered a spatial layer, ie it has some form of geometry associated w...
The class is used as a container of context for various read/write operations on other objects.
QList< QgsWeakMapLayerPointer > QgsWeakMapLayerPointerList
A list of weak pointers to QgsMapLayers.