QGIS API Documentation  2.99.0-Master (23ddace)
qgslayertreenode.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgslayertreenode.cpp
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 #include "qgslayertreenode.h"
17 
18 #include "qgslayertree.h"
19 #include "qgslayertreeutils.h"
20 
21 #include <QDomElement>
22 #include <QStringList>
23 
24 
26  : mNodeType( t )
27  , mChecked( checked )
28  , mParent( nullptr )
29  , mExpanded( true )
30 {
31 }
32 
34  : QObject()
35  , mNodeType( other.mNodeType )
36  , mChecked( other.mChecked )
37  , mParent( nullptr )
38  , mExpanded( other.mExpanded )
39  , mProperties( other.mProperties )
40 {
41  QList<QgsLayerTreeNode *> clonedChildren;
42  Q_FOREACH ( QgsLayerTreeNode *child, other.mChildren )
43  clonedChildren << child->clone();
44  insertChildrenPrivate( -1, clonedChildren );
45 }
46 
48 {
49  qDeleteAll( mChildren );
50 }
51 
53 {
54  QgsLayerTreeNode *node = nullptr;
55  if ( element.tagName() == QLatin1String( "layer-tree-group" ) )
56  node = QgsLayerTreeGroup::readXml( element );
57  else if ( element.tagName() == QLatin1String( "layer-tree-layer" ) )
58  node = QgsLayerTreeLayer::readXml( element );
59 
60  return node;
61 }
62 
63 QgsLayerTreeNode *QgsLayerTreeNode::readXml( QDomElement &element, const QgsProject *project )
64 {
65  QgsLayerTreeNode *node = readXml( element );
66  if ( node )
67  node->resolveReferences( project );
68  return node;
69 }
70 
71 
73 {
74  if ( mChecked == checked )
75  return;
76  mChecked = checked;
77  emit visibilityChanged( this );
78 }
79 
81 {
82  setItemVisibilityChecked( checked );
83 }
84 
86 {
87  setItemVisibilityChecked( checked );
88  if ( mParent )
90 }
91 
93 {
94  return mChecked && ( !mParent || mParent->isVisible() );
95 }
96 
97 
99 {
100  return mExpanded;
101 }
102 
104 {
105  if ( !mChecked )
106  return false;
107  Q_FOREACH ( QgsLayerTreeNode *child, mChildren )
108  {
109  if ( !child->isItemVisibilityCheckedRecursive() )
110  return false;
111  }
112 
113  return true;
114 }
115 
117 {
118  if ( mChecked )
119  return false;
120  Q_FOREACH ( QgsLayerTreeNode *child, mChildren )
121  {
122  if ( !child->isItemVisibilityUncheckedRecursive() )
123  return false;
124  }
125 
126  return true;
127 }
128 
129 void fetchCheckedLayers( const QgsLayerTreeNode *node, QList<QgsMapLayer *> &layers )
130 {
131  if ( QgsLayerTree::isLayer( node ) )
132  {
133  const QgsLayerTreeLayer *nodeLayer = QgsLayerTree::toLayer( node );
134  if ( nodeLayer->isVisible() )
135  layers << nodeLayer->layer();
136  }
137 
138  Q_FOREACH ( QgsLayerTreeNode *child, node->children() )
139  fetchCheckedLayers( child, layers );
140 }
141 
142 QList<QgsMapLayer *> QgsLayerTreeNode::checkedLayers() const
143 {
144  QList<QgsMapLayer *> layers;
145  fetchCheckedLayers( this, layers );
146  return layers;
147 }
148 
149 void QgsLayerTreeNode::setExpanded( bool expanded )
150 {
151  if ( mExpanded == expanded )
152  return;
153 
154  mExpanded = expanded;
155  emit expandedChanged( this, expanded );
156 }
157 
158 
159 void QgsLayerTreeNode::setCustomProperty( const QString &key, const QVariant &value )
160 {
161  mProperties.setValue( key, value );
162  emit customPropertyChanged( this, key );
163 }
164 
165 QVariant QgsLayerTreeNode::customProperty( const QString &key, const QVariant &defaultValue ) const
166 {
167  return mProperties.value( key, defaultValue );
168 }
169 
170 void QgsLayerTreeNode::removeCustomProperty( const QString &key )
171 {
172  mProperties.remove( key );
173  emit customPropertyChanged( this, key );
174 }
175 
177 {
178  return mProperties.keys();
179 }
180 
181 void QgsLayerTreeNode::readCommonXml( QDomElement &element )
182 {
183  mProperties.readXml( element );
184 }
185 
186 void QgsLayerTreeNode::writeCommonXml( QDomElement &element )
187 {
188  QDomDocument doc( element.ownerDocument() );
189  mProperties.writeXml( element, doc );
190 }
191 
192 void QgsLayerTreeNode::insertChildrenPrivate( int index, QList<QgsLayerTreeNode *> nodes )
193 {
194  if ( nodes.isEmpty() )
195  return;
196 
197  Q_FOREACH ( QgsLayerTreeNode *node, nodes )
198  {
199  Q_ASSERT( !node->mParent );
200  node->mParent = this;
201  }
202 
203  if ( index < 0 || index >= mChildren.count() )
204  index = mChildren.count();
205 
206  int indexTo = index + nodes.count() - 1;
207  emit willAddChildren( this, index, indexTo );
208  for ( int i = 0; i < nodes.count(); ++i )
209  {
210  QgsLayerTreeNode *node = nodes.at( i );
211  mChildren.insert( index + i, node );
212 
213  // forward the signal towards the root
222  }
223  emit addedChildren( this, index, indexTo );
224 }
225 
226 void QgsLayerTreeNode::removeChildrenPrivate( int from, int count, bool destroy )
227 {
228  if ( from < 0 || count <= 0 )
229  return;
230 
231  int to = from + count - 1;
232  if ( to >= mChildren.count() )
233  return;
234  emit willRemoveChildren( this, from, to );
235  while ( --count >= 0 )
236  {
237  QgsLayerTreeNode *node = mChildren.takeAt( from );
238  node->mParent = nullptr;
239  if ( destroy )
240  delete node;
241  }
242  emit removedChildren( this, from, to );
243 }
244 
246 {
247  int index = mChildren.indexOf( node );
248  if ( index < 0 )
249  return false;
250 
251  int n = mChildren.size();
252 
253  removeChildrenPrivate( index, 1, false );
254 
255  return mChildren.size() < n;
256 }
static QgsLayerTreeLayer * toLayer(QgsLayerTreeNode *node)
Cast node to a layer.
Definition: qgslayertree.h:74
bool isItemVisibilityUncheckedRecursive() const
Return whether this node is unchecked and all its children.
void readXml(const QDomNode &parentNode, const QString &keyStartsWith=QString())
Read store contents from XML.
void insertChildrenPrivate(int index, QList< QgsLayerTreeNode *> nodes)
Low-level insertion of children to the node. The children must not have any parent yet! ...
bool takeChild(QgsLayerTreeNode *node)
Remove a child from a node.
virtual QgsLayerTreeNode * clone() const =0
Create a copy of the node. Returns new instance.
bool mExpanded
whether the node should be shown in GUI as expanded
void willRemoveChildren(QgsLayerTreeNode *node, int indexFrom, int indexTo)
Emitted when one or more nodes will be removed from a node within the tree.
bool isVisible() const
Returns whether a node is really visible (ie checked and all its ancestors checked as well) ...
QVariant value(const QString &key, const QVariant &defaultValue=QVariant()) const
Return value for the given key. If the key is not stored, default value will be used.
static QgsLayerTreeGroup * readXml(QDomElement &element)
Read group (tree) from XML element <layer-tree-group> and return the newly created group (or null on ...
void writeXml(QDomNode &parentNode, QDomDocument &doc) const
Write store contents to XML.
bool isExpanded() const
Return whether the node should be shown as expanded or collapsed in GUI.
NodeType
Enumeration of possible tree node types.
QStringList customProperties() const
Return list of keys stored in custom properties.
void remove(const QString &key)
Remove a key (entry) from the store.
void willAddChildren(QgsLayerTreeNode *node, int indexFrom, int indexTo)
Emitted when one or more nodes will be added to a node within the tree.
QList< QgsMapLayer *> checkedLayers() const
Returns a list of any checked layers which belong to this node or its children.
void removedChildren(QgsLayerTreeNode *node, int indexFrom, int indexTo)
Emitted when one or more nodes has been removed from a node within the tree.
NodeType mNodeType
type of the node - determines which subclass is used
virtual void setItemVisibilityCheckedRecursive(bool checked)
Check or uncheck a node and all its children (taking into account exclusion rules) ...
void setValue(const QString &key, const QVariant &value)
Add an entry to the store. If the entry with the keys exists already, it will be overwritten.
QList< QgsLayerTreeNode * > children()
Get list of children of the node. Children are owned by the parent.
void expandedChanged(QgsLayerTreeNode *node, bool expanded)
Emitted when the collapsed/expanded state of a node within the tree has been changed.
void addedChildren(QgsLayerTreeNode *node, int indexFrom, int indexTo)
Emitted when one or more nodes have been added to a node within the tree.
void writeCommonXml(QDomElement &element)
Write common XML elements.
QVariant customProperty(const QString &key, const QVariant &defaultValue=QVariant()) const
Read a custom property from layer. Properties are stored in a map and saved in project file...
static QgsLayerTreeLayer * readXml(QDomElement &element)
Read layer node from XML.
QgsLayerTreeNode * mParent
pointer to the parent node - null in case of root node
static QgsLayerTreeNode * readXml(QDomElement &element)
Read layer tree from XML.
static bool isLayer(const QgsLayerTreeNode *node)
Check whether the node is a valid layer node.
Definition: qgslayertree.h:52
This class is a base class for nodes in a layer tree.
Reads and writes project states.
Definition: qgsproject.h:78
void removeCustomProperty(const QString &key)
Remove a custom property from layer. Properties are stored in a map and saved in project file...
QStringList keys() const
Return list of stored keys.
void setExpanded(bool expanded)
Set whether the node should be shown as expanded or collapsed in GUI.
void setItemVisibilityCheckedParentRecursive(bool checked)
Check or uncheck a node and all its parents.
void removeChildrenPrivate(int from, int count, bool destroy=true)
Low-level removal of children from the node.
bool isItemVisibilityCheckedRecursive() const
Return whether this node is checked and all its children.
QgsMapLayer * layer() const
void visibilityChanged(QgsLayerTreeNode *node)
Emitted when check state of a node within the tree has been changed.
void readCommonXml(QDomElement &element)
Read common XML elements.
virtual void resolveReferences(const QgsProject *project, bool looseMatching=false)=0
Turn textual references to layers into map layer object from project.
void setItemVisibilityChecked(bool checked)
Check or uncheck a node (independently of its ancestors or children)
void fetchCheckedLayers(const QgsLayerTreeNode *node, QList< QgsMapLayer *> &layers)
QList< QgsLayerTreeNode * > mChildren
list of children - node is responsible for their deletion
void customPropertyChanged(QgsLayerTreeNode *node, const QString &key)
Emitted when a custom property of a node within the tree has been changed or removed.
void nameChanged(QgsLayerTreeNode *node, QString name)
Emitted when the name of the node is changed.
QgsLayerTreeNode(NodeType t, bool checked=true)
Constructor.
void setCustomProperty(const QString &key, const QVariant &value)
Set a custom property for the node. Properties are stored in a map and saved in project file...
Layer tree node points to a map layer.
QgsObjectCustomProperties mProperties
custom properties attached to the node