Salome HOME
Merge branch 'Dev_2.8.0'
[modules/shaper.git] / src / XGUI / XGUI_DataModel.cpp
index 88fb28eeb6c6eff05766fad8028f74dd1611e778..746b33e03ec1f2b8a4530c33cb27996a186c01cf 100644 (file)
@@ -1,10 +1,27 @@
-// 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"
+#include "XGUI_Workshop.h"
+#include "XGUI_ObjectsBrowser.h"
+#include "XGUI_Displayer.h"
 
 #include <ModuleBase_IconFactory.h>
 
@@ -67,6 +84,9 @@ ModelAPI_Document* getSubDocument(void* theObj)
 XGUI_DataModel::XGUI_DataModel(QObject* theParent) : QAbstractItemModel(theParent)//,
   //myIsEventsProcessingBlocked(false)
 {
+  XGUI_ObjectsBrowser* aOB = qobject_cast<XGUI_ObjectsBrowser*>(theParent);
+  myWorkshop = aOB->workshop();
+
   Events_Loop* aLoop = Events_Loop::loop();
   aLoop->registerListener(this, Events_Loop::eventByName(EVENT_OBJECT_CREATED));
   aLoop->registerListener(this, Events_Loop::eventByName(EVENT_OBJECT_DELETED));
@@ -110,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;
@@ -128,15 +152,19 @@ void XGUI_DataModel::processEvent(const std::shared_ptr<Events_Message>& theMess
         }
       } 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());
@@ -207,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());
@@ -237,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;
             }
@@ -262,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);
           }
@@ -294,7 +324,7 @@ void XGUI_DataModel::processEvent(const std::shared_ptr<Events_Message>& theMess
         // 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
@@ -308,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
@@ -349,7 +379,7 @@ ObjectPtr XGUI_DataModel::object(const QModelIndex& theIndex) const
 }
 
 //******************************************************
-QModelIndex XGUI_DataModel::objectIndex(const ObjectPtr theObject) const
+QModelIndex XGUI_DataModel::objectIndex(const ObjectPtr theObject, int theColumn) const
 {
   std::string aType = theObject->groupName();
   DocumentPtr aDoc = theObject->document();
@@ -372,19 +402,14 @@ 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);
         }
       }
     }
     if (aRow == -1)
       return QModelIndex();
     else
-      return createIndex(aRow, 0, theObject.get());
+      return createIndex(aRow, theColumn, theObject.get());
   }
   SessionPtr aSession = ModelAPI_Session::get();
   DocumentPtr aRootDoc = aSession->moduleDocument();
@@ -395,7 +420,7 @@ QModelIndex XGUI_DataModel::objectIndex(const ObjectPtr theObject) const
     // The object from sub document
     aRow += foldersCount(aDoc.get());
   }
-  return createIndex(aRow, 0, theObject.get());
+  return createIndex(aRow, theColumn, theObject.get());
 }
 
 //******************************************************
@@ -406,10 +431,26 @@ QVariant XGUI_DataModel::data(const QModelIndex& theIndex, int theRole) const
   int aNbFolders = foldersCount();
   int theIndexRow = theIndex.row();
 
-  if ((theRole == Qt::DecorationRole) && (theIndex == lastHistoryIndex()))
-    return QIcon(":pictures/arrow.png");
+  if (theRole == Qt::DecorationRole) {
+    if (theIndex == lastHistoryIndex())
+      return QIcon(":pictures/arrow.png");
+    else if (theIndex.column() == 0) {
+      VisibilityState aState = getVisibilityState(theIndex);
+      switch (aState) {
+      case NoneState:
+        return QIcon();
+      case Visible:
+        return QIcon(":pictures/eyeopen.png");
+      case SemiVisible:
+        return QIcon(":pictures/eyemiclosed.png");
+      case Hidden:
+        return QIcon(":pictures/eyeclosed.png");
+      }
+    }
+  }
 
-  if (theIndex.column() == 1)
+  //if (theIndex.column() == 1)
+  if (theIndex.column() != 1)
     return QVariant();
 
   quintptr aParentId = theIndex.internalId();
@@ -582,7 +623,7 @@ int XGUI_DataModel::rowCount(const QModelIndex& theParent) const
 //******************************************************
 int XGUI_DataModel::columnCount(const QModelIndex& theParent) const
 {
-  return 2;
+  return 3;
 }
 
 //******************************************************
