Salome HOME
Fix for the issue #2753 : error when dump/load script
[modules/shaper.git] / src / XGUI / XGUI_ObjectsBrowser.cpp
index 6c2e61ffae991a90c92db7162c75805d6497d0cf..b08ccc36e8fd56b2ca403aef51190fc6f529b660 100644 (file)
 #include <ModelAPI_Data.h>
 #include <ModelAPI_Session.h>
 #include <ModelAPI_Document.h>
-#include <ModelAPI_ResultCompSolid.h>
 #include <ModelAPI_Tools.h>
 
 #include <ModuleBase_Tools.h>
+#include <ModuleBase_ITreeNode.h>
 
 #include <XGUI_Workshop.h>
 
@@ -212,7 +212,7 @@ void XGUI_DataTree::processHistoryChange(const QModelIndex& theIndex)
     if ((theIndex.internalId() == 0) && (aDoc != aMgr->moduleDocument()))
       // Clicked folder under root but active document is another
       return;
-    if ((theIndex.internalId() != 0) && (aDoc.get() != theIndex.internalPointer()))
+    if ((theIndex.internalId() != 0) && (aDoc != aModel->document(theIndex)))
       // Cliced not on active document folder
       return;
 
@@ -220,7 +220,6 @@ void XGUI_DataTree::processHistoryChange(const QModelIndex& theIndex)
     aDoc->setCurrentFeature(FeaturePtr(), true);
     aMgr->finishOperation();
   }
-  QModelIndex aNewIndex = aModel->lastHistoryIndex();
   QModelIndex aParent = theIndex.parent();
   int aSize = aModel->rowCount(aParent);
   for (int i = 0; i < aSize; i++) {
@@ -426,9 +425,10 @@ XGUI_ObjectsBrowser::~XGUI_ObjectsBrowser()
 {
 }
 
-void XGUI_ObjectsBrowser::setXMLReader(Config_DataModelReader* theReader)
+void XGUI_ObjectsBrowser::initialize(ModuleBase_ITreeNode* theRoot)
 {
-  myDocModel->setXMLReader(theReader);
+  //myDocModel->setXMLReader(theReader);
+  myDocModel->setRoot(theRoot);
   myTreeView->setModel(myDocModel);
 
   // It has to be done after setting of model
@@ -525,6 +525,7 @@ QModelIndexList XGUI_ObjectsBrowser::expandedItems(const QModelIndex& theParent)
 //***************************************************
 void XGUI_ObjectsBrowser::rebuildDataTree()
 {
+  myDocModel->root()->update();
   myDocModel->rebuildDataTree();
   update();
 }
@@ -545,6 +546,20 @@ void XGUI_ObjectsBrowser::setObjectsSelected(const QObjectPtrList& theObjects)
   }
 }
 
+//***************************************************
+void XGUI_ObjectsBrowser::ensureVisible(const ObjectPtr theObject)
+{
+  QModelIndex aIndex = myDocModel->objectIndex(theObject);
+  if (aIndex.isValid())  {
+    QModelIndex aParent = aIndex.parent();
+    while (aParent.isValid()) {
+      myTreeView->expand(aParent);
+      aParent = aParent.parent();
+    }
+    myTreeView->scrollTo(aIndex);
+  }
+}
+
 //***************************************************
 void XGUI_ObjectsBrowser::clearContent()
 {
@@ -592,7 +607,8 @@ void XGUI_ObjectsBrowser::onBeforeReset()
 void XGUI_ObjectsBrowser::onAfterModelReset()
 {
   foreach(QModelIndex aIndex, myExpandedItems) {
-    myTreeView->setExpanded(aIndex, true);
+    if (myTreeView->dataModel()->hasIndex(aIndex))
+      myTreeView->setExpanded(aIndex, true);
   }
 }
 
@@ -632,9 +648,10 @@ void XGUI_ObjectsBrowser::updateAllIndexes(int theColumn, const QModelIndex& the
   int aNb = myDocModel->rowCount(theParent);
   for (int i = 0; i < aNb; i++) {
     QModelIndex aIdx = myDocModel->index(i, theColumn, theParent);
-    if (aIdx.isValid()) {
+    if (aIdx.isValid() && myDocModel->hasIndex(aIdx)) {
       myTreeView->update(aIdx);
-      updateAllIndexes(theColumn, aIdx);
+      if (myTreeView->isExpanded(aIdx))
+        updateAllIndexes(theColumn, aIdx);
     }
   }
 }
@@ -647,7 +664,7 @@ QMap<ObjectPtr, bool> XGUI_ObjectsBrowser::getFoldersState(DocumentPtr theDoc) c
   ObjectPtr aObj;
   for (int i = 0; i < aNb; i++) {
     aObj = theDoc->object(ModelAPI_Folder::group(), i);
-    QModelIndex aIdx = myDocModel->objectIndex(aObj);
+    QModelIndex aIdx = myDocModel->objectIndex(aObj, 0);
     aMap[aObj] = myTreeView->isExpanded(aIdx);
   }
   return aMap;
@@ -657,7 +674,7 @@ void XGUI_ObjectsBrowser::setFoldersState(const QMap<ObjectPtr, bool>& theStates
 {
   QMap<ObjectPtr, bool>::const_iterator aIt;
   for (aIt = theStates.constBegin(); aIt != theStates.constEnd(); aIt++) {
-    QModelIndex aIdx = myDocModel->objectIndex(aIt.key());
+    QModelIndex aIdx = myDocModel->objectIndex(aIt.key(), 0);
     myTreeView->setExpanded(aIdx, aIt.value());
   }
 }