QGIS API Documentation  2.14.0-Essen
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 );
55  else if ( element.tagName() == "layer-tree-layer" )
56  node = QgsLayerTreeLayer::readXML( element );
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  }
140  emit addedChildren( this, index, indexTo );
141 }
142 
143 void QgsLayerTreeNode::removeChildrenPrivate( int from, int count, bool destroy )
144 {
145  if ( from < 0 || count <= 0 )
146  return;
147 
148  int to = from + count - 1;
149  if ( to >= mChildren.count() )
150  return;
151  emit willRemoveChildren( this, from, to );
152  while ( --count >= 0 )
153  {
154  QgsLayerTreeNode *node = mChildren.takeAt( from );
155  node->mParent = nullptr;
156  if ( destroy )
157  delete node;
158  }
159  emit removedChildren( this, from, to );
160 }
161 
163 {
164  int index = mChildren.indexOf( node );
165  if ( index < 0 )
166  return false;
167 
168  int n = mChildren.size();
169 
170  removeChildrenPrivate( index, 1, false );
171 
172  return mChildren.size() < n;
173 }
QObject * child(const char *objName, const char *inheritsClass, bool recursiveSearch) const
static unsigned index
void readXml(const QDomNode &parentNode, const QString &keyStartsWith=QString())
Read store contents from XML.
bool takeChild(QgsLayerTreeNode *node)
Remove a child from a node.
QVariant customProperty(const QString &key, const QVariant &defaultValue=QVariant()) const
Read a custom property from layer.
static QgsLayerTreeGroup * readXML(QDomElement &element)
Read group (tree) from XML element <layer-tree-group> and return the newly created group (or null on ...
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
QStringList customProperties() const
Return list of keys stored in custom properties.
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)
T takeAt(int i)
NodeType
Enumeration of possible tree node types.
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)
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.
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 insertChildrenPrivate(int index, QList< QgsLayerTreeNode * > nodes)
Low-level insertion of children to the node. The children must not have any parent yet! ...
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
QStringList keys() const
Return list of stored keys.
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.
static QgsLayerTreeNode * readXML(QDomElement &element)
Read layer tree from XML. Returns new instance.
bool isExpanded() const
Return whether the node should be shown as expanded or collapsed in GUI.
QList< QgsLayerTreeNode * > mChildren
list of children - node is responsible for their deletion
void removeCustomProperty(const QString &key)
Remove a custom property from layer.
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)
void writeXml(QDomNode &parentNode, QDomDocument &doc) const
Write store contents to XML.
void insert(int i, const T &value)
static QgsLayerTreeLayer * readXML(QDomElement &element)
QString tagName() const
void customPropertyChanged(QgsLayerTreeNode *node, const QString &key)
Emitted when a custom property of a node within the tree has been changed or removed.
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