QmlDesigner: Hide material bundle if QtQuick3D version too low

Most materials in the material bundle require at least QtQuick3D 6.3,
so we hide the bundle materials from material browser when the detected
module version is less than 6.3.

Fixes: QDS-8100
Change-Id: I9f50b507c3c3c50f821fa6a902995b999da24464
Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org>
Reviewed-by: Mahmoud Badri <mahmoud.badri@qt.io>
This commit is contained in:
Miikka Heikkinen
2022-10-27 16:40:52 +03:00
parent d72f5674b5
commit bc370aecfd
6 changed files with 91 additions and 22 deletions

View File

@@ -94,7 +94,7 @@ QHash<int, QByteArray> MaterialBrowserBundleModel::roleNames() const
void MaterialBrowserBundleModel::loadMaterialBundle() void MaterialBrowserBundleModel::loadMaterialBundle()
{ {
if (m_matBundleExists || m_probeMatBundleDir) if (m_matBundleLoaded || m_probeMatBundleDir)
return; return;
QDir matBundleDir(qEnvironmentVariable("MATERIAL_BUNDLE_PATH")); QDir matBundleDir(qEnvironmentVariable("MATERIAL_BUNDLE_PATH"));
@@ -130,7 +130,7 @@ void MaterialBrowserBundleModel::loadMaterialBundle()
} }
} }
m_matBundleExists = true; m_matBundleLoaded = true;
QString bundleId = m_matBundleObj.value("id").toString(); QString bundleId = m_matBundleObj.value("id").toString();
@@ -184,20 +184,6 @@ void MaterialBrowserBundleModel::loadMaterialBundle()
}); });
} }
bool MaterialBrowserBundleModel::hasQuick3DImport() const
{
return m_hasQuick3DImport;
}
void MaterialBrowserBundleModel::setHasQuick3DImport(bool b)
{
if (b == m_hasQuick3DImport)
return;
m_hasQuick3DImport = b;
emit hasQuick3DImportChanged();
}
bool MaterialBrowserBundleModel::hasMaterialRoot() const bool MaterialBrowserBundleModel::hasMaterialRoot() const
{ {
return m_hasMaterialRoot; return m_hasMaterialRoot;
@@ -212,6 +198,11 @@ void MaterialBrowserBundleModel::setHasMaterialRoot(bool b)
emit hasMaterialRootChanged(); emit hasMaterialRootChanged();
} }
bool MaterialBrowserBundleModel::matBundleExists() const
{
return m_matBundleLoaded && m_quick3dMajorVersion == 6 && m_quick3dMinorVersion >= 3;
}
Internal::BundleImporter *MaterialBrowserBundleModel::bundleImporter() const Internal::BundleImporter *MaterialBrowserBundleModel::bundleImporter() const
{ {
return m_importer; return m_importer;
@@ -253,6 +244,17 @@ void MaterialBrowserBundleModel::updateImportedState(const QStringList &imported
resetModel(); resetModel();
} }
void MaterialBrowserBundleModel::setQuick3DImportVersion(int major, int minor)
{
bool bundleExisted = matBundleExists();
m_quick3dMajorVersion = major;
m_quick3dMinorVersion = minor;
if (bundleExisted != matBundleExists())
emit matBundleExistsChanged();
}
void MaterialBrowserBundleModel::resetModel() void MaterialBrowserBundleModel::resetModel()
{ {
beginResetModel(); beginResetModel();

View File

@@ -46,9 +46,8 @@ class MaterialBrowserBundleModel : public QAbstractListModel
{ {
Q_OBJECT Q_OBJECT
Q_PROPERTY(bool matBundleExists MEMBER m_matBundleExists CONSTANT) Q_PROPERTY(bool matBundleExists READ matBundleExists NOTIFY matBundleExistsChanged)
Q_PROPERTY(bool isEmpty MEMBER m_isEmpty NOTIFY isEmptyChanged) Q_PROPERTY(bool isEmpty MEMBER m_isEmpty NOTIFY isEmptyChanged)
Q_PROPERTY(bool hasQuick3DImport READ hasQuick3DImport WRITE setHasQuick3DImport NOTIFY hasQuick3DImportChanged)
Q_PROPERTY(bool hasMaterialRoot READ hasMaterialRoot WRITE setHasMaterialRoot NOTIFY hasMaterialRootChanged) Q_PROPERTY(bool hasMaterialRoot READ hasMaterialRoot WRITE setHasMaterialRoot NOTIFY hasMaterialRootChanged)
Q_PROPERTY(bool importerRunning MEMBER m_importerRunning NOTIFY importerRunningChanged) Q_PROPERTY(bool importerRunning MEMBER m_importerRunning NOTIFY importerRunningChanged)
@@ -63,12 +62,13 @@ public:
void setSearchText(const QString &searchText); void setSearchText(const QString &searchText);
void updateImportedState(const QStringList &importedMats); void updateImportedState(const QStringList &importedMats);
bool hasQuick3DImport() const; void setQuick3DImportVersion(int major, int minor);
void setHasQuick3DImport(bool b);
bool hasMaterialRoot() const; bool hasMaterialRoot() const;
void setHasMaterialRoot(bool b); void setHasMaterialRoot(bool b);
bool matBundleExists() const;
Internal::BundleImporter *bundleImporter() const; Internal::BundleImporter *bundleImporter() const;
void resetModel(); void resetModel();
@@ -87,6 +87,7 @@ signals:
void bundleMaterialAboutToUnimport(const QmlDesigner::TypeName &type); void bundleMaterialAboutToUnimport(const QmlDesigner::TypeName &type);
void bundleMaterialUnimported(const QmlDesigner::NodeMetaInfo &metaInfo); void bundleMaterialUnimported(const QmlDesigner::NodeMetaInfo &metaInfo);
void importerRunningChanged(); void importerRunningChanged();
void matBundleExistsChanged();
private: private:
void loadMaterialBundle(); void loadMaterialBundle();
@@ -98,11 +99,13 @@ private:
Internal::BundleImporter *m_importer = nullptr; Internal::BundleImporter *m_importer = nullptr;
bool m_isEmpty = true; bool m_isEmpty = true;
bool m_hasQuick3DImport = false;
bool m_hasMaterialRoot = false; bool m_hasMaterialRoot = false;
bool m_matBundleExists = false; bool m_matBundleLoaded = false;
bool m_probeMatBundleDir = false; bool m_probeMatBundleDir = false;
bool m_importerRunning = false; bool m_importerRunning = false;
int m_quick3dMajorVersion = -1;
int m_quick3dMinorVersion = -1;
}; };
} // namespace QmlDesigner } // namespace QmlDesigner

View File

@@ -43,6 +43,14 @@
#include <qmldesignerconstants.h> #include <qmldesignerconstants.h>
#include <utils/algorithm.h> #include <utils/algorithm.h>
#ifndef QMLDESIGNER_TEST
#include <projectexplorer/kit.h>
#include <projectexplorer/session.h>
#include <projectexplorer/target.h>
#include <qtsupport/baseqtversion.h>
#include <qtsupport/qtkitinformation.h>
#endif
#include <QQuickItem> #include <QQuickItem>
#include <QRegularExpression> #include <QRegularExpression>
#include <QTimer> #include <QTimer>
@@ -289,6 +297,7 @@ void MaterialBrowserView::modelAttached(Model *model)
m_widget->materialBrowserModel()->setHasMaterialRoot(rootModelNode().isSubclassOf("QtQuick3D.Material")); m_widget->materialBrowserModel()->setHasMaterialRoot(rootModelNode().isSubclassOf("QtQuick3D.Material"));
m_hasQuick3DImport = model->hasImport("QtQuick3D"); m_hasQuick3DImport = model->hasImport("QtQuick3D");
updateBundleMaterialsQuick3DVersion();
updateBundleMaterialsImportedState(); updateBundleMaterialsImportedState();
// Project load is already very busy and may even trigger puppet reset, so let's wait a moment // Project load is already very busy and may even trigger puppet reset, so let's wait a moment
@@ -482,6 +491,41 @@ void MaterialBrowserView::updateBundleMaterialsImportedState()
m_widget->materialBrowserBundleModel()->updateImportedState(importedBundleMats); m_widget->materialBrowserBundleModel()->updateImportedState(importedBundleMats);
} }
void MaterialBrowserView::updateBundleMaterialsQuick3DVersion()
{
bool hasImport = false;
int major = -1;
int minor = -1;
const QString url {"QtQuick3D"};
const auto imports = model()->imports();
for (const auto &import : imports) {
if (import.url() == url) {
hasImport = true;
const int importMajor = import.majorVersion();
if (major < importMajor) {
minor = -1;
major = importMajor;
}
if (major == importMajor)
minor = qMax(minor, import.minorVersion());
}
}
#ifndef QMLDESIGNER_TEST
if (hasImport && major == -1) {
// Import without specifying version, so we take the kit version
auto target = ProjectExplorer::SessionManager::startupTarget();
if (target) {
QtSupport::QtVersion *qtVersion = QtSupport::QtKitAspect::qtVersion(target->kit());
if (qtVersion) {
major = qtVersion->qtVersion().majorVersion;
minor = qtVersion->qtVersion().minorVersion;
}
}
}
#endif
m_widget->materialBrowserBundleModel()->setQuick3DImportVersion(major, minor);
}
ModelNode MaterialBrowserView::getBundleMaterialDefaultInstance(const TypeName &type) ModelNode MaterialBrowserView::getBundleMaterialDefaultInstance(const TypeName &type)
{ {
const QList<ModelNode> materials = m_widget->materialBrowserModel()->materials(); const QList<ModelNode> materials = m_widget->materialBrowserModel()->materials();
@@ -511,6 +555,8 @@ void MaterialBrowserView::importsChanged(const QList<Import> &addedImports, cons
bool hasQuick3DImport = model()->hasImport("QtQuick3D"); bool hasQuick3DImport = model()->hasImport("QtQuick3D");
updateBundleMaterialsQuick3DVersion();
if (hasQuick3DImport == m_hasQuick3DImport) if (hasQuick3DImport == m_hasQuick3DImport)
return; return;

View File

@@ -69,6 +69,7 @@ private:
bool isMaterial(const ModelNode &node) const; bool isMaterial(const ModelNode &node) const;
void loadPropertyGroups(); void loadPropertyGroups();
void updateBundleMaterialsImportedState(); void updateBundleMaterialsImportedState();
void updateBundleMaterialsQuick3DVersion();
void applyBundleMaterialToDropTarget(const ModelNode &bundleMat, const NodeMetaInfo &metaInfo = {}); void applyBundleMaterialToDropTarget(const ModelNode &bundleMat, const NodeMetaInfo &metaInfo = {});
ModelNode getBundleMaterialDefaultInstance(const TypeName &type); ModelNode getBundleMaterialDefaultInstance(const TypeName &type);

View File

@@ -63,7 +63,9 @@ public:
bool isSameModule(const Import &other) const; bool isSameModule(const Import &other) const;
int majorVersion() const; int majorVersion() const;
int minorVersion() const;
static int majorFromVersion(const QString &version); static int majorFromVersion(const QString &version);
static int minorFromVersion(const QString &version);
private: private:
Import(const QString &url, const QString &file, const QString &version, const QString &alias, const QStringList &importPaths); Import(const QString &url, const QString &file, const QString &version, const QString &alias, const QStringList &importPaths);

View File

@@ -102,6 +102,11 @@ int Import::majorVersion() const
return majorFromVersion(m_version); return majorFromVersion(m_version);
} }
int Import::minorVersion() const
{
return minorFromVersion(m_version);
}
int Import::majorFromVersion(const QString &version) int Import::majorFromVersion(const QString &version)
{ {
if (version.isEmpty()) if (version.isEmpty())
@@ -109,6 +114,16 @@ int Import::majorFromVersion(const QString &version)
return version.split('.').first().toInt(); return version.split('.').first().toInt();
} }
int Import::minorFromVersion(const QString &version)
{
if (version.isEmpty())
return -1;
const QStringList parts = version.split('.');
if (parts.size() < 2)
return -1;
return parts[1].toInt();
}
Utils::QHashValueType qHash(const Import &import) Utils::QHashValueType qHash(const Import &import)
{ {
return ::qHash(import.url()) ^ ::qHash(import.file()) ^ ::qHash(import.version()) ^ ::qHash(import.alias()); return ::qHash(import.url()) ^ ::qHash(import.file()) ^ ::qHash(import.version()) ^ ::qHash(import.alias());