Salome HOME
Issue #2998: Add help description for automatic creation of constraints
[modules/shaper.git] / src / ModuleBase / ModuleBase_ITreeNode.h
index fcd6c7bd46b2560519cb26c0a7ad81a60820c990..630251a560c529459bbd4b420e957a225b7181c2 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2014-2017  CEA/DEN, EDF R&D
+// Copyright (C) 2014-2019  CEA/DEN, EDF R&D
 //
 // This library is free software; you can redistribute it and/or
 // modify it under the terms of the GNU Lesser General Public
 //
 // You should have received a copy of the GNU Lesser General Public
 // License along with this library; if not, write to the Free Software
-// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
 //
-// See http://www.salome-platform.org/ or
-// email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
+// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
 //
 
 #ifndef ModuleBase_ITreeNode_H
@@ -178,23 +177,36 @@ protected:
   }
 
   void sortChildren() {
-    int i = 0;
-    ModuleBase_ITreeNode* aNode = 0;
-    ObjectPtr aObject;
-    int aIdx;
-    while (i < myChildren.size()) {
-      aNode = myChildren.at(i);
-      aObject = aNode->object();
-      if (aObject.get()) {
-        aIdx = aObject->document()->index(aObject, true);
-        if (aIdx != i) {
-          myChildren.removeAll(aNode);
-          myChildren.insert(aIdx, aNode);
-          i = 0;
-          continue;
+    if (myChildren.size() > 1) {
+      int i = 0;
+      ModuleBase_ITreeNode* aNode = 0;
+      ObjectPtr aObject;
+      int aIdx;
+      int aCount = 0;
+      int aSize = myChildren.size();
+      int aMaxCount = aSize * aSize;
+      int aShift = 0;
+      while (i < aSize) {
+        aCount++;
+        // To avoid unlimited cycling
+        if (aCount > aMaxCount)
+          break;
+
+        aNode = myChildren.at(i);
+        aObject = aNode->object();
+        if (aObject.get()) {
+          aIdx = aObject->document()->index(aObject, true) + aShift;
+          if (aIdx != i) {
+            myChildren.removeAll(aNode);
+            myChildren.insert(aIdx, aNode);
+            i = 0;
+            continue;
+          }
         }
+        else
+          aShift++;
+        i++;
       }
-      i++;
     }
   }