Implement texture context menu

Apply to selected model/material, delete, duplicate, and
create new options are available in the menu.

Fixes: QDS-8342
Change-Id: Ib9bdc1738500a87361000bcd3e89403e3b8ccef8
Reviewed-by: Mahmoud Badri <mahmoud.badri@qt.io>
This commit is contained in:
Miikka Heikkinen
2022-11-17 15:46:11 +02:00
parent e3a817ec77
commit ac1af9a582
12 changed files with 264 additions and 124 deletions

View File

@@ -145,7 +145,7 @@ Rectangle {
let matId = materialsListView.currentItem.id()
let prop = propertiesListView.currentItem.propName()
rootView.applyTextureToMaterial(matId, prop)
rootView.applyTextureToProperty(matId, prop)
}
}
}

View File

@@ -21,6 +21,7 @@ Item {
function closeContextMenu()
{
ctxMenu.close()
ctxMenuTextures.close()
}
// Called from C++ to refresh a preview material after it changes
@@ -64,6 +65,8 @@ Item {
if (mouse.y < matsSecBottom)
ctxMenu.popupMenu()
else
ctxMenuTextures.popupMenu()
}
}
@@ -83,6 +86,10 @@ Item {
id: ctxMenu
}
TextureBrowserContextMenu {
id: ctxMenuTextures
}
Column {
id: col
y: 5
@@ -130,7 +137,7 @@ Item {
height: root.height - searchBox.height
clip: true
visible: root.enableUiElements
interactive: !ctxMenu.opened
interactive: !ctxMenu.opened && !ctxMenuTextures.opened
Column {
Item {
@@ -243,7 +250,7 @@ Item {
height: root.cellWidth
onShowContextMenu: {
// ctxMenuTexture.popupMenu(this, model) // TODO: implement textures context menu
ctxMenuTextures.popupMenu(model)
}
}
}

View File

@@ -0,0 +1,55 @@
// Copyright (C) 2022 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0 WITH Qt-GPL-exception-1.0
import QtQuick
import HelperWidgets
import StudioControls as StudioControls
import StudioTheme as StudioTheme
StudioControls.Menu {
id: root
property var targetTexture: null
property int copiedTextureInternalId: -1
function popupMenu(targetTexture = null)
{
this.targetTexture = targetTexture
popup()
}
closePolicy: StudioControls.Menu.CloseOnEscape | StudioControls.Menu.CloseOnPressOutside
StudioControls.MenuItem {
text: qsTr("Apply to selected model")
enabled: root.targetTexture && materialBrowserTexturesModel.hasSingleModelSelection
onTriggered: materialBrowserTexturesModel.applyToSelectedModel(root.targetTexture.textureInternalId)
}
StudioControls.MenuItem {
text: qsTr("Apply to selected material")
enabled: root.targetTexture && materialBrowserModel.selectedIndex >= 0
onTriggered: materialBrowserTexturesModel.applyToSelectedMaterial(root.targetTexture.textureInternalId)
}
StudioControls.MenuSeparator {}
StudioControls.MenuItem {
text: qsTr("Duplicate")
enabled: root.targetTexture
onTriggered: materialBrowserTexturesModel.duplicateTexture(materialBrowserTexturesModel.selectedIndex)
}
StudioControls.MenuItem {
text: qsTr("Delete")
enabled: root.targetTexture
onTriggered: materialBrowserTexturesModel.deleteTexture(materialBrowserTexturesModel.selectedIndex)
}
StudioControls.MenuSeparator {}
StudioControls.MenuItem {
text: qsTr("Create New Texture")
onTriggered: materialBrowserTexturesModel.addNewTexture()
}
}

View File

@@ -28,9 +28,6 @@
#include <utils/qtcassert.h>
#include <utils/utilsicons.h>
#include <QQmlContext>
#include <QQmlEngine>
#include <QQuickView>
#include <QToolButton>
namespace QmlDesigner {
@@ -274,26 +271,6 @@ void Edit3DView::customNotification([[maybe_unused]] const AbstractView *view,
{
if (identifier == "asset_import_update")
resetPuppet();
else if (identifier == "apply_texture_to_model3D")
applyTextureToModel3D(nodeList.at(0), nodeList.at(1));
}
bool Edit3DView::eventFilter(QObject *obj, QEvent *event)
{
if (event->type() == QEvent::KeyPress) {
QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
if (keyEvent->key() == Qt::Key_Escape) {
if (obj == m_chooseMatPropsView)
m_chooseMatPropsView->close();
}
} else if (event->type() == QEvent::Close) {
if (obj == m_chooseMatPropsView) {
m_droppedModelNode = {};
m_chooseMatPropsView->deleteLater();
}
}
return AbstractView::eventFilter(obj, event);
}
/**
@@ -322,65 +299,13 @@ void Edit3DView::nodeAtPosReady(const ModelNode &modelNode, const QVector3D &pos
} else if (m_nodeAtPosReqType == NodeAtPosReqType::BundleMaterialDrop) {
emitCustomNotification("drop_bundle_material", {modelNode}); // To ContentLibraryView
} else if (m_nodeAtPosReqType == NodeAtPosReqType::TextureDrop) {
applyTextureToModel3D(modelNode, m_droppedModelNode);
emitCustomNotification("apply_texture_to_model3D", {modelNode, m_droppedModelNode});
}
if (m_nodeAtPosReqType != NodeAtPosReqType::TextureDrop)
m_droppedModelNode = {};
m_droppedModelNode = {};
m_nodeAtPosReqType = NodeAtPosReqType::None;
}
void Edit3DView::applyTextureToModel3D(const ModelNode &model3D, const ModelNode &texture)
{
if (!texture.isValid() || !model3D.isValid() || !model3D.metaInfo().isQtQuick3DModel())
return;
m_droppedModelNode = texture;
// get model's material list
BindingProperty matsProp = model3D.bindingProperty("materials");
QList<ModelNode> materials;
if (hasId(matsProp.expression()))
materials.append(modelNodeForId(matsProp.expression()));
else
materials = matsProp.resolveToModelNodeList();
if (materials.size() > 0) {
m_textureModels.clear();
QStringList materialsModel;
for (const ModelNode &mat : std::as_const(materials)) {
QString matName = mat.variantProperty("objectName").value().toString();
materialsModel.append(QLatin1String("%1 (%2)").arg(matName, mat.id()));
QList<PropertyName> texProps;
for (const PropertyMetaInfo &p : mat.metaInfo().properties()) {
if (p.propertyType().isQtQuick3DTexture())
texProps.append(p.name());
}
m_textureModels.insert(mat.id(), texProps);
}
QString path = MaterialBrowserWidget::qmlSourcesPath() + "/ChooseMaterialProperty.qml";
m_chooseMatPropsView = new QQuickView;
m_chooseMatPropsView->setTitle(tr("Select a material property"));
m_chooseMatPropsView->setResizeMode(QQuickView::SizeRootObjectToView);
m_chooseMatPropsView->setMinimumSize({150, 100});
m_chooseMatPropsView->setMaximumSize({600, 400});
m_chooseMatPropsView->setWidth(450);
m_chooseMatPropsView->setHeight(300);
m_chooseMatPropsView->setFlags(Qt::Widget);
m_chooseMatPropsView->setModality(Qt::ApplicationModal);
m_chooseMatPropsView->engine()->addImportPath(propertyEditorResourcesPath() + "/imports");
m_chooseMatPropsView->rootContext()->setContextProperties({
{"rootView", QVariant::fromValue(this)},
{"materialsModel", QVariant::fromValue(materialsModel)},
{"propertiesModel", QVariant::fromValue(m_textureModels.value(materials.at(0).id()))},
});
m_chooseMatPropsView->setSource(QUrl::fromLocalFile(path));
m_chooseMatPropsView->installEventFilter(this);
m_chooseMatPropsView->show();
}
}
void Edit3DView::sendInputEvent(QInputEvent *e) const
{
if (nodeInstanceView())
@@ -950,30 +875,4 @@ void Edit3DView::dropTexture(const ModelNode &textureNode, const QPointF &pos)
emitView3DAction(View3DActionType::GetNodeAtPos, pos);
}
void Edit3DView::updatePropsModel(const QString &matId)
{
m_chooseMatPropsView->rootContext()->setContextProperty("propertiesModel",
QVariant::fromValue(m_textureModels.value(matId)));
}
void Edit3DView::applyTextureToMaterial(const QString &matId, const QString &propName)
{
QTC_ASSERT(m_droppedModelNode.isValid(), return);
ModelNode mat = modelNodeForId(matId);
QTC_ASSERT(mat.isValid(), return);
BindingProperty texProp = mat.bindingProperty(propName.toLatin1());
QTC_ASSERT(texProp.isValid(), return);
texProp.setExpression(m_droppedModelNode.id());
closeChooseMatPropsView();
}
void Edit3DView::closeChooseMatPropsView()
{
m_chooseMatPropsView->close();
}
} // namespace QmlDesigner

View File

@@ -17,7 +17,6 @@
QT_BEGIN_NAMESPACE
class QAction;
class QInputEvent;
class QQuickView;
QT_END_NAMESPACE
namespace QmlDesigner {
@@ -65,14 +64,6 @@ public:
void dropMaterial(const ModelNode &matNode, const QPointF &pos);
void dropBundleMaterial(const QPointF &pos);
void dropTexture(const ModelNode &textureNode, const QPointF &pos);
void applyTextureToModel3D(const ModelNode &model3D, const ModelNode &texture);
Q_INVOKABLE void updatePropsModel(const QString &matId);
Q_INVOKABLE void applyTextureToMaterial(const QString &matId, const QString &propName);
Q_INVOKABLE void closeChooseMatPropsView();
protected:
bool eventFilter(QObject *obj, QEvent *event) override;
private slots:
void onEntriesChanged();
@@ -89,6 +80,7 @@ private:
void createEdit3DWidget();
void checkImports();
void handleEntriesChanged();
void showMaterialPropertiesView();
Edit3DAction *createSelectBackgroundColorAction(QAction *syncBackgroundColorAction);
Edit3DAction *createGridColorSelectionAction();
@@ -128,8 +120,6 @@ private:
NodeAtPosReqType m_nodeAtPosReqType;
QPoint m_contextMenuPos;
QTimer m_compressionTimer;
QPointer<QQuickView> m_chooseMatPropsView;
QHash<QString, QList<PropertyName>> m_textureModels;
};
} // namespace QmlDesigner

View File

@@ -315,6 +315,13 @@ ModelNode MaterialBrowserModel::materialAt(int idx) const
return {};
}
ModelNode MaterialBrowserModel::selectedMaterial() const
{
if (isValidIndex(m_selectedIndex))
return m_materialList[m_selectedIndex];
return {};
}
void MaterialBrowserModel::resetModel()
{
beginResetModel();

View File

@@ -58,6 +58,7 @@ public:
void updateSelectedMaterial();
int materialIndex(const ModelNode &material) const;
ModelNode materialAt(int idx) const;
ModelNode selectedMaterial() const;
bool loadPropertyGroups(const QString &path);
void unloadPropertyGroups();
@@ -115,7 +116,7 @@ private:
QHash<qint32, int> m_materialIndexHash; // internalId -> index
QJsonObject m_propertyGroupsObj;
int m_selectedIndex = 0;
int m_selectedIndex = -1;
bool m_isEmpty = true;
bool m_hasQuick3DImport = false;
bool m_hasModelSelection = false;

View File

@@ -44,6 +44,9 @@ QVariant MaterialBrowserTexturesModel::data(const QModelIndex &index, int role)
if (roleName == "hasDynamicProperties")
return !m_textureList.at(index.row()).dynamicProperties().isEmpty();
if (roleName == "textureInternalId")
return m_textureList.at(index.row()).internalId();
return {};
}
@@ -67,7 +70,8 @@ QHash<int, QByteArray> MaterialBrowserTexturesModel::roleNames() const
static const QHash<int, QByteArray> roles {
{Qt::UserRole + 1, "textureSource"},
{Qt::UserRole + 2, "textureVisible"},
{Qt::UserRole + 3, "hasDynamicProperties"}
{Qt::UserRole + 3, "hasDynamicProperties"},
{Qt::UserRole + 4, "textureInternalId"}
};
return roles;
}
@@ -242,4 +246,22 @@ void MaterialBrowserTexturesModel::deleteTexture(int idx)
}
}
void MaterialBrowserTexturesModel::applyToSelectedMaterial(qint64 internalId)
{
int idx = m_textureIndexHash.value(internalId);
if (idx != -1) {
ModelNode tex = m_textureList.at(idx);
emit applyToSelectedMaterialTriggered(tex);
}
}
void MaterialBrowserTexturesModel::applyToSelectedModel(qint64 internalId)
{
int idx = m_textureIndexHash.value(internalId);
if (idx != -1) {
ModelNode tex = m_textureList.at(idx);
emit applyToSelectedModelTriggered(tex);
}
}
} // namespace QmlDesigner

View File

@@ -47,13 +47,16 @@ public:
Q_INVOKABLE void addNewTexture();
Q_INVOKABLE void duplicateTexture(int idx);
Q_INVOKABLE void deleteTexture(int idx);
Q_INVOKABLE void applyToSelectedMaterial(qint64 internalId);
Q_INVOKABLE void applyToSelectedModel(qint64 internalId);
signals:
void isEmptyChanged();
void hasSingleModelSelectionChanged();
void materialSectionsChanged();
void selectedIndexChanged(int idx);
void duplicateTextureTriggered(const QmlDesigner::ModelNode &material);
void duplicateTextureTriggered(const QmlDesigner::ModelNode &texture);
void applyToSelectedMaterialTriggered(const QmlDesigner::ModelNode &texture);
void applyToSelectedModelTriggered(const QmlDesigner::ModelNode &texture);
void addNewTextureTriggered();
private:

View File

@@ -12,20 +12,35 @@
#include "qmlobjectnode.h"
#include "variantproperty.h"
#include <coreplugin/icore.h>
#include <designmodecontext.h>
#include <nodeinstanceview.h>
#include <nodemetainfo.h>
#include <nodelistproperty.h>
#include <qmldesignerconstants.h>
#include <utils/algorithm.h>
#include <coreplugin/icore.h>
#include <utils/algorithm.h>
#include <utils/qtcassert.h>
#include <QQmlContext>
#include <QQmlEngine>
#include <QQuickItem>
#include <QQuickView>
#include <QRegularExpression>
#include <QTimer>
namespace QmlDesigner {
static QString propertyEditorResourcesPath()
{
#ifdef SHARE_QML_PATH
if (qEnvironmentVariableIsSet("LOAD_QML_FROM_SOURCE"))
return QLatin1String(SHARE_QML_PATH) + "/propertyEditorQmlSources";
#endif
return Core::ICore::resourcePath("qmldesigner/propertyEditorQmlSources").toString();
}
MaterialBrowserView::MaterialBrowserView(ExternalDependenciesInterface &externalDependencies)
: AbstractView{externalDependencies}
{
@@ -146,6 +161,25 @@ WidgetInfo MaterialBrowserView::widgetInfo()
ModelNode texNode = m_widget->materialBrowserTexturesModel()->textureAt(idx);
emitCustomNotification("selected_texture_changed", {texNode}, {});
});
connect(texturesModel, &MaterialBrowserTexturesModel::duplicateTextureTriggered, this,
[&] (const ModelNode &texture) {
emitCustomNotification("duplicate_texture", {texture});
});
connect(texturesModel, &MaterialBrowserTexturesModel::applyToSelectedMaterialTriggered, this,
[&] (const ModelNode &texture) {
if (!m_widget)
return;
const ModelNode material = m_widget->materialBrowserModel()->selectedMaterial();
applyTextureToMaterial({material}, texture);
});
connect(texturesModel, &MaterialBrowserTexturesModel::applyToSelectedModelTriggered, this,
[&] (const ModelNode &texture) {
if (m_selectedModels.size() != 1)
return;
applyTextureToModel3D(m_selectedModels[0], texture);
});
connect(texturesModel, &MaterialBrowserTexturesModel::addNewTextureTriggered, this, [&] {
emitCustomNotification("add_new_texture");
@@ -406,6 +440,10 @@ void MaterialBrowserView::customNotification(const AbstractView *view,
});
} else if (identifier == "delete_selected_material") {
m_widget->materialBrowserModel()->deleteSelectedMaterial();
} else if (identifier == "apply_texture_to_model3D") {
applyTextureToModel3D(nodeList.at(0), nodeList.at(1));
} else if (identifier == "apply_texture_to_material") {
applyTextureToMaterial({nodeList.at(0)}, nodeList.at(1));
}
}
@@ -443,4 +481,105 @@ void MaterialBrowserView::instancePropertyChanged(const QList<QPair<ModelNode, P
}
}
void MaterialBrowserView::applyTextureToModel3D(const ModelNode &model3D, const ModelNode &texture)
{
if (!texture.isValid() || !model3D.isValid() || !model3D.metaInfo().isQtQuick3DModel())
return;
BindingProperty matsProp = model3D.bindingProperty("materials");
QList<ModelNode> materials;
if (hasId(matsProp.expression()))
materials.append(modelNodeForId(matsProp.expression()));
else
materials = matsProp.resolveToModelNodeList();
applyTextureToMaterial(materials, texture);
}
void MaterialBrowserView::applyTextureToMaterial(const QList<ModelNode> &materials,
const ModelNode &texture)
{
if (materials.size() > 0) {
m_appliedTextureId = texture.id();
m_textureModels.clear();
QStringList materialsModel;
for (const ModelNode &mat : std::as_const(materials)) {
QString matName = mat.variantProperty("objectName").value().toString();
materialsModel.append(QLatin1String("%1 (%2)").arg(matName, mat.id()));
QList<PropertyName> texProps;
for (const PropertyMetaInfo &p : mat.metaInfo().properties()) {
if (p.propertyType().isQtQuick3DTexture())
texProps.append(p.name());
}
m_textureModels.insert(mat.id(), texProps);
}
QString path = MaterialBrowserWidget::qmlSourcesPath() + "/ChooseMaterialProperty.qml";
m_chooseMatPropsView = new QQuickView;
m_chooseMatPropsView->setTitle(tr("Select a material property"));
m_chooseMatPropsView->setResizeMode(QQuickView::SizeRootObjectToView);
m_chooseMatPropsView->setMinimumSize({150, 100});
m_chooseMatPropsView->setMaximumSize({600, 400});
m_chooseMatPropsView->setWidth(450);
m_chooseMatPropsView->setHeight(300);
m_chooseMatPropsView->setFlags(Qt::Widget);
m_chooseMatPropsView->setModality(Qt::ApplicationModal);
m_chooseMatPropsView->engine()->addImportPath(propertyEditorResourcesPath() + "/imports");
m_chooseMatPropsView->rootContext()->setContextProperties({
{"rootView", QVariant::fromValue(this)},
{"materialsModel", QVariant::fromValue(materialsModel)},
{"propertiesModel", QVariant::fromValue(m_textureModels.value(materials.at(0).id()))},
});
m_chooseMatPropsView->setSource(QUrl::fromLocalFile(path));
m_chooseMatPropsView->installEventFilter(this);
m_chooseMatPropsView->show();
}
}
void MaterialBrowserView::updatePropsModel(const QString &matId)
{
m_chooseMatPropsView->rootContext()->setContextProperty("propertiesModel",
QVariant::fromValue(m_textureModels.value(matId)));
}
void MaterialBrowserView::applyTextureToProperty(const QString &matId, const QString &propName)
{
QTC_ASSERT(!m_appliedTextureId.isEmpty(), return);
ModelNode mat = modelNodeForId(matId);
QTC_ASSERT(mat.isValid(), return);
BindingProperty texProp = mat.bindingProperty(propName.toLatin1());
QTC_ASSERT(texProp.isValid(), return);
QmlObjectNode qmlObjNode(mat);
qmlObjNode.setBindingProperty(propName.toLatin1(), m_appliedTextureId);
closeChooseMatPropsView();
}
void MaterialBrowserView::closeChooseMatPropsView()
{
m_chooseMatPropsView->close();
}
bool MaterialBrowserView::eventFilter(QObject *obj, QEvent *event)
{
if (event->type() == QEvent::KeyPress) {
QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
if (keyEvent->key() == Qt::Key_Escape) {
if (obj == m_chooseMatPropsView)
m_chooseMatPropsView->close();
}
} else if (event->type() == QEvent::Close) {
if (obj == m_chooseMatPropsView) {
m_appliedTextureId.clear();
m_chooseMatPropsView->deleteLater();
}
}
return AbstractView::eventFilter(obj, event);
}
} // namespace QmlDesigner

View File

@@ -9,6 +9,10 @@
#include <QSet>
#include <QTimer>
QT_BEGIN_NAMESPACE
class QQuickView;
QT_END_NAMESPACE
namespace QmlDesigner {
class MaterialBrowserWidget;
@@ -43,6 +47,16 @@ public:
void instancesCompleted(const QVector<ModelNode> &completedNodeList) override;
void instancePropertyChanged(const QList<QPair<ModelNode, PropertyName> > &propertyList) override;
void applyTextureToModel3D(const ModelNode &model3D, const ModelNode &texture);
void applyTextureToMaterial(const QList<ModelNode> &materials, const ModelNode &texture);
Q_INVOKABLE void updatePropsModel(const QString &matId);
Q_INVOKABLE void applyTextureToProperty(const QString &matId, const QString &propName);
Q_INVOKABLE void closeChooseMatPropsView();
protected:
bool eventFilter(QObject *obj, QEvent *event) override;
private:
void refreshModel(bool updateImages);
bool isMaterial(const ModelNode &node) const;
@@ -60,6 +74,9 @@ private:
QTimer m_previewTimer;
QSet<ModelNode> m_previewRequests;
QPointer<QQuickView> m_chooseMatPropsView;
QHash<QString, QList<PropertyName>> m_textureModels;
QString m_appliedTextureId;
};
} // namespace QmlDesigner

View File

@@ -787,7 +787,7 @@ void TextureEditorView::customNotification([[maybe_unused]] const AbstractView *
m_dynamicPropertiesModel->setSelectedNode(m_selectedTexture);
QTimer::singleShot(0, this, &TextureEditorView::resetView);
}
} else if (identifier == "apply_texture_to_selected_triggered") {
} else if (identifier == "apply_texture_to_selected_model") {
applyTextureToSelectedModel(nodeList.first());
} else if (identifier == "add_new_texture") {
handleToolBarAction(TextureEditorContextObject::AddNewTexture);