diff options
author | Shrief Gabr <shrief.gabr@qt.io> | 2025-07-15 15:38:34 +0300 |
---|---|---|
committer | Shrief Gabr <shrief.gabr@qt.io> | 2025-08-08 13:39:27 +0000 |
commit | 2257249ed5379a3e7ba87a6b37ba8e2d6c57123f (patch) | |
tree | 52ed84e4d84cf815e7e047cb17469c07b9e00896 | |
parent | 7d5452ac336493f1446a925114cbfe025402adfa (diff) |
QmlDesigner: Enable 2D drag & drop from Navigator to User Assetsqds/dev
Fixes: QDS-14526
Change-Id: I804ed325d3bae36c8e27e2e2b8c657c1e8e60d1b
Reviewed-by: Miikka Heikkinen <miikka.heikkinen@qt.io>
Reviewed-by: Ali Kianian <ali.kianian@qt.io>
6 files changed, 55 insertions, 24 deletions
diff --git a/share/qtcreator/qmldesigner/contentLibraryQmlSource/ContentLibraryUserView.qml b/share/qtcreator/qmldesigner/contentLibraryQmlSource/ContentLibraryUserView.qml index aaf281cc2f6..9873b97739c 100644 --- a/share/qtcreator/qmldesigner/contentLibraryQmlSource/ContentLibraryUserView.qml +++ b/share/qtcreator/qmldesigner/contentLibraryQmlSource/ContentLibraryUserView.qml @@ -150,15 +150,21 @@ Item { onCountChanged: root.assignMaxCount() onDropEnter: (drag) => { - let has3DNode = ContentLibraryBackend.rootView - .has3DNode(drag.getDataAsArrayBuffer(drag.formats[0])) let hasTexture = ContentLibraryBackend.rootView .hasTexture(drag.formats[0], drag.urls) + let isValid2DNode = categoryTitle === "2D" + && ContentLibraryBackend.rootView + .has2DNode(drag.getDataAsArrayBuffer(drag.formats[0])) + let isValid3DNode = categoryTitle === "3D" + && ContentLibraryBackend.rootView + .has3DNode(drag.getDataAsArrayBuffer(drag.formats[0])) drag.accepted = (categoryTitle === "Textures" && hasTexture) - || (categoryTitle === "Materials" && drag.formats[0] === "application/vnd.qtdesignstudio.material") - || (categoryTitle === "3D" && has3DNode) + || (categoryTitle === "Materials" + && drag.formats[0] === "application/vnd.qtdesignstudio.material") + || isValid2DNode + || isValid3DNode || (section.isCustomCat && hasTexture) section.highlight = drag.accepted @@ -180,8 +186,8 @@ Item { ContentLibraryBackend.rootView.acceptTextureDrop(drag.getDataAsString(drag.formats[0])) } else if (categoryTitle === "Materials") { ContentLibraryBackend.rootView.acceptMaterialDrop(drag.getDataAsString(drag.formats[0])) - } else if (categoryTitle === "3D") { - ContentLibraryBackend.rootView.accept3DDrop(drag.getDataAsArrayBuffer(drag.formats[0])) + } else if (categoryTitle === "2D" || categoryTitle === "3D") { + ContentLibraryBackend.rootView.acceptNodeDrop(drag.getDataAsArrayBuffer(drag.formats[0])) } else { // custom bundle folder if (drag.formats[0] === "application/vnd.qtdesignstudio.assets") { diff --git a/src/plugins/qmldesigner/components/contentlibrary/contentlibraryview.cpp b/src/plugins/qmldesigner/components/contentlibrary/contentlibraryview.cpp index e0b31474600..c7f51398478 100644 --- a/src/plugins/qmldesigner/components/contentlibrary/contentlibraryview.cpp +++ b/src/plugins/qmldesigner/components/contentlibrary/contentlibraryview.cpp @@ -139,7 +139,7 @@ WidgetInfo ContentLibraryView::widgetInfo() model()->endDrag(); }); - connect(m_widget, &ContentLibraryWidget::accept3DDrop, this, + connect(m_widget, &ContentLibraryWidget::acceptNodeDrop, this, &ContentLibraryView::decodeAndDropToContentLib); connect(m_widget, @@ -481,7 +481,7 @@ void ContentLibraryView::customNotification(const AbstractView *view, for (const ModelNode &node : selectedNodes) { if (node.isComponent()) - addLib3DComponent(node); + addLibComponent(node); else addLibItem(node); } @@ -630,12 +630,15 @@ void ContentLibraryView::addLibAssets(const QStringList &paths, const QString &b } // TODO: combine this method with BundleHelper::exportComponent() -// TODO: rename to addLibComponent and change code to work with 2D and 3D components -void ContentLibraryView::addLib3DComponent(const ModelNode &node) +void ContentLibraryView::addLibComponent(const ModelNode &node) { - auto bundlePath = Utils::FilePath::fromString(Paths::bundlesPathSetting() + "/User/3d/"); + bool is3DComponent = node.metaInfo().isQtQuick3DNode(); + auto bundlePath = is3DComponent + ? Utils::FilePath::fromString(Paths::bundlesPathSetting() + "/User/3d/") + : Utils::FilePath::fromString(Paths::bundlesPathSetting() + "/User/2d/"); - m_bundleId = m_compUtils.user3DBundleId(); + m_bundleId = is3DComponent ? m_compUtils.user3DBundleId() + : m_compUtils.user2DBundleId(); Utils::FilePath compFilePath = Utils::FilePath::fromString(ModelUtils::componentFilePath(node)); Utils::FilePath compDir = compFilePath.parentDir(); @@ -645,8 +648,8 @@ void ContentLibraryView::addLib3DComponent(const ModelNode &node) // confirm overwrite if an item with same name exists if (bundlePath.pathAppended(compFileName).exists()) { // Show a QML confirmation dialog before proceeding - QMessageBox::StandardButton reply = QMessageBox::question(m_widget, tr("3D Item Exists"), - tr("A 3D item with the same name '%1' already exists in the Content Library, are you sure you want to overwrite it?") + QMessageBox::StandardButton reply = QMessageBox::question(m_widget, tr("Item Exists"), + tr("An item with the same name '%1' already exists in the Content Library, are you sure you want to overwrite it?") .arg(compFileName), QMessageBox::Yes | QMessageBox::No); if (reply == QMessageBox::No) return; @@ -661,7 +664,7 @@ void ContentLibraryView::addLib3DComponent(const ModelNode &node) const QSet<AssetPath> compDependencies = m_bundleHelper->getComponentDependencies(compFilePath, compDir); - QStringList filesList; // 3D component's assets (dependencies) + QStringList filesList; // component's assets (dependencies) for (const AssetPath &asset : compDependencies) { Utils::FilePath assetAbsPath = asset.absFilPath(); QByteArray assetContent = asset.fileContent(); @@ -862,15 +865,13 @@ void ContentLibraryView::decodeAndDropToContentLib(const QByteArray &data) } for (int internalId : std::as_const(internalIds)) { - ModelNode node3D = QmlDesignerPlugin::instance()->viewManager() + ModelNode node = QmlDesignerPlugin::instance()->viewManager() .view()->modelNodeForInternalId(internalId); - if (!node3D.metaInfo().isQtQuick3DNode()) - continue; - if (node3D.isComponent()) - addLib3DComponent(node3D); + if (node.isComponent()) + addLibComponent(node); else - addLibItem(node3D); + addLibItem(node); } model()->endDrag(); diff --git a/src/plugins/qmldesigner/components/contentlibrary/contentlibraryview.h b/src/plugins/qmldesigner/components/contentlibrary/contentlibraryview.h index cdf50cf1013..fe3512409cd 100644 --- a/src/plugins/qmldesigner/components/contentlibrary/contentlibraryview.h +++ b/src/plugins/qmldesigner/components/contentlibrary/contentlibraryview.h @@ -66,7 +66,7 @@ private: void active3DSceneChanged(qint32 sceneId); void updateBundlesQuick3DVersion(); void addLibAssets(const QStringList &paths, const QString &bundlePath = {}); - void addLib3DComponent(const ModelNode &node); + void addLibComponent(const ModelNode &node); void addLibItem(const ModelNode &node, const QPixmap &iconPixmap = {}); void importBundleToContentLib(); void saveIconToBundle(const auto &image, const QString &iconPath); diff --git a/src/plugins/qmldesigner/components/contentlibrary/contentlibrarywidget.cpp b/src/plugins/qmldesigner/components/contentlibrary/contentlibrarywidget.cpp index 61fe60fc6be..47bea24991c 100644 --- a/src/plugins/qmldesigner/components/contentlibrary/contentlibrarywidget.cpp +++ b/src/plugins/qmldesigner/components/contentlibrary/contentlibrarywidget.cpp @@ -664,6 +664,28 @@ void ContentLibraryWidget::markTextureUpdated(const QString &textureKey) m_texturesModel->markTextureHasNoUpdates(subcategory, textureKey); } +bool ContentLibraryWidget::has2DNode(const QByteArray &data) const +{ + QByteArray encodedData = data; + QDataStream stream(&encodedData, QIODevice::ReadOnly); + + int internalId = 0; + while (!stream.atEnd()) { + stream >> internalId; + + if (internalId == 0) + continue; + + ModelNode modelNode = QmlDesignerPlugin::instance()->viewManager() + .view()->modelNodeForInternalId(internalId); + + if (modelNode.metaInfo().isQtQuickItem()) + return true; + } + + return false; +} + bool ContentLibraryWidget::has3DNode(const QByteArray &data) const { QByteArray encodedData = data; diff --git a/src/plugins/qmldesigner/components/contentlibrary/contentlibrarywidget.h b/src/plugins/qmldesigner/components/contentlibrary/contentlibrarywidget.h index 59d8d7f390d..161b592f2ec 100644 --- a/src/plugins/qmldesigner/components/contentlibrary/contentlibrarywidget.h +++ b/src/plugins/qmldesigner/components/contentlibrary/contentlibrarywidget.h @@ -100,6 +100,7 @@ public: Q_INVOKABLE void addLightProbe(QmlDesigner::ContentLibraryTexture *tex); Q_INVOKABLE void updateSceneEnvState(); Q_INVOKABLE void markTextureUpdated(const QString &textureKey); + Q_INVOKABLE bool has2DNode(const QByteArray &data) const; Q_INVOKABLE bool has3DNode(const QByteArray &data) const; Q_INVOKABLE bool hasTexture(const QString &format, const QVariant &data) const; Q_INVOKABLE void addQtQuick3D(); @@ -131,7 +132,7 @@ signals: void acceptTexturesDrop(const QList<QUrl> &urls, const QString &bundlePath = {}); void acceptTextureDrop(const QString &internalId, const QString &bundlePath = {}); void acceptMaterialDrop(const QString &internalId); - void accept3DDrop(const QByteArray &internalIds); + void acceptNodeDrop(const QByteArray &internalIds); void importQtQuick3D(); protected: diff --git a/src/plugins/qmldesigner/components/navigator/navigatortreemodel.cpp b/src/plugins/qmldesigner/components/navigator/navigatortreemodel.cpp index 76bb3560192..d452fdbb441 100644 --- a/src/plugins/qmldesigner/components/navigator/navigatortreemodel.cpp +++ b/src/plugins/qmldesigner/components/navigator/navigatortreemodel.cpp @@ -586,7 +586,8 @@ QMimeData *NavigatorTreeModel::mimeData(const QModelIndexList &modelIndexList) c const QModelIndex idModelIndex = modelIndex.sibling(modelIndex.row(), 0); if (!rowAlreadyUsedSet.contains(idModelIndex)) { rowAlreadyUsedSet.insert(idModelIndex); - encodedModelNodeDataStream << idModelIndex.internalId(); + qint32 internalId = idModelIndex.internalId(); + encodedModelNodeDataStream << internalId; } } } |