Salome HOME
Base implementation of Notebook
[modules/kernel.git] / src / DF / DF_ChildIterator.cxx
1 //  Copyright (C) 2007-2008  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 //  Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 //  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 //  This library is free software; you can redistribute it and/or
7 //  modify it under the terms of the GNU Lesser General Public
8 //  License as published by the Free Software Foundation; either
9 //  version 2.1 of the License.
10 //
11 //  This library is distributed in the hope that it will be useful,
12 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
13 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 //  Lesser General Public License for more details.
15 //
16 //  You should have received a copy of the GNU Lesser General Public
17 //  License along with this library; if not, write to the Free Software
18 //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 //  See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22 #include "DF_ChildIterator.hxx"
23
24 using namespace std;
25
26
27 //Constructor
28 DF_ChildIterator::DF_ChildIterator(const DF_Label& theLabel, bool allLevels)
29   :_root(NULL), _current(NULL)
30 {
31   Init(theLabel, allLevels);
32 }
33
34 DF_ChildIterator::DF_ChildIterator()
35   :_root(NULL), _current(NULL)
36 {
37 }
38
39 DF_ChildIterator::~DF_ChildIterator() 
40 {
41   _root = NULL;
42   _current = NULL;
43 }
44
45 //Initializes the iterator
46 void DF_ChildIterator::Init(const DF_Label& theLabel, bool allLevels)
47 {
48   _root = theLabel._node;
49   _allLevels = allLevels;
50   if(_root) _current = _root->_firstChild;
51 }
52
53 //Returns a current Label
54 DF_Label DF_ChildIterator::Value()
55 {
56   return DF_Label(_current);
57 }
58
59 //Returns true if there is a current Label
60 bool DF_ChildIterator::More()
61 {
62   return bool(_current);
63 }
64
65 //Moves to the next Label
66 void DF_ChildIterator::Next()
67 {
68   if(!_allLevels) {
69     _current = _current->_next; //Move to the next brother
70     return;
71   }
72   else {
73     if(_current->_firstChild) { //Go down to the first child
74       _current = _current->_firstChild;
75     }
76     else {
77       if(_current->_next) { //Next Brother
78         _current = _current->_next;
79       }
80       else {
81         if(_current->_father && _current->_father != _root) {
82           DF_LabelNode *father = _current->_father;
83           _current = father->_next;
84           if(!_current) {
85             while(father && father != _root) {
86               father = father->_father;
87               if(father->_next) break;
88             } 
89             if(father == _root) father = NULL;
90             if(father) _current = father->_next;
91             else _current = NULL;
92           }
93         }
94         else {
95           _current = NULL; //We iterate the whole sub tree
96         }
97       }
98     }
99   }
100 }
101