From 842df0568db1889f43d79818eb9f16ee7bc6bef4 Mon Sep 17 00:00:00 2001 From: Miikka Heikkinen Date: Tue, 18 Feb 2025 13:49:55 +0200 Subject: [PATCH] QmlDesigner: Ensure material library gets created always ComponentView already has some material library related functionality and it is guaranteed to always be attached, so move material library ensuring functionality there from material and texture editor views. This also reduces duplicated code. Fixes: QDS-13387 Change-Id: I60732d8f32eb79c64b83c0dc0406f720c5f11493 Reviewed-by: Ali Kianian Reviewed-by: Mahmoud Badri --- .../components/integration/componentview.cpp | 54 +++++++++++++++++++ .../components/integration/componentview.h | 3 ++ .../materialeditor/materialeditorview.cpp | 35 +----------- .../materialeditor/materialeditorview.h | 2 - .../textureeditor/textureeditorview.cpp | 28 ---------- .../textureeditor/textureeditorview.h | 2 - 6 files changed, 59 insertions(+), 65 deletions(-) diff --git a/src/plugins/qmldesigner/components/integration/componentview.cpp b/src/plugins/qmldesigner/components/integration/componentview.cpp index 59f8273fda8..0e3f0243724 100644 --- a/src/plugins/qmldesigner/components/integration/componentview.cpp +++ b/src/plugins/qmldesigner/components/integration/componentview.cpp @@ -26,6 +26,8 @@ ComponentView::ComponentView(ExternalDependenciesInterface &externalDependencies , m_standardItemModel(new QStandardItemModel(this)) , m_componentAction(new ComponentAction(this)) { + connect(&m_ensureMatLibTimer, &QTimer::timeout, this, &ComponentView::ensureMatLibTriggered); + m_ensureMatLibTimer.setInterval(500); } void ComponentView::nodeAboutToBeRemoved(const ModelNode &removedNode) @@ -161,6 +163,48 @@ bool ComponentView::isSubComponentNode(const ModelNode &node) const && node.metaInfo().isGraphicalItem()); } +void ComponentView::ensureMatLibTriggered() +{ + if (!model() || !model()->rewriterView() + || model()->rewriterView()->hasIncompleteTypeInformation() + || !model()->rewriterView()->errors().isEmpty()) { + return; + } + + m_ensureMatLibTimer.stop(); + ModelNode matLib = Utils3D::materialLibraryNode(this); + if (matLib.isValid()) + return; + + DesignDocument *doc = QmlDesignerPlugin::instance()->currentDesignDocument(); + if (doc && !doc->inFileComponentModelActive()) + Utils3D::ensureMaterialLibraryNode(this); + + matLib = Utils3D::materialLibraryNode(this); + if (!matLib.isValid()) + return; + + bool texSelected = Utils3D::selectedTexture(this).isValid(); + bool matSelected = Utils3D::selectedMaterial(this).isValid(); + if (!texSelected || !matSelected) { + const QList matLibNodes = matLib.directSubModelNodes(); + for (const ModelNode &node : matLibNodes) { + if (!texSelected && node.metaInfo().isQtQuick3DTexture()) { + Utils3D::selectTexture(node); + if (matSelected) + break; + texSelected = true; + } + if (!matSelected && node.metaInfo().isQtQuick3DMaterial()) { + Utils3D::selectMaterial(node); + if (texSelected) + break; + matSelected = true; + } + } + } +} + void ComponentView::modelAttached(Model *model) { if (AbstractView::model() == model) @@ -172,12 +216,19 @@ void ComponentView::modelAttached(Model *model) AbstractView::modelAttached(model); searchForComponentAndAddToList(rootModelNode()); + + if (model->hasImport("QtQuick3D")) { + // Creating the material library node on model attach causes errors as long as the type + // information is not complete yet, so we keep checking until type info is complete. + m_ensureMatLibTimer.start(); + } } void ComponentView::modelAboutToBeDetached(Model *model) { QSignalBlocker blocker(m_componentAction); m_standardItemModel->clear(); + m_ensureMatLibTimer.stop(); AbstractView::modelAboutToBeDetached(model); } @@ -359,6 +410,9 @@ void ComponentView::importsChanged(const Imports &addedImports, const Imports &r resetPuppet(); } } + + if (model()->hasImport("QtQuick3D")) + m_ensureMatLibTimer.start(); } void ComponentView::possibleImportsChanged([[maybe_unused]] const Imports &possibleImports) diff --git a/src/plugins/qmldesigner/components/integration/componentview.h b/src/plugins/qmldesigner/components/integration/componentview.h index 1226e7dd614..1e6da67b528 100644 --- a/src/plugins/qmldesigner/components/integration/componentview.h +++ b/src/plugins/qmldesigner/components/integration/componentview.h @@ -7,6 +7,7 @@ #include #include +#include QT_BEGIN_NAMESPACE class QStandardItemModel; @@ -71,6 +72,7 @@ private: //functions QString descriptionForNode(const ModelNode &node) const; void updateDescription(const ModelNode &node); bool isSubComponentNode(const ModelNode &node) const; + void ensureMatLibTriggered(); private: QStandardItemModel *m_standardItemModel; @@ -78,6 +80,7 @@ private: QVariantMap m_importableExtensions3DMap; QVariantMap m_importOptions3DMap; + QTimer m_ensureMatLibTimer; }; } // namespace QmlDesigner diff --git a/src/plugins/qmldesigner/components/materialeditor/materialeditorview.cpp b/src/plugins/qmldesigner/components/materialeditor/materialeditorview.cpp index 47798ff4347..328e62dff0f 100644 --- a/src/plugins/qmldesigner/components/materialeditor/materialeditorview.cpp +++ b/src/plugins/qmldesigner/components/materialeditor/materialeditorview.cpp @@ -86,30 +86,6 @@ MaterialEditorView::MaterialEditorView(ExternalDependenciesInterface &externalDe m_updateShortcut = new QShortcut(QKeySequence(Qt::CTRL | Qt::Key_F7), m_stackedWidget); connect(m_updateShortcut, &QShortcut::activated, this, &MaterialEditorView::reloadQml); - m_ensureMatLibTimer.callOnTimeout([this] { - if (model() && model()->rewriterView() && !model()->rewriterView()->hasIncompleteTypeInformation() - && model()->rewriterView()->errors().isEmpty()) { - DesignDocument *doc = QmlDesignerPlugin::instance()->currentDesignDocument(); - if (doc && !doc->inFileComponentModelActive()) - Utils3D::ensureMaterialLibraryNode(this); - ModelNode matLib = Utils3D::materialLibraryNode(this); - if (m_qmlBackEnd && m_qmlBackEnd->contextObject()) - m_qmlBackEnd->contextObject()->setHasMaterialLibrary(matLib.isValid()); - m_ensureMatLibTimer.stop(); - - ModelNode mat = Utils3D::selectedMaterial(this); - if (!mat.isValid()) { - const QList matLibNodes = matLib.directSubModelNodes(); - for (const ModelNode &node : matLibNodes) { - if (node.metaInfo().isQtQuick3DMaterial()) { - Utils3D::selectMaterial(node); - break; - } - } - } - } - }); - QmlDesignerPlugin::trackWidgetFocusTime(m_stackedWidget, Constants::EVENT_MATERIALEDITOR_TIME); MaterialEditorDynamicPropertiesProxyModel::registerDeclarativeType(); @@ -769,14 +745,10 @@ void MaterialEditorView::modelAttached(Model *model) m_hasQuick3DImport = model->hasImport("QtQuick3D"); m_hasMaterialRoot = rootModelNode().metaInfo().isQtQuick3DMaterial(); - if (m_hasMaterialRoot) { + if (m_hasMaterialRoot) m_selectedMaterial = rootModelNode(); - } else if (m_hasQuick3DImport) { - // Creating the material library node on model attach causes errors as long as the type - // information is not complete yet, so we keep checking until type info is complete. - m_ensureMatLibTimer.start(500); + else if (m_hasQuick3DImport) m_selectedMaterial = Utils3D::selectedMaterial(this); - } if (!m_setupCompleted) { reloadQml(); @@ -1028,9 +1000,6 @@ void MaterialEditorView::importsChanged([[maybe_unused]] const Imports &addedImp if (m_qmlBackEnd) m_qmlBackEnd->contextObject()->setHasQuick3DImport(m_hasQuick3DImport); - if (m_hasQuick3DImport) - m_ensureMatLibTimer.start(500); - resetView(); } diff --git a/src/plugins/qmldesigner/components/materialeditor/materialeditorview.h b/src/plugins/qmldesigner/components/materialeditor/materialeditorview.h index a513b3768b7..65e1df9bb2b 100644 --- a/src/plugins/qmldesigner/components/materialeditor/materialeditorview.h +++ b/src/plugins/qmldesigner/components/materialeditor/materialeditorview.h @@ -10,7 +10,6 @@ #include #include #include -#include QT_BEGIN_NAMESPACE class QShortcut; @@ -132,7 +131,6 @@ private: void asyncResetView(); ModelNode m_selectedMaterial; - QTimer m_ensureMatLibTimer; QShortcut *m_updateShortcut = nullptr; int m_timerId = 0; QStackedWidget *m_stackedWidget = nullptr; diff --git a/src/plugins/qmldesigner/components/textureeditor/textureeditorview.cpp b/src/plugins/qmldesigner/components/textureeditor/textureeditorview.cpp index da745f26e03..8678e9433d1 100644 --- a/src/plugins/qmldesigner/components/textureeditor/textureeditorview.cpp +++ b/src/plugins/qmldesigner/components/textureeditor/textureeditorview.cpp @@ -67,30 +67,6 @@ TextureEditorView::TextureEditorView(AsynchronousImageCache &imageCache, m_updateShortcut = new QShortcut(QKeySequence(Qt::CTRL | Qt::Key_F12), m_stackedWidget); connect(m_updateShortcut, &QShortcut::activated, this, &TextureEditorView::reloadQml); - m_ensureMatLibTimer.callOnTimeout([this] { - if (model() && model()->rewriterView() && !model()->rewriterView()->hasIncompleteTypeInformation() - && model()->rewriterView()->errors().isEmpty()) { - DesignDocument *doc = QmlDesignerPlugin::instance()->currentDesignDocument(); - if (doc && !doc->inFileComponentModelActive()) - Utils3D::ensureMaterialLibraryNode(this); - ModelNode matLib = Utils3D::materialLibraryNode(this); - if (m_qmlBackEnd && m_qmlBackEnd->contextObject()) - m_qmlBackEnd->contextObject()->setHasMaterialLibrary(matLib.isValid()); - m_ensureMatLibTimer.stop(); - - ModelNode tex = Utils3D::selectedTexture(this); - if (!tex.isValid()) { - const QList matLibNodes = matLib.directSubModelNodes(); - for (const ModelNode &node : matLibNodes) { - if (node.metaInfo().isQtQuick3DTexture()) { - Utils3D::selectTexture(node); - break; - } - } - } - } - }); - m_stackedWidget->setStyleSheet(Theme::replaceCssColors( QString::fromUtf8(Utils::FileReader::fetchQrc(":/qmldesigner/stylesheet.css")))); m_stackedWidget->setMinimumWidth(250); @@ -619,7 +595,6 @@ void TextureEditorView::modelAttached(Model *model) } else if (m_hasQuick3DImport) { // Creating the material library node on model attach causes errors as long as the type // information is not complete yet, so we keep checking until type info is complete. - m_ensureMatLibTimer.start(500); m_selectedTexture = Utils3D::selectedTexture(this); } @@ -896,9 +871,6 @@ void TextureEditorView::importsChanged([[maybe_unused]] const Imports &addedImpo m_hasQuick3DImport = model()->hasImport("QtQuick3D"); m_qmlBackEnd->contextObject()->setHasQuick3DImport(m_hasQuick3DImport); - if (m_hasQuick3DImport) - m_ensureMatLibTimer.start(500); - resetView(); } diff --git a/src/plugins/qmldesigner/components/textureeditor/textureeditorview.h b/src/plugins/qmldesigner/components/textureeditor/textureeditorview.h index c07b0d4475b..a816c2d1499 100644 --- a/src/plugins/qmldesigner/components/textureeditor/textureeditorview.h +++ b/src/plugins/qmldesigner/components/textureeditor/textureeditorview.h @@ -9,7 +9,6 @@ #include #include -#include QT_BEGIN_NAMESPACE class QShortcut; @@ -119,7 +118,6 @@ private: AsynchronousImageCache &m_imageCache; ModelNode m_selectedTexture; - QTimer m_ensureMatLibTimer; QShortcut *m_updateShortcut = nullptr; int m_timerId = 0; QStackedWidget *m_stackedWidget = nullptr;