Salome HOME
Issue #2916: Remember expanded items before Object Browser update and restore them...
authorvsv <vsv@opencascade.com>
Tue, 28 May 2019 12:45:56 +0000 (15:45 +0300)
committervsv <vsv@opencascade.com>
Tue, 28 May 2019 12:45:56 +0000 (15:45 +0300)
src/XGUI/XGUI_DataModel.cpp
src/XGUI/XGUI_DataModel.h
src/XGUI/XGUI_ObjectsBrowser.cpp
src/XGUI/XGUI_ObjectsBrowser.h

index a6aa8cba1b20515955ada266dc8707b785285836..6594e26ef8491a73eb3440da589695ef0fbc8c38 100644 (file)
@@ -70,17 +70,22 @@ void XGUI_DataModel::processEvent(const std::shared_ptr<Events_Message>& theMess
     }
     if (aCreated.length() == 0)
       return;
+
+    emit beforeTreeRebuild();
     QTreeNodesList aNodes = myRoot->objectCreated(aCreated);
     ModuleBase_ITreeNode* aParent;
     int aRow = 0;
     QModelIndex aParentIndex1, aParentIndex2;
     ObjectPtr aObj;
+    bool aRebuildAll = false;
+    bool isInserted = false;
+
     foreach(ModuleBase_ITreeNode* aNode, aNodes) {
       aObj = aNode->object();
       aParent = aNode->parent();
       if (aObj.get() && (aObj->groupName() == ModelAPI_Folder::group())) {
         aParent->update();
-        rebuildDataTree();
+        aRebuildAll = true;
       }
       else {
         aRow = aParent->nodeRow(aNode);
@@ -88,8 +93,15 @@ void XGUI_DataModel::processEvent(const std::shared_ptr<Events_Message>& theMess
         aParentIndex2 = getParentIndex(aNode, 2);
         insertRows(aRow, 1, aParentIndex1);
         dataChanged(aParentIndex1, aParentIndex2);
+        isInserted = true;
       }
     }
+    if (aRebuildAll)
+      rebuildDataTree();
+    else if (isInserted)
+      endInsertRows();
+
+    emit treeRebuilt();
   }
   else if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_OBJECT_DELETED)) {
     std::shared_ptr<ModelAPI_ObjectDeletedMessage> aUpdMsg =
@@ -98,6 +110,7 @@ void XGUI_DataModel::processEvent(const std::shared_ptr<Events_Message>& theMess
       aUpdMsg->groups();
     QTreeNodesList aList;
     std::list<std::pair<std::shared_ptr<ModelAPI_Document>, std::string>>::const_iterator aIt;
+    emit beforeTreeRebuild();
     for (aIt = aMsgGroups.cbegin(); aIt != aMsgGroups.cend(); aIt++) {
       aList.append(myRoot->objectsDeleted(aIt->first, aIt->second.c_str()));
     }
@@ -113,6 +126,7 @@ void XGUI_DataModel::processEvent(const std::shared_ptr<Events_Message>& theMess
         aNode->parent()->update();
     }
     rebuildDataTree();
+    emit treeRebuilt();
   }
   else if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_OBJECT_UPDATED)) {
     std::shared_ptr<ModelAPI_ObjectUpdatedMessage> aUpdMsg =
@@ -122,6 +136,7 @@ void XGUI_DataModel::processEvent(const std::shared_ptr<Events_Message>& theMess
     QObjectPtrList aCreated;
     std::set<ObjectPtr>::const_iterator aIt;
     bool aRebuildAll = false;
+    emit beforeTreeRebuild();
     for (aIt = aObjects.cbegin(); aIt != aObjects.cend(); aIt++) {
       ObjectPtr aObj = (*aIt);
       if (!aObj->isInHistory())
@@ -137,7 +152,6 @@ void XGUI_DataModel::processEvent(const std::shared_ptr<Events_Message>& theMess
     }
     if (aRebuildAll) {
       myRoot->update();
-      rebuildDataTree();
     }
     else {
       QSet<ModuleBase_ITreeNode*> aParents;
@@ -160,8 +174,9 @@ void XGUI_DataModel::processEvent(const std::shared_ptr<Events_Message>& theMess
       foreach(ModuleBase_ITreeNode* aNode, aParents) {
         aNode->update();
       }
-      rebuildDataTree();
     }
+    rebuildDataTree();
+    emit treeRebuilt();
   }
   else if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_ORDER_UPDATED)) {
     std::shared_ptr<ModelAPI_OrderUpdatedMessage> aUpdMsg =
@@ -171,8 +186,10 @@ void XGUI_DataModel::processEvent(const std::shared_ptr<Events_Message>& theMess
       std::string aGroup = aUpdMsg->reordered()->group();
       ModuleBase_ITreeNode* aNode = myRoot->findParent(aDoc, aGroup.c_str());
       if (aNode) {
+        emit beforeTreeRebuild();
         aNode->update();
         rebuildDataTree();
+        emit treeRebuilt();
       }
     }
   }
@@ -197,6 +214,11 @@ void XGUI_DataModel::processEvent(const std::shared_ptr<Events_Message>& theMess
         aCreated.append(aObj);
       }
     }
