QGIS API Documentation  2.8.2-Wien
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups 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, 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 = 0;
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 }