QGIS API Documentation  3.16.0-Hannover (43b64b13f3)
qgslayertree.h
Go to the documentation of this file.
1 /***************************************************************************
2  qgslayertree.h
3  --------------------------------------
4  Date : May 2014
5  Copyright : (C) 2014 by Martin Dobias
6  Email : wonder dot sk at gmail dot com
7  ***************************************************************************
8  * *
9  * This program is free software; you can redistribute it and/or modify *
10  * it under the terms of the GNU General Public License as published by *
11  * the Free Software Foundation; either version 2 of the License, or *
12  * (at your option) any later version. *
13  * *
14  ***************************************************************************/
15 
16 #ifndef QGSLAYERTREE_H
17 #define QGSLAYERTREE_H
18 
19 #include "qgslayertreenode.h"
20 #include "qgslayertreegroup.h"
21 #include "qgslayertreelayer.h"
22 
32 class CORE_EXPORT QgsLayerTree : public QgsLayerTreeGroup
33 {
34  Q_OBJECT
35 
36  public:
37 
43  static inline bool isGroup( QgsLayerTreeNode *node )
44  {
45  return node && node->nodeType() == QgsLayerTreeNode::NodeGroup;
46  }
47 
53  static inline bool isLayer( const QgsLayerTreeNode *node )
54  {
55  return node && node->nodeType() == QgsLayerTreeNode::NodeLayer;
56  }
57 
65  {
66  return static_cast<QgsLayerTreeGroup *>( node );
67  }
68 
76  {
77  return static_cast<QgsLayerTreeLayer *>( node );
78  }
79 
86  static inline const QgsLayerTreeLayer *toLayer( const QgsLayerTreeNode *node ) SIP_SKIP
87  {
88  return static_cast< const QgsLayerTreeLayer *>( node );
89  }
90 
94  QgsLayerTree();
95 
107  QList<QgsMapLayer *> customLayerOrder() const;
108 
120  void setCustomLayerOrder( const QList<QgsMapLayer *> &customLayerOrder );
121 
133  void setCustomLayerOrder( const QStringList &customLayerOrder ) SIP_PYNAME( setCustomLayerOrderByIds );
134 
145  QList<QgsMapLayer *> layerOrder() const;
146 
155  bool hasCustomLayerOrder() const;
156 
165  void setHasCustomLayerOrder( bool hasCustomLayerOrder );
166 
175  static QgsLayerTree *readXml( QDomElement &element, const QgsReadWriteContext &context );
176 
183  void readLayerOrderFromXml( const QDomElement &doc );
184 
185  void writeXml( QDomElement &parentElement, const QgsReadWriteContext &context ) override;
186 
187  QgsLayerTree *clone() const override SIP_FACTORY;
188 
194  void clear();
195 
196  signals:
197 
203  void customLayerOrderChanged();
204 
210  void layerOrderChanged();
211 
219  void hasCustomLayerOrderChanged( bool hasCustomLayerOrder );
220 
221  private slots:
222  void nodeAddedChildren( QgsLayerTreeNode *node, int indexFrom, int indexTo );
223  void nodeRemovedChildren();
224 
225  private:
227  QgsLayerTree( const QgsLayerTree &other );
228  void addMissingLayers();
229  QgsWeakMapLayerPointerList mCustomLayerOrder;
230  bool mHasCustomLayerOrder = false;
231 
232  QgsLayerTree &operator= ( const QgsLayerTree & ) = delete;
233 };
234 
235 #endif // QGSLAYERTREE_H
QgsLayerTreeNode::NodeGroup
@ NodeGroup
Container of other groups and layers.
Definition: qgslayertreenode.h:101
SIP_PYNAME
#define SIP_PYNAME(name)
Definition: qgis_sip.h:81
QgsLayerTreeNode
This class is a base class for nodes in a layer tree.
Definition: qgslayertreenode.h:75
QgsWeakMapLayerPointerList
QList< QgsWeakMapLayerPointer > QgsWeakMapLayerPointerList
A list of weak pointers to QgsMapLayers.
Definition: qgsmaplayer.h:1695
QgsReadWriteContext
The class is used as a container of context for various read/write operations on other objects.
Definition: qgsreadwritecontext.h:35
QgsLayerTreeGroup::writeXml
void writeXml(QDomElement &parentElement, const QgsReadWriteContext &context) override
Write group (tree) as XML element <layer-tree-group> and add it to the given parent element.
Definition: qgslayertreegroup.cpp:299
QgsLayerTreeNode::NodeLayer
@ NodeLayer
Leaf node pointing to a layer.
Definition: qgslayertreenode.h:102
qgslayertreelayer.h
QgsLayerTree::toLayer
static QgsLayerTreeLayer * toLayer(QgsLayerTreeNode *node)
Cast node to a layer.
Definition: qgslayertree.h:75
QgsLayerTree::toGroup
static QgsLayerTreeGroup * toGroup(QgsLayerTreeNode *node)
Cast node to a group.
Definition: qgslayertree.h:64
SIP_FACTORY
#define SIP_FACTORY
Definition: qgis_sip.h:76
QgsLayerTreeNode::nodeType
NodeType nodeType() const
Find out about type of the node. It is usually shorter to use convenience functions from QgsLayerTree...
Definition: qgslayertreenode.h:108
QgsLayerTree::toLayer
static const QgsLayerTreeLayer * toLayer(const QgsLayerTreeNode *node)
Cast node to a layer.
Definition: qgslayertree.h:86
SIP_SKIP
#define SIP_SKIP
Definition: qgis_sip.h:126
QgsLayerTree
Namespace with helper functions for layer tree operations.
Definition: qgslayertree.h:33
qgslayertreegroup.h
QgsLayerTreeLayer
Layer tree node points to a map layer.
Definition: qgslayertreelayer.h:44
QgsLayerTreeGroup
Layer tree group node serves as a container for layers and further groups.
Definition: qgslayertreegroup.h:35
QgsLayerTreeGroup::readXml
static QgsLayerTreeGroup * readXml(QDomElement &element, const QgsReadWriteContext &context)
Read group (tree) from XML element <layer-tree-group> and return the newly created group (or nullptr ...
Definition: qgslayertreegroup.cpp:268
QgsLayerTree::isLayer
static bool isLayer(const QgsLayerTreeNode *node)
Check whether the node is a valid layer node.
Definition: qgslayertree.h:53
qgslayertreenode.h
QgsLayerTreeGroup::clone
QgsLayerTreeGroup * clone() const override
Returns a clone of the group.
Definition: qgslayertreegroup.cpp:347
QgsLayerTree::isGroup
static bool isGroup(QgsLayerTreeNode *node)
Check whether the node is a valid group node.
Definition: qgslayertree.h:43