+    if (aCreated.length() == 0)
+      return;
+    bool isInsert = false;
+    bool isRemove = false;
+    emit beforeTreeRebuild();
     foreach(ObjectPtr aObj, aCreated) {
       ModuleBase_ITreeNode* aNode = myRoot->subNode(aObj);
       if (aNode) {
@@ -209,16 +231,23 @@ void XGUI_DataModel::processEvent(const std::shared_ptr<Events_Message>& theMess
 
         if (aNewNb > aOldNb) {
           insertRows(aOldNb - 1, aNewNb - aOldNb, aFirstIdx);
+          isInsert = true;
         }
         else if (aNewNb < aOldNb) {
           if (aNewNb)
             removeRows(aNewNb - 1, aOldNb - aNewNb, aFirstIdx);
           else if (aOldNb)
             removeRows(0, aOldNb, aFirstIdx);
+          isRemove = aNewNb || aOldNb;
         }
         dataChanged(aFirstIdx, aLastIdx);
       }
     }
+    if (isRemove)
+      endRemoveRows();
+    if (isInsert)
+      endInsertRows();
+    emit treeRebuilt();
   }
 }
 
@@ -234,7 +263,6 @@ void XGUI_DataModel::rebuildDataTree()
 {
   beginResetModel();
   endResetModel();
-  emit treeRebuilt();
 }
 
 //******************************************************
@@ -319,7 +347,6 @@ bool XGUI_DataModel::hasChildren(const QModelIndex& theParent) const
 bool XGUI_DataModel::insertRows(int theRow, int theCount, const QModelIndex& theParent)
 {
   beginInsertRows(theParent, theRow, theRow + theCount - 1);
-  endInsertRows();
   return true;
 }
 
