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

View File

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

View File

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