Reload material property groups when model changes

The property groups would be unloaded when a model is detached,
and it would be loaded when the model is attached.
The problem was that the unload function was missing for the
MaterialBrowserModel.

Task-number: QDS-7515
Change-Id: I89064a78552c10228c0b18e42c1e0df4a92fb4a6
Reviewed-by: Miikka Heikkinen <miikka.heikkinen@qt.io>
Reviewed-by: Mahmoud Badri <mahmoud.badri@qt.io>
This commit is contained in:
Ali Kianian
2022-10-20 17:06:58 +03:00
parent 0e7bb80408
commit f2f6f6e6cc
3 changed files with 30 additions and 14 deletions

View File

@@ -105,22 +105,19 @@ bool MaterialBrowserModel::loadPropertyGroups(const QString &path)
{
bool ok = true;
if (m_propertyGroupsObj.isEmpty()) {
QFile matPropsFile(path);
QFile matPropsFile(path);
if (!matPropsFile.open(QIODevice::ReadOnly)) {
qWarning("Couldn't open propertyGroups.json");
ok = false;
}
if (!matPropsFile.open(QIODevice::ReadOnly)) {
qWarning("Couldn't open propertyGroups.json");
if (ok) {
QJsonDocument matPropsJsonDoc = QJsonDocument::fromJson(matPropsFile.readAll());
if (matPropsJsonDoc.isNull()) {
qWarning("Invalid propertyGroups.json file");
ok = false;
}
if (ok) {
QJsonDocument matPropsJsonDoc = QJsonDocument::fromJson(matPropsFile.readAll());
if (matPropsJsonDoc.isNull()) {
qWarning("Invalid propertyGroups.json file");
ok = false;
} else {
m_propertyGroupsObj = matPropsJsonDoc.object();
}
} else {
m_propertyGroupsObj = matPropsJsonDoc.object();
}
}
@@ -134,12 +131,25 @@ bool MaterialBrowserModel::loadPropertyGroups(const QString &path)
QStringList customMatSections = m_propertyGroupsObj.value("CustomMaterial").toObject().keys();
if (customMatSections.size() > 1) // as of now custom material has only 1 section, so we don't add it
m_customMaterialSections.append(customMatSections);
} else {
m_propertyGroupsObj = {};
}
emit materialSectionsChanged();
return ok;
}
void MaterialBrowserModel::unloadPropertyGroups()
{
if (!m_propertyGroupsObj.isEmpty()) {
m_propertyGroupsObj = {};
m_defaultMaterialSections.clear();
m_principledMaterialSections.clear();
m_customMaterialSections.clear();
emit materialSectionsChanged();
}
}
QHash<int, QByteArray> MaterialBrowserModel::roleNames() const
{
static const QHash<int, QByteArray> roles {

View File

@@ -80,6 +80,7 @@ public:
int materialIndex(const ModelNode &material) const;
ModelNode materialAt(int idx) const;
bool loadPropertyGroups(const QString &path);
void unloadPropertyGroups();
void resetModel();

View File

@@ -336,6 +336,11 @@ void MaterialBrowserView::modelAboutToBeDetached(Model *model)
{
m_widget->materialBrowserModel()->setMaterials({}, m_hasQuick3DImport);
if (m_propertyGroupsLoaded) {
m_propertyGroupsLoaded = false;
m_widget->materialBrowserModel()->unloadPropertyGroups();
}
AbstractView::modelAboutToBeDetached(model);
}