Back to index

salome-gui  6.5.0
SUIT_DataObjectIterator.cxx
Go to the documentation of this file.
00001 // Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
00002 //
00003 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
00004 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
00005 //
00006 // This library is free software; you can redistribute it and/or
00007 // modify it under the terms of the GNU Lesser General Public
00008 // License as published by the Free Software Foundation; either
00009 // version 2.1 of the License.
00010 //
00011 // This library is distributed in the hope that it will be useful,
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014 // Lesser General Public License for more details.
00015 //
00016 // You should have received a copy of the GNU Lesser General Public
00017 // License along with this library; if not, write to the Free Software
00018 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00019 //
00020 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
00021 //
00022 
00023 #include "SUIT_DataObjectIterator.h"
00024 
00028 SUIT_DataObjectIterator::SUIT_DataObjectIterator( SUIT_DataObject* root, const int det, const bool fromTrueRoot )
00029 : myRoot( root ),
00030 myDetourType( det )
00031 {
00032   if ( myRoot && fromTrueRoot )
00033     myRoot = myRoot->root();
00034 
00035   myCurrent = myExtremeChild = myRoot;
00036 }
00037 
00041 SUIT_DataObject* SUIT_DataObjectIterator::parent( SUIT_DataObject* obj ) const
00042 {
00043   SUIT_DataObject* result = 0;
00044   if ( obj && obj != myRoot )
00045     result = obj->parent();
00046   return result;
00047 }
00048 
00052 void SUIT_DataObjectIterator::operator++()
00053 {
00054   SUIT_DataObject* aNext = 0;
00055   SUIT_DataObject* aParent = 0;
00056 
00057   bool exit;
00058 
00059   if ( myCurrent )
00060   {
00061     if ( myDetourType == DepthLeft || myDetourType == DepthRight )
00062     {
00063             //Depth detour algorithm
00064       if ( myCurrent->myChildren.count() > 0 )
00065       {
00066         myCurrent = extreme( myCurrent->myChildren, myDetourType == DepthLeft );
00067         myChildrenIndexes.append(myDetourType == DepthLeft ? 0 : myCurrent->myChildren.size() - 1);
00068       }
00069       else do
00070       {
00071         exit = false;
00072         aParent = parent( myCurrent );
00073         if ( !aParent )
00074         {
00075           myCurrent = 0; //the tree is passed completely
00076           exit = true;
00077         }
00078         else
00079         {
00080           int idx = myChildrenIndexes.last();
00081           if (myDetourType == DepthLeft && idx < aParent->myChildren.count() - 1) 
00082           {
00083             myChildrenIndexes.last()++;
00084             myCurrent = aParent->myChildren[idx + 1];
00085             exit = true;
00086           }
00087           else if (myDetourType == DepthRight && idx > 0) 
00088           {
00089             myChildrenIndexes.last()--;
00090             myCurrent = aParent->myChildren[idx - 1];
00091             exit = true;
00092           } else {
00093             myCurrent = aParent;
00094             myChildrenIndexes.removeLast();
00095           }
00096         }
00097       }
00098       while ( !exit );
00099     }
00100     else if ( myDetourType == BreadthLeft || myDetourType == BreadthRight )
00101     {
00102       //Breadth detour algorithm
00103       aNext = globalSibling( myCurrent, myDetourType == BreadthLeft );
00104       if ( !aNext )
00105       {
00106         myCurrent = 0;
00107         for ( SUIT_DataObject* cur = myExtremeChild; cur && !myCurrent; cur = globalSibling( cur, myDetourType == BreadthLeft ) )
00108         {
00109           if ( cur->myChildren.count() > 0 )
00110           {
00111             myExtremeChild = extreme( cur->myChildren, myDetourType == BreadthLeft );
00112             myCurrent = myExtremeChild;
00113             myChildrenIndexes.append(myDetourType == BreadthLeft ? 0 : myCurrent->myChildren.size() - 1);
00114           }
00115         }
00116       }
00117       else
00118         myCurrent = aNext;
00119     }
00120   }
00121 }
00122 
00126 SUIT_DataObject* SUIT_DataObjectIterator::current() const
00127 {
00128   return myCurrent;
00129 }
00130 
00134 int SUIT_DataObjectIterator::depth() const
00135 {
00136   return myChildrenIndexes.size();
00137 }
00138 
00142 int SUIT_DataObjectIterator::detour() const
00143 {
00144   return myDetourType;
00145 }
00146 
00150 SUIT_DataObject* SUIT_DataObjectIterator::globalSibling( SUIT_DataObject* obj, bool next ) const
00151 {
00152   SUIT_DataObject* par;
00153 
00154   if ( obj && ( par = parent( obj ) ) )
00155   {
00156     int idx = par->myChildren.indexOf( obj );
00157     if ( idx < par->myChildren.count() - 1 )
00158       return par->myChildren[idx + 1];
00159     else
00160     {
00161       for ( ; par; par = globalSibling( par, next ) )
00162       {
00163         if ( par->myChildren.count() > 0 )
00164           return extreme( par->myChildren, next );
00165       }
00166     }
00167     return 0;
00168   }
00169   else
00170     return 0;
00171 }
00172 
00177 SUIT_DataObject* SUIT_DataObjectIterator::extreme( DataObjectList& aList, bool FromLeft ) const
00178 {
00179   if ( FromLeft )
00180     return aList.first();
00181   else
00182     return aList.last();
00183 }
00184 
00188 SUIT_DataObjectLevelIterator::SUIT_DataObjectLevelIterator( SUIT_DataObject* root,
00189                                                             int start, int end, bool LeftToRight )
00190 : SUIT_DataObjectIterator( root, LeftToRight ? BreadthLeft : BreadthRight )
00191 {
00192   myStartLevel = start;
00193   if ( end > start )
00194     myEndLevel = end;
00195   else
00196     myEndLevel = myStartLevel;
00197 
00198   while ( current() && depth() < myStartLevel )
00199     SUIT_DataObjectIterator::operator++();
00200 }
00201 
00205 void SUIT_DataObjectLevelIterator::operator++()
00206 {
00207   if ( myCurrent )
00208   {
00209     SUIT_DataObjectIterator::operator++();
00210     if ( depth() > myEndLevel )
00211       myCurrent = 0;
00212   }
00213 }