QGIS API Documentation  2.99.0-Master (5753576)
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 
65 {
66  if ( mChecked == checked )
67  return;
68  mChecked = checked;
69  emit visibilityChanged( this );
70 }
71 
73 {
74  setItemVisibilityChecked( checked );
75 }
76 
78 {
79  setItemVisibilityChecked( checked );
80  if ( mParent )
82 }
83 
85 {
86  return mChecked && ( !mParent || mParent->isVisible() );
87 }
88 
89 
91 {
92  return mExpanded;
93 }
94 
96 {
97  if ( !mChecked )
98  return false;
99  Q_FOREACH ( QgsLayerTreeNode* child, mChildren )
100  {
101  if ( !child->isItemVisibilityCheckedRecursive() )
102  return false;
103  }
104 
105  return true;
106 }
107 
109 {
110  if ( mChecked )
111  return false;
112  Q_FOREACH ( QgsLayerTreeNode* child, mChildren )
113  {
114  if ( !child->isItemVisibilityUncheckedRecursive() )
115  return false;
116  }
117 
118  return true;
119 }
120 
121 void QgsLayerTreeNode::setExpanded( bool expanded )
122 {
123  if ( mExpanded == expanded )
124  return;
125 
126  mExpanded = expanded;
127  emit expandedChanged( this, expanded );
128 }
129 
130 
131 void QgsLayerTreeNode::setCustomProperty( const QString &key, const QVariant &value )
132 {
133  mProperties.setValue( key, value );
134  emit customPropertyChanged( this, key );
135 }
136 
137 QVariant QgsLayerTreeNode::customProperty( const QString &key, const QVariant &defaultValue ) const
138 {
139  return mProperties.value( key, defaultValue );
140 }
141 
142 void QgsLayerTreeNode::removeCustomProperty( const QString &key )
143 {
144  mProperties.remove( key );
145  emit customPropertyChanged( this, key );
146 }
147 
149 {
150  return mProperties.keys();
151 }
152 
153 void QgsLayerTreeNode::readCommonXml( QDomElement& element )
154 {
155  mProperties.readXml( element );
156 }
157 
158 void QgsLayerTreeNode::writeCommonXml( QDomElement& element )
159 {
160  QDomDocument doc( element.ownerDocument() );
161  mProperties.writeXml( element, doc );
162 }
163 
164 void QgsLayerTreeNode::insertChildrenPrivate( int index, QList<QgsLayerTreeNode*> nodes )
165 {
166  if ( nodes.isEmpty() )
167  return;
168 
169  Q_FOREACH ( QgsLayerTreeNode *node, nodes )
170  {
171  Q_ASSERT( !node->mParent );
172  node->mParent = this;
173  }
174 
175  if ( index < 0 || index >= mChildren.count() )
176  index = mChildren.count();
177 
178  int indexTo = index + nodes.count() - 1;
179  emit willAddChildren( this, index, indexTo );
180  for ( int i = 0; i < nodes.count(); ++i )
181  {
182  mChildren.insert( index + i, nodes[i] );
183 
184  // forward the signal towards the root
185  connect( nodes[i], SIGNAL( willAddChildren( QgsLayerTreeNode*, int, int ) ), this, SIGNAL( willAddChildren( QgsLayerTreeNode*, int, int ) ) );
186  connect( nodes[i], SIGNAL( addedChildren( QgsLayerTreeNode*, int, int ) ), this, SIGNAL( addedChildren( QgsLayerTreeNode*, int, int ) ) );
187  connect( nodes[i], SIGNAL( willRemoveChildren( QgsLayerTreeNode*, int, int ) ), this, SIGNAL( willRemoveChildren( QgsLayerTreeNode*, int, int ) ) );
188  connect( nodes[i], SIGNAL( removedChildren( QgsLayerTreeNode*, int, int ) ), this, SIGNAL( removedChildren( QgsLayerTreeNode*, int, int ) ) );
189  connect( nodes[i], SIGNAL( customPropertyChanged( QgsLayerTreeNode*, QString ) ), this, SIGNAL( customPropertyChanged( QgsLayerTreeNode*, QString ) ) );
191  connect( nodes[i], SIGNAL( expandedChanged( QgsLayerTreeNode*, bool ) ), this, SIGNAL( expandedChanged( QgsLayerTreeNode*, bool ) ) );
192  connect( nodes[i], SIGNAL( nameChanged( QgsLayerTreeNode*, QString ) ), this, SIGNAL( nameChanged( QgsLayerTreeNode*, QString ) ) );
193  }
194  emit addedChildren( this, index, indexTo );
195 }
196 
197 void QgsLayerTreeNode::removeChildrenPrivate( int from, int count, bool destroy )
198 {
199  if ( from < 0 || count <= 0 )
200  return;
201 
202  int to = from + count - 1;
203  if ( to >= mChildren.count() )
204  return;
205  emit willRemoveChildren( this, from, to );
206  while ( --count >= 0 )
207  {
208  QgsLayerTreeNode *node = mChildren.takeAt( from );
209  node->mParent = nullptr;
210  if ( destroy )
211  delete node;
212  }
213  emit removedChildren( this, from, to );
214 }
215 
217 {
218  int index = mChildren.indexOf( node );
219  if ( index < 0 )
220  return false;
221 
222  int n = mChildren.size();
223 
224  removeChildrenPrivate( index, 1, false );
225 
226  return mChildren.size() < n;
227 }
static unsigned index
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.
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.
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)
QgsLayerTreeNode * mParent
pointer to the parent node - null in case of root node
static QgsLayerTreeNode * readXml(QDomElement &element)
Read layer tree from XML. Returns new instance.
This class is a base class for nodes in a layer tree.
QList< QgsLayerTreeNode * > mChildren
list of children - node is responsible for their deletion
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.
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.
void setItemVisibilityChecked(bool checked)
Check or uncheck a node (independently of its ancestors or children)
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...
QgsObjectCustomProperties mProperties
custom properties attached to the node