Salome HOME
Fix for the issue #2753 : error when dump/load script
[modules/shaper.git] / src / XGUI / XGUI_ObjectsBrowser.cpp
index 5b86c1c0338802ccd92a85e420016d1b66dc3b49..b08ccc36e8fd56b2ca403aef51190fc6f529b660 100644 (file)
@@ -28,6 +28,9 @@
 #include <ModelAPI_Tools.h>
 
 #include <ModuleBase_Tools.h>
+#include <ModuleBase_ITreeNode.h>
+
+#include <XGUI_Workshop.h>
 
 #include <QLayout>
 #include <QLineEdit>
@@ -43,7 +46,7 @@
 #endif
 
 /// Width of second column (minimum acceptable = 27)
-#define FIRST_COL_WIDTH 30
+#define FIRST_COL_WIDTH 20
 #define SECOND_COL_WIDTH 30
 
 
@@ -147,7 +150,7 @@ void XGUI_DataTree::resizeEvent(QResizeEvent* theEvent)
   QSize aSize = theEvent->size();
   if (aSize.isValid()) {
     setColumnWidth(0, FIRST_COL_WIDTH);
-    setColumnWidth(1, aSize.width() - SECOND_COL_WIDTH - FIRST_COL_WIDTH - 12);
+    setColumnWidth(1, aSize.width() - SECOND_COL_WIDTH - FIRST_COL_WIDTH - 10);
     setColumnWidth(2, SECOND_COL_WIDTH);
   }
 }
@@ -159,10 +162,12 @@ void XGUI_DataTree::mousePressEvent(QMouseEvent* theEvent)
   if (theEvent->button() != Qt::MidButton)
     return;
   QModelIndex aInd = indexAt(theEvent->pos());
-  QString aTxt = QString("r=%1 c=%2 p=%3").arg(aInd.row()).arg(aInd.column()).arg((long)aInd.internalPointer());
+  QString aTxt =
+    QString("r=%1 c=%2 p=%3").arg(aInd.row()).arg(aInd.column()).arg((long)aInd.internalPointer());
 
   QModelIndex aPar = aInd.parent();
-  QString aTxt1 = QString("r=%1 c=%2 p=%3").arg(aPar.row()).arg(aPar.column()).arg((long)aPar.internalPointer());
+  QString aTxt1 =
+    QString("r=%1 c=%2 p=%3").arg(aPar.row()).arg(aPar.column()).arg((long)aPar.internalPointer());
   QToolTip::showText(theEvent->globalPos(), aTxt + '\n' + aTxt1);
 }
 #endif
@@ -207,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;
 
@@ -215,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++) {
@@ -231,13 +235,23 @@ void XGUI_DataTree::processEyeClick(const QModelIndex& theIndex)
   ObjectPtr aObj = aModel->object(theIndex);
   if (aObj.get()) {
     ResultPtr aResObj = std::dynamic_pointer_cast<ModelAPI_Result>(aObj);
+    XGUI_ObjectsBrowser* aObjBrowser = qobject_cast<XGUI_ObjectsBrowser*>(parent());
     if (aResObj.get()) {
-      aResObj->setDisplayed(!aResObj->isDisplayed());
+      std::set<ObjectPtr> anObjects;
+      anObjects.insert(aResObj);
+
+      bool hasHiddenState = aModel->hasHiddenState(theIndex);
+      if (aObjBrowser && hasHiddenState && !aObjBrowser->workshop()->prepareForDisplay(anObjects))
+        return;
+      if (hasHiddenState) // #issue 2335(hide all faces then show solid problem)
+        aResObj->setDisplayed(true);
+      else
+        aResObj->setDisplayed(!aResObj->isDisplayed());
       Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY));
       update(theIndex);
     }
-    // Update list of selected objects because this event happens after selection event in object browser
-    XGUI_ObjectsBrowser* aObjBrowser = qobject_cast<XGUI_ObjectsBrowser*>(parent());
+    // Update list of selected objects because this event happens after
+    // selection event in object browser
     if (aObjBrowser) {
       aObjBrowser->onSelectionChanged();
     }
@@ -411,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
@@ -510,6 +525,7 @@ QModelIndexList XGUI_ObjectsBrowser::expandedItems(const QModelIndex& theParent)
 //***************************************************
 void XGUI_ObjectsBrowser::rebuildDataTree()
 {
+  myDocModel->root()->update();
   myDocModel->rebuildDataTree();
   update();
 }
@@ -530,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()
 {
@@ -577,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);
   }
 }
 
@@ -588,7 +619,7 @@ std::list<bool> XGUI_ObjectsBrowser::getStateForDoc(DocumentPtr theDoc) const
   QModelIndex aRootIdx = aModel->documentRootIndex(theDoc);
   int aNbChild = aModel->rowCount(aRootIdx);
   for (int i = 0; i < aNbChild; i++) {
-    QModelIndex aIdx = aModel->index(i, 1, aRootIdx);
+    QModelIndex aIdx = aModel->index(i, 0, aRootIdx);
     aStates.push_back(myTreeView->isExpanded(aIdx));
   }
   return aStates;
@@ -611,3 +642,39 @@ void XGUI_ObjectsBrowser::setStateForDoc(DocumentPtr theDoc, const std::list<boo
     myTreeView->setExpanded(aIdx, (*aIt));
   }
 }
+
+void XGUI_ObjectsBrowser::updateAllIndexes(int theColumn, const QModelIndex& theParent)
+{
+  int aNb = myDocModel->rowCount(theParent);
+  for (int i = 0; i < aNb; i++) {
+    QModelIndex aIdx = myDocModel->index(i, theColumn, theParent);
+    if (aIdx.isValid() && myDocModel->hasIndex(aIdx)) {
+      myTreeView->update(aIdx);
+      if (myTreeView->isExpanded(aIdx))
+        updateAllIndexes(theColumn, aIdx);
+    }
+  }
+}
+
+QMap<ObjectPtr, bool> XGUI_ObjectsBrowser::getFoldersState(DocumentPtr theDoc) const
+{
+  QMap<ObjectPtr, bool> aMap;
+
+  int aNb = theDoc->size(ModelAPI_Folder::group());
+  ObjectPtr aObj;
+  for (int i = 0; i < aNb; i++) {
+    aObj = theDoc->object(ModelAPI_Folder::group(), i);
+    QModelIndex aIdx = myDocModel->objectIndex(aObj, 0);
+    aMap[aObj] = myTreeView->isExpanded(aIdx);
+  }
+  return aMap;
+}
+
+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(), 0);
+    myTreeView->setExpanded(aIdx, aIt.value());
+  }
+}