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