From a0911a69fdfc9182eb8bde509867198063853e34 Mon Sep 17 00:00:00 2001 From: huxingyi Date: Sun, 12 Apr 2020 16:18:24 +0930 Subject: [PATCH] Optimize nodes deletion [skip ci] If all nodes belong to a part are selected to remove, then the part got removed, remove part is more light weight than remove nodes and edges --- src/document.cpp | 1 + src/documentwindow.cpp | 1 + src/skeletongraphicswidget.cpp | 59 ++++++++++++++++++++++++++++++++-- src/skeletongraphicswidget.h | 1 + 4 files changed, 60 insertions(+), 2 deletions(-) diff --git a/src/document.cpp b/src/document.cpp index a335778d..4a171ac7 100644 --- a/src/document.cpp +++ b/src/document.cpp @@ -3209,6 +3209,7 @@ void Document::paste() Snapshot snapshot; loadSkeletonFromXmlStream(&snapshot, xmlStreamReader); addFromSnapshot(snapshot, SnapshotSource::Paste); + saveSnapshot(); } } diff --git a/src/documentwindow.cpp b/src/documentwindow.cpp index b71aaf4e..5b82f9dd 100644 --- a/src/documentwindow.cpp +++ b/src/documentwindow.cpp @@ -994,6 +994,7 @@ DocumentWindow::DocumentWindow() : connect(graphicsWidget, &SkeletonGraphicsWidget::setNodeBoneMark, m_document, &Document::setNodeBoneMark); connect(graphicsWidget, &SkeletonGraphicsWidget::clearNodeCutFaceSettings, m_document, &Document::clearNodeCutFaceSettings); connect(graphicsWidget, &SkeletonGraphicsWidget::removeNode, m_document, &Document::removeNode); + connect(graphicsWidget, &SkeletonGraphicsWidget::removePart, m_document, &Document::removePart); connect(graphicsWidget, &SkeletonGraphicsWidget::setEditMode, m_document, &Document::setEditMode); connect(graphicsWidget, &SkeletonGraphicsWidget::removeEdge, m_document, &Document::removeEdge); connect(graphicsWidget, &SkeletonGraphicsWidget::addEdge, m_document, &Document::addEdge); diff --git a/src/skeletongraphicswidget.cpp b/src/skeletongraphicswidget.cpp index 2432f664..36de49ce 100644 --- a/src/skeletongraphicswidget.cpp +++ b/src/skeletongraphicswidget.cpp @@ -1732,6 +1732,59 @@ void SkeletonGraphicsWidget::deleteSelected() std::set nodeIdSet; std::set edgeIdSet; readSkeletonNodeAndEdgeIdSetFromRangeSelection(&nodeIdSet, &edgeIdSet); + + std::set partIds; + for (const auto &it: nodeIdSet) { + const SkeletonNode *node = m_document->findNode(it); + if (nullptr == node) + continue; + partIds.insert(node->partId); + } + for (const auto &it: edgeIdSet) { + const SkeletonEdge *edge = m_document->findEdge(it); + if (nullptr == edge) + continue; + partIds.insert(edge->partId); + } + std::set cleanupPartIds; + for (const auto &partId: partIds) { + const SkeletonPart *part = m_document->findPart(partId); + if (nullptr != part) { + bool allNodesSelected = true; + for (const auto &it: part->nodeIds) { + if (nodeIdSet.find(it) == nodeIdSet.end()) { + allNodesSelected = false; + break; + } + } + if (allNodesSelected) { + cleanupPartIds.insert(partId); + } + } + } + for (const auto &partId: cleanupPartIds) { + for (auto it = nodeIdSet.begin(); it != nodeIdSet.end(); ) { + const SkeletonNode *node = m_document->findNode(*it); + if (nullptr != node && node->partId == partId) { + it = nodeIdSet.erase(it); + continue; + } + ++it; + } + for (auto it = edgeIdSet.begin(); it != edgeIdSet.end(); ) { + const SkeletonEdge *edge = m_document->findEdge(*it); + if (nullptr != edge && edge->partId == partId) { + it = edgeIdSet.erase(it); + continue; + } + ++it; + } + } + + for (const auto &partId: cleanupPartIds) { + emit removePart(partId); + } + for (const auto &it: nodeIdSet) { m_deferredRemoveNodeIds.insert(it); } @@ -1739,8 +1792,10 @@ void SkeletonGraphicsWidget::deleteSelected() m_deferredRemoveEdgeIds.insert(it); } - if (nullptr == m_deferredRemoveTimer) { - timeToRemoveDeferredNodesAndEdges(); + if (!nodeIdSet.empty() || !edgeIdSet.empty()) { + if (nullptr == m_deferredRemoveTimer) { + timeToRemoveDeferredNodesAndEdges(); + } } } diff --git a/src/skeletongraphicswidget.h b/src/skeletongraphicswidget.h index c6739778..781a45b8 100644 --- a/src/skeletongraphicswidget.h +++ b/src/skeletongraphicswidget.h @@ -468,6 +468,7 @@ signals: void scaleNodeByAddRadius(QUuid nodeId, float amount); void moveNodeBy(QUuid nodeId, float x, float y, float z); void removeNode(QUuid nodeId); + void removePart(QUuid partId); void setEditMode(SkeletonDocumentEditMode mode); void removeEdge(QUuid edgeId); void addEdge(QUuid fromNodeId, QUuid toNodeId);