QmlDesigner: Add support for importing materials to content lib

Fixes: QDS-12965
Change-Id: I422a45f305593eb4b58cd59b807e3bdefd9d056f
Reviewed-by: Shrief Gabr <shrief.gabr@qt.io>
Reviewed-by: Miikka Heikkinen <miikka.heikkinen@qt.io>
This commit is contained in:
Mahmoud Badri
2024-06-10 14:04:11 +03:00
parent ed263f5ee5
commit b7131bbd30
3 changed files with 42 additions and 17 deletions

View File

@@ -129,9 +129,9 @@ void ContentLibraryUserModel::add3DItem(const QString &name, const QString &qml,
m_user3DItems.append(new ContentLibraryItem(this, name, qml, type, icon, files));
}
void ContentLibraryUserModel::refresh3DSection()
void ContentLibraryUserModel::refreshSection(SectionIndex sectionIndex)
{
emit dataChanged(index(Items3DSectionIdx), index(Items3DSectionIdx));
emit dataChanged(index(sectionIndex), index(sectionIndex));
}
void ContentLibraryUserModel::addTextures(const QStringList &paths)
@@ -244,6 +244,17 @@ void ContentLibraryUserModel::remove3DFromContentLibByName(const QString &qmlFil
remove3DFromContentLib(itemToRemove);
}
void ContentLibraryUserModel::removeMaterialFromContentLibByName(const QString &qmlFileName)
{
ContentLibraryMaterial *itemToRemove = Utils::findOr(m_userMaterials, nullptr,
[&qmlFileName](ContentLibraryMaterial *item) {
return item->qml() == qmlFileName;
});
if (itemToRemove)
removeMaterialFromContentLib(itemToRemove);
}
void ContentLibraryUserModel::remove3DFromContentLib(ContentLibraryItem *item)
{
QJsonArray itemsArr = m_bundleObj3D.value("items").toArray();

View File

@@ -31,6 +31,11 @@ class ContentLibraryUserModel : public QAbstractListModel
Q_PROPERTY(QList<ContentLibraryItem *> userEffects MEMBER m_userEffects NOTIFY userEffectsChanged)
public:
enum SectionIndex { MaterialsSectionIdx = 0,
TexturesSectionIdx,
Items3DSectionIdx,
EffectsSectionIdx };
ContentLibraryUserModel(ContentLibraryWidget *parent = nullptr);
int rowCount(const QModelIndex &parent = QModelIndex()) const override;
@@ -59,12 +64,13 @@ public:
void addMaterial(const QString &name, const QString &qml, const QUrl &icon, const QStringList &files);
void add3DItem(const QString &name, const QString &qml, const QUrl &icon, const QStringList &files);
void refresh3DSection();
void refreshSection(SectionIndex sectionIndex);
void addTextures(const QStringList &paths);
void add3DInstance(ContentLibraryItem *bundleItem);
void remove3DFromContentLibByName(const QString &qmlFileName);
void removeMaterialFromContentLibByName(const QString &qmlFileName);
void setBundleObj(const QJsonObject &newBundleObj);
QJsonObject &bundleJsonMaterialObjectRef();
@@ -89,11 +95,6 @@ signals:
void bundle3DExistsChanged();
private:
enum SectionIndex { MaterialsSectionIdx = 0,
TexturesSectionIdx,
Items3DSectionIdx,
EffectsSectionIdx };
void loadMaterialBundle();
void load3DBundle();
void loadTextureBundle();

View File

@@ -710,7 +710,7 @@ void ContentLibraryView::addLib3DComponent(const ModelNode &node)
getImageFromCache(compDir.pathAppended(compFileName).path(), [&](const QImage &image) {
bool iconSaved = image.save(m_iconSavePath.toFSPathString());
if (iconSaved)
m_widget->userModel()->refresh3DSection();
m_widget->userModel()->refreshSection(ContentLibraryUserModel::Items3DSectionIdx);
else
qWarning() << "ContentLibraryView::getImageFromCache(): icon save failed" << iconPath;
});
@@ -848,7 +848,7 @@ void ContentLibraryView::addLib3DItem(const ModelNode &node)
getImageFromCache(qmlPath, [&](const QImage &image) {
bool iconSaved = image.save(m_iconSavePath.toFSPathString());
if (iconSaved)
m_widget->userModel()->refresh3DSection();
m_widget->userModel()->refreshSection(ContentLibraryUserModel::Items3DSectionIdx);
else
qWarning() << "ContentLibraryView::getImageFromCache(): icon save failed" << iconPath;
});
@@ -988,13 +988,10 @@ void ContentLibraryView::exportLib3DItem(const ModelNode &node, const QPixmap &i
void ContentLibraryView::importBundle()
{
// TODO: support importing materials
QString importPath = getImportPath();
if (importPath.isEmpty())
return;
auto bundlePath = Utils::FilePath::fromString(Paths::bundlesPathSetting() + "/User/3d/");
ZipReader zipReader(importPath);
QByteArray bundleJsonContent = zipReader.fileData(Constants::BUNDLE_JSON_FILENAME);
@@ -1004,7 +1001,14 @@ void ContentLibraryView::importBundle()
const QJsonArray importedItemsArr = importedJsonObj.value("items").toArray();
QTC_ASSERT(!importedItemsArr.isEmpty(), return);
QJsonObject &jsonRef = m_widget->userModel()->bundleJson3DObjectRef();
bool isMat = isMaterialBundle(importedJsonObj.value("id").toString());
QString bundleFolderName = isMat ? QLatin1String("materials") : QLatin1String("3d");
auto bundlePath = Utils::FilePath::fromString(QLatin1String("%1/User/%3/")
.arg(Paths::bundlesPathSetting(), bundleFolderName));
QJsonObject &jsonRef = isMat ? m_widget->userModel()->bundleJsonMaterialObjectRef()
: m_widget->userModel()->bundleJson3DObjectRef();
QJsonArray itemsArr = jsonRef.value("items").toArray();
QStringList existingQmls;
@@ -1026,7 +1030,10 @@ void ContentLibraryView::importBundle()
continue;
// before overwriting remove old item (to avoid partial items and dangling assets)
m_widget->userModel()->remove3DFromContentLibByName(qml);
if (isMat)
m_widget->userModel()->removeMaterialFromContentLibByName(qml);
else
m_widget->userModel()->remove3DFromContentLibByName(qml);
}
// add entry to json
@@ -1037,7 +1044,6 @@ void ContentLibraryView::importBundle()
QStringList files = itemObj.value("files").toVariant().toStringList();
QString icon = itemObj.value("icon").toString();
QUrl iconUrl = bundlePath.pathAppended(icon).toUrl();
m_widget->userModel()->add3DItem(name, qml, iconUrl, files);
// copy files
files << qml << icon; // all files
@@ -1046,6 +1052,11 @@ void ContentLibraryView::importBundle()
filePath.parentDir().ensureWritableDir();
QTC_ASSERT_EXPECTED(filePath.writeFileContents(zipReader.fileData(file)),);
}
if (isMat)
m_widget->userModel()->addMaterial(name, qml, iconUrl, files);
else
m_widget->userModel()->add3DItem(name, qml, iconUrl, files);
}
zipReader.close();
@@ -1056,7 +1067,9 @@ void ContentLibraryView::importBundle()
.writeFileContents(QJsonDocument(jsonRef).toJson());
QTC_ASSERT_EXPECTED(result,);
m_widget->userModel()->refresh3DSection();
auto sectionIdx = isMat ? ContentLibraryUserModel::MaterialsSectionIdx
: ContentLibraryUserModel::Items3DSectionIdx;
m_widget->userModel()->refreshSection(sectionIdx);
}
/**