From 623cf628d621ea6262a6a6999c700cc5076b44c5 Mon Sep 17 00:00:00 2001 From: Mahmoud Badri Date: Wed, 5 Jun 2024 19:22:55 +0300 Subject: [PATCH] QmlDesigner: Implement exporting a material ContentLibraryView::exportLib3DItem() updated to work with materials. Fixes: QDS-12905 Change-Id: If09b361851db366ef4ce4f1597d272fe5006e599 Reviewed-by: Ali Kianian Reviewed-by: Miikka Heikkinen --- .../MaterialBrowserContextMenu.qml | 12 +++--- .../contentlibrary/contentlibraryview.cpp | 38 +++++++++++-------- .../contentlibrary/contentlibraryview.h | 2 +- 3 files changed, 29 insertions(+), 23 deletions(-) diff --git a/share/qtcreator/qmldesigner/materialBrowserQmlSource/MaterialBrowserContextMenu.qml b/share/qtcreator/qmldesigner/materialBrowserQmlSource/MaterialBrowserContextMenu.qml index e211b18b221..b3ca25c2e92 100644 --- a/share/qtcreator/qmldesigner/materialBrowserQmlSource/MaterialBrowserContextMenu.qml +++ b/share/qtcreator/qmldesigner/materialBrowserQmlSource/MaterialBrowserContextMenu.qml @@ -135,12 +135,10 @@ StudioControls.Menu { onTriggered: MaterialBrowserBackend.rootView.addMaterialToContentLibrary() } - // TODO: implement - // StudioControls.MenuItem { - // text: qsTr("Export material") - // enabled: !materialBrowserModel.selectedMaterialIsComponent // TODO: support component materials - // visible: false + StudioControls.MenuItem { + text: qsTr("Export Material") + enabled: !materialBrowserModel.selectedMaterialIsComponent // TODO: support component materials - // onTriggered: MaterialBrowserBackend.rootView.exportMaterial() - // } + onTriggered: MaterialBrowserBackend.rootView.exportMaterial() + } } diff --git a/src/plugins/qmldesigner/components/contentlibrary/contentlibraryview.cpp b/src/plugins/qmldesigner/components/contentlibrary/contentlibraryview.cpp index 4873a8732f0..ca410dee11a 100644 --- a/src/plugins/qmldesigner/components/contentlibrary/contentlibraryview.cpp +++ b/src/plugins/qmldesigner/components/contentlibrary/contentlibraryview.cpp @@ -381,7 +381,7 @@ void ContentLibraryView::customNotification(const AbstractView *view, } else if (identifier == "export_item_as_bundle") { exportLib3DItem(nodeList.first()); } else if (identifier == "export_material_as_bundle") { - // TODO + exportLib3DItem(nodeList.first(), data.first().value()); } } @@ -810,7 +810,7 @@ void ContentLibraryView::addLib3DItem(const ModelNode &node) m_widget->userModel()->add3DItem(name, qml, m_iconSavePath.toUrl(), depAssetsList); } -void ContentLibraryView::exportLib3DItem(const ModelNode &node) +void ContentLibraryView::exportLib3DItem(const ModelNode &node, const QPixmap &iconPixmap) { // prompt and get the exported bundle path QString defaultExportFileName = QLatin1String("%1.%2").arg(node.id(), Constants::BUNDLE_SUFFIX); @@ -820,13 +820,15 @@ void ContentLibraryView::exportLib3DItem(const ModelNode &node) .currentDesignDocument()->fileName().parentDir(); } - QString exportPath = QFileDialog::getSaveFileName(m_widget, tr("Export Component"), + QString dialogTitle = node.metaInfo().isQtQuick3DMaterial() ? tr("Export Material") + : tr("Export Component"); + QString exportPath = QFileDialog::getSaveFileName(m_widget, dialogTitle, projectFP.pathAppended(defaultExportFileName).toFSPathString(), tr("Qt Design Studio Bundle Files (*.%1)").arg(Constants::BUNDLE_SUFFIX)); if (exportPath.isEmpty()) return; - // targetPath is a temp path for collectiong and zipping assets, actual export target is where + // targetPath is a temp path for collecting and zipping assets, actual export target is where // the user chose to export (i.e. exportPath) QTemporaryDir tempDir; QTC_ASSERT(tempDir.isValid(), return); @@ -849,18 +851,7 @@ void ContentLibraryView::exportLib3DItem(const ModelNode &node) QTC_ASSERT_EXPECTED(result, return); m_zipWriter->addFile(qmlFilePath.fileName(), qmlString.toUtf8()); - // generate and save icon QString iconPath = QLatin1String("icons/%1").arg(icon); - m_iconSavePath = targetPath.pathAppended(iconPath); - getImageFromCache(qmlFilePath.toFSPathString(), [&](const QImage &image) { - QByteArray iconByteArray; - QBuffer buffer(&iconByteArray); - buffer.open(QIODevice::WriteOnly); - image.save(&buffer, "PNG"); - - m_zipWriter->addFile("icons/" + m_iconSavePath.fileName(), iconByteArray); - m_zipWriter->close(); - }); // add the item to the bundle json QJsonObject jsonObj; @@ -882,6 +873,23 @@ void ContentLibraryView::exportLib3DItem(const ModelNode &node) Utils::FilePath assetPathSource = DocumentManager::currentResourcePath().pathAppended(assetPath); m_zipWriter->addFile(assetPath, assetPathSource.fileContents().value_or("")); } + + // add icon + auto addIconAndCloseZip = [&] (const auto &image) { // auto: QImage or QPixmap + QByteArray iconByteArray; + QBuffer buffer(&iconByteArray); + buffer.open(QIODevice::WriteOnly); + image.save(&buffer, "PNG"); + + m_zipWriter->addFile("icons/" + m_iconSavePath.fileName(), iconByteArray); + m_zipWriter->close(); + }; + + m_iconSavePath = targetPath.pathAppended(iconPath); + if (iconPixmap.isNull()) + getImageFromCache(qmlFilePath.toFSPathString(), addIconAndCloseZip); + else + addIconAndCloseZip(iconPixmap); } /** diff --git a/src/plugins/qmldesigner/components/contentlibrary/contentlibraryview.h b/src/plugins/qmldesigner/components/contentlibrary/contentlibraryview.h index a0602ea9be7..93840a20ed6 100644 --- a/src/plugins/qmldesigner/components/contentlibrary/contentlibraryview.h +++ b/src/plugins/qmldesigner/components/contentlibrary/contentlibraryview.h @@ -63,7 +63,7 @@ private: void addLibAssets(const QStringList &paths); void addLib3DComponent(const ModelNode &node); void addLib3DItem(const ModelNode &node); - void exportLib3DItem(const ModelNode &node); + void exportLib3DItem(const ModelNode &node, const QPixmap &iconPixmap = {}); void getImageFromCache(const QString &qmlPath, std::function successCallback); QPair> modelNodeToQmlString(const ModelNode &node, int depth = 0);