From b071603175eb420b44b35df946d180dbd17419b8 Mon Sep 17 00:00:00 2001 From: Thomas Hartmann Date: Wed, 6 Oct 2021 13:14:16 +0200 Subject: [PATCH] QmlDesigner: Sanitze QML file on save Before writing the annotations we also sanitize the QML file. This removes dangling PropertyChanges and dangling KeyFrameGroups. Those are simply noise and have no relevance. We only sanitize ui.qml files. Change-Id: If4bb993b808f9d96ab89296117e776e8e9eec2c3 Reviewed-by: Miikka Heikkinen --- .../components/integration/designdocument.cpp | 5 ++- .../designercore/include/rewriterview.h | 2 ++ .../designercore/model/rewriterview.cpp | 33 +++++++++++++++++++ 3 files changed, 39 insertions(+), 1 deletion(-) diff --git a/src/plugins/qmldesigner/components/integration/designdocument.cpp b/src/plugins/qmldesigner/components/integration/designdocument.cpp index 9374e61776b..0b7d7fa2c92 100644 --- a/src/plugins/qmldesigner/components/integration/designdocument.cpp +++ b/src/plugins/qmldesigner/components/integration/designdocument.cpp @@ -634,8 +634,11 @@ void DesignDocument::setEditor(Core::IEditor *editor) connect(Core::EditorManager::instance(), &Core::EditorManager::aboutToSave, this, [this](Core::IDocument *document) { if (m_textEditor && m_textEditor->document() == document) { - if (m_documentModel && m_documentModel->rewriterView()) + if (m_documentModel && m_documentModel->rewriterView()) { + if (fileName().completeSuffix() == "ui.qml") + m_documentModel->rewriterView()->sanitizeModel(); m_documentModel->rewriterView()->writeAuxiliaryData(); + } } }); diff --git a/src/plugins/qmldesigner/designercore/include/rewriterview.h b/src/plugins/qmldesigner/designercore/include/rewriterview.h index ac251ef064c..44e804f25df 100644 --- a/src/plugins/qmldesigner/designercore/include/rewriterview.h +++ b/src/plugins/qmldesigner/designercore/include/rewriterview.h @@ -177,6 +177,8 @@ public: ModelNode getNodeForCanonicalIndex(int index); + void sanitizeModel(); + signals: void modelInterfaceProjectUpdated(); diff --git a/src/plugins/qmldesigner/designercore/model/rewriterview.cpp b/src/plugins/qmldesigner/designercore/model/rewriterview.cpp index af066b81c23..e33c82b1245 100644 --- a/src/plugins/qmldesigner/designercore/model/rewriterview.cpp +++ b/src/plugins/qmldesigner/designercore/model/rewriterview.cpp @@ -39,6 +39,8 @@ #include #include #include +#include +#include #ifndef QMLDESIGNER_TEST #include @@ -57,6 +59,7 @@ #include #include +#include using namespace QmlDesigner::Internal; @@ -658,6 +661,36 @@ ModelNode RewriterView::getNodeForCanonicalIndex(int index) return m_canonicalIntModelNode.value(index); } +void RewriterView::sanitizeModel() +{ + if (inErrorState()) + return; + + QmlObjectNode root = rootModelNode(); + + QTC_ASSERT(root.isValid(), return); + + QList danglingNodes; + + const auto danglingStates = root.allInvalidStateOperations(); + const auto danglingKeyframeGroups = QmlTimelineKeyframeGroup::allInvalidTimelineKeyframeGroups(this); + + std::transform(danglingStates.begin(), + danglingStates.end(), + std::back_inserter(danglingNodes), + [](const auto &node) { return node.modelNode(); }); + + std::transform(danglingKeyframeGroups.begin(), + danglingKeyframeGroups.end(), + std::back_inserter(danglingNodes), + [](const auto &node) { return node.modelNode(); }); + + executeInTransaction("RewriterView::sanitizeModel", [&]() { + for (auto node : std::as_const(danglingNodes)) + node.destroy(); + }); +} + Internal::ModelNodePositionStorage *RewriterView::positionStorage() const { return m_positionStorage.data();