Developer Documentation
TreeItem.cc
1 /*===========================================================================*\
2 * *
3 * OpenFlipper *
4  * Copyright (c) 2001-2015, RWTH-Aachen University *
5  * Department of Computer Graphics and Multimedia *
6  * All rights reserved. *
7  * www.openflipper.org *
8  * *
9  *---------------------------------------------------------------------------*
10  * This file is part of OpenFlipper. *
11  *---------------------------------------------------------------------------*
12  * *
13  * Redistribution and use in source and binary forms, with or without *
14  * modification, are permitted provided that the following conditions *
15  * are met: *
16  * *
17  * 1. Redistributions of source code must retain the above copyright notice, *
18  * this list of conditions and the following disclaimer. *
19  * *
20  * 2. Redistributions in binary form must reproduce the above copyright *
21  * notice, this list of conditions and the following disclaimer in the *
22  * documentation and/or other materials provided with the distribution. *
23  * *
24  * 3. Neither the name of the copyright holder nor the names of its *
25  * contributors may be used to endorse or promote products derived from *
26  * this software without specific prior written permission. *
27  * *
28  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS *
29  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED *
30  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A *
31  * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER *
32  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, *
33  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, *
34  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR *
35  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF *
36  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING *
37  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS *
38  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *
39 * *
40 \*===========================================================================*/
41 
42 /*===========================================================================*\
43 * *
44 * $Revision$ *
45 * $LastChangedBy$ *
46 * $Date$ *
47 * *
48 \*===========================================================================*/
49 
50 #include "TreeItem.hh"
51 
52 //--------------------------------------------------------------------------------
53 
54 TreeItem::TreeItem(int _id, QString _name, DataType _type, TreeItem* _parent) :
55  id_(_id),
56  dataType_(_type),
57  visible_(true),
58  name_(_name),
59  parentItem_(_parent)
60 {
61 }
62 
63 
64 // ===============================================================================
65 // Static Members
66 // ===============================================================================
67 
68 int TreeItem::id() {
69  return id_;
70 }
71 
72 //--------------------------------------------------------------------------------
73 
74 bool TreeItem::dataType(DataType _type) {
75  if ( _type == DATA_ALL ) {
76  return true;
77  }
78 
79  return ( (dataType_ & _type) );
80 }
81 
82 //--------------------------------------------------------------------------------
83 
85  return dataType_;
86 }
87 
88 //--------------------------------------------------------------------------------
89 
90 int TreeItem::group() {
91  // Skip root node
92  if ( parent() == 0 )
93  return -1;
94 
95  // Dont count root node as a group
96  if ( parent()->parent() == 0 )
97  return -1;
98 
99  // Only consider groups
100  if ( !parent()->dataType(DATA_GROUP) )
101  return -1;
102 
103  // Get the group id
104  return ( parent()->id() );
105 }
106 
107 //--------------------------------------------------------------------------------
108 
109 bool TreeItem::isGroup() {
110  return ( dataType(DATA_GROUP) );
111 }
112 
113 // ===============================================================================
114 // Dynamic Members
115 // ===============================================================================
116 
117 bool TreeItem::visible() {
118  return visible_;
119 }
120 
121 //--------------------------------------------------------------------------------
122 
123 void TreeItem::visible(bool _visible) {
124  visible_ = _visible;
125 }
126 
127 //--------------------------------------------------------------------------------
128 
129 QString TreeItem::name() {
130  return name_;
131 }
132 
133 //--------------------------------------------------------------------------------
134 
135 void TreeItem::name(QString _name ) {
136  name_ = _name;
137 }
138 
139 // ===============================================================================
140 // Tree Structure
141 // ===============================================================================
142 
144  // Visit child item of this node
145  if ( childItems_.size() > 0 ) {
146  return childItems_[0];
147  }
148 
149  // No Child Item so visit the next child of the parentItem_
150  if ( parentItem_ ) {
151 
152  TreeItem* parentPointer = parentItem_;
153  TreeItem* thisPointer = this;
154 
155  // while we are not at the root node
156  while ( parentPointer ) {
157 
158  // If there is an unvisited child of the parent, return this one
159  if ( parentPointer->childCount() > ( thisPointer->row() + 1) ) {
160  return parentPointer->childItems_[ thisPointer->row() + 1 ];
161  }
162 
163  // Go to the next level
164  thisPointer = parentPointer;
165  parentPointer = parentPointer->parentItem_;
166 
167  }
168 
169  return thisPointer;
170  }
171 
172  return this;
173 
174 }
175 
176 //--------------------------------------------------------------------------------
177 
178 int TreeItem::level() {
179  int level = 0;
180  TreeItem* current = this;
181 
182  // Go up and count the levels to the root node
183  while ( current->parent() != 0 ) {
184  level++;
185  current = current->parent();
186  }
187 
188  return level;
189 }
190 
191 //--------------------------------------------------------------------------------
192 
193 int TreeItem::row() const
194 {
195  if (parentItem_)
196  return parentItem_->childItems_.indexOf(const_cast<TreeItem*>(this));
197 
198  return 0;
199 }
200 
201 //--------------------------------------------------------------------------------
202 
204 {
205  return parentItem_;
206 }
207 
208 //--------------------------------------------------------------------------------
209 
210 void TreeItem::setParent(TreeItem* _parent) {
211  parentItem_ = _parent;
212 }
213 
214 //--------------------------------------------------------------------------------
215 
217 {
218  childItems_.append(item);
219 }
220 
221 //--------------------------------------------------------------------------------
222 
224 {
225  return childItems_.value(row);
226 }
227 
228 //--------------------------------------------------------------------------------
229 
230 int TreeItem::childCount() const
231 {
232  return childItems_.count();
233 }
234 
235 //--------------------------------------------------------------------------------
236 
237 TreeItem* TreeItem::childExists(int _objectId) {
238 
239  // Check if this object has the requested id
240  if ( id_ == _objectId )
241  return this;
242 
243  // search in children
244  for ( int i = 0 ; i < childItems_.size(); ++i ) {
245  TreeItem* tmp = childItems_[i]->childExists(_objectId);
246  if ( tmp != 0)
247  return tmp;
248  }
249 
250  return 0;
251 }
252 
253 //--------------------------------------------------------------------------------
254 
256 
257  // Check if this object has the requested id
258  if ( name() == _name )
259  return this;
260 
261  // search in children
262  for ( int i = 0 ; i < childItems_.size(); ++i ) {
263  TreeItem* tmp = childItems_[i]->childExists(_name);
264  if ( tmp != 0)
265  return tmp;
266  }
267 
268  return 0;
269 }
270 
271 //--------------------------------------------------------------------------------
272 
273 void TreeItem::removeChild( TreeItem* _item ) {
274 
275  bool found = false;
276  QList<TreeItem*>::iterator i;
277  for (i = childItems_.begin(); i != childItems_.end(); ++i) {
278  if ( *i == _item ) {
279  found = true;
280  break;
281  }
282  }
283 
284  if ( !found ) {
285  std::cerr << "TreeItem: Illegal remove request" << std::endl;
286  return;
287  }
288 
289  childItems_.erase(i);
290 }
291 
292 //--------------------------------------------------------------------------------
293 
294 QList< TreeItem* > TreeItem::getLeafs() {
295 
296  QList< TreeItem* > items;
297 
298  for ( int i = 0 ; i < childItems_.size(); ++i ) {
299  items = items + childItems_[i]->getLeafs();
300  }
301 
302  // If we are a leave...
303  if ( childCount() == 0 )
304  items.push_back(this);
305 
306  return items;
307 }
308 
309 //--------------------------------------------------------------------------------
310 
312 
313  // call function for all children of this node
314  for ( int i = 0 ; i < childItems_.size(); ++i) {
315 
316  // remove the subtree recursively
317  childItems_[i]->deleteSubtree();
318 
319  // delete child
320  delete childItems_[i];
321  }
322 
323  // clear the array
324  childItems_.clear();
325 }
326 
327 //=============================================================================
void deleteSubtree()
delete the whole subtree below this item ( The item itself is not touched )
Definition: TreeItem.cc:351
bool visible()
visible
Definition: TreeItem.cc:159
Predefined datatypes.
Definition: DataTypes.hh:96
TreeItem * parent()
Get the parent item ( 0 if root item )
Definition: TreeItem.cc:243
int group()
group
Definition: TreeItem.cc:108
TreeItem * childExists(int _objectId)
Check if the element exists in the subtree of this element.
Definition: TreeItem.cc:279
QString name()
name
Definition: TreeItem.cc:171
QList< TreeItem * > childItems_
Children of this node.
Definition: TreeItem.hh:127
void setParent(TreeItem *_parent)
Set the parent pointer.
Definition: TreeItem.cc:250
int id()
id
Definition: TreeItem.cc:86
TreeItem * next()
Definition: TreeItem.cc:185
void appendChild(TreeItem *child)
add a child to this node
Definition: TreeItem.cc:256
int row() const
get the row of this item from the parent
Definition: TreeItem.cc:236
TreeItem * child(int row)
return a child
Definition: TreeItem.cc:265
const DataType DATA_ALL(UINT_MAX)
Identifier for all available objects.
TreeItem * parentItem_
Parent item or 0 if root node.
Definition: TreeItem.hh:121
void removeChild(TreeItem *_item)
Remove a child from this object.
Definition: TreeItem.cc:316
const DataType DATA_GROUP(1)
Items used for Grouping.
int childCount() const
get the number of children
Definition: TreeItem.cc:272
int level()
Definition: TreeItem.cc:221
QList< TreeItem * > getLeafs()
get all leafes of the tree below this object ( These will be all visible objects ) ...
Definition: TreeItem.cc:334
DataType dataType()
dataType
Definition: TreeItem.cc:102