QGIS API Documentation  3.26.3-Buenos Aires (65e4edfdad)
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 
64  static inline QgsLayerTreeGroup *toGroup( QgsLayerTreeNode *node ) SIP_SKIP
65  {
66  return static_cast<QgsLayerTreeGroup *>( node );
67  }
68 
75  static inline QgsLayerTreeLayer *toLayer( QgsLayerTreeNode *node ) SIP_SKIP
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 
96 #ifdef SIP_RUN
97  SIP_PYOBJECT __repr__();
98  % MethodCode
99  // override parent QgsLayerTreeGroup __repr__ and resort back to default repr for QgsLayerTree -- there's no extra useful context we can show
100  QString str = QStringLiteral( "<qgis._core.QgsLayerTree object at 0x%1>" ).arg( reinterpret_cast<quintptr>( sipCpp ), 2 * QT_POINTER_SIZE, 16, QLatin1Char( '0' ) );
101  sipRes = PyUnicode_FromString( str.toUtf8().constData() );
102  % End
103 #endif
104 
116  QList<QgsMapLayer *> customLayerOrder() const;
117 
129  void setCustomLayerOrder( const QList<QgsMapLayer *> &customLayerOrder );
130 
142  void setCustomLayerOrder( const QStringList &customLayerOrder ) SIP_PYNAME( setCustomLayerOrderByIds );
143 
154  QList<QgsMapLayer *> layerOrder() const;
155 
164  bool hasCustomLayerOrder() const;
165 
174  void setHasCustomLayerOrder( bool hasCustomLayerOrder );
175 
184  static QgsLayerTree *readXml( QDomElement &element, const QgsReadWriteContext &context );
185 
192  void readLayerOrderFromXml( const QDomElement &doc );
193 
194  void writeXml( QDomElement &parentElement, const QgsReadWriteContext &context ) override;
195 
196  QgsLayerTree *clone() const override SIP_FACTORY;
197 
203  void clear();
204 
205  signals:
206 
212  void customLayerOrderChanged();
213 
219  void layerOrderChanged();
220 
228  void hasCustomLayerOrderChanged( bool hasCustomLayerOrder );
229 
230  private slots:
231  void nodeAddedChildren( QgsLayerTreeNode *node, int indexFrom, int indexTo );
232  void nodeRemovedChildren();
233 
234  private:
236  QgsLayerTree( const QgsLayerTree &other );
237 
238  void init();
239 
240  void addMissingLayers();
241  QgsWeakMapLayerPointerList mCustomLayerOrder;
242  bool mHasCustomLayerOrder = false;
243 
244  QgsLayerTree &operator= ( const QgsLayerTree & ) = delete;
245 };
246 
247 #endif // QGSLAYERTREE_H
QgsLayerTreeNode::NodeGroup
@ NodeGroup
Container of other groups and layers.
Definition: qgslayertreenode.h:102
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:2153
QgsReadWriteContext
The class is used as a container of context for various read/write operations on other objects.
Definition: qgsreadwritecontext.h:34
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:360
QgsLayerTreeNode::NodeLayer
@ NodeLayer
Leaf node pointing to a layer.
Definition: qgslayertreenode.h:103
qgslayertreelayer.h
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:117
SIP_SKIP
#define SIP_SKIP
Definition: qgis_sip.h:126
QgsLayerTree
Namespace with helper functions for layer tree operations.
Definition: qgslayertree.h:32
qgslayertreegroup.h
QgsLayerTreeLayer
Layer tree node points to a map layer.
Definition: qgslayertreelayer.h:43
QgsLayerTreeGroup
Layer tree group node serves as a container for layers and further groups.
Definition: qgslayertreegroup.h:40
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:327
str
#define str(x)
Definition: qgis.cpp:37
qgslayertreenode.h
QgsLayerTreeGroup::clone
QgsLayerTreeGroup * clone() const override
Returns a clone of the group.
Definition: qgslayertreegroup.cpp:409