Salome HOME
Issue #1834: Fix length of lines
[modules/shaper.git] / src / XGUI / XGUI_DataModel.cpp
index 3e7b0e7967c38f7e2e2127e5d4dfd97429daeda1..724553906cfc994de3f6dd259bc77b00613b3411 100644 (file)
@@ -22,7 +22,6 @@
 #include <Config_DataModelReader.h>
 
 #include <Events_Loop.h>
-#include <Events_Error.h>
 
 #include <QIcon>
 #include <QBrush>
@@ -150,11 +149,10 @@ void XGUI_DataModel::processEvent(const std::shared_ptr<Events_Message>& theMess
               }
             }
           }
-        } 
-#ifdef _DEBUG
-        else
-          Events_Error::send("Problem with Data Model definition of sub-document");
-#endif
+        } else {
+          rebuildDataTree();
+          break;
+        }
       }
     }
     // Deleted object event ***********************
@@ -162,8 +160,21 @@ void XGUI_DataModel::processEvent(const std::shared_ptr<Events_Message>& theMess
     std::shared_ptr<ModelAPI_ObjectDeletedMessage> aUpdMsg =
         std::dynamic_pointer_cast<ModelAPI_ObjectDeletedMessage>(theMessage);
     DocumentPtr aDoc = aUpdMsg->document();
-    std::set<std::string> aGroups = aUpdMsg->groups();
-    std::set<std::string>::const_iterator aIt;
+    std::set<std::string> aMsgGroups = aUpdMsg->groups();
+
+    /// Sort groups because RootType deletion has to be done after others
+    std::string aType = (aDoc == aRootDoc)? aRootType : aSubType;
+    std::list<std::string> aGroups;
+    std::set<std::string>::const_iterator aSetIt;
+    for (aSetIt = aMsgGroups.begin(); aSetIt != aMsgGroups.end(); ++aSetIt) {
+      std::string aGroup = (*aSetIt);
+      if (aGroup == aType)
+        aGroups.push_back(aGroup);
+      else
+        aGroups.push_front(aGroup);
+    }
+
+    std::list<std::string>::const_iterator aIt;
     for (aIt = aGroups.begin(); aIt != aGroups.end(); ++aIt) {
       std::string aGroup = (*aIt);
       if (aDoc == aRootDoc) {  // If root objects
@@ -221,6 +232,9 @@ void XGUI_DataModel::processEvent(const std::shared_ptr<Events_Message>& theMess
               break;
             }
           }
+        } else {
+          rebuildDataTree();
+          break;
         }
       }
     }
@@ -233,36 +247,46 @@ void XGUI_DataModel::processEvent(const std::shared_ptr<Events_Message>& theMess
     std::string aObjType;
     for (aIt = aObjects.begin(); aIt != aObjects.end(); ++aIt) {
       ObjectPtr aObject = (*aIt);
-      QModelIndex aIndex = objectIndex(aObject);
-      if (aIndex.isValid())
-        emit dataChanged(aIndex, aIndex);
+      if (aObject->data()->isValid()) {
+        QModelIndex aIndex = objectIndex(aObject);
+        if (aIndex.isValid()) {
+          emit dataChanged(aIndex, aIndex);
+        }
+      } else {
+        rebuildDataTree();
+        break;
+      }
     }
   } else if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_ORDER_UPDATED)) {
     std::shared_ptr<ModelAPI_OrderUpdatedMessage> aUpdMsg =
         std::dynamic_pointer_cast<ModelAPI_OrderUpdatedMessage>(theMessage);
-    DocumentPtr aDoc = aUpdMsg->document();
-    std::string aGroup = aUpdMsg->group();
-
-    QModelIndex aParent;
-    int aStartId = 0;
-    if (aDoc == aRootDoc) {
-      // Update a group under root
-      if (aGroup == myXMLReader->rootType()) // Update objects under root
-        aStartId = foldersCount();
-      else // Update objects in folder under root 
-        aParent = createIndex(folderId(aGroup), 0, -1);
+    if (aUpdMsg->reordered().get()) {
+      DocumentPtr aDoc = aUpdMsg->reordered()->document();
+      std::string aGroup = aUpdMsg->reordered()->group();
+
+      QModelIndex aParent;
+      int aStartId = 0;
+      if (aDoc == aRootDoc) {
+        // Update a group under root
+        if (aGroup == myXMLReader->rootType()) // Update objects under root
+          aStartId = foldersCount();
+        else // Update objects in folder under root 
+          aParent = createIndex(folderId(aGroup), 0, -1);
+      } else {
+        // Update a sub-document
+        if (aGroup == myXMLReader->subType()) {
+          // Update sub-document root
+          aParent = findDocumentRootIndex(aDoc.get());
+          aStartId = foldersCount(aDoc.get());
+        } else 
+          // update folder in sub-document
+          aParent = createIndex(folderId(aGroup, aDoc.get()), 0, aDoc.get());
+      }
+      int aChildNb = rowCount(aParent);
+      rebuildBranch(aStartId, aChildNb - aStartId, aParent);
     } else {
-      // Update a sub-document
-      if (aGroup == myXMLReader->subType()) {
-        // Update sub-document root
-        aParent = findDocumentRootIndex(aDoc.get());
-        aStartId = foldersCount(aDoc.get());
-      } else 
-        // update folder in sub-document
-        aParent = createIndex(folderId(aGroup, aDoc.get()), 0, aDoc.get());
+      rebuildDataTree();
     }
-    int aChildNb = rowCount(aParent);
-    rebuildBranch(aStartId, aChildNb - aStartId, aParent);
   } else if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_DOCUMENT_CHANGED)) {
     DocumentPtr aDoc = ModelAPI_Session::get()->activeDocument();
     if (aDoc != aRootDoc) {
@@ -272,10 +296,6 @@ void XGUI_DataModel::processEvent(const std::shared_ptr<Events_Message>& theMess
       else 
         // We have got a new document
         rebuildDataTree();
-//#ifdef _DEBUG
-//      else
-//        Events_Error::send("Problem with Data Model definition of sub-document");
-//#endif
     }
   } 
 }