@@ -602,7 +643,7 @@ QModelIndex XGUI_DataModel::index(int theRow, int theColumn, const QModelIndex &
       int aObjId = theRow - aNbFolders;
       if (aObjId < aRootDoc->size(aType)) {
         ObjectPtr aObj = aRootDoc->object(aType, aObjId);
-        aIndex = objectIndex(aObj);
+        aIndex = objectIndex(aObj, theColumn);
       }
     }
   } else {
@@ -612,7 +653,7 @@ QModelIndex XGUI_DataModel::index(int theRow, int theColumn, const QModelIndex &
       std::string aType = myXMLReader->rootFolderType(aParentPos);
       if (theRow < aRootDoc->size(aType)) {
         ObjectPtr aObj = aRootDoc->object(aType, theRow);
-        aIndex = objectIndex(aObj);
+        aIndex = objectIndex(aObj, theColumn);
       }
     } else {
       // It is an object which could have children
@@ -627,7 +668,7 @@ QModelIndex XGUI_DataModel::index(int theRow, int theColumn, const QModelIndex &
           std::string aType = myXMLReader->subFolderType(aParentRow);
           if (theRow < aDoc->size(aType)) {
             ObjectPtr aObj = aDoc->object(aType, theRow);
-            aIndex = objectIndex(aObj);
+            aIndex = objectIndex(aObj, theColumn);
           }
         }
       } else {
@@ -644,29 +685,25 @@ QModelIndex XGUI_DataModel::index(int theRow, int theColumn, const QModelIndex &
           } else {
             // this is an object under sub document root
             std::string aType = myXMLReader->subType();
-            int aCount = theRow - aNbSubFolders;
-            // To check number of objects before using
-            if (aSubDoc->size(aType) <= aCount)
-              return QModelIndex();
-            ObjectPtr aObj = aSubDoc->object(aType, aCount);
-            aIndex = objectIndex(aObj);
+            ObjectPtr aObj = aSubDoc->object(aType, theRow - aNbSubFolders);
+            aIndex = objectIndex(aObj, theColumn);
           }
         } else {
           // Check for composite object
           ModelAPI_CompositeFeature* aCompFeature =
             dynamic_cast<ModelAPI_CompositeFeature*>(aParentObj);
           if (aCompFeature) {
-            aIndex = objectIndex(aCompFeature->subFeature(theRow));
+            aIndex = objectIndex(aCompFeature->subFeature(theRow), theColumn);
           } else {
             ModelAPI_ResultCompSolid* aCompRes =
               dynamic_cast<ModelAPI_ResultCompSolid*>(aParentObj);
             if (aCompRes)
-              aIndex = objectIndex(aCompRes->subResult(theRow));
+              aIndex = objectIndex(aCompRes->subResult(theRow), theColumn);
             else {
               ModelAPI_ResultField* aFieldRes =
                 dynamic_cast<ModelAPI_ResultField*>(aParentObj);
               if (aFieldRes) {
-                aIndex = createIndex(theRow, 0, aFieldRes->step(theRow));
+                aIndex = createIndex(theRow, theColumn, aFieldRes->step(theRow));
               }
             }
           }
@@ -674,8 +711,6 @@ QModelIndex XGUI_DataModel::index(int theRow, int theColumn, const QModelIndex &
       }
     }
   }
-  if (theColumn != 0)
-    return createIndex(aIndex.row(), theColumn, aIndex.internalPointer());
   return aIndex;
 }
 
@@ -744,7 +779,7 @@ QModelIndex XGUI_DataModel::parent(const QModelIndex& theIndex) const
         // return first level of folder index
         int aFolderId = myXMLReader->rootFolderId(aType);
         // Items in a one row must have the same parent
-        return createIndex(aFolderId, 0, (void*)Q_NULLPTR);
+        return createIndex(aFolderId, 1, (void*)Q_NULLPTR);
       }
     } else {
       if (aType == myXMLReader->subType())
@@ -753,7 +788,7 @@ QModelIndex XGUI_DataModel::parent(const QModelIndex& theIndex) const
         // return first level of folder index
         int aFolderId = folderId(aType, aSubDoc.get());
         // Items in a one row must have the same parent
-        return createIndex(aFolderId, 0, aSubDoc.get());
+        return createIndex(aFolderId, 1, aSubDoc.get());
       }
     }
   }
