From 1cba6bf5af4670f0c3d206b4685c1f2ac3a00d01 Mon Sep 17 00:00:00 2001 From: Miikka Heikkinen Date: Tue, 4 Mar 2025 15:16:15 +0200 Subject: [PATCH] QmlDesigner: Close bundle zip properly In some cases icons are added synchronously, in which case the final json addition would reopen the zip file without closing. Fixes: QDS-14856 Change-Id: Ie6401e274c50901c7e871e6d8bed53eaf3c3c356 Reviewed-by: Mahmoud Badri --- .../components/componentcore/bundlehelper.cpp | 21 ++++++++++++------- .../components/componentcore/bundlehelper.h | 5 +++-- 2 files changed, 16 insertions(+), 10 deletions(-) diff --git a/src/plugins/qmldesigner/components/componentcore/bundlehelper.cpp b/src/plugins/qmldesigner/components/componentcore/bundlehelper.cpp index 671cbcc8f52..82c950a3e63 100644 --- a/src/plugins/qmldesigner/components/componentcore/bundlehelper.cpp +++ b/src/plugins/qmldesigner/components/componentcore/bundlehelper.cpp @@ -236,7 +236,7 @@ void BundleHelper::exportBundle(const QList &nodes, const QPixmap &ic nodesToExport.append(node); } - m_remainingIcons = nodesToExport.size(); + m_remainingFiles = nodesToExport.size() + 1; for (const ModelNode &node : std::as_const(nodesToExport)) { if (node.isComponent()) @@ -247,6 +247,7 @@ void BundleHelper::exportBundle(const QList &nodes, const QPixmap &ic jsonObj["items"] = itemsArr; m_zipWriter->addFile(Constants::BUNDLE_JSON_FILENAME, QJsonDocument(jsonObj).toJson()); + maybeCloseZip(); } QJsonObject BundleHelper::exportComponent(const ModelNode &node) @@ -281,7 +282,7 @@ QJsonObject BundleHelper::exportComponent(const ModelNode &node) // add icon QString filePath = compFilePath.path(); getImageFromCache(filePath, [this, iconPath](const QImage &image) { - addIconAndCloseZip(iconPath, image); + addIconToZip(iconPath, image); }); return { @@ -344,10 +345,10 @@ QJsonObject BundleHelper::exportNode(const ModelNode &node, const QPixmap &iconP if (iconPixmapToSave.isNull()) { getImageFromCache(qmlFilePath.toFSPathString(), [this, iconPath](const QImage &image) { - addIconAndCloseZip(iconPath, image); + addIconToZip(iconPath, image); }); } else { - addIconAndCloseZip(iconPath, iconPixmapToSave); + addIconToZip(iconPath, iconPixmapToSave); } return { @@ -358,6 +359,12 @@ QJsonObject BundleHelper::exportNode(const ModelNode &node, const QPixmap &iconP }; } +void BundleHelper::maybeCloseZip() +{ + if (--m_remainingFiles <= 0) + m_zipWriter->close(); +} + QPair> BundleHelper::modelNodeToQmlString(const ModelNode &node, int depth) { static QStringList depListIds; @@ -541,16 +548,14 @@ void BundleHelper::getImageFromCache(const QString &qmlPath, }); } -void BundleHelper::addIconAndCloseZip(const QString &iconPath, const auto &image) { // auto: QImage or QPixmap +void BundleHelper::addIconToZip(const QString &iconPath, const auto &image) { // auto: QImage or QPixmap QByteArray iconByteArray; QBuffer buffer(&iconByteArray); buffer.open(QIODevice::WriteOnly); image.save(&buffer, "PNG"); m_zipWriter->addFile(iconPath, iconByteArray); - - if (--m_remainingIcons <= 0) - m_zipWriter->close(); + maybeCloseZip(); }; QString BundleHelper::getImportPath() const diff --git a/src/plugins/qmldesigner/components/componentcore/bundlehelper.h b/src/plugins/qmldesigner/components/componentcore/bundlehelper.h index ddd79f33158..edeb91f76e6 100644 --- a/src/plugins/qmldesigner/components/componentcore/bundlehelper.h +++ b/src/plugins/qmldesigner/components/componentcore/bundlehelper.h @@ -71,18 +71,19 @@ private: QString getExportPath(const ModelNode &node) const; bool isMaterialBundle(const QString &bundleId) const; bool isItemBundle(const QString &bundleId) const; - void addIconAndCloseZip(const QString &iconPath, const auto &image); + void addIconToZip(const QString &iconPath, const auto &image); Utils::FilePath componentPath(const ModelNode &node) const; QSet getBundleComponentDependencies(const ModelNode &node) const; QJsonObject exportComponent(const ModelNode &node); QJsonObject exportNode(const ModelNode &node, const QPixmap &iconPixmap = QPixmap()); + void maybeCloseZip(); QPointer m_view; QPointer m_widget; Utils::UniqueObjectPtr m_importer; std::unique_ptr m_zipWriter; std::unique_ptr m_tempDir; - int m_remainingIcons = 0; + int m_remainingFiles = 0; static constexpr char BUNDLE_VERSION[] = "1.0"; };