@@ -291,6 +311,7 @@ void XGUI_DataModel::rebuildDataTree()
 {
   beginResetModel();
   endResetModel();
+  emit treeRebuilt();
 }
 
 //******************************************************
@@ -418,7 +439,8 @@ QVariant XGUI_DataModel::data(const QModelIndex& theIndex, int theRole) const
         {
           if (aObj->groupName() == ModelAPI_ResultParameter::group()) {
             ModelAPI_ResultParameter* aParam = dynamic_cast<ModelAPI_ResultParameter*>(aObj);
-            AttributeDoublePtr aValueAttribute = aParam->data()->real(ModelAPI_ResultParameter::VALUE());
+            AttributeDoublePtr aValueAttribute = 
+              aParam->data()->real(ModelAPI_ResultParameter::VALUE());
             QString aVal = QString::number(aValueAttribute->value());
             QString aTitle = QString(aObj->data()->name().c_str());
             return aTitle + " = " + aVal;
@@ -578,11 +600,13 @@ QModelIndex XGUI_DataModel::index(int theRow, int theColumn, const QModelIndex &
           }
         } else {
           // Check for composite object
-          ModelAPI_CompositeFeature* aCompFeature = dynamic_cast<ModelAPI_CompositeFeature*>(aParentObj);
+          ModelAPI_CompositeFeature* aCompFeature = 
+            dynamic_cast<ModelAPI_CompositeFeature*>(aParentObj);
           if (aCompFeature) {
             aIndex = objectIndex(aCompFeature->subFeature(theRow));
           } else {
-            ModelAPI_ResultCompSolid* aCompRes = dynamic_cast<ModelAPI_ResultCompSolid*>(aParentObj);
+            ModelAPI_ResultCompSolid* aCompRes = 
+              dynamic_cast<ModelAPI_ResultCompSolid*>(aParentObj);
             if (aCompRes) 
               aIndex = objectIndex(aCompRes->subResult(theRow));
           }
@@ -599,6 +623,8 @@ QModelIndex XGUI_DataModel::index(int theRow, int theColumn, const QModelIndex &
 static QModelIndex MYLastDeleted;
 QModelIndex XGUI_DataModel::parent(const QModelIndex& theIndex) const
 {
+  if (!theIndex.isValid())
+    return QModelIndex();
   // To avoid additional request about index which was already deleted
   if (theIndex == MYLastDeleted)
     return QModelIndex();
@@ -712,6 +738,10 @@ Qt::ItemFlags XGUI_DataModel::flags(const QModelIndex& theIndex) const
     // An object
     if (aObj->isDisabled()) 
       return theIndex.column() == 1? Qt::ItemIsSelectable : aNullFlag;
+
+    if (aSession->moduleDocument() != aObj->document())
+      if (aActiveDoc != aObj->document())
+        return theIndex.column() == 1? Qt::ItemIsSelectable : aNullFlag;
     
     bool isCompositeSub = false;
     // An object which is sub-object of a composite object can not be accessible in column 1