Salome HOME
Update copyrights
[modules/shaper.git] / src / PartSet / PartSet_TreeNodes.cpp
index a2bc7be624e6a76f2677355c102beabf2870c215..c08539925ec34de2a67ea6c9985c940525bc0739 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
 //
 
 #include "PartSet_TreeNodes.h"
@@ -270,6 +269,7 @@ void PartSet_ObjectNode::update()
         else {
           aNode = new PartSet_ObjectNode(aBody, this);
           myChildren.append(aNode);
+          aNode->update();
         }
       }
       else if (aFieldRes.get()) {
@@ -368,7 +368,6 @@ QTreeNodesList PartSet_ObjectNode::objectsDeleted(
         aObj = subObject(aId);
         if (aNode->object() != aObj) {
           myChildren.removeAll(aNode);
-          aResult.removeAll(aNode);
           delete aNode;
           isDeleted = true;
         }
@@ -483,7 +482,9 @@ ModuleBase_ITreeNode* PartSet_FolderNode::createNode(const ObjectPtr& theObj)
   //ResultCompSolidPtr aCompRes = std::dynamic_pointer_cast<ModelAPI_ResultCompSolid>(theObj);
   //if (aCompRes.get())
   //  return new PartSet_CompsolidNode(theObj, this);
-  return new PartSet_ObjectNode(theObj, this);
+  ModuleBase_ITreeNode* aNode = new PartSet_ObjectNode(theObj, this);
+  aNode->update();
+  return aNode;
 }
 
 void PartSet_FolderNode::update()
@@ -567,18 +568,20 @@ QTreeNodesList PartSet_FolderNode::objectCreated(const QObjectPtrList& theObject
     }
   }
   // Add nodes in correct order
