forked from qt-creator/qt-creator
QmlDesigner: Reload bundles when id change
i.e. when opening a project that has a different import folder style from the currently open project. Fixes: QDS-12639 Change-Id: I8d341c38ec771dc5e28c4436cd9ad191e27c38a8 Reviewed-by: Miikka Heikkinen <miikka.heikkinen@qt.io> Reviewed-by: Qt CI Patch Build Bot <ci_patchbuild_bot@qt.io>
This commit is contained in:
@@ -95,9 +95,21 @@ QHash<int, QByteArray> ContentLibraryEffectsModel::roleNames() const
|
||||
|
||||
void ContentLibraryEffectsModel::loadBundle()
|
||||
{
|
||||
if (m_bundleExists || m_probeBundleDir)
|
||||
auto compUtils = QmlDesignerPlugin::instance()->documentManager().generatedComponentUtils();
|
||||
|
||||
if (m_probeBundleDir || (m_bundleExists && m_bundleId == compUtils.effectsBundleId()))
|
||||
return;
|
||||
|
||||
// clean up
|
||||
qDeleteAll(m_bundleCategories);
|
||||
m_bundleCategories.clear();
|
||||
m_bundleExists = false;
|
||||
m_isEmpty = true;
|
||||
m_probeBundleDir = false;
|
||||
m_bundleObj = {};
|
||||
m_bundleId.clear();
|
||||
m_bundlePath.clear();
|
||||
|
||||
QDir bundleDir;
|
||||
|
||||
if (!qEnvironmentVariable("EFFECT_BUNDLE_PATH").isEmpty())
|
||||
@@ -112,30 +124,30 @@ void ContentLibraryEffectsModel::loadBundle()
|
||||
while (!bundleDir.cd("effect_bundle") && bundleDir.cdUp())
|
||||
; // do nothing
|
||||
|
||||
if (bundleDir.dirName() != "effect_bundle") // bundlePathDir not found
|
||||
if (bundleDir.dirName() != "effect_bundle") { // bundlePathDir not found
|
||||
resetModel();
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
QString bundlePath = bundleDir.filePath("effect_bundle.json");
|
||||
|
||||
if (m_bundleObj.isEmpty()) {
|
||||
QFile propsFile(bundlePath);
|
||||
|
||||
if (!propsFile.open(QIODevice::ReadOnly)) {
|
||||
QFile bundleFile(bundlePath);
|
||||
if (!bundleFile.open(QIODevice::ReadOnly)) {
|
||||
qWarning("Couldn't open effect_bundle.json");
|
||||
resetModel();
|
||||
return;
|
||||
}
|
||||
|
||||
QJsonDocument bundleJsonDoc = QJsonDocument::fromJson(propsFile.readAll());
|
||||
QJsonDocument bundleJsonDoc = QJsonDocument::fromJson(bundleFile.readAll());
|
||||
if (bundleJsonDoc.isNull()) {
|
||||
qWarning("Invalid effect_bundle.json file");
|
||||
resetModel();
|
||||
return;
|
||||
} else {
|
||||
m_bundleObj = bundleJsonDoc.object();
|
||||
}
|
||||
}
|
||||
|
||||
auto compUtils = QmlDesignerPlugin::instance()->documentManager().generatedComponentUtils();
|
||||
m_bundleObj = bundleJsonDoc.object();
|
||||
|
||||
QString bundleType = compUtils.effectsBundleType();
|
||||
m_bundleId = compUtils.effectsBundleId();
|
||||
|
||||
@@ -145,9 +157,9 @@ void ContentLibraryEffectsModel::loadBundle()
|
||||
auto category = new ContentLibraryEffectsCategory(this, cat);
|
||||
|
||||
const QJsonObject itemsObj = catsObj.value(cat).toObject();
|
||||
const QStringList items = itemsObj.keys();
|
||||
for (const QString &item : items) {
|
||||
const QJsonObject itemObj = itemsObj.value(item).toObject();
|
||||
const QStringList itemsNames = itemsObj.keys();
|
||||
for (const QString &itemName : itemsNames) {
|
||||
const QJsonObject itemObj = itemsObj.value(itemName).toObject();
|
||||
|
||||
QStringList files;
|
||||
const QJsonArray assetsArr = itemObj.value("files").toArray();
|
||||
@@ -159,7 +171,7 @@ void ContentLibraryEffectsModel::loadBundle()
|
||||
TypeName type = QLatin1String("%1.%2")
|
||||
.arg(bundleType, qml.chopped(4)).toLatin1(); // chopped(4): remove .qml
|
||||
|
||||
auto bundleItem = new ContentLibraryEffect(category, item, qml, type, icon, files);
|
||||
auto bundleItem = new ContentLibraryEffect(category, itemName, qml, type, icon, files);
|
||||
|
||||
category->addBundleItem(bundleItem);
|
||||
}
|
||||
@@ -171,11 +183,10 @@ void ContentLibraryEffectsModel::loadBundle()
|
||||
for (const QJsonValueConstRef &file : sharedFilesArr)
|
||||
m_bundleSharedFiles.append(file.toString());
|
||||
|
||||
resetModel();
|
||||
updateIsEmpty();
|
||||
m_bundlePath = bundleDir.path();
|
||||
m_bundleExists = true;
|
||||
emit bundleExistsChanged();
|
||||
updateIsEmpty();
|
||||
resetModel();
|
||||
}
|
||||
|
||||
bool ContentLibraryEffectsModel::hasRequiredQuick3DImport() const
|
||||
|
||||
@@ -212,35 +212,43 @@ QString ContentLibraryMaterialsModel::bundleId() const
|
||||
return m_bundleId;
|
||||
}
|
||||
|
||||
void ContentLibraryMaterialsModel::loadMaterialBundle(const QDir &matBundleDir)
|
||||
void ContentLibraryMaterialsModel::loadMaterialBundle(const QDir &bundleDir)
|
||||
{
|
||||
if (m_matBundleExists)
|
||||
return;
|
||||
|
||||
QString matBundlePath = matBundleDir.filePath("material_bundle.json");
|
||||
|
||||
if (m_matBundleObj.isEmpty()) {
|
||||
QFile matPropsFile(matBundlePath);
|
||||
|
||||
if (!matPropsFile.open(QIODevice::ReadOnly)) {
|
||||
qWarning("Couldn't open material_bundle.json");
|
||||
return;
|
||||
}
|
||||
|
||||
QJsonDocument matBundleJsonDoc = QJsonDocument::fromJson(matPropsFile.readAll());
|
||||
if (matBundleJsonDoc.isNull()) {
|
||||
qWarning("Invalid material_bundle.json file");
|
||||
return;
|
||||
} else {
|
||||
m_matBundleObj = matBundleJsonDoc.object();
|
||||
}
|
||||
}
|
||||
|
||||
auto compUtils = QmlDesignerPlugin::instance()->documentManager().generatedComponentUtils();
|
||||
|
||||
if (m_bundleExists && m_bundleId == compUtils.materialsBundleId())
|
||||
return;
|
||||
|
||||
// clean up
|
||||
qDeleteAll(m_bundleCategories);
|
||||
m_bundleCategories.clear();
|
||||
m_bundleExists = false;
|
||||
m_isEmpty = true;
|
||||
m_bundleObj = {};
|
||||
m_bundleId.clear();
|
||||
|
||||
QString bundlePath = bundleDir.filePath("material_bundle.json");
|
||||
|
||||
QFile bundleFile(bundlePath);
|
||||
if (!bundleFile.open(QIODevice::ReadOnly)) {
|
||||
qWarning("Couldn't open material_bundle.json");
|
||||
resetModel();
|
||||
return;
|
||||
}
|
||||
|
||||
QJsonDocument bundleJsonDoc = QJsonDocument::fromJson(bundleFile.readAll());
|
||||
if (bundleJsonDoc.isNull()) {
|
||||
qWarning("Invalid material_bundle.json file");
|
||||
resetModel();
|
||||
return;
|
||||
}
|
||||
|
||||
m_bundleObj = bundleJsonDoc.object();
|
||||
|
||||
QString bundleType = compUtils.materialsBundleType();
|
||||
m_bundleId = compUtils.materialsBundleId();
|
||||
|
||||
const QJsonObject catsObj = m_matBundleObj.value("categories").toObject();
|
||||
const QJsonObject catsObj = m_bundleObj.value("categories").toObject();
|
||||
const QStringList categories = catsObj.keys();
|
||||
for (const QString &cat : categories) {
|
||||
auto category = new ContentLibraryMaterialsCategory(this, cat);
|
||||
@@ -255,7 +263,7 @@ void ContentLibraryMaterialsModel::loadMaterialBundle(const QDir &matBundleDir)
|
||||
for (const QJsonValueConstRef &asset : assetsArr)
|
||||
files.append(asset.toString());
|
||||
|
||||
QUrl icon = QUrl::fromLocalFile(matBundleDir.filePath(matObj.value("icon").toString()));
|
||||
QUrl icon = QUrl::fromLocalFile(bundleDir.filePath(matObj.value("icon").toString()));
|
||||
QString qml = matObj.value("qml").toString();
|
||||
TypeName type = QLatin1String("%1.%2")
|
||||
.arg(bundleType, qml.chopped(4)).toLatin1(); // chopped(4): remove .qml
|
||||
@@ -269,23 +277,22 @@ void ContentLibraryMaterialsModel::loadMaterialBundle(const QDir &matBundleDir)
|
||||
}
|
||||
|
||||
m_bundleSharedFiles.clear();
|
||||
const QJsonArray sharedFilesArr = m_matBundleObj.value("sharedFiles").toArray();
|
||||
const QJsonArray sharedFilesArr = m_bundleObj.value("sharedFiles").toArray();
|
||||
for (const QJsonValueConstRef &file : sharedFilesArr)
|
||||
m_bundleSharedFiles.append(file.toString());
|
||||
|
||||
QStringList missingSharedFiles;
|
||||
for (const QString &s : std::as_const(m_bundleSharedFiles)) {
|
||||
if (!QFileInfo::exists(matBundleDir.filePath(s)))
|
||||
if (!QFileInfo::exists(bundleDir.filePath(s)))
|
||||
missingSharedFiles.push_back(s);
|
||||
}
|
||||
|
||||
if (missingSharedFiles.length() > 0)
|
||||
downloadSharedFiles(matBundleDir, missingSharedFiles);
|
||||
downloadSharedFiles(bundleDir, missingSharedFiles);
|
||||
|
||||
resetModel();
|
||||
m_bundleExists = true;
|
||||
updateIsEmpty();
|
||||
m_matBundleExists = true;
|
||||
emit matBundleExistsChanged();
|
||||
resetModel();
|
||||
}
|
||||
|
||||
bool ContentLibraryMaterialsModel::hasRequiredQuick3DImport() const
|
||||
@@ -295,7 +302,7 @@ bool ContentLibraryMaterialsModel::hasRequiredQuick3DImport() const
|
||||
|
||||
bool ContentLibraryMaterialsModel::matBundleExists() const
|
||||
{
|
||||
return m_matBundleExists;
|
||||
return m_bundleExists;
|
||||
}
|
||||
|
||||
void ContentLibraryMaterialsModel::setSearchText(const QString &searchText)
|
||||
|
||||
@@ -73,10 +73,10 @@ private:
|
||||
QString m_bundleId;
|
||||
QStringList m_bundleSharedFiles;
|
||||
QList<ContentLibraryMaterialsCategory *> m_bundleCategories;
|
||||
QJsonObject m_matBundleObj;
|
||||
QJsonObject m_bundleObj;
|
||||
|
||||
bool m_isEmpty = true;
|
||||
bool m_matBundleExists = false;
|
||||
bool m_bundleExists = false;
|
||||
bool m_hasModelSelection = false;
|
||||
|
||||
int m_quick3dMajorVersion = -1;
|
||||
|
||||
@@ -226,13 +226,24 @@ QJsonObject &ContentLibraryUserModel::bundleJsonObjectRef()
|
||||
|
||||
void ContentLibraryUserModel::loadMaterialBundle()
|
||||
{
|
||||
if (m_matBundleExists)
|
||||
auto compUtils = QmlDesignerPlugin::instance()->documentManager().generatedComponentUtils();
|
||||
|
||||
if (m_matBundleExists && m_bundleIdMaterial == compUtils.userMaterialsBundleId())
|
||||
return;
|
||||
|
||||
// clean up
|
||||
qDeleteAll(m_userMaterials);
|
||||
m_userMaterials.clear();
|
||||
m_matBundleExists = false;
|
||||
m_isEmpty = true;
|
||||
m_bundleObj = {};
|
||||
m_bundleIdMaterial.clear();
|
||||
|
||||
int matSectionIdx = 0;
|
||||
|
||||
QDir bundleDir{Paths::bundlesPathSetting() + "/User/materials"};
|
||||
bundleDir.mkpath(".");
|
||||
|
||||
if (m_bundleObj.isEmpty()) {
|
||||
auto jsonFilePath = Utils::FilePath::fromString(bundleDir.filePath("user_materials_bundle.json"));
|
||||
if (!jsonFilePath.exists()) {
|
||||
QString jsonContent = "{\n";
|
||||
@@ -246,19 +257,18 @@ void ContentLibraryUserModel::loadMaterialBundle()
|
||||
QFile jsonFile(jsonFilePath.path());
|
||||
if (!jsonFile.open(QIODevice::ReadOnly)) {
|
||||
qWarning("Couldn't open user_materials_bundle.json");
|
||||
emit dataChanged(index(matSectionIdx), index(matSectionIdx));
|
||||
return;
|
||||
}
|
||||
|
||||
QJsonDocument matBundleJsonDoc = QJsonDocument::fromJson(jsonFile.readAll());
|
||||
if (matBundleJsonDoc.isNull()) {
|
||||
qWarning("Invalid user_materials_bundle.json file");
|
||||
emit dataChanged(index(matSectionIdx), index(matSectionIdx));
|
||||
return;
|
||||
} else {
|
||||
m_bundleObj = matBundleJsonDoc.object();
|
||||
}
|
||||
}
|
||||
|
||||
auto compUtils = QmlDesignerPlugin::instance()->documentManager().generatedComponentUtils();
|
||||
m_bundleObj = matBundleJsonDoc.object();
|
||||
m_bundleObj["id"] = compUtils.userMaterialsBundleId();
|
||||
|
||||
// parse materials
|
||||
@@ -290,6 +300,7 @@ void ContentLibraryUserModel::loadMaterialBundle()
|
||||
|
||||
m_matBundleExists = true;
|
||||
emit matBundleExistsChanged();
|
||||
emit dataChanged(index(matSectionIdx), index(matSectionIdx));
|
||||
}
|
||||
|
||||
void ContentLibraryUserModel::loadTextureBundle()
|
||||
|
||||
Reference in New Issue
Block a user