Salome HOME
Update comments
[modules/gui.git] / src / SUIT / SUIT_DataObjectIterator.cxx
1 #include "SUIT_DataObjectIterator.h"
2
3 /*!
4   Constructor.
5 */
6 SUIT_DataObjectIterator::SUIT_DataObjectIterator( SUIT_DataObject* root, const int det, const bool fromTrueRoot )
7 : myRoot( root ),
8 myDetourType( det ),
9 myCurrentLevel( 0 )
10 {
11   if ( myRoot && fromTrueRoot )
12     myRoot = myRoot->root();
13
14   myCurrent = myExtremeChild = myRoot;
15 }
16
17 /*!
18   Gets parent for object \a obj.
19 */
20 SUIT_DataObject* SUIT_DataObjectIterator::parent( SUIT_DataObject* obj ) const
21 {
22   SUIT_DataObject* result = 0;
23   if ( obj && obj != myRoot )
24     result = obj->parent();
25   return result;
26 }
27
28 /*!
29   Increment operator.
30 */
31 void SUIT_DataObjectIterator::operator++()
32 {
33   SUIT_DataObject* aNext = 0;
34   SUIT_DataObject* aParent = 0;
35
36   bool exit;
37
38   if ( myCurrent )
39   {
40     if ( myDetourType == DepthLeft || myDetourType == DepthRight )
41     {
42             //Depth detour algorithm
43       if ( myCurrent->myChildren.count() > 0 )
44       {
45         myCurrent = extreme( myCurrent->myChildren, myDetourType == DepthLeft );
46         myCurrentLevel++;
47       }
48       else do
49       {
50         exit = false;
51         aParent = parent( myCurrent );
52         if ( !aParent )
53         {
54           myCurrent = 0; //the tree is passed completely
55           exit = true;
56         }
57         else
58         {
59           aParent->myChildren.find( myCurrent );
60           if ( myDetourType == DepthLeft )
61             myCurrent = aParent->myChildren.next();
62           else
63             myCurrent = aParent->myChildren.prev();
64           if ( !myCurrent )
65           {
66             myCurrent = aParent;
67             myCurrentLevel--;
68           }
69           else
70             exit = true;
71         }
72       }
73       while ( !exit );
74     }
75     else if ( myDetourType == BreadthLeft || myDetourType == BreadthRight )
76     {
77       //Breadth detour algorithm
78       aNext = globalSibling( myCurrent, myDetourType == BreadthLeft );
79       if ( !aNext )
80       {
81         myCurrent = 0;
82         for ( SUIT_DataObject* cur = myExtremeChild; cur && !myCurrent; cur = globalSibling( cur, myDetourType == BreadthLeft ) )
83         {
84           if ( cur->myChildren.count() > 0 )
85           {
86             myExtremeChild = extreme( cur->myChildren, myDetourType == BreadthLeft );
87             myCurrent = myExtremeChild;
88             myCurrentLevel++;
89           }
90         }
91       }
92       else
93         myCurrent = aNext;
94     }
95   }
96 }
97
98 /*!
99   Gets current data object.
100 */
101 SUIT_DataObject* SUIT_DataObjectIterator::current() const
102 {
103   return myCurrent;
104 }
105
106 /*!
107   Gets depth of current lavel.
108 */
109 int SUIT_DataObjectIterator::depth() const
110 {
111   return myCurrentLevel;
112 }
113
114 /*!
115   Gets detour type.
116 */
117 int SUIT_DataObjectIterator::detour() const
118 {
119   return myDetourType;
120 }
121
122 /*!
123   Gets global sibling for object \a obj
124 */
125 SUIT_DataObject* SUIT_DataObjectIterator::globalSibling( SUIT_DataObject* obj, bool next ) const
126 {
127   SUIT_DataObject* par;
128
129   if ( obj && ( par = parent( obj ) ) )
130   {
131     par->myChildren.find( obj );
132     if ( par->myChildren.next() )
133       return par->myChildren.current();
134     else
135     {
136       for ( ; par; par = globalSibling( par, next ) )
137       {
138         if ( par->myChildren.count() > 0 )
139           return extreme( par->myChildren, next );
140       }
141     }
142     return 0;
143   }
144   else
145     return 0;
146 }
147
148 /*!
149  * Gets first or last data object from list.
150  * Get firls, if \a FromLeft == true, else last.
151  */
152 SUIT_DataObject* SUIT_DataObjectIterator::extreme( DataObjectList& aList, bool FromLeft ) const
153 {
154   if ( FromLeft )
155     return aList.getFirst();
156   else
157     return aList.getLast();
158 }
159
160 /*!
161   Constructor.
162 */
163 SUIT_DataObjectLevelIterator::SUIT_DataObjectLevelIterator( SUIT_DataObject* root,
164                                                             int start, int end, bool LeftToRight )
165 : SUIT_DataObjectIterator( root, LeftToRight ? BreadthLeft : BreadthRight )
166 {
167   myStartLevel = start;
168   if ( end > start )
169     myEndLevel = end;
170   else
171     myEndLevel = myStartLevel;
172
173   while ( current() && depth() < myStartLevel )
174     SUIT_DataObjectIterator::operator++();
175 }
176
177 /*!
178   Increment operator.
179 */
180 void SUIT_DataObjectLevelIterator::operator++()
181 {
182   if ( myCurrent )
183   {
184     SUIT_DataObjectIterator::operator++();
185     if ( depth() > myEndLevel )
186       myCurrent = 0;
187   }
188 }