-  int i;
-  for (i = 0; i < myChildren.size(); i++) {
-    if (aNewNodes.contains(i)) {
-      myChildren.insert(i, aNewNodes[i]);
+  if (aNewNodes.size() > 0) {
+    int i;
+    for (i = 0; i < myChildren.size(); i++) {
+      if (aNewNodes.contains(i)) {
+        myChildren.insert(i, aNewNodes[i]);
+        aNewNodes.remove(i);
+      }
+    }
+    while (aNewNodes.size()) {
+      i = myChildren.size();
+      myChildren.append(aNewNodes[i]);
       aNewNodes.remove(i);
     }
   }
-  while (aNewNodes.size()) {
-    i = myChildren.size();
-    myChildren.append(aNewNodes[i]);
-    aNewNodes.remove(i);
-  }
   foreach(ModuleBase_ITreeNode* aNode, myChildren) {
     aResult.append(aNode->objectCreated(theObjects));
   }
@@ -602,7 +605,6 @@ QTreeNodesList PartSet_FolderNode::objectsDeleted(const DocumentPtr& theDoc,
       aToSort |= ((aIndex != -1) && (aId != aIndex));
       if (aIndex == -1) {
         myChildren.removeAll(aNode);
-        aResult.removeAll(aNode);
         delete aNode;
         aRemoved = true;
       }
@@ -648,19 +650,22 @@ QTreeNodesList PartSet_FeatureFolderNode::objectCreated(const QObjectPtrList& th
     }
   }
   // To add in correct order
-  int i;
-  for (i = 0; i < myChildren.size(); i++) {
-    if (aNewNodes.contains(i)) {
-      myChildren.insert(i, aNewNodes[i]);
-      aNewNodes.remove(i);
+  if (aNewNodes.size() > 0) {
+    int i;
+    for (i = 0; i < myChildren.size(); i++) {
+      if (aNewNodes.contains(i)) {
+        myChildren.insert(i, aNewNodes[i]);
+        aNewNodes.remove(i);
+      }
+    }
+    while (aNewNodes.size()) {
+      i = myChildren.size();
+      if (aNewNodes.contains(i)) {
+        myChildren.append(aNewNodes[i]);
+        aNewNodes.remove(i);
+      }
     }
   }
-  while (aNewNodes.size()) {
-    i = myChildren.size();
-    myChildren.append(aNewNodes[i]);
-    aNewNodes.remove(i);
-  }
-
   // Update sub-folders
   foreach(ModuleBase_ITreeNode* aNode, myChildren) {
     aResult.append(aNode->objectCreated(theObjects));
@@ -699,7 +704,6 @@ QTreeNodesList PartSet_FeatureFolderNode::objectsDeleted(const DocumentPtr& theD
         aToSort |= ((aIndex != -1) && (aId != (aIndex + aNb)));
         if (aIndex == -1) {
           myChildren.removeAll(aNode);
-          aResult.removeAll(aNode);
           delete aNode;
           aRemoved = true;
           continue;
@@ -816,7 +820,9 @@ ModuleBase_ITreeNode* PartSet_RootNode::createNode(const ObjectPtr& theObj)
   if (aFeature->getKind() == PartSetPlugin_Part::ID())
     return new PartSet_PartRootNode(theObj, this);
 
-  return new PartSet_ObjectNode(theObj, this);
+  PartSet_ObjectNode* aNode = new PartSet_ObjectNode(theObj, this);
+  aNode->update();
+  return aNode;
 }
 
 //////////////////////////////////////////////////////////////////////////////////
@@ -862,16 +868,18 @@ void PartSet_PartRootNode::update()
 
   bool aHasFields = myFieldsFolder->childrenCount() > 0;
   bool aHasGroups = myGroupsFolder->childrenCount() > 0;
-  if (aHasFields && (!myChildren.contains(myFieldsFolder))) {
-    myChildren.insert(3, myFieldsFolder);
-  }
-  else if (myChildren.contains(myFieldsFolder)) {
+  if (aHasFields) {
+    if (!myChildren.contains(myFieldsFolder)) {
+      myChildren.insert(3, myFieldsFolder);
+    }
+  } else if (myChildren.contains(myFieldsFolder)) {
     myChildren.removeAll(myFieldsFolder);
   }
-  if (aHasGroups && (!myChildren.contains(myGroupsFolder))) {
-    myChildren.insert(aHasFields ? 4 : 3, myGroupsFolder);
-  }
-  else if (myChildren.contains(myGroupsFolder)) {
+  if (aHasGroups) {
+    if (!myChildren.contains(myGroupsFolder)) {
+      myChildren.insert(aHasFields ? 4 : 3, myGroupsFolder);
+    }
+  } else if (myChildren.contains(myGroupsFolder)) {
     myChildren.removeAll(myGroupsFolder);
   }
 
@@ -881,13 +889,17 @@ void PartSet_PartRootNode::update()
   // Remove extra sub-nodes
   int aIndex = -1;
   int aId = aRows;
+  QMap<int, ModuleBase_ITreeNode*> aExistingNodes;
   while (aId < myChildren.size()) {
     ModuleBase_ITreeNode* aNode = myChildren.at(aId);
     if (aNode->object().get()) {
       aIndex = aDoc->index(aNode->object(), true);
       if ((aIndex == -1) || (aId != (aIndex + aRows))) {
         myChildren.removeAll(aNode);
-        delete aNode;
+        if (aIndex == -1)
+          delete aNode;
+        else
+          aExistingNodes[aIndex + aRows] = aNode;
         continue;
       }
     }
@@ -902,12 +914,19 @@ void PartSet_PartRootNode::update()
     aId = i + aRows; // Take into account existing folders
     if (aId < myChildren.size()) {
       if (myChildren.at(aId)->object() != aObj) {
-        ModuleBase_ITreeNode* aNode = createNode(aObj);
-        myChildren.insert(aId, aNode);
+        if (aExistingNodes.contains(aId)) {
+          myChildren.insert(aId, aExistingNodes[aId]);
+          aExistingNodes.remove(aId);
+        }
+        else {
+          myChildren.insert(aId, createNode(aObj));
+        }
       }
     } else {
-      ModuleBase_ITreeNode* aNode = createNode(aObj);
-      myChildren.append(aNode);
+      if (aExistingNodes.contains(myChildren.size()))
+        myChildren.append(aExistingNodes[myChildren.size()]);
+      else
+        myChildren.append(createNode(aObj));
     }
   }
   // Update sub-folders
@@ -969,7 +988,9 @@ ModuleBase_ITreeNode* PartSet_PartRootNode::createNode(const ObjectPtr& theObj)
 {
   if (theObj->groupName() == ModelAPI_Folder::group())
     return new PartSet_ObjectFolderNode(theObj, this);
-  return new PartSet_ObjectNode(theObj, this);
+  PartSet_ObjectNode* aNode = new PartSet_ObjectNode(theObj, this);
+  aNode->update();
+  return aNode;
 }
 
 int PartSet_PartRootNode::numberOfFolders() const
@@ -1075,11 +1096,13 @@ void PartSet_ObjectFolderNode::update()
         if (aObj != myChildren.at(i)->object()) {
           aNode = new PartSet_ObjectNode(aObj, this);
           myChildren.insert(i, aNode);
+          aNode->update();
         }
       }
       else {
         aNode = new PartSet_ObjectNode(aObj, this);
         myChildren.append(aNode);
+        aNode->update();
       }
     }
   }
@@ -1107,11 +1130,13 @@ QTreeNodesList PartSet_ObjectFolderNode::objectCreated(const QObjectPtrList& the
         aNode = new PartSet_ObjectNode(aObj, this);
         myChildren.insert(i, aNode);
         aResult.append(aNode);
+        aNode->update();
       }
     } else {
       aNode = new PartSet_ObjectNode(aObj, this);
       myChildren.append(aNode);
       aResult.append(aNode);
+      aNode->update();
     }
   }
   return aResult;
@@ -1143,7 +1168,6 @@ QTreeNodesList PartSet_ObjectFolderNode::objectsDeleted(const DocumentPtr& theDo
     if ((aFirst + aId) < aNbOfFeatures) {
       if (aNode->object() != aDoc->object(ModelAPI_Feature::group(), aFirst + aId)) {
         myChildren.removeAll(aNode);
-        aResult.removeAll(aNode);
         delete aNode;
         aRemoved = true;
         continue;