Salome HOME
Issue #2260: Correction for processing of empty/non-empty folders
[modules/shaper.git] / src / XGUI / XGUI_DataModel.cpp
index 5a628eefee0f5e5396b7bab318185e78e8c832a4..45dc147902acb228177d5a95c6355151ccddc290 100644 (file)
@@ -1,8 +1,22 @@
-// Copyright (C) 2014-20xx CEA/DEN, EDF R&D -->
-
-// File:        ModuleBase_IDocumentDataModel.cpp
-// Created:     28 Apr 2015
-// Author:      Vitaly SMETANNIKOV
+// Copyright (C) 2014-2017  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
+// License as published by the Free Software Foundation; either
+// version 2.1 of the License, or (at your option) any later version.
+//
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+// Lesser General Public License for more details.
+//
+// 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
+//
+// See http://www.salome-platform.org/ or
+// email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
+//
 
 #include "XGUI_DataModel.h"
 
@@ -77,6 +91,7 @@ XGUI_DataModel::XGUI_DataModel(QObject* theParent) : QAbstractItemModel(theParen
 
 XGUI_DataModel::~XGUI_DataModel()
 {
+  clear();
 }
 
 //******************************************************
@@ -109,9 +124,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;
@@ -132,10 +151,14 @@ void XGUI_DataModel::processEvent(const std::shared_ptr<Events_Message>& theMess
           // 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());
@@ -153,6 +176,9 @@ void XGUI_DataModel::processEvent(const std::shared_ptr<Events_Message>& theMess
                 }
               }
             }
+          } else {
+            rebuildDataTree();
+            break;
           }
         } else {
           rebuildDataTree();
@@ -203,6 +229,7 @@ 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;
           }
@@ -233,6 +260,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;
             }
@@ -317,7 +345,8 @@ void XGUI_DataModel::processEvent(const std::shared_ptr<Events_Message>& theMess
 //******************************************************
 void XGUI_DataModel::clear()
 {
-
+  beginResetModel();
+  endResetModel();
 }
 
 //******************************************************
@@ -367,12 +396,7 @@ QModelIndex XGUI_DataModel::objectIndex(const ObjectPtr theObject) const
       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);
         }
       }
     }