QGIS API Documentation  2.18.21-Las Palmas (9fba24a)
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  , mParent( nullptr )
28  , mExpanded( true )
29 {
30 }
31 
33  : QObject()
34  , mNodeType( other.mNodeType )
35  , mParent( nullptr )
36  , mExpanded( other.mExpanded )
37  , mProperties( other.mProperties )
38 {
39  QList<QgsLayerTreeNode*> clonedChildren;
40  Q_FOREACH ( QgsLayerTreeNode* child, other.mChildren )
41  clonedChildren << child->clone();
42  insertChildrenPrivate( -1, clonedChildren );
43 }
44 
46 {
47  qDeleteAll( mChildren );
48 }
49 
51 {
52  QgsLayerTreeNode* node = nullptr;
53  if ( element.tagName() == "layer-tree-group" )
54  node = QgsLayerTreeGroup::readXML( element, looseMatch );
55  else if ( element.tagName() == "layer-tree-layer" )
56  node = QgsLayerTreeLayer::readXML( element, looseMatch );
57 
58  return node;
59 }
60 
61 
63 {
64  return mExpanded;
65 }
66 
67 
68 void QgsLayerTreeNode::setExpanded( bool expanded )
69 {
70  if ( mExpanded == expanded )
71  return;
72 
73  mExpanded = expanded;
74  emit expandedChanged( this, expanded );
75 }
76 
77 
78 void QgsLayerTreeNode::setCustomProperty( const QString &key, const QVariant &value )
79 {
80  mProperties.setValue( key, value );
81  emit customPropertyChanged( this, key );
82 }
83 
84 QVariant QgsLayerTreeNode::customProperty( const QString &key, const QVariant &defaultValue ) const
85 {
86  return mProperties.value( key, defaultValue );
87 }
88 
90 {
91  mProperties.remove( key );
92  emit customPropertyChanged( this, key );
93 }
94 
96 {
97  return mProperties.keys();
98 }
99 
101 {
102  mProperties.readXml( element );
103 }
104 
106 {
107  QDomDocument doc( element.ownerDocument() );
108  mProperties.writeXml( element, doc );
109 }
110 
112 {
113  if ( nodes.isEmpty() )
114  return;
115 
116  Q_FOREACH ( QgsLayerTreeNode *node, nodes )
117  {
118  Q_ASSERT( !node->mParent );
119  node->mParent = this;
120  }
121 
122  if ( index < 0 || index >= mChildren.count() )
123  index = mChildren.count();
124 
125  int indexTo = index + nodes.count() - 1;
126  emit willAddChildren( this, index, indexTo );
127  for ( int i = 0; i < nodes.count(); ++i )
128  {
129  mChildren.insert( index + i, nodes[i] );
130 
131  // forward the signal towards the root
132  connect( nodes[i], SIGNAL( willAddChildren( QgsLayerTreeNode*, int, int ) ), this, SIGNAL( willAddChildren( QgsLayerTreeNode*, int, int ) ) );
133  connect( nodes[i], SIGNAL( addedChildren( QgsLayerTreeNode*, int, int ) ), this, SIGNAL( addedChildren( QgsLayerTreeNode*, int, int ) ) );
134  connect( nodes[i], SIGNAL( willRemoveChildren( QgsLayerTreeNode*, int, int ) ), this, SIGNAL( willRemoveChildren( QgsLayerTreeNode*, int, int ) ) );
135  connect( nodes[i], SIGNAL( removedChildren( QgsLayerTreeNode*, int, int ) ), this, SIGNAL( removedChildren( QgsLayerTreeNode*, int, int ) ) );
136  connect( nodes[i], SIGNAL( customPropertyChanged( QgsLayerTreeNode*, QString ) ), this, SIGNAL( customPropertyChanged( QgsLayerTreeNode*, QString ) ) );
137  connect( nodes[i], SIGNAL( visibilityChanged( QgsLayerTreeNode*, Qt::CheckState ) ), this, SIGNAL( visibilityChanged( QgsLayerTreeNode*, Qt::CheckState ) ) );
138  connect( nodes[i], SIGNAL( expandedChanged( QgsLayerTreeNode*, bool ) ), this, SIGNAL( expandedChanged( QgsLayerTreeNode*, bool ) ) );
139  connect( nodes[i], SIGNAL( nameChanged( QgsLayerTreeNode*, QString ) ), this, SIGNAL( nameChanged( QgsLayerTreeNode*, QString ) ) );
140  }
141  emit addedChildren( this, index, indexTo );
142 }
143 
144 void QgsLayerTreeNode::removeChildrenPrivate( int from, int count, bool destroy )
145 {
146  if ( from < 0 || count <= 0 )
147  return;
148 
149  int to = from + count - 1;
150  if ( to >= mChildren.count() )
151  return;
152  emit willRemoveChildren( this, from, to );
153  while ( --count >= 0 )
154  {
155  QgsLayerTreeNode *node = mChildren.takeAt( from );
156  node->mParent = nullptr;
157  if ( destroy )
158  delete node;
159  }
160  emit removedChildren( this, from, to );
161 }
162 
164 {
165  int index = mChildren.indexOf( node );
166  if ( index < 0 )
167  return false;
168 
169  int n = mChildren.size();
170 
171  removeChildrenPrivate( index, 1, false );
172 
173  return mChildren.size() < n;
174 }
QObject * child(const char *objName, const char *inheritsClass, bool recursiveSearch) const
static unsigned index
static QgsLayerTreeGroup * readXML(QDomElement &element, bool looseMatch=false)
Read group (tree) from XML element <layer-tree-group> and return the newly created group (or null on ...
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.
void readCommonXML(QDomElement &element)
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.
T takeAt(int i)
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.
int size() const
void removedChildren(QgsLayerTreeNode *node, int indexFrom, int indexTo)
Emitted when one or more nodes has been removed from a node within the tree.
int indexOf(const T &value, int from) const
QgsLayerTreeNode(NodeType t)
NodeType mNodeType
type of the node - determines which subclass is used
void visibilityChanged(QgsLayerTreeNode *node, Qt::CheckState state)
Emitted when check state of a node within the tree has been changed.
int count(const T &value) const
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.
QDomDocument ownerDocument() const
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.
bool isEmpty() const
QVariant customProperty(const QString &key, const QVariant &defaultValue=QVariant()) const
Read a custom property from layer.
QgsLayerTreeNode * mParent
pointer to the parent node - null in case of root node
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.
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 removeChildrenPrivate(int from, int count, bool destroy=true)
Low-level removal of children from the node.
void writeCommonXML(QDomElement &element)
static QgsLayerTreeNode * readXML(QDomElement &element, bool looseMatch=false)
Read layer tree from XML.
void insert(int i, const T &value)
QString tagName() const
static QgsLayerTreeLayer * readXML(QDomElement &element, bool looseMatch=false)
Creates a new layer from an XML definition.
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.
bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
void setCustomProperty(const QString &key, const QVariant &value)
Set a custom property for the node.
QgsObjectCustomProperties mProperties
custom properties attached to the node