Salome HOME
updated copyright message
[modules/shaper.git] / src / ModuleBase / ModuleBase_ITreeNode.h
index bf7c8bbcff49d792b20dec4c1c682d2891af9441..05469ce2c2953ddf76fc5853a9451af1230f0841 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2014-2017  CEA/DEN, EDF R&D
+// Copyright (C) 2014-2023  CEA, EDF
 //
 // 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
 #include <QIcon>
 #include <QVariant>
 
+#ifdef _MSC_VER
+#pragma warning(disable: 4100)
+#endif
+
 class ModuleBase_ITreeNode;
 class ModuleBase_IWorkshop;
 
@@ -58,7 +61,9 @@ public:
   virtual QVariant data(int theColumn, int theRole) const { return QVariant(); }
 
   /// Returns properties flag of the item
-  virtual Qt::ItemFlags flags(int theColumn) const { return Qt::ItemIsSelectable | Qt::ItemIsEnabled; }
+  virtual Qt::ItemFlags flags(int theColumn) const {
+    return Qt::ItemIsSelectable | Qt::ItemIsEnabled;
+  }
 
   /// Returns parent node of the current node
   ModuleBase_ITreeNode* parent() const { return myParent; }
@@ -121,7 +126,9 @@ public:
   /// Process creation of objects.
   /// \param theObjects a list of created objects
   /// \return a list of nodes which corresponds to the created objects
-  virtual QTreeNodesList objectCreated(const QObjectPtrList& theObjects) { return QTreeNodesList(); }
+  virtual QTreeNodesList objectCreated(const QObjectPtrList& theObjects) {
+    return QTreeNodesList();
+  }
 
   /// Process deletion of objects.
   /// \param theDoc a document where objects were deleted
@@ -173,6 +180,40 @@ protected:
     }
   }
 
+  void sortChildren() {
+    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++;
+      }
+    }
+  }
+
   ModuleBase_ITreeNode* myParent; //!< Parent of the node
   QTreeNodesList myChildren; //!< Children of the node
 };