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