@@ -811,15 +846,15 @@ Qt::ItemFlags XGUI_DataModel::flags(const QModelIndex& theIndex) const
   if (aObj) {
     // An object
     if (aObj->isDisabled())
-      return theIndex.column() == 1? Qt::ItemIsSelectable : aNullFlag;
+      return theIndex.column() == 2? Qt::ItemIsSelectable : aNullFlag;
 
     if (aSession->moduleDocument() != aObj->document())
       if (aActiveDoc != aObj->document())
-        return theIndex.column() == 1? Qt::ItemIsSelectable : aNullFlag;
+        return theIndex.column() == 2? Qt::ItemIsSelectable : aNullFlag;
 
     bool isCompositeSub = false;
-    // An object which is sub-object of a composite object can not be accessible in column 1
-    if (theIndex.column() == 1) {
+    // An object which is sub-object of a composite object can not be accessible in column 2
+    if (theIndex.column() == 2) {
       ObjectPtr aObjPtr = aObj->data()->owner();
       FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aObjPtr);
       if (aFeature.get()) {
@@ -856,7 +891,8 @@ Qt::ItemFlags XGUI_DataModel::flags(const QModelIndex& theIndex) const
 }
 
 //******************************************************
-QModelIndex XGUI_DataModel::findDocumentRootIndex(const ModelAPI_Document* theDoc) const
+QModelIndex
+  XGUI_DataModel::findDocumentRootIndex(const ModelAPI_Document* theDoc, int aColumn) const
 {
   SessionPtr aSession = ModelAPI_Session::get();
   DocumentPtr aRootDoc = aSession->moduleDocument();
@@ -872,7 +908,7 @@ QModelIndex XGUI_DataModel::findDocumentRootIndex(const ModelAPI_Document* theDo
         if (myXMLReader->rootType() == ModelAPI_Feature::group()) {
           aRow += foldersCount();
         }
-        return createIndex(aRow, 0, aObj.get());
+        return createIndex(aRow, aColumn, aObj.get());
       }
     }
   } else { // If document is attached to feature
@@ -886,7 +922,7 @@ QModelIndex XGUI_DataModel::findDocumentRootIndex(const ModelAPI_Document* theDo
         int aRow = i;
         if (myXMLReader->rootType() == ModelAPI_Feature::group())
           aRow += foldersCount();
-        return createIndex(aRow, 0, aObj.get());
+        return createIndex(aRow, aColumn, aObj.get());
       }
     }
   }
@@ -894,14 +930,14 @@ QModelIndex XGUI_DataModel::findDocumentRootIndex(const ModelAPI_Document* theDo
 }
 
 //******************************************************
-QModelIndex XGUI_DataModel::documentRootIndex(DocumentPtr theDoc) const
+QModelIndex XGUI_DataModel::documentRootIndex(DocumentPtr theDoc, int theColumn) const
 {
   SessionPtr aSession = ModelAPI_Session::get();
   DocumentPtr aRootDoc = aSession->moduleDocument();
   if (theDoc == aRootDoc)
     return QModelIndex();
   else
-    return findDocumentRootIndex(theDoc.get());
+    return findDocumentRootIndex(theDoc.get(), theColumn);
 }
 
 //******************************************************
@@ -984,12 +1020,12 @@ QModelIndex XGUI_DataModel::lastHistoryIndex() const
   FeaturePtr aFeature = aCurDoc->currentFeature(true);
   if (aFeature.get()) {
     QModelIndex aInd = objectIndex(aFeature);
-    return createIndex(aInd.row(), 1, aInd.internalPointer());
+    return createIndex(aInd.row(), 2, aInd.internalPointer());
   } else {
     if (aCurDoc == aSession->moduleDocument())
-      return createIndex(foldersCount() - 1, 1, -1);
+      return createIndex(foldersCount() - 1, 2, -1);
     else
-      return createIndex(foldersCount(aCurDoc.get()) - 1, 1, aCurDoc.get());
+      return createIndex(foldersCount(aCurDoc.get()) - 1, 2, aCurDoc.get());
   }
 }
 
@@ -1042,3 +1078,42 @@ void XGUI_DataModel::rebuildBranch(int theRow, int theCount, const QModelIndex&
 //  myIsEventsProcessingBlocked = theState;
 //  return aPreviousState;
 //}
+
+//******************************************************
+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);
+    if (aResObj.get()) {
+      XGUI_Displayer* aDisplayer = myWorkshop->displayer();
+      ResultCompSolidPtr aCompRes = std::dynamic_pointer_cast<ModelAPI_ResultCompSolid>(aResObj);
+      if (aCompRes.get()) {
+        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;
+          if (aState == NoneState)
+            aState = aS;
+          else if (aState != aS) {
+            aState = SemiVisible;
+            break;
+          }
+        }
+        return aState;
+      } else {
+        if (aDisplayer->isVisible(aResObj))
+          return Visible;
+        else
+          return Hidden;
+      }
+    }
+  }
+  return NoneState;
+}