diff --git a/src/plugins/qmldesigner/components/contentlibrary/contentlibraryeffectsmodel.cpp b/src/plugins/qmldesigner/components/contentlibrary/contentlibraryeffectsmodel.cpp index fb7fde1014c..b496eb71ee9 100644 --- a/src/plugins/qmldesigner/components/contentlibrary/contentlibraryeffectsmodel.cpp +++ b/src/plugins/qmldesigner/components/contentlibrary/contentlibraryeffectsmodel.cpp @@ -98,8 +98,10 @@ void ContentLibraryEffectsModel::createImporter() [&](const QmlDesigner::TypeName &typeName) { m_importerRunning = false; emit importerRunningChanged(); - if (typeName.size()) + if (typeName.size()) { emit bundleItemImported(typeName); + updateImportedState(); + } }); #else connect(m_importer, @@ -108,8 +110,10 @@ void ContentLibraryEffectsModel::createImporter() [&](const QmlDesigner::NodeMetaInfo &metaInfo) { m_importerRunning = false; emit importerRunningChanged(); - if (metaInfo.isValid()) + if (metaInfo.isValid()) { emit bundleItemImported(metaInfo); + updateImportedState(); + } }); #endif @@ -119,6 +123,7 @@ void ContentLibraryEffectsModel::createImporter() m_importerRunning = false; emit importerRunningChanged(); emit bundleItemUnimported(metaInfo); + updateImportedState(); }); resetModel(); @@ -244,8 +249,20 @@ void ContentLibraryEffectsModel::setSearchText(const QString &searchText) updateIsEmpty(); } -void ContentLibraryEffectsModel::updateImportedState(const QStringList &importedItems) +void ContentLibraryEffectsModel::updateImportedState() { + if (!m_importer) + return; + + QString bundleId = m_bundleObj.value("id").toString(); + Utils::FilePath bundlePath = m_importer->resolveBundleImportPath(bundleId); + + QStringList importedItems; + if (bundlePath.exists()) { + importedItems = transform(bundlePath.dirEntries({{"*.qml"}, QDir::Files}), + [](const Utils::FilePath &f) { return f.baseName(); }); + } + bool changed = false; for (ContentLibraryEffectsCategory *cat : std::as_const(m_bundleCategories)) changed |= cat->updateImportedState(importedItems); diff --git a/src/plugins/qmldesigner/components/contentlibrary/contentlibraryeffectsmodel.h b/src/plugins/qmldesigner/components/contentlibrary/contentlibraryeffectsmodel.h index f63e080b159..8accec8819e 100644 --- a/src/plugins/qmldesigner/components/contentlibrary/contentlibraryeffectsmodel.h +++ b/src/plugins/qmldesigner/components/contentlibrary/contentlibraryeffectsmodel.h @@ -35,7 +35,7 @@ public: void loadBundle(); void setSearchText(const QString &searchText); - void updateImportedState(const QStringList &importedItems); + void updateImportedState(); void setQuick3DImportVersion(int major, int minor); diff --git a/src/plugins/qmldesigner/components/contentlibrary/contentlibrarymaterialsmodel.cpp b/src/plugins/qmldesigner/components/contentlibrary/contentlibrarymaterialsmodel.cpp index 1b5f0398982..8c035ea1b0b 100644 --- a/src/plugins/qmldesigner/components/contentlibrary/contentlibrarymaterialsmodel.cpp +++ b/src/plugins/qmldesigner/components/contentlibrary/contentlibrarymaterialsmodel.cpp @@ -217,8 +217,10 @@ void ContentLibraryMaterialsModel::createImporter() [&](const QmlDesigner::TypeName &typeName) { m_importerRunning = false; emit importerRunningChanged(); - if (typeName.size()) + if (typeName.size()) { emit bundleMaterialImported(typeName); + updateImportedState(); + } }); #else connect(m_importer, @@ -227,8 +229,10 @@ void ContentLibraryMaterialsModel::createImporter() [&](const QmlDesigner::NodeMetaInfo &metaInfo) { m_importerRunning = false; emit importerRunningChanged(); - if (metaInfo.isValid()) + if (metaInfo.isValid()) { emit bundleMaterialImported(metaInfo); + updateImportedState(); + } }); #endif @@ -238,6 +242,7 @@ void ContentLibraryMaterialsModel::createImporter() m_importerRunning = false; emit importerRunningChanged(); emit bundleMaterialUnimported(metaInfo); + updateImportedState(); }); resetModel(); @@ -355,11 +360,23 @@ void ContentLibraryMaterialsModel::setSearchText(const QString &searchText) updateIsEmpty(); } -void ContentLibraryMaterialsModel::updateImportedState(const QStringList &importedMats) +void ContentLibraryMaterialsModel::updateImportedState() { + if (!m_importer) + return; + + QString bundleId = m_matBundleObj.value("id").toString(); + Utils::FilePath bundlePath = m_importer->resolveBundleImportPath(bundleId); + + QStringList importedItems; + if (bundlePath.exists()) { + importedItems = transform(bundlePath.dirEntries({{"*.qml"}, QDir::Files}), + [](const Utils::FilePath &f) { return f.baseName(); }); + } + bool changed = false; for (ContentLibraryMaterialsCategory *cat : std::as_const(m_bundleCategories)) - changed |= cat->updateImportedState(importedMats); + changed |= cat->updateImportedState(importedItems); if (changed) resetModel(); diff --git a/src/plugins/qmldesigner/components/contentlibrary/contentlibrarymaterialsmodel.h b/src/plugins/qmldesigner/components/contentlibrary/contentlibrarymaterialsmodel.h index 6b0705e86d9..93a353b6f04 100644 --- a/src/plugins/qmldesigner/components/contentlibrary/contentlibrarymaterialsmodel.h +++ b/src/plugins/qmldesigner/components/contentlibrary/contentlibrarymaterialsmodel.h @@ -35,7 +35,7 @@ public: QHash roleNames() const override; void setSearchText(const QString &searchText); - void updateImportedState(const QStringList &importedMats); + void updateImportedState(); void setQuick3DImportVersion(int major, int minor); diff --git a/src/plugins/qmldesigner/components/contentlibrary/contentlibraryusermodel.cpp b/src/plugins/qmldesigner/components/contentlibrary/contentlibraryusermodel.cpp index 6a511f42bd0..e72b80e296b 100644 --- a/src/plugins/qmldesigner/components/contentlibrary/contentlibraryusermodel.cpp +++ b/src/plugins/qmldesigner/components/contentlibrary/contentlibraryusermodel.cpp @@ -209,7 +209,7 @@ TypeName ContentLibraryUserModel::qmlToModule(const QString &qmlName) const { return QLatin1String("%1.%2.%3").arg(QmlDesignerPlugin::instance()->documentManager() .generatedComponentUtils().componentBundlesTypePrefix(), - m_bundleId, + m_bundleIdMaterial, qmlName.chopped(4)).toLatin1(); // chopped(4): remove .qml } @@ -233,8 +233,10 @@ void ContentLibraryUserModel::createImporter() [&](const QmlDesigner::TypeName &typeName) { m_importerRunning = false; emit importerRunningChanged(); - if (typeName.size()) + if (typeName.size()) { emit bundleMaterialImported(typeName); + updateImportedState(); + } }); #else connect(m_importer, @@ -243,8 +245,10 @@ void ContentLibraryUserModel::createImporter() [&](const QmlDesigner::NodeMetaInfo &metaInfo) { m_importerRunning = false; emit importerRunningChanged(); - if (metaInfo.isValid()) + if (metaInfo.isValid()) { emit bundleMaterialImported(metaInfo); + updateImportedState(); + } }); #endif @@ -254,6 +258,7 @@ void ContentLibraryUserModel::createImporter() m_importerRunning = false; emit importerRunningChanged(); emit bundleMaterialUnimported(metaInfo); + updateImportedState(); }); resetModel(); @@ -299,7 +304,7 @@ void ContentLibraryUserModel::loadMaterialBundle() } } - m_bundleId = m_bundleObj.value("id").toString(); + m_bundleIdMaterial = m_bundleObj.value("id").toString(); // parse materials const QJsonObject matsObj = m_bundleObj.value("materials").toObject(); @@ -390,15 +395,28 @@ void ContentLibraryUserModel::setSearchText(const QString &searchText) updateIsEmpty(); } -void ContentLibraryUserModel::updateImportedState(const QStringList &importedMats) +void ContentLibraryUserModel::updateImportedState() { + if (!m_importer) + return; + + QString bundleId = m_bundleObj.value("id").toString(); + Utils::FilePath bundlePath = m_importer->resolveBundleImportPath(bundleId); + + QStringList importedItems; + if (bundlePath.exists()) { + importedItems = transform(bundlePath.dirEntries({{"*.qml"}, QDir::Files}), + [](const Utils::FilePath &f) { return f.baseName(); }); + } + bool changed = false; - for (ContentLibraryMaterial *mat : std::as_const(m_userMaterials)) - changed |= mat->setImported(importedMats.contains(mat->qml().chopped(4))); + changed |= mat->setImported(importedItems.contains(mat->qml().chopped(4))); - if (changed) - resetModel(); + if (changed) { + int matSectionIdx = 0; + emit dataChanged(index(matSectionIdx), index(matSectionIdx)); + } } void ContentLibraryUserModel::setQuick3DImportVersion(int major, int minor) diff --git a/src/plugins/qmldesigner/components/contentlibrary/contentlibraryusermodel.h b/src/plugins/qmldesigner/components/contentlibrary/contentlibraryusermodel.h index 35c2de662be..e4bf030c57c 100644 --- a/src/plugins/qmldesigner/components/contentlibrary/contentlibraryusermodel.h +++ b/src/plugins/qmldesigner/components/contentlibrary/contentlibraryusermodel.h @@ -41,7 +41,7 @@ public: QHash roleNames() const override; void setSearchText(const QString &searchText); - void updateImportedState(const QStringList &importedMats); + void updateImportedState(); QPair getUniqueLibMaterialNameAndQml(const QString &matName) const; TypeName qmlToModule(const QString &qmlName) const; @@ -102,7 +102,7 @@ private: ContentLibraryWidget *m_widget = nullptr; QString m_searchText; - QString m_bundleId; + QString m_bundleIdMaterial; QList m_userMaterials; QList m_userTextures; diff --git a/src/plugins/qmldesigner/components/contentlibrary/contentlibraryview.cpp b/src/plugins/qmldesigner/components/contentlibrary/contentlibraryview.cpp index 5981907f6c5..fde46a7dc26 100644 --- a/src/plugins/qmldesigner/components/contentlibrary/contentlibraryview.cpp +++ b/src/plugins/qmldesigner/components/contentlibrary/contentlibraryview.cpp @@ -114,7 +114,6 @@ WidgetInfo ContentLibraryView::widgetInfo() this, [&](const QmlDesigner::TypeName &typeName) { applyBundleMaterialToDropTarget({}, typeName); - updateBundleMaterialsImportedState(); }); #else connect(materialsModel, @@ -122,7 +121,6 @@ WidgetInfo ContentLibraryView::widgetInfo() this, [&](const QmlDesigner::NodeMetaInfo &metaInfo) { applyBundleMaterialToDropTarget({}, metaInfo); - updateBundleMaterialsImportedState(); }); #endif @@ -141,9 +139,6 @@ WidgetInfo ContentLibraryView::widgetInfo() }); }); - connect(materialsModel, &ContentLibraryMaterialsModel::bundleMaterialUnimported, this, - &ContentLibraryView::updateBundleMaterialsImportedState); - ContentLibraryEffectsModel *effectsModel = m_widget->effectsModel().data(); #ifdef QDS_USE_PROJECTSTORAGE @@ -167,7 +162,6 @@ WidgetInfo ContentLibraryView::widgetInfo() selectModelNode(newEffNode); }); - updateBundleEffectsImportedState(); m_bundleEffectTarget = {}; m_bundleEffectPos = {}; }); @@ -196,7 +190,6 @@ WidgetInfo ContentLibraryView::widgetInfo() selectModelNode(newEffNode); }); - updateBundleEffectsImportedState(); m_bundleEffectTarget = {}; m_bundleEffectPos = {}; }); @@ -212,9 +205,6 @@ WidgetInfo ContentLibraryView::widgetInfo() }); }); - connect(effectsModel, &ContentLibraryEffectsModel::bundleItemUnimported, this, - &ContentLibraryView::updateBundleEffectsImportedState); - connectUserBundle(); } @@ -252,7 +242,6 @@ void ContentLibraryView::connectUserBundle() this, [&](const QmlDesigner::TypeName &typeName) { applyBundleMaterialToDropTarget({}, typeName); - updateBundleUserMaterialsImportedState(); }); #else connect(userModel, @@ -260,7 +249,6 @@ void ContentLibraryView::connectUserBundle() this, [&](const QmlDesigner::NodeMetaInfo &metaInfo) { applyBundleMaterialToDropTarget({}, metaInfo); - updateBundleUserMaterialsImportedState(); }); #endif @@ -278,9 +266,6 @@ void ContentLibraryView::connectUserBundle() }); }); }); - - connect(userModel, &ContentLibraryUserModel::bundleMaterialUnimported, this, - &ContentLibraryView::updateBundleUserMaterialsImportedState); } void ContentLibraryView::modelAttached(Model *model) @@ -308,9 +293,9 @@ void ContentLibraryView::modelAttached(Model *model) m_widget->userModel()->loadMaterialBundle(); m_widget->userModel()->loadTextureBundle(); - updateBundleMaterialsImportedState(); - updateBundleEffectsImportedState(); - updateBundleUserMaterialsImportedState(); + m_widget->materialsModel()->updateImportedState(); + m_widget->effectsModel()->updateImportedState(); + m_widget->userModel()->updateImportedState(); } void ContentLibraryView::modelAboutToBeDetached(Model *model) @@ -769,65 +754,6 @@ ModelNode ContentLibraryView::createMaterial(const NodeMetaInfo &metaInfo) } #endif -void ContentLibraryView::updateBundleMaterialsImportedState() -{ - using namespace Utils; - - if (!m_widget->materialsModel()->bundleImporter()) - return; - - QStringList importedBundleMats; - - // TODO: this will be refactored next: no need for the round trip from model to view then back to model - // (same applies for the similar cases for effects and user material bundles) - FilePath materialBundlePath = m_widget->materialsModel()->bundleImporter()->resolveBundleImportPath("MaterialBundle"); - - if (materialBundlePath.exists()) { - importedBundleMats = transform(materialBundlePath.dirEntries({{"*.qml"}, QDir::Files}), - [](const FilePath &f) { return f.fileName().chopped(4); }); - } - - m_widget->materialsModel()->updateImportedState(importedBundleMats); -} - -void ContentLibraryView::updateBundleUserMaterialsImportedState() -{ - using namespace Utils; - - if (!m_widget->userModel()->bundleImporter()) - return; - - QStringList importedBundleMats; - - FilePath bundlePath = m_widget->userModel()->bundleImporter()->resolveBundleImportPath("UserMaterialBundle"); - - if (bundlePath.exists()) { - importedBundleMats = transform(bundlePath.dirEntries({{"*.qml"}, QDir::Files}), - [](const FilePath &f) { return f.fileName().chopped(4); }); - } - - m_widget->userModel()->updateImportedState(importedBundleMats); -} - -void ContentLibraryView::updateBundleEffectsImportedState() -{ - using namespace Utils; - - if (!m_widget->effectsModel()->bundleImporter()) - return; - - QStringList importedBundleEffs; - - FilePath bundlePath = m_widget->effectsModel()->bundleImporter()->resolveBundleImportPath("EffectBundle"); - - if (bundlePath.exists()) { - importedBundleEffs = transform(bundlePath.dirEntries({{"*.qml"}, QDir::Files}), - [](const FilePath &f) { return f.fileName().chopped(4); }); - } - - m_widget->effectsModel()->updateImportedState(importedBundleEffs); -} - void ContentLibraryView::updateBundlesQuick3DVersion() { bool hasImport = false; diff --git a/src/plugins/qmldesigner/components/contentlibrary/contentlibraryview.h b/src/plugins/qmldesigner/components/contentlibrary/contentlibraryview.h index 03d42fa8bcd..4ff2a5db8fd 100644 --- a/src/plugins/qmldesigner/components/contentlibrary/contentlibraryview.h +++ b/src/plugins/qmldesigner/components/contentlibrary/contentlibraryview.h @@ -50,9 +50,6 @@ public: private: void connectUserBundle(); void active3DSceneChanged(qint32 sceneId); - void updateBundleMaterialsImportedState(); - void updateBundleUserMaterialsImportedState(); - void updateBundleEffectsImportedState(); void updateBundlesQuick3DVersion(); void addLibMaterial(const ModelNode &mat, const QPixmap &icon); void addLibAssets(const QStringList &paths);