28 , mCustomLayerOrder( other.mCustomLayerOrder )
29 , mHasCustomLayerOrder( other.mHasCustomLayerOrder )
37 return _qgis_listQPointerToRaw( mCustomLayerOrder );
44 if ( newOrder == mCustomLayerOrder )
47 mCustomLayerOrder = newOrder;
50 if ( mHasCustomLayerOrder )
56 QList<QgsMapLayer *> layers;
58 for (
const auto &layerId : customLayerOrder )
69 layers.append( layer );
77 if ( mHasCustomLayerOrder )
83 QList<QgsMapLayer *> layers;
84 const QList< QgsLayerTreeLayer * > foundLayers =
findLayers();
85 for (
const auto &treeLayer : foundLayers )
92 layers.append( layer );
100 return mHasCustomLayerOrder;
105 if ( hasCustomLayerOrder == mHasCustomLayerOrder )
127 QDomDocument doc = parentElement.ownerDocument();
128 QDomElement elem = doc.createElement( QStringLiteral(
"layer-tree-group" ) );
135 QDomElement customOrderElem = doc.createElement( QStringLiteral(
"custom-order" ) );
136 customOrderElem.setAttribute( QStringLiteral(
"enabled" ), mHasCustomLayerOrder ? 1 : 0 );
137 elem.appendChild( customOrderElem );
139 Q_FOREACH (
QgsMapLayer *layer, mCustomLayerOrder )
145 QDomElement layerElem = doc.createElement( QStringLiteral(
"item" ) );
146 layerElem.appendChild( doc.createTextNode( layer->
id() ) );
147 customOrderElem.appendChild( layerElem );
150 elem.appendChild( customOrderElem );
152 parentElement.appendChild( elem );
167 void QgsLayerTree::nodeAddedChildren(
QgsLayerTreeNode *node,
int indexFrom,
int indexTo )
172 QList<QgsMapLayer *> layers;
175 for (
int i = indexFrom; i <= indexTo; ++i )
185 layers << nodeL->
layer();
191 if ( !mCustomLayerOrder.contains( layer ) && layer )
192 mCustomLayerOrder.append( layer );
199 void QgsLayerTree::nodeRemovedChildren()
202 auto layer = layers.begin();
204 while ( layer != layers.end() )
207 layer = layers.erase( layer );
218 const bool emitSignal = _qgis_listRawToQPointer( layers ) == mCustomLayerOrder;
227 void QgsLayerTree::addMissingLayers()
229 bool changed =
false;
231 const QList< QgsLayerTreeLayer * > foundLayers =
findLayers();
232 for (
const auto layer : foundLayers )
234 if ( !mCustomLayerOrder.contains( layer->layer() ) &&
235 layer->layer() && layer->layer()->isSpatial() )
237 mCustomLayerOrder.append( layer->layer() );
245 if ( mHasCustomLayerOrder )
254 QDomElement customOrderElem = elem.firstChildElement( QStringLiteral(
"custom-order" ) );
255 if ( !customOrderElem.isNull() )
259 QDomElement itemElem = customOrderElem.firstChildElement( QStringLiteral(
"item" ) );
260 while ( !itemElem.isNull() )
262 order.append( itemElem.text() );
263 itemElem = itemElem.nextSiblingElement( QStringLiteral(
"item" ) );
The class is used as a container of context for various read/write operations on other objects...
void readChildrenFromXml(QDomElement &element, const QgsReadWriteContext &context)
Read children from XML and append them to the group.
static QgsLayerTreeLayer * toLayer(QgsLayerTreeNode *node)
Cast node to a layer.
Base class for all map layer types.
static bool isGroup(QgsLayerTreeNode *node)
Check whether the node is a valid group node.
virtual void writeXml(QDomElement &parentElement, const QgsReadWriteContext &context)=0
Write layer tree to XML.
QgsMapLayer * layer() const
Returns the map layer associated with this node.
void removeAllChildren()
Remove all child nodes.
static QgsLayerTreeGroup * toGroup(QgsLayerTreeNode *node)
Cast node to a group.
void layerOrderChanged()
Emitted when the layer order has changed.
void customLayerOrderChanged()
Emitted when the custom layer order has changed.
void removedChildren(QgsLayerTreeNode *node, int indexFrom, int indexTo)
Emitted when one or more nodes has been removed from a node within the tree.
void setCustomLayerOrder(const QList< QgsMapLayer * > &customLayerOrder)
The order in which layers will be rendered on the canvas.
QList< QgsLayerTreeNode * > children()
Gets list of children of the node. Children are owned by the parent.
Namespace with helper functions for layer tree operations.
void addedChildren(QgsLayerTreeNode *node, int indexFrom, int indexTo)
Emitted when one or more nodes have been added to a node within the tree.
QList< QgsLayerTreeLayer * > findLayers() const
Find all layer nodes.
QList< QgsMapLayer * > layerOrder() const
The order in which layers will be rendered on the canvas.
void writeCommonXml(QDomElement &element)
Write common XML elements.
void setHasCustomLayerOrder(bool hasCustomLayerOrder)
Determines if the layer order should be derived from the layer tree or if a custom override order sha...
void clear()
Clear any information from this layer tree.
static bool isLayer(const QgsLayerTreeNode *node)
Check whether the node is a valid layer node.
This class is a base class for nodes in a layer tree.
QString id() const
Returns the layer's unique ID, which is used to access this layer from QgsProject.
bool hasCustomLayerOrder() const
Determines if the layer order should be derived from the layer tree or if a custom override order sha...
void readLayerOrderFromXml(const QDomElement &doc)
Load the layer order from an XML element.
QList< QgsMapLayer * > customLayerOrder() const
The order in which layers will be rendered on the canvas.
void hasCustomLayerOrderChanged(bool hasCustomLayerOrder)
Emitted when the hasCustomLayerOrder flag changes.
QgsLayerTreeGroup(const QString &name=QString(), bool checked=true)
Constructor.
void writeXml(QDomElement &parentElement, const QgsReadWriteContext &context) override
Write layer tree to XML.
QList< QgsWeakMapLayerPointer > QgsWeakMapLayerPointerList
A list of weak pointers to QgsMapLayers.
static QgsLayerTree * readXml(QDomElement &element, const QgsReadWriteContext &context)
Load the layer tree from an XML element.
void readCommonXml(QDomElement &element)
Read common XML elements.
QgsLayerTree()
Create a new empty layer tree.
QList< QgsLayerTreeNode * > mChildren
list of children - node is responsible for their deletion
QgsLayerTreeLayer * findLayer(QgsMapLayer *layer) const
Find layer node representing the map layer.
virtual bool isSpatial() const
Returns true if the layer is considered a spatial layer, ie it has some form of geometry associated w...
QgsLayerTree * clone() const override
Create a copy of the node. Returns new instance.
Layer tree node points to a map layer.