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