Salome HOME
Key_Escape processing in File selector widget. Otherwise, crash happens if file dialo...
[modules/shaper.git] / src / XGUI / XGUI_DataModel.cpp
index 9ba1da899a1fe9a427c64f80b4370a03907ae9ce..746b33e03ec1f2b8a4530c33cb27996a186c01cf 100644 (file)
@@ -130,9 +130,13 @@ void XGUI_DataModel::processEvent(const std::shared_ptr<Events_Message>& theMess
         // Check that new folders could appear
         QStringList aNotEmptyFolders = listOfShowNotEmptyFolders();
         foreach (QString aNotEmptyFolder, aNotEmptyFolders) {
-          if ((aNotEmptyFolder.toStdString() == aObjType) && (aRootDoc->size(aObjType) == 1))
+          if ((aNotEmptyFolder.toStdString() == aObjType) && (aRootDoc->size(aObjType) > 0)) {
             // Appears first object in folder which can not be shown empty
-            insertRow(myXMLReader->rootFolderId(aObjType));
+            if (!hasShownFolder(aRootDoc, aNotEmptyFolder)) {
+              insertRow(myXMLReader->rootFolderId(aObjType));
+              addShownFolder(aRootDoc, aNotEmptyFolder);
+            }
+          }
         }
         // Insert new object
         int aRow = aRootDoc->size(aObjType) - 1;
@@ -142,21 +146,25 @@ void XGUI_DataModel::processEvent(const std::shared_ptr<Events_Message>& theMess
           } else {
             int aFolderId = myXMLReader->rootFolderId(aObjType);
             if (aFolderId != -1) {
-              insertRow(aRow, createIndex(aFolderId, 1, (void*)Q_NULLPTR));
+              insertRow(aRow, createIndex(aFolderId, 0, (void*)Q_NULLPTR));
             }
           }
         }
       } else {
         // Object created in sub-document
-        QModelIndex aDocRoot = findDocumentRootIndex(aDoc.get());
+        QModelIndex aDocRoot = findDocumentRootIndex(aDoc.get(), 0);
         if (aDocRoot.isValid()) {
           // Check that new folders could appear
           QStringList aNotEmptyFolders = listOfShowNotEmptyFolders(false);
           foreach (QString aNotEmptyFolder, aNotEmptyFolders) {
-            if ((aNotEmptyFolder.toStdString() == aObjType) && (aDoc->size(aObjType) == 1))
+            if ((aNotEmptyFolder.toStdString() == aObjType) && (aDoc->size(aObjType) > 0)) {
               // Appears first object in folder which can not be shown empty
-              insertRow(myXMLReader->subFolderId(aObjType), aDocRoot);
-          }
+              if (!hasShownFolder(aDoc, aNotEmptyFolder)) {
+                insertRow(myXMLReader->subFolderId(aObjType), aDocRoot);
+                addShownFolder(aDoc, aNotEmptyFolder);
+              }
+            }
+         }
           int aRow = aDoc->index(aObject);
           if (aRow != -1) {
             int aNbSubFolders = foldersCount(aDoc.get());
@@ -168,7 +176,7 @@ void XGUI_DataModel::processEvent(const std::shared_ptr<Events_Message>& theMess
               if (aRow != -1) {
                 int aFolderId = folderId(aObjType, aDoc.get());
                 if (aFolderId != -1) {
-                  QModelIndex aParentFolder = createIndex(aFolderId, 1, aDoc.get());
+                  QModelIndex aParentFolder = createIndex(aFolderId, 0, aDoc.get());
                   insertRow(aRow, aParentFolder);
                   emit dataChanged(aParentFolder, aParentFolder);
                 }
@@ -216,7 +224,7 @@ void XGUI_DataModel::processEvent(const std::shared_ptr<Events_Message>& theMess
           // Process root sub-folder
           int aFolderId = myXMLReader->rootFolderId(aGroup);
           if (aFolderId != -1) {
-            QModelIndex aFolderIndex = createIndex(aFolderId, 1, (void*)Q_NULLPTR);
+            QModelIndex aFolderIndex = createIndex(aFolderId, 0, (void*)Q_NULLPTR);
             removeRow(aRow, aFolderIndex);
             //rebuildBranch(0, aRow);
           }
@@ -227,13 +235,14 @@ void XGUI_DataModel::processEvent(const std::shared_ptr<Events_Message>& theMess
           if ((aNotEmptyFolder.toStdString() == aGroup) && (aRootDoc->size(aGroup) == 0)) {
             // Appears first object in folder which can not be shown empty
             removeRow(myXMLReader->rootFolderId(aGroup));
+            removeShownFolder(aRootDoc, aNotEmptyFolder);
             //rebuildBranch(0, aNbFolders + aDoc->size(myXMLReader->rootType()));
             break;
           }
         }
       } else {
         // Remove row for sub-document
-        QModelIndex aDocRoot = findDocumentRootIndex(aDoc.get());
+        QModelIndex aDocRoot = findDocumentRootIndex(aDoc.get(), 0);
         if (aDocRoot.isValid()) {
           int aRow = aDoc->size(aGroup);
           int aNbSubFolders = foldersCount(aDoc.get());
@@ -245,7 +254,7 @@ void XGUI_DataModel::processEvent(const std::shared_ptr<Events_Message>& theMess
             // List of objects under a folder
             int aFolderId = folderId(aGroup, aDoc.get());
             if (aFolderId != -1) {
-              QModelIndex aFolderRoot = createIndex(aFolderId, 1, aDoc.get());
+              QModelIndex aFolderRoot = createIndex(aFolderId, 0, aDoc.get());
               removeRow(aRow, aFolderRoot);
               //rebuildBranch(0, aRow, aFolderRoot);
             }
@@ -257,6 +266,7 @@ void XGUI_DataModel::processEvent(const std::shared_ptr<Events_Message>& theMess
             if ((aNotEmptyFolder.toStdString() == aGroup) && (aSize == 0)) {
               // Appears first object in folder which can not be shown empty
               removeRow(myXMLReader->subFolderId(aGroup), aDocRoot);
+              removeShownFolder(aDoc, aNotEmptyFolder);
               //rebuildBranch(0, aNbSubFolders + aDoc->size(myXMLReader->subType()), aDocRoot);
               break;
             }
@@ -282,10 +292,10 @@ void XGUI_DataModel::processEvent(const std::shared_ptr<Events_Message>& theMess
           && (aFeature->firstResult()->groupName() == ModelAPI_ResultField::group())) {
             ResultFieldPtr aResult =
               std::dynamic_pointer_cast<ModelAPI_ResultField>(aFeature->firstResult());
-            QModelIndex aIndex = objectIndex(aResult);
+            QModelIndex aIndex = objectIndex(aResult, 0);
             removeRows(0, aResult->stepsSize(), aIndex);
         } else {
-          QModelIndex aIndex = objectIndex(aObject);
+          QModelIndex aIndex = objectIndex(aObject, 0);
           if (aIndex.isValid()) {
             emit dataChanged(aIndex, aIndex);
           }
@@ -309,16 +319,16 @@ void XGUI_DataModel::processEvent(const std::shared_ptr<Events_Message>& theMess
         if (aGroup == myXMLReader->rootType()) // Update objects under root
           aStartId = foldersCount();
         else // Update objects in folder under root
-          aParent = createIndex(folderId(aGroup), 1, (void*)Q_NULLPTR);
+          aParent = createIndex(folderId(aGroup), 0, (void*)Q_NULLPTR);
       } else {
         // Update a sub-document
         if (aGroup == myXMLReader->subType()) {
           // Update sub-document root
-          aParent = findDocumentRootIndex(aDoc.get());
+          aParent = findDocumentRootIndex(aDoc.get(), 0);
           aStartId = foldersCount(aDoc.get());
         } else
           // update folder in sub-document
-          aParent = createIndex(folderId(aGroup, aDoc.get()), 1, aDoc.get());
+          aParent = createIndex(folderId(aGroup, aDoc.get()), 0, aDoc.get());
       }
       int aChildNb = rowCount(aParent);
       rebuildBranch(aStartId, aChildNb - aStartId, aParent);
@@ -328,7 +338,7 @@ void XGUI_DataModel::processEvent(const std::shared_ptr<Events_Message>& theMess
   } else if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_DOCUMENT_CHANGED)) {
     DocumentPtr aDoc = ModelAPI_Session::get()->activeDocument();
     if (aDoc != aRootDoc) {
-      QModelIndex aDocRoot = findDocumentRootIndex(aDoc.get());
+      QModelIndex aDocRoot = findDocumentRootIndex(aDoc.get(), 0);
       if (aDocRoot.isValid())
         emit dataChanged(aDocRoot, aDocRoot);
       else
@@ -392,12 +402,7 @@ QModelIndex XGUI_DataModel::objectIndex(const ObjectPtr theObject, int theColumn
       if (aResult.get()) {
         ResultCompSolidPtr aCompRes = ModelAPI_Tools::compSolidOwner(aResult);
         if (aCompRes.get()) {
-          for (int i = 0; i < aCompRes->numberOfSubs(true); i++) {
-            if (aCompRes->subResult(i, true) == theObject) {
-              aRow = i;
-              break;
-            }
-          }
+          aRow = ModelAPI_Tools::compSolidIndex(aResult);
         }
       }
     }
@@ -1078,6 +1083,10 @@ void XGUI_DataModel::rebuildBranch(int theRow, int theCount, const QModelIndex&
 XGUI_DataModel::VisibilityState
   XGUI_DataModel::getVisibilityState(const QModelIndex& theIndex) const
 {
+  Qt::ItemFlags aFlags = theIndex.flags();
+  if (aFlags == Qt::ItemFlags())
+    return NoneState;
+
   ObjectPtr aObj = object(theIndex);
   if (aObj.get()) {
     ResultPtr aResObj = std::dynamic_pointer_cast<ModelAPI_Result>(aObj);
@@ -1085,7 +1094,8 @@ XGUI_DataModel::VisibilityState
       XGUI_Displayer* aDisplayer = myWorkshop->displayer();
       ResultCompSolidPtr aCompRes = std::dynamic_pointer_cast<ModelAPI_ResultCompSolid>(aResObj);
       if (aCompRes.get()) {
-        VisibilityState aState = NoneState;
+        VisibilityState aState = aCompRes->numberOfSubs(true) == 0 ?
+          (aDisplayer->isVisible(aCompRes)? Visible : Hidden) : NoneState;
         for (int i = 0; i < aCompRes->numberOfSubs(true); i++) {
           ResultPtr aSubRes = aCompRes->subResult(i, true);
           VisibilityState aS = aDisplayer->isVisible(aSubRes)? Visible : Hidden;