Salome HOME
Implementation of task "3.1. Ability to export the PartSet to GEOM"
[modules/shaper.git] / src / XGUI / XGUI_DataModel.cpp
index b31e32f361e159232d2aaf780f8fb04d7b1f073b..a6aa8cba1b20515955ada266dc8707b785285836 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 "XGUI_DataModel.h"
@@ -69,17 +68,27 @@ void XGUI_DataModel::processEvent(const std::shared_ptr<Events_Message>& theMess
       if ((*aIt)->isInHistory())
         aCreated.append(*aIt);
     }
+    if (aCreated.length() == 0)
+      return;
     QTreeNodesList aNodes = myRoot->objectCreated(aCreated);
     ModuleBase_ITreeNode* aParent;
     int aRow = 0;
     QModelIndex aParentIndex1, aParentIndex2;
+    ObjectPtr aObj;
     foreach(ModuleBase_ITreeNode* aNode, aNodes) {
+      aObj = aNode->object();
       aParent = aNode->parent();
-      aRow = aParent->nodeRow(aNode);
-      aParentIndex1 = getParentIndex(aNode, 0);
-      aParentIndex2 = getParentIndex(aNode, 2);
-      insertRows(aRow, 1, aParentIndex1);
-      dataChanged(aParentIndex1, aParentIndex2);
+      if (aObj.get() && (aObj->groupName() == ModelAPI_Folder::group())) {
+        aParent->update();
+        rebuildDataTree();
+      }
+      else {
+        aRow = aParent->nodeRow(aNode);
+        aParentIndex1 = getParentIndex(aNode, 0);
+        aParentIndex2 = getParentIndex(aNode, 2);
+        insertRows(aRow, 1, aParentIndex1);
+        dataChanged(aParentIndex1, aParentIndex2);
+      }
     }
   }
   else if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_OBJECT_DELETED)) {
@@ -87,9 +96,22 @@ void XGUI_DataModel::processEvent(const std::shared_ptr<Events_Message>& theMess
       std::dynamic_pointer_cast<ModelAPI_ObjectDeletedMessage>(theMessage);
     const std::list<std::pair<std::shared_ptr<ModelAPI_Document>, std::string>>& aMsgGroups =
       aUpdMsg->groups();
+    QTreeNodesList aList;
     std::list<std::pair<std::shared_ptr<ModelAPI_Document>, std::string>>::const_iterator aIt;
-    for (aIt = aMsgGroups.cbegin(); aIt != aMsgGroups.cend(); aIt++)
-      QTreeNodesList aList = myRoot->objectsDeleted(aIt->first, aIt->second.c_str());
+    for (aIt = aMsgGroups.cbegin(); aIt != aMsgGroups.cend(); aIt++) {
+      aList.append(myRoot->objectsDeleted(aIt->first, aIt->second.c_str()));
+    }
+    // Remove obsolete nodes
+    QTreeNodesList aRemaining;
+    foreach(ModuleBase_ITreeNode* aNode, aList) {
+      if (myRoot->hasSubNode(aNode))
+        aRemaining.append(aNode);
+    }
+    // Update remaining nodes
+    foreach(ModuleBase_ITreeNode* aNode, aRemaining) {
+      if (aNode->parent())
+        aNode->parent()->update();
+    }
     rebuildDataTree();
   }
   else if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_OBJECT_UPDATED)) {
@@ -118,28 +140,27 @@ void XGUI_DataModel::processEvent(const std::shared_ptr<Events_Message>& theMess
       rebuildDataTree();
     }
     else {
+      QSet<ModuleBase_ITreeNode*> aParents;
       foreach(ObjectPtr aObj, aCreated) {
         ModuleBase_ITreeNode* aNode = myRoot->subNode(aObj);
         if (aNode) {
-          int aOldNb = aNode->childrenCount();
-          aNode->update();
-          int aNewNb = aNode->childrenCount();
-
-          QModelIndex aFirstIdx = getIndex(aNode, 0);
-          QModelIndex aLastIdx = getIndex(aNode, 2);
-
-          if (aNewNb > aOldNb) {
-            insertRows(aOldNb - 1, aNewNb - aOldNb, aFirstIdx);
+          if (aNode->parent()) {
+            if (aNode->parent() == myRoot) {
+              aParents.clear();
+              aParents.insert(myRoot);
+              break;
+            }
+            else {
+              aNode = aNode->parent();
+            }
           }
-          else if (aNewNb < aOldNb) {
-            if (aNewNb)
-              removeRows(aNewNb - 1, aOldNb - aNewNb, aFirstIdx);
-            else if (aOldNb)
-              removeRows(0, aOldNb, aFirstIdx);
-          }
-          dataChanged(aFirstIdx, aLastIdx);
+          aParents.insert(aNode);
         }
       }
+      foreach(ModuleBase_ITreeNode* aNode, aParents) {
+        aNode->update();
+      }
+      rebuildDataTree();
     }
   }
   else if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_ORDER_UPDATED)) {
@@ -151,7 +172,7 @@ void XGUI_DataModel::processEvent(const std::shared_ptr<Events_Message>& theMess
       ModuleBase_ITreeNode* aNode = myRoot->findParent(aDoc, aGroup.c_str());
       if (aNode) {
         aNode->update();
-        updateSubTree(aNode);
+        rebuildDataTree();
       }
     }
   }
@@ -269,7 +290,6 @@ int XGUI_DataModel::columnCount(const QModelIndex& theParent) const
 //******************************************************
 QModelIndex XGUI_DataModel::index(int theRow, int theColumn, const QModelIndex &theParent) const
 {
-  int aa = theParent.row();
   ModuleBase_ITreeNode* aParentNode = (theParent.isValid()) ?
     (ModuleBase_ITreeNode*)theParent.internalPointer() : myRoot;
   ModuleBase_ITreeNode* aSubNode = aParentNode->subNode(theRow);
@@ -392,3 +412,11 @@ void XGUI_DataModel::updateSubTree(ModuleBase_ITreeNode* theParent)
     dataChanged(aFirstIdx, aLastIdx);
   }
 }
+
+
+//******************************************************
+DocumentPtr XGUI_DataModel::document(const QModelIndex& theIndex) const
+{
+  ModuleBase_ITreeNode* aNode = (ModuleBase_ITreeNode*)theIndex.internalPointer();
+  return aNode->document();
+}