From 843dcc1ff6c89ddd2af5a670d8dad28c383ec9f0 Mon Sep 17 00:00:00 2001 From: Mahmoud Badri Date: Wed, 19 Jun 2024 00:06:10 +0300 Subject: [PATCH] QmlDesigner: Fix icon generation fail when adding an item to content lib Copy the added item's QML file to a temp directory and generate from there. Also, fix that generated icons not appearing immediately by separating the model refresh from the addItem(), ensuring the refresh occurs after the icon is generated. Change-Id: Ic4f32677b3c2f3895b7825aef7727ac6dd6240b2 Reviewed-by: Miikka Heikkinen --- .../contentlibrary/contentlibraryusermodel.cpp | 1 - .../contentlibrary/contentlibraryview.cpp | 14 ++++++++++++-- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/src/plugins/qmldesigner/components/contentlibrary/contentlibraryusermodel.cpp b/src/plugins/qmldesigner/components/contentlibrary/contentlibraryusermodel.cpp index ab88aea07f5..77aae694117 100644 --- a/src/plugins/qmldesigner/components/contentlibrary/contentlibraryusermodel.cpp +++ b/src/plugins/qmldesigner/components/contentlibrary/contentlibraryusermodel.cpp @@ -91,7 +91,6 @@ void ContentLibraryUserModel::addItem(const QString &bundleId, const QString &na UserCategory *cat = m_userCategories[sectionIndex]; cat->addItem(new ContentLibraryItem(cat, name, qml, type, icon, files, bundleId)); - emit dataChanged(index(sectionIndex), index(sectionIndex), {ItemsRole, EmptyRole}); updateIsEmpty(); } diff --git a/src/plugins/qmldesigner/components/contentlibrary/contentlibraryview.cpp b/src/plugins/qmldesigner/components/contentlibrary/contentlibraryview.cpp index c0b16b6c556..bdf820d9432 100644 --- a/src/plugins/qmldesigner/components/contentlibrary/contentlibraryview.cpp +++ b/src/plugins/qmldesigner/components/contentlibrary/contentlibraryview.cpp @@ -573,6 +573,7 @@ void ContentLibraryView::addLibMaterial(const ModelNode &node, const QPixmap &ic } m_widget->userModel()->addItem(bundleId, name, qml, QUrl::fromLocalFile(fullIconPath), depAssetsList); + m_widget->userModel()->refreshSection(bundleId); } QPair> ContentLibraryView::modelNodeToQmlString(const ModelNode &node, int depth) @@ -854,16 +855,24 @@ void ContentLibraryView::addLib3DItem(const ModelNode &node) QTC_ASSERT_EXPECTED(result,); // generate and save icon - QString qmlPath = bundlePath.pathAppended(qml).toFSPathString(); QString iconPath = QLatin1String("icons/%1").arg(icon); m_iconSavePath = bundlePath.pathAppended(iconPath); m_iconSavePath.parentDir().ensureWritableDir(); - getImageFromCache(qmlPath, [&](const QImage &image) { + + // copy qml to a temp folder as icon generation fails sometimes when generating directly from + // the bundle folder + m_tempDir = std::make_unique(); + QTC_ASSERT(m_tempDir->isValid(), return); + Utils::FilePath qmlPathTemp = Utils::FilePath::fromString(m_tempDir->path()).pathAppended(qml); + bundlePath.pathAppended(qml).copyFile(qmlPathTemp); + + getImageFromCache(qmlPathTemp.toFSPathString(), [&](const QImage &image) { bool iconSaved = image.save(m_iconSavePath.toFSPathString()); if (iconSaved) m_widget->userModel()->refreshSection(compUtils.user3DBundleId()); else qWarning() << "ContentLibraryView::getImageFromCache(): icon save failed" << iconPath; + m_tempDir.reset(); }); // add the item to the bundle json @@ -1072,6 +1081,7 @@ void ContentLibraryView::importBundle() m_widget->userModel()->addItem(bundleId, name, qml, iconUrl, files); } + m_widget->userModel()->refreshSection(bundleId); zipReader.close();