QmlDesigner: Refactor update bundle imported state

Remove the useless round trip from model to view and back

Change-Id: I38a5826e165014f64d7855139b58e9691b0f6310
Reviewed-by: Miikka Heikkinen <miikka.heikkinen@qt.io>
This commit is contained in:
Mahmoud Badri
2024-04-29 17:28:39 +03:00
parent 465a2e7ac4
commit 25f8081032
8 changed files with 75 additions and 100 deletions

View File

@@ -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);

View File

@@ -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);

View File

@@ -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();

View File

@@ -35,7 +35,7 @@ public:
QHash<int, QByteArray> roleNames() const override;
void setSearchText(const QString &searchText);
void updateImportedState(const QStringList &importedMats);
void updateImportedState();
void setQuick3DImportVersion(int major, int minor);

View File

@@ -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)

View File

@@ -41,7 +41,7 @@ public:
QHash<int, QByteArray> roleNames() const override;
void setSearchText(const QString &searchText);
void updateImportedState(const QStringList &importedMats);
void updateImportedState();
QPair<QString, QString> 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<ContentLibraryMaterial *> m_userMaterials;
QList<ContentLibraryTexture *> m_userTextures;

View File

@@ -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;

View File

@@ -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);