@@ -327,7 +354,6 @@ bool XGUI_DataModel::insertRows(int theRow, int theCount, const QModelIndex& the
 bool XGUI_DataModel::removeRows(int theRow, int theCount, const QModelIndex& theParent)
 {
   beginRemoveRows(theParent, theRow, theRow + theCount - 1);
-  endRemoveRows();
   return true;
 }
 
index f5805c328f57788d7aa0d7ef47455f6c8b248cdd..6ca86ce55a297daca72483fb7511b769df55c2ff 100644 (file)
@@ -157,7 +157,12 @@ public:
 
   DocumentPtr document(const QModelIndex& theIndex) const;
 
+  QModelIndex getIndex(ModuleBase_ITreeNode* theNode, int thCol) const;
+
 signals:
+  void beforeTreeRebuild();
+
+
   /// Signal about tree had been rebuilt
   void treeRebuilt();
 
@@ -171,8 +176,6 @@ private:
 
   QModelIndex getParentIndex(ModuleBase_ITreeNode* theNode, int thCol) const;
 
-  QModelIndex getIndex(ModuleBase_ITreeNode* theNode, int thCol) const;
-
   void updateSubTree(ModuleBase_ITreeNode* theParent);
 
   /// Find a root index which contains objects of the given document
index 44858157f45c123c9cbf6905c08b6afc61eb02b5..0253ec56dfe0d4039312e1eda85fdb33734905ac 100644 (file)
@@ -49,7 +49,6 @@
 #define FIRST_COL_WIDTH 20
 #define SECOND_COL_WIDTH 30
 
-
 /**
 * \ingroup GUI
 * Tree item delegate for definition of data in column items editor
@@ -416,7 +415,7 @@ XGUI_ObjectsBrowser::XGUI_ObjectsBrowser(QWidget* theParent, XGUI_Workshop* theW
   aLabelWgt->setPalette(aPalet);
 
   myDocModel = new XGUI_DataModel(this);
-  connect(myDocModel, SIGNAL(modelAboutToBeReset()), SLOT(onBeforeReset()));
+  connect(myDocModel, SIGNAL(beforeTreeRebuild()), SLOT(onBeforeReset()));
   connect(myDocModel, SIGNAL(treeRebuilt()), SLOT(onAfterModelReset()));
 
   connect(myTreeView, SIGNAL(contextMenuRequested(QContextMenuEvent*)), this,
@@ -509,16 +508,17 @@ void XGUI_ObjectsBrowser::onEditItem()
 }
 
 //***************************************************
-QModelIndexList XGUI_ObjectsBrowser::expandedItems(const QModelIndex& theParent) const
+QList<ModuleBase_ITreeNode*> XGUI_ObjectsBrowser::expandedItems(const QModelIndex& theParent) const
 {
-  QModelIndexList aIndexes;
+  QList<ModuleBase_ITreeNode*> aIndexes;
   QModelIndex aIndex;
-  for (int i = 0; i < myDocModel->rowCount(theParent); i++) {
+  int aCount = myDocModel->rowCount(theParent);
+  for (int i = 0; i < aCount; i++) {
     aIndex = myDocModel->index(i, 0, theParent);
     if (myDocModel->hasChildren(aIndex)) {
       if (myTreeView->isExpanded(aIndex)) {
-        aIndexes.append(aIndex);
-        QModelIndexList aSubIndexes = expandedItems(aIndex);
+        aIndexes.append((ModuleBase_ITreeNode*)aIndex.internalPointer());
+        QList<ModuleBase_ITreeNode*> aSubIndexes = expandedItems(aIndex);
         if (!aSubIndexes.isEmpty())
           aIndexes.append(aSubIndexes);
       }
@@ -655,8 +655,11 @@ void XGUI_ObjectsBrowser::onBeforeReset()
 
 void XGUI_ObjectsBrowser::onAfterModelReset()
 {
-  foreach(QModelIndex aIndex, myExpandedItems) {
-    if (myTreeView->dataModel()->hasIndex(aIndex))
+  XGUI_DataModel* aModel = myTreeView->dataModel();
+  QModelIndex aIndex;
+  foreach(ModuleBase_ITreeNode* aNode, myExpandedItems) {
+    aIndex = aModel->getIndex(aNode, 0);
+    if (aIndex.isValid() && (myTreeView->dataModel()->hasIndex(aIndex)) )
       myTreeView->setExpanded(aIndex, true);
   }
 }
index 6be4b39da9cb0fa23c4d0d88f10cd46d9d15d6e1..f3e335eefd4cedeb448d075fdce4def7167cd6e4 100644 (file)
@@ -263,7 +263,7 @@ protected:
   void onAfterModelReset();
 
  private:
-  QModelIndexList expandedItems(const QModelIndex& theParent = QModelIndex()) const;
+   QList<ModuleBase_ITreeNode*> expandedItems(const QModelIndex& theParent = QModelIndex()) const;
 
   //! Internal model
   XGUI_DataModel* myDocModel;
@@ -272,7 +272,8 @@ protected:
   XGUI_Workshop* myWorkshop;
 
   /// A field to store expanded items before model reset
-  QModelIndexList myExpandedItems;
+  //QModelIndexList myExpandedItems;
+  QList<ModuleBase_ITreeNode*> myExpandedItems;
 };
 
 #endif