QGIS API Documentation  2.7.0-Master
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
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( 0 )
28  , mExpanded( true )
29 {
30 }
31 
33  : QObject()
34  , mNodeType( other.mNodeType )
35  , mParent( 0 )
36  , mExpanded( other.mExpanded )
37  , mProperties( other.mProperties )
38 {
39  QList<QgsLayerTreeNode*> clonedChildren;
40  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 = 0;
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 
89 void QgsLayerTreeNode::removeCustomProperty( const QString &key )
90 {
91  mProperties.remove( key );
92  emit customPropertyChanged( this, key );
93 }
94 
96 {
97  return mProperties.keys();
98 }
99 
100 void QgsLayerTreeNode::readCommonXML( QDomElement& element )
101 {
102  mProperties.readXml( element );
103 }
104 
105 void QgsLayerTreeNode::writeCommonXML( QDomElement& element )
106 {
107  QDomDocument doc( element.ownerDocument() );
108  mProperties.writeXml( element, doc );
109 }
110 
111 void QgsLayerTreeNode::insertChildrenPrivate( int index, QList<QgsLayerTreeNode*> nodes )
112 {
113  if ( nodes.count() == 0 )
114  return;
115 
116  foreach ( QgsLayerTreeNode* node, nodes )
117  {
118  Q_ASSERT( node->mParent == 0 );
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 )
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 = 0;
156  delete node;
157  }
158  emit removedChildren( this, from, to );
159 }
static unsigned index
void readXml(const QDomNode &parentNode, const QString &keyStartsWith=QString())
Read store contents from XML.
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 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)
void customPropertyChanged(QgsLayerTreeNode *node, QString key)
Emitted when a custom property of a node within the tree has been changed or removed.
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.
void removedChildren(QgsLayerTreeNode *node, int indexFrom, int indexTo)
Emitted when one or more nodes has been removed from a node within the tree.
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.
void removeChildrenPrivate(int from, int count)
Low-level removal of children from the node.
void visibilityChanged(QgsLayerTreeNode *node, Qt::CheckState state)
Emitted when check state of a node within the tree has been changed.
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 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.
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 writeCommonXML(QDomElement &element)
void writeXml(QDomNode &parentNode, QDomDocument &doc) const
Write store contents to XML.
static QgsLayerTreeLayer * readXML(QDomElement &element)
void setCustomProperty(const QString &key, const QVariant &value)
Set a custom property for the node.
QgsObjectCustomProperties mProperties
custom properties attached to the node