//-------------------------------------------------------------------------------------------------- /// Makes the destinationSubTreeRoot tree become identical to the tree in sourceSubTreeRoot, /// calling begin..() end..() to make the UI update accordingly. /// This assumes that all the items have a pointer an unique PdmObject //-------------------------------------------------------------------------------------------------- void UiTreeModelPdm::updateModelSubTree(const QModelIndex& modelIdxOfDestinationSubTreeRoot, PdmUiTreeItem* destinationSubTreeRoot, PdmUiTreeItem* sourceSubTreeRoot) { // First loop over children in the old ui tree, deleting the ones not present in // the newUiTree for (int resultChildIdx = 0; resultChildIdx < destinationSubTreeRoot->childCount() ; ++resultChildIdx) { PdmUiTreeItem* oldChild = destinationSubTreeRoot->child(resultChildIdx); int childIndex = sourceSubTreeRoot->findChildItemIndex(oldChild->dataObject()); if (childIndex == -1) // Not found { this->beginRemoveRows(modelIdxOfDestinationSubTreeRoot, resultChildIdx, resultChildIdx); destinationSubTreeRoot->removeChildren(resultChildIdx, 1); this->endRemoveRows(); resultChildIdx--; } } // Then loop over the children in the new ui tree, finding the corresponding items in the old tree. // If they are found, we move them to the correct position. // If not found, we pulls the item out of the old ui tree, inserting it into the new tree to avoid the default delete operation in ~UiTreeItem() int sourceChildCount = sourceSubTreeRoot->childCount(); int sourceChildIdx = 0; for (int resultChildIdx = 0; resultChildIdx < sourceChildCount; ++resultChildIdx, ++sourceChildIdx) { PdmUiTreeItem* newChild = sourceSubTreeRoot->child(sourceChildIdx); int childIndex = destinationSubTreeRoot->findChildItemIndex(newChild->dataObject()); if (childIndex == -1) // Not found { this->beginInsertRows(modelIdxOfDestinationSubTreeRoot, resultChildIdx, resultChildIdx); destinationSubTreeRoot->insertChild(resultChildIdx, newChild); this->endInsertRows(); sourceSubTreeRoot->removeChildrenNoDelete(sourceChildIdx, 1); sourceChildIdx--; } else if (childIndex != resultChildIdx) // Found, but must be moved { assert(childIndex > resultChildIdx); PdmUiTreeItem* oldChild = destinationSubTreeRoot->child(childIndex); this->beginMoveRows(modelIdxOfDestinationSubTreeRoot, childIndex, childIndex, modelIdxOfDestinationSubTreeRoot, resultChildIdx); destinationSubTreeRoot->removeChildrenNoDelete(childIndex, 1); destinationSubTreeRoot->insertChild(resultChildIdx, oldChild); this->endMoveRows(); updateModelSubTree( index(resultChildIdx, 0, modelIdxOfDestinationSubTreeRoot), oldChild, newChild); } else // Found the corresponding item in the right place. { PdmUiTreeItem* oldChild = destinationSubTreeRoot->child(childIndex); updateModelSubTree( index(resultChildIdx, 0, modelIdxOfDestinationSubTreeRoot), oldChild, newChild); } } }
//-------------------------------------------------------------------------------------------------- /// Makes the olUiTreeRoot tree become identical to the tree in newUiTreeRoot, /// calling begin..() end..() to make the UI update accordingly. /// This assumes that all the items have a pointer an unique PdmObject //-------------------------------------------------------------------------------------------------- void UiTreeModelPdm::updateModelSubTree(const QModelIndex& uiSubTreeRootModelIdx, PdmUiTreeItem* uiModelSubTreeRoot, PdmUiTreeItem* updatedPdmSubTreeRoot) { // First loop over children in the old ui tree, deleting the ones not present in // the newUiTree for (int i = 0; i < uiModelSubTreeRoot->childCount() ; ++i) { PdmUiTreeItem* oldChild = uiModelSubTreeRoot->child(i); int childIndex = updatedPdmSubTreeRoot->findChildItemIndex(oldChild->dataObject()); if (childIndex == -1) // Not found { this->beginRemoveRows(uiSubTreeRootModelIdx, i, i); uiModelSubTreeRoot->removeChildren(i, 1); this->endRemoveRows(); i--; } } // Then loop over the children in the new ui tree, finding the corresponding items in the old tree. // If they are found, we move them to the correct position. // If not found, we pulls the item out of the old ui tree, inserting it into the old tree. for (int i = 0; i < updatedPdmSubTreeRoot->childCount() ; ++i) { PdmUiTreeItem* newChild = updatedPdmSubTreeRoot->child(i); int childIndex = uiModelSubTreeRoot->findChildItemIndex(newChild->dataObject()); if (childIndex == -1) // Not found { this->beginInsertRows(uiSubTreeRootModelIdx, i, i); uiModelSubTreeRoot->insertChild(i, newChild); this->endInsertRows(); updatedPdmSubTreeRoot->removeChildrenNoDelete(i, 1); i--; } else if (childIndex != i) // Found, but must be moved { assert(childIndex > i); PdmUiTreeItem* oldChild = uiModelSubTreeRoot->child(childIndex); this->beginMoveRows(uiSubTreeRootModelIdx, childIndex, childIndex, uiSubTreeRootModelIdx, i); uiModelSubTreeRoot->removeChildrenNoDelete(childIndex, 1); uiModelSubTreeRoot->insertChild(i, oldChild); this->endMoveRows(); updateModelSubTree( index(i, 0, uiSubTreeRootModelIdx) ,oldChild, newChild); } else // Found the corresponding item in the right place. { PdmUiTreeItem* oldChild = uiModelSubTreeRoot->child(childIndex); updateModelSubTree( index(i, 0, uiSubTreeRootModelIdx) ,oldChild, newChild); } } }
//-------------------------------------------------------------------------------------------------- /// Refreshes the UI-tree below the supplied root PdmObject //-------------------------------------------------------------------------------------------------- void UiTreeModelPdm::updateUiSubTree(PdmObject* pdmRoot) { // Build the new "Correct" Tree PdmUiTreeItem* tempUpdatedPdmTree = UiTreeItemBuilderPdm::buildViewItems(NULL, -1, pdmRoot); // Find the corresponding entry for "root" in the existing Ui tree QModelIndex uiSubTreeRootModelIdx = getModelIndexFromPdmObject(pdmRoot); PdmUiTreeItem* uiModelSubTreeRoot = NULL; if (uiSubTreeRootModelIdx.isValid()) { uiModelSubTreeRoot = getTreeItemFromIndex(uiSubTreeRootModelIdx); } else { uiModelSubTreeRoot = m_treeItemRoot; } updateModelSubTree(uiSubTreeRootModelIdx, uiModelSubTreeRoot, tempUpdatedPdmTree); delete tempUpdatedPdmTree; }