From 4bf2b843c86c87ec48d433d623083a232401af34 Mon Sep 17 00:00:00 2001 From: Miikka Heikkinen Date: Mon, 13 Feb 2023 15:17:33 +0200 Subject: [PATCH] QmlDesigner: Allow texture/light probe creation for hdr and ktx files Also refactored Asset class a bit to optimize cases where multiple type checks are done against same asset by resolving type at constructor instead of on demand. Pretty much all cases where Asset instance is needed also require resolving the type, so this makes sense. Refactored the remaining cases to not create Asset instance unnecessarily. Fixes: QDS-9128 Change-Id: If9d518c9dcfcc70962e5d4e9881889c6ac243c97 Reviewed-by: Mahmoud Badri (cherry picked from commit 0b9eb65d975b499179db32457f1e76009914efe1) Reviewed-by: Tim Jenssen --- .../AssetsContextMenu.qml | 4 +- .../assetslibrary/assetslibrarymodel.cpp | 5 +- .../assetslibrary/assetslibrarymodel.h | 2 +- .../assetslibrary/assetslibrarywidget.cpp | 2 +- .../propertyeditor/assetimageprovider.cpp | 6 +- src/plugins/qmldesigner/utils/asset.cpp | 74 ++++++++++--------- src/plugins/qmldesigner/utils/asset.h | 17 ++++- 7 files changed, 65 insertions(+), 45 deletions(-) diff --git a/share/qtcreator/qmldesigner/itemLibraryQmlSources/AssetsContextMenu.qml b/share/qtcreator/qmldesigner/itemLibraryQmlSources/AssetsContextMenu.qml index 2eda6f07736..03051c747dc 100644 --- a/share/qtcreator/qmldesigner/itemLibraryQmlSources/AssetsContextMenu.qml +++ b/share/qtcreator/qmldesigner/itemLibraryQmlSources/AssetsContextMenu.qml @@ -120,7 +120,7 @@ StudioControls.Menu { id: addTexturesItem text: qsTr("Add Texture") enabled: rootView.hasMaterialLibrary - visible: root.__fileIndex && assetsModel.allFilePathsAreImages(root.__selectedAssetPathsList) + visible: root.__fileIndex && assetsModel.allFilePathsAreTextures(root.__selectedAssetPathsList) height: addTexturesItem.visible ? addTexturesItem.implicitHeight : 0 onTriggered: rootView.addTextures(root.__selectedAssetPathsList) } @@ -130,7 +130,7 @@ StudioControls.Menu { text: qsTr("Add Light Probe") enabled: rootView.hasMaterialLibrary visible: root.__fileIndex && root.__selectedAssetPathsList.length === 1 - && assetsModel.allFilePathsAreImages(root.__selectedAssetPathsList) + && assetsModel.allFilePathsAreTextures(root.__selectedAssetPathsList) height: addLightProbes.visible ? addLightProbes.implicitHeight : 0 onTriggered: rootView.addLightProbe(root.__selectedAssetPathsList[0]) } diff --git a/src/plugins/qmldesigner/components/assetslibrary/assetslibrarymodel.cpp b/src/plugins/qmldesigner/components/assetslibrary/assetslibrarymodel.cpp index c4f1845368d..e7142aa6f70 100644 --- a/src/plugins/qmldesigner/components/assetslibrary/assetslibrarymodel.cpp +++ b/src/plugins/qmldesigner/components/assetslibrary/assetslibrarymodel.cpp @@ -160,10 +160,11 @@ bool AssetsLibraryModel::deleteFolderRecursively(const QModelIndex &folderIndex) return ok; } -bool AssetsLibraryModel::allFilePathsAreImages(const QStringList &filePaths) const +bool AssetsLibraryModel::allFilePathsAreTextures(const QStringList &filePaths) const { return Utils::allOf(filePaths, [](const QString &path) { - return Asset(path).isImage(); + Asset asset(path); + return asset.isImage() || asset.isTexture3D(); }); } diff --git a/src/plugins/qmldesigner/components/assetslibrary/assetslibrarymodel.h b/src/plugins/qmldesigner/components/assetslibrary/assetslibrarymodel.h index c7ccba33391..625216af8a6 100644 --- a/src/plugins/qmldesigner/components/assetslibrary/assetslibrarymodel.h +++ b/src/plugins/qmldesigner/components/assetslibrary/assetslibrarymodel.h @@ -46,7 +46,7 @@ public: Q_INVOKABLE bool renameFolder(const QString &folderPath, const QString &newName); Q_INVOKABLE bool addNewFolder(const QString &folderPath); Q_INVOKABLE bool deleteFolderRecursively(const QModelIndex &folderIndex); - Q_INVOKABLE bool allFilePathsAreImages(const QStringList &filePaths) const; + Q_INVOKABLE bool allFilePathsAreTextures(const QStringList &filePaths) const; int columnCount(const QModelIndex &parent = QModelIndex()) const override { diff --git a/src/plugins/qmldesigner/components/assetslibrary/assetslibrarywidget.cpp b/src/plugins/qmldesigner/components/assetslibrary/assetslibrarywidget.cpp index bf82e08aae7..9f9fde4992e 100644 --- a/src/plugins/qmldesigner/components/assetslibrary/assetslibrarywidget.cpp +++ b/src/plugins/qmldesigner/components/assetslibrary/assetslibrarywidget.cpp @@ -320,7 +320,7 @@ QSet AssetsLibraryWidget::supportedAssetSuffixes(bool complex) QSet suffixes; for (const AddResourceHandler &handler : handlers) { - if (Asset(handler.filter).isSupported() != complex) + if (Asset::isSupported(handler.filter) != complex) suffixes.insert(handler.filter); } diff --git a/src/plugins/qmldesigner/components/propertyeditor/assetimageprovider.cpp b/src/plugins/qmldesigner/components/propertyeditor/assetimageprovider.cpp index a4f5a85108f..ad60079c5aa 100644 --- a/src/plugins/qmldesigner/components/propertyeditor/assetimageprovider.cpp +++ b/src/plugins/qmldesigner/components/propertyeditor/assetimageprovider.cpp @@ -18,12 +18,10 @@ namespace QmlDesigner { QQuickImageResponse *AssetImageProvider::requestImageResponse(const QString &id, const QSize &requestedSize) { - Asset asset(id); - - if (asset.suffix() == "*.mesh") + if (id.endsWith(".mesh")) return m_imageCacheProvider.requestImageResponse(id, {}); - if (asset.suffix() == "*.builtin") + if (id.endsWith(".builtin")) return m_imageCacheProvider.requestImageResponse("#" + id.split('.').first(), {}); return m_imageCacheProvider.requestImageResponse(id, requestedSize); diff --git a/src/plugins/qmldesigner/utils/asset.cpp b/src/plugins/qmldesigner/utils/asset.cpp index 7e8931d9cb9..738d49f3340 100644 --- a/src/plugins/qmldesigner/utils/asset.cpp +++ b/src/plugins/qmldesigner/utils/asset.cpp @@ -13,6 +13,8 @@ Asset::Asset(const QString &filePath) const QStringList split = filePath.split('.'); if (split.size() > 1) m_suffix = "*." + split.last().toLower(); + + resolveType(); } @@ -92,68 +94,49 @@ const QSet &Asset::supportedSuffixes() return allSuffixes; } +bool Asset::isSupported(const QString &path) +{ + return supportedSuffixes().contains(path); +} + Asset::Type Asset::type() const { - if (supportedImageSuffixes().contains(m_suffix)) - return Asset::Type::Image; - - if (supportedFragmentShaderSuffixes().contains(m_suffix)) - return Asset::Type::FragmentShader; - - if (supportedShaderSuffixes().contains(m_suffix)) - return Asset::Type::Shader; - - if (supportedFontSuffixes().contains(m_suffix)) - return Asset::Type::Font; - - if (supportedAudioSuffixes().contains(m_suffix)) - return Asset::Type::Audio; - - if (supportedVideoSuffixes().contains(m_suffix)) - return Asset::Type::Video; - - if (supportedTexture3DSuffixes().contains(m_suffix)) - return Asset::Type::Texture3D; - - if (supportedEffectMakerSuffixes().contains(m_suffix)) - return Asset::Type::Effect; - - return Asset::Type::Unknown; + return m_type; } bool Asset::isImage() const { - return type() == Asset::Type::Image; + return m_type == Asset::Type::Image; } bool Asset::isFragmentShader() const { - return type() == Asset::Type::FragmentShader; + return m_type == Asset::Type::FragmentShader; } bool Asset::isShader() const { - return type() == Asset::Type::Shader; + return m_type == Asset::Type::Shader; } bool Asset::isFont() const { - return type() == Asset::Type::Font; + return m_type == Asset::Type::Font; } bool Asset::isAudio() const { - return type() == Asset::Type::Audio; + return m_type == Asset::Type::Audio; } bool Asset::isVideo() const { - return type() == Asset::Type::Video; + return m_type == Asset::Type::Video; } bool Asset::isTexture3D() const { - return type() == Asset::Type::Texture3D; + return m_type == Asset::Type::Texture3D; } bool Asset::isHdrFile() const @@ -168,7 +151,7 @@ bool Asset::isKtxFile() const bool Asset::isEffect() const { - return type() == Asset::Type::Effect; + return m_type == Asset::Type::Effect; } const QString Asset::suffix() const @@ -183,7 +166,30 @@ const QString Asset::id() const bool Asset::isSupported() const { - return supportedSuffixes().contains(m_filePath); + return m_type != Asset::Type::Unknown; +} + +void Asset::resolveType() +{ + if (m_suffix.isEmpty()) + return; + + if (supportedImageSuffixes().contains(m_suffix)) + m_type = Asset::Type::Image; + else if (supportedFragmentShaderSuffixes().contains(m_suffix)) + m_type = Asset::Type::FragmentShader; + else if (supportedShaderSuffixes().contains(m_suffix)) + m_type = Asset::Type::Shader; + else if (supportedFontSuffixes().contains(m_suffix)) + m_type = Asset::Type::Font; + else if (supportedAudioSuffixes().contains(m_suffix)) + m_type = Asset::Type::Audio; + else if (supportedVideoSuffixes().contains(m_suffix)) + m_type = Asset::Type::Video; + else if (supportedTexture3DSuffixes().contains(m_suffix)) + m_type = Asset::Type::Texture3D; + else if (supportedEffectMakerSuffixes().contains(m_suffix)) + m_type = Asset::Type::Effect; } bool Asset::hasSuffix() const diff --git a/src/plugins/qmldesigner/utils/asset.h b/src/plugins/qmldesigner/utils/asset.h index 9218cac3b87..a9d6d0a135a 100644 --- a/src/plugins/qmldesigner/utils/asset.h +++ b/src/plugins/qmldesigner/utils/asset.h @@ -3,12 +3,23 @@ #pragma once +#include + namespace QmlDesigner { class Asset { public: - enum Type { Unknown, Image, MissingImage, FragmentShader, Font, Audio, Video, Texture3D, Effect, Shader }; + enum Type { Unknown, + Image, + MissingImage, + FragmentShader, + Font, + Audio, + Video, + Texture3D, + Effect, + Shader }; Asset(const QString &filePath); @@ -21,6 +32,7 @@ public: static const QStringList &supportedTexture3DSuffixes(); static const QStringList &supportedEffectMakerSuffixes(); static const QSet &supportedSuffixes(); + static bool isSupported(const QString &path); const QString suffix() const; const QString id() const; @@ -40,8 +52,11 @@ public: bool isSupported() const; private: + void resolveType(); + QString m_filePath; QString m_suffix; + Type m_type = Unknown; }; } // namespace QmlDesigner