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 #include "TreeItem.hh"
45 
46 //--------------------------------------------------------------------------------
47 
48 TreeItem::TreeItem(int _id, QString _name, DataType _type, TreeItem* _parent) :
49  id_(_id),
50  dataType_(_type),
51  visible_(true),
52  name_(_name),
53  parentItem_(_parent)
54 {
55 }
56 
57 
58 // ===============================================================================
59 // Static Members
60 // ===============================================================================
61 
62 int TreeItem::id() {
63  return id_;
64 }
65 
66 //--------------------------------------------------------------------------------
67 
68 bool TreeItem::dataType(DataType _type) {
69  if ( _type == DATA_ALL ) {
70  return true;
71  }
72 
73  return ( (dataType_ & _type) );
74 }
75 
76 //--------------------------------------------------------------------------------
77 
79  return dataType_;
80 }
81 
82 //--------------------------------------------------------------------------------
83 
84 int TreeItem::group() {
85  // Skip root node
86  if ( parent() == 0 )
87  return -1;
88 
89  // Dont count root node as a group
90  if ( parent()->parent() == 0 )
91  return -1;
92 
93  // Only consider groups
94  if ( !parent()->dataType(DATA_GROUP) )
95  return -1;
96 
97  // Get the group id
98  return ( parent()->id() );
99 }
100 
101 //--------------------------------------------------------------------------------
102 
103 bool TreeItem::isGroup() {
104  return ( dataType(DATA_GROUP) );
105 }
106 
107 // ===============================================================================
108 // Dynamic Members
109 // ===============================================================================
110 
111 bool TreeItem::visible() {
112  return visible_;
113 }
114 
115 //--------------------------------------------------------------------------------
116 
117 void TreeItem::visible(bool _visible) {
118  visible_ = _visible;
119 }
120 
121 //--------------------------------------------------------------------------------
122 
123 QString TreeItem::name() {
124  return name_;
125 }
126 
127 //--------------------------------------------------------------------------------
128 
129 void TreeItem::name(QString _name ) {
130  name_ = _name;
131 }
132 
133 // ===============================================================================
134 // Tree Structure
135 // ===============================================================================
136 
138  // Visit child item of this node
139  if ( childItems_.size() > 0 ) {
140  return childItems_[0];
141  }
142 
143  // No Child Item so visit the next child of the parentItem_
144  if ( parentItem_ ) {
145 
146  TreeItem* parentPointer = parentItem_;
147  TreeItem* thisPointer = this;
148 
149  // while we are not at the root node
150  while ( parentPointer ) {
151 
152  // If there is an unvisited child of the parent, return this one
153  if ( parentPointer->childCount() > ( thisPointer->row() + 1) ) {
154  return parentPointer->childItems_[ thisPointer->row() + 1 ];
155  }
156 
157  // Go to the next level
158  thisPointer = parentPointer;
159  parentPointer = parentPointer->parentItem_;
160 
161  }
162 
163  return thisPointer;
164  }
165 
166  return this;
167 
168 }
169 
170 //--------------------------------------------------------------------------------
171 
172 int TreeItem::level() {
173  int level = 0;
174  TreeItem* current = this;
175 
176  // Go up and count the levels to the root node
177  while ( current->parent() != 0 ) {
178  level++;
179  current = current->parent();
180  }
181 
182  return level;
183 }
184 
185 //--------------------------------------------------------------------------------
186 
187 int TreeItem::row() const
188 {
189  if (parentItem_)
190  return parentItem_->childItems_.indexOf(const_cast<TreeItem*>(this));
191 
192  return 0;
193 }
194 
195 //--------------------------------------------------------------------------------
196 
198 {
199  return parentItem_;
200 }
201 
202 //--------------------------------------------------------------------------------
203 
204 void TreeItem::setParent(TreeItem* _parent) {
205  parentItem_ = _parent;
206 }
207 
208 //--------------------------------------------------------------------------------
209 
211 {
212  childItems_.append(item);
213 }
214 
215 //--------------------------------------------------------------------------------
216 
218 {
219  return childItems_.value(row);
220 }
221 
222 //--------------------------------------------------------------------------------
223 
224 int TreeItem::childCount() const
225 {
226  return childItems_.count();
227 }
228 
229 //--------------------------------------------------------------------------------
230 
231 TreeItem* TreeItem::childExists(int _objectId) {
232 
233  // Check if this object has the requested id
234  if ( id_ == _objectId )
235  return this;
236 
237  // search in children
238  for ( int i = 0 ; i < childItems_.size(); ++i ) {
239  TreeItem* tmp = childItems_[i]->childExists(_objectId);
240  if ( tmp != 0)
241  return tmp;
242  }
243 
244  return 0;
245 }
246 
247 //--------------------------------------------------------------------------------
248 
250 
251  // Check if this object has the requested id
252  if ( name() == _name )
253  return this;
254 
255  // search in children
256  for ( int i = 0 ; i < childItems_.size(); ++i ) {
257  TreeItem* tmp = childItems_[i]->childExists(_name);
258  if ( tmp != 0)
259  return tmp;
260  }
261 
262  return 0;
263 }
264 
265 //--------------------------------------------------------------------------------
266 
267 void TreeItem::removeChild( TreeItem* _item ) {
268 
269  bool found = false;
270  QList<TreeItem*>::iterator i;
271  for (i = childItems_.begin(); i != childItems_.end(); ++i) {
272  if ( *i == _item ) {
273  found = true;
274  break;
275  }
276  }
277 
278  if ( !found ) {
279  std::cerr << "TreeItem: Illegal remove request" << std::endl;
280  return;
281  }
282 
283  childItems_.erase(i);
284 }
285 
286 //--------------------------------------------------------------------------------
287 
288 QList< TreeItem* > TreeItem::getLeafs() {
289 
290  QList< TreeItem* > items;
291 
292  for ( int i = 0 ; i < childItems_.size(); ++i ) {
293  items = items + childItems_[i]->getLeafs();
294  }
295 
296  // If we are a leave...
297  if ( childCount() == 0 )
298  items.push_back(this);
299 
300  return items;
301 }
302 
303 //--------------------------------------------------------------------------------
304 
306 
307  // call function for all children of this node
308  for ( int i = 0 ; i < childItems_.size(); ++i) {
309 
310  // remove the subtree recursively
311  childItems_[i]->deleteSubtree();
312 
313  // delete child
314  delete childItems_[i];
315  }
316 
317  // clear the array
318  childItems_.clear();
319 }
320 
321 //=============================================================================
DataType dataType()
dataType
Definition: TreeItem.cc:94
int row() const
get the row of this item from the parent
Definition: TreeItem.cc:228
void deleteSubtree()
delete the whole subtree below this item ( The item itself is not touched )
Definition: TreeItem.cc:343
int level()
Definition: TreeItem.cc:213
int group()
group
Definition: TreeItem.cc:100
TreeItem * child(int row)
return a child
Definition: TreeItem.cc:257
TreeItem * childExists(int _objectId)
Check if the element exists in the subtree of this element.
Definition: TreeItem.cc:271
bool visible()
visible
Definition: TreeItem.cc:151
void setParent(TreeItem *_parent)
Set the parent pointer.
Definition: TreeItem.cc:242
const DataType DATA_GROUP(1)
Items used for Grouping.
TreeItem * parent()
Get the parent item ( 0 if root item )
Definition: TreeItem.cc:235
Predefined datatypes.
Definition: DataTypes.hh:83
QList< TreeItem *> getLeafs()
get all leafes of the tree below this object ( These will be all visible objects ) ...
Definition: TreeItem.cc:326
void appendChild(TreeItem *child)
add a child to this node
Definition: TreeItem.cc:248
QList< TreeItem * > childItems_
Children of this node.
Definition: TreeItem.hh:120
TreeItem * next()
Definition: TreeItem.cc:177
int id()
id
Definition: TreeItem.cc:78
QString name()
name
Definition: TreeItem.cc:163
TreeItem * parentItem_
Parent item or 0 if root node.
Definition: TreeItem.hh:114
void removeChild(TreeItem *_item)
Remove a child from this object.
Definition: TreeItem.cc:308
int childCount() const
get the number of children
Definition: TreeItem.cc:264
const DataType DATA_ALL(UINT_MAX)
Identifier for all available objects.