Fix component remove

master
Jeremy Hu 2018-08-31 09:40:53 +08:00
parent 134078fc05
commit 6bab154cf5
3 changed files with 25 additions and 5 deletions

View File

@ -1534,14 +1534,15 @@ void SkeletonDocument::removeComponentRecursively(QUuid componentId)
return; return;
} }
for (const auto &childId: component->second.childrenIds) {
removeComponent(childId);
}
if (!component->second.linkToPartId.isNull()) { if (!component->second.linkToPartId.isNull()) {
removePartDontCareComponent(component->second.linkToPartId); removePartDontCareComponent(component->second.linkToPartId);
} }
auto childrenIds = component->second.childrenIds;
for (const auto &childId: childrenIds) {
removeComponentRecursively(childId);
}
QUuid parentId = component->second.parentId; QUuid parentId = component->second.parentId;
if (!parentId.isNull()) { if (!parentId.isNull()) {
auto parentComponent = componentMap.find(parentId); auto parentComponent = componentMap.find(parentId);

View File

@ -375,6 +375,24 @@ void SkeletonPartTreeWidget::addComponentChildrenToItem(QUuid componentId, QTree
} }
} }
void SkeletonPartTreeWidget::deleteItemChildren(QTreeWidgetItem *item)
{
auto children = item->takeChildren();
while (!children.isEmpty()) {
auto first = children.takeFirst();
auto componentId = QUuid(first->data(0, Qt::UserRole).toString());
const SkeletonComponent *component = m_document->findComponent(componentId);
if (nullptr != component) {
m_componentItemMap.erase(componentId);
if (!component->linkToPartId.isNull()) {
m_partItemMap.erase(component->linkToPartId);
}
}
deleteItemChildren(first);
delete first;
}
}
void SkeletonPartTreeWidget::componentChildrenChanged(QUuid componentId) void SkeletonPartTreeWidget::componentChildrenChanged(QUuid componentId)
{ {
QTreeWidgetItem *parentItem = findComponentItem(componentId); QTreeWidgetItem *parentItem = findComponentItem(componentId);
@ -382,7 +400,7 @@ void SkeletonPartTreeWidget::componentChildrenChanged(QUuid componentId)
qDebug() << "Find component item failed:" << componentId; qDebug() << "Find component item failed:" << componentId;
return; return;
} }
qDeleteAll(parentItem->takeChildren()); deleteItemChildren(parentItem);
addComponentChildrenToItem(componentId, parentItem); addComponentChildrenToItem(componentId, parentItem);
// Fix the last item show in the wrong place sometimes // Fix the last item show in the wrong place sometimes

View File

@ -67,6 +67,7 @@ protected:
virtual void mousePressEvent(QMouseEvent *event); virtual void mousePressEvent(QMouseEvent *event);
private: private:
void addComponentChildrenToItem(QUuid componentId, QTreeWidgetItem *parentItem); void addComponentChildrenToItem(QUuid componentId, QTreeWidgetItem *parentItem);
void deleteItemChildren(QTreeWidgetItem *item);
private: private:
const SkeletonDocument *m_document = nullptr; const SkeletonDocument *m_document = nullptr;
std::map<QUuid, QTreeWidgetItem *> m_partItemMap; std::map<QUuid, QTreeWidgetItem *> m_partItemMap;