From 913dc6473ddd93dbc6d3d260a4a4445696ba3b09 Mon Sep 17 00:00:00 2001 From: Miikka Heikkinen Date: Fri, 28 Aug 2020 14:00:43 +0300 Subject: QmlDesigner: Choose target property when dragging image to material When dragging an image to a Quick3D default or principled material, pop up a dialog to choose the target texture property. Change-Id: I8d97ef5bf7c5192c2651fcd8cf64a7f4a87c9847 Fixes: QDS-2326 Reviewed-by: Mahmoud Badri Reviewed-by: Thomas Hartmann --- .../navigator/choosetexturepropertydialog.cpp | 91 +++++++++++++++++ .../navigator/choosetexturepropertydialog.h | 54 ++++++++++ .../navigator/choosetexturepropertydialog.ui | 110 +++++++++++++++++++++ .../qmldesigner/components/navigator/navigator.pri | 8 +- .../components/navigator/navigatortreemodel.cpp | 26 +++-- 5 files changed, 277 insertions(+), 12 deletions(-) create mode 100644 src/plugins/qmldesigner/components/navigator/choosetexturepropertydialog.cpp create mode 100644 src/plugins/qmldesigner/components/navigator/choosetexturepropertydialog.h create mode 100644 src/plugins/qmldesigner/components/navigator/choosetexturepropertydialog.ui diff --git a/src/plugins/qmldesigner/components/navigator/choosetexturepropertydialog.cpp b/src/plugins/qmldesigner/components/navigator/choosetexturepropertydialog.cpp new file mode 100644 index 0000000000..826ed580ca --- /dev/null +++ b/src/plugins/qmldesigner/components/navigator/choosetexturepropertydialog.cpp @@ -0,0 +1,91 @@ +/**************************************************************************** +** +** Copyright (C) 2020 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of Qt Creator. +** +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 as published by the Free Software +** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-3.0.html. +** +****************************************************************************/ + +#include "choosetexturepropertydialog.h" +#include "nodemetainfo.h" +#include "ui_choosetexturepropertydialog.h" + +namespace QmlDesigner { + +// This dialog displays all texture properties of an object and allows the user to choose one +ChooseTexturePropertyDialog::ChooseTexturePropertyDialog(const ModelNode &node, QWidget *parent) + : QDialog(parent) + , m_ui(new Ui::ChooseTexturePropertyDialog) +{ + m_ui->setupUi(this); + setWindowTitle(tr("Select Texture Property")); + m_ui->label->setText(tr("Set texture to property:")); + setFixedSize(size()); + + connect(m_ui->listProps, &QListWidget::itemClicked, this, [this](QListWidgetItem *item) { + m_selectedProperty = item->isSelected() ? item->data(Qt::DisplayRole).toByteArray() : QByteArray(); + }); + + connect(m_ui->listProps, &QListWidget::itemDoubleClicked, this, [this](QListWidgetItem *item) { + Q_UNUSED(item) + QDialog::accept(); + }); + + fillList(node); +} + +ChooseTexturePropertyDialog::~ChooseTexturePropertyDialog() +{ + delete m_ui; +} + +TypeName ChooseTexturePropertyDialog::selectedProperty() const +{ + return m_selectedProperty; +} + +void ChooseTexturePropertyDialog::fillList(const ModelNode &node) +{ + // Fill the list with all properties of type Texture + const auto metaInfo = node.metaInfo(); + const auto propNames = metaInfo.propertyNames(); + const TypeName textureProp("QtQuick3D.Texture"); + QStringList nameList; + for (const auto &propName : propNames) { + if (metaInfo.propertyTypeName(propName) == textureProp) + nameList.append(QString::fromLatin1(propName)); + } + + if (!nameList.isEmpty()) { + QString defaultProp = nameList.first(); + + nameList.sort(); + for (const auto &propName : qAsConst(nameList)) { + QListWidgetItem *newItem = new QListWidgetItem(propName); + m_ui->listProps->addItem(newItem); + } + + // Select the default prop + m_ui->listProps->setCurrentRow(nameList.indexOf(defaultProp)); + m_selectedProperty = defaultProp.toLatin1(); + } +} + +} diff --git a/src/plugins/qmldesigner/components/navigator/choosetexturepropertydialog.h b/src/plugins/qmldesigner/components/navigator/choosetexturepropertydialog.h new file mode 100644 index 0000000000..7604e6f942 --- /dev/null +++ b/src/plugins/qmldesigner/components/navigator/choosetexturepropertydialog.h @@ -0,0 +1,54 @@ +/**************************************************************************** +** +** Copyright (C) 2020 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of Qt Creator. +** +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 as published by the Free Software +** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-3.0.html. +** +****************************************************************************/ + +#pragma once + +#include +#include + +#include + +namespace QmlDesigner { +namespace Ui { +class ChooseTexturePropertyDialog; +} + +class ChooseTexturePropertyDialog : public QDialog +{ + Q_OBJECT + +public: + explicit ChooseTexturePropertyDialog(const ModelNode &node, QWidget *parent = 0); + ~ChooseTexturePropertyDialog(); + + TypeName selectedProperty() const; + +private: + void fillList(const ModelNode &node); + + Ui::ChooseTexturePropertyDialog *m_ui; + TypeName m_selectedProperty; +}; +} diff --git a/src/plugins/qmldesigner/components/navigator/choosetexturepropertydialog.ui b/src/plugins/qmldesigner/components/navigator/choosetexturepropertydialog.ui new file mode 100644 index 0000000000..8643e866d5 --- /dev/null +++ b/src/plugins/qmldesigner/components/navigator/choosetexturepropertydialog.ui @@ -0,0 +1,110 @@ + + + QmlDesigner::ChooseTexturePropertyDialog + + + + 0 + 0 + 250 + 250 + + + + + 200 + 150 + + + + + 1000 + 1000 + + + + + + + false + + + false + + + + 0 + + + 0 + + + 0 + + + 0 + + + + + + + + + + + + + + + + + + Qt::Horizontal + + + QDialogButtonBox::Cancel|QDialogButtonBox::Ok + + + + + + + + + + + + buttonBox + accepted() + QmlDesigner::ChooseTexturePropertyDialog + accept() + + + 240 + 240 + + + 157 + 274 + + + + + buttonBox + rejected() + QmlDesigner::ChooseTexturePropertyDialog + reject() + + + 240 + 240 + + + 286 + 274 + + + + + diff --git a/src/plugins/qmldesigner/components/navigator/navigator.pri b/src/plugins/qmldesigner/components/navigator/navigator.pri index 08432e7f86..7344f1bc5a 100644 --- a/src/plugins/qmldesigner/components/navigator/navigator.pri +++ b/src/plugins/qmldesigner/components/navigator/navigator.pri @@ -5,7 +5,8 @@ SOURCES += navigatorview.cpp \ navigatorwidget.cpp \ nameitemdelegate.cpp \ iconcheckboxitemdelegate.cpp \ - navigatortreeview.cpp + navigatortreeview.cpp \ + choosetexturepropertydialog.cpp HEADERS += navigatorview.h \ navigatortreemodel.h \ @@ -13,6 +14,9 @@ HEADERS += navigatorview.h \ nameitemdelegate.h \ iconcheckboxitemdelegate.h \ navigatortreeview.h \ - navigatormodelinterface.h + navigatormodelinterface.h \ + choosetexturepropertydialog.h RESOURCES += navigator.qrc + +FORMS += choosetexturepropertydialog.ui diff --git a/src/plugins/qmldesigner/components/navigator/navigatortreemodel.cpp b/src/plugins/qmldesigner/components/navigator/navigatortreemodel.cpp index e2cc2970b8..c93b3145e9 100644 --- a/src/plugins/qmldesigner/components/navigator/navigatortreemodel.cpp +++ b/src/plugins/qmldesigner/components/navigator/navigatortreemodel.cpp @@ -25,6 +25,7 @@ #include "navigatortreemodel.h" #include "navigatorview.h" +#include "choosetexturepropertydialog.h" #include "qmldesignerplugin.h" #include @@ -661,16 +662,21 @@ void NavigatorTreeModel::handleItemLibraryImageDrop(const QMimeData *mimeData, i if (targetNode.isSubclassOf("QtQuick3D.Material")) { // if dropping an image on a default material, create a texture instead of image - m_view->executeInTransaction("NavigatorTreeModel::handleItemLibraryImageDrop", [&] { - if (createTextureNode(targetProperty)) { - // Automatically set the texture to default property - // TODO: allow the user to choose which map property to set the texture for (QDS-2326) - if (targetNode.isSubclassOf("QtQuick3D.DefaultMaterial")) - targetNode.bindingProperty("diffuseMap").setExpression(newModelNode.validId()); - else if (targetNode.isSubclassOf("QtQuick3D.PrincipledMaterial")) - targetNode.bindingProperty("baseColorMap").setExpression(newModelNode.validId()); - } - }); + ChooseTexturePropertyDialog *dialog = nullptr; + if (targetNode.isSubclassOf("QtQuick3D.DefaultMaterial") || targetNode.isSubclassOf("QtQuick3D.PrincipledMaterial")) { + // Show texture property selection dialog + dialog = new ChooseTexturePropertyDialog(targetNode, Core::ICore::dialogParent()); + dialog->exec(); + } + if (!dialog || dialog->result() == QDialog::Accepted) { + m_view->executeInTransaction("NavigatorTreeModel::handleItemLibraryImageDrop", [&] { + if (createTextureNode(targetProperty) && dialog) { + // Automatically set the texture to selected property + targetNode.bindingProperty(dialog->selectedProperty()).setExpression(newModelNode.validId()); + } + }); + } + delete dialog; } else if (targetNode.isSubclassOf("QtQuick3D.TextureInput")) { // If dropping an image on a TextureInput, create a texture on the same level as // TextureInput, as the TextureInput doesn't support Texture children (QTBUG-86219) -- cgit v1.2.3