From 4a5359cb868dbf869d4a91ba5a942297b7d8b776 Mon Sep 17 00:00:00 2001 From: Samuel Ghinet Date: Thu, 15 Dec 2022 19:26:49 +0200 Subject: [PATCH] QmlDesigner: Extract asset type stuff from the AssetsLibraryModel This is in preparation for the task that will show metadata of assets. The functions for checking the asset type have little to nothing to do with the model. This change will also clean up the code a bit. Task-number: QDS-8177 Change-Id: Ibab28f5b63228f626f517a59e2442d2718c2fc07 Reviewed-by: Miikka Heikkinen Reviewed-by: Thomas Hartmann --- src/plugins/qmldesigner/CMakeLists.txt | 1 + .../assetslibraryiconprovider.cpp | 19 +- .../assetslibrary/assetslibrarymodel.cpp | 86 +-------- .../assetslibrary/assetslibrarymodel.h | 16 +- .../assetslibrary/assetslibrarywidget.cpp | 31 +-- .../propertyeditorimageprovider.cpp | 18 +- src/plugins/qmldesigner/utils/asset.cpp | 182 ++++++++++++++++++ src/plugins/qmldesigner/utils/asset.h | 45 +++++ .../qt5informationnodeinstanceserver.cpp | 4 +- 9 files changed, 272 insertions(+), 130 deletions(-) create mode 100644 src/plugins/qmldesigner/utils/asset.cpp create mode 100644 src/plugins/qmldesigner/utils/asset.h diff --git a/src/plugins/qmldesigner/CMakeLists.txt b/src/plugins/qmldesigner/CMakeLists.txt index 8018e76044d..ce772baf43c 100644 --- a/src/plugins/qmldesigner/CMakeLists.txt +++ b/src/plugins/qmldesigner/CMakeLists.txt @@ -20,6 +20,7 @@ add_qtc_library(QmlDesignerUtils STATIC PUBLIC_INCLUDES ${CMAKE_CURRENT_LIST_DIR}/utils SOURCES_PREFIX ${CMAKE_CURRENT_LIST_DIR}/utils SOURCES + asset.cpp asset.h designersettings.cpp designersettings.h hdrimage.cpp hdrimage.h imageutils.cpp imageutils.h diff --git a/src/plugins/qmldesigner/components/assetslibrary/assetslibraryiconprovider.cpp b/src/plugins/qmldesigner/components/assetslibrary/assetslibraryiconprovider.cpp index e9d3b42855c..14a433e8602 100644 --- a/src/plugins/qmldesigner/components/assetslibrary/assetslibraryiconprovider.cpp +++ b/src/plugins/qmldesigner/components/assetslibrary/assetslibraryiconprovider.cpp @@ -2,7 +2,7 @@ // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0+ OR GPL-3.0 WITH Qt-GPL-exception-1.0 #include "assetslibraryiconprovider.h" -#include "assetslibrarymodel.h" +#include "asset.h" #include "modelnodeoperations.h" #include @@ -57,24 +57,25 @@ QPixmap AssetsLibraryIconProvider::generateFontIcons(const QString &filePath, co QPixmap AssetsLibraryIconProvider::fetchPixmap(const QString &id, const QSize &requestedSize) const { - const QString suffix = "*." + id.split('.').last().toLower(); + Asset asset(id); + if (id == "browse") { return Utils::StyleHelper::dpiSpecificImageFile(":/AssetsLibrary/images/browse.png"); - } else if (AssetsLibraryModel::supportedFontSuffixes().contains(suffix)) { + } else if (asset.isFont()) { return generateFontIcons(id, requestedSize); - } else if (AssetsLibraryModel::supportedImageSuffixes().contains(suffix)) { + } else if (asset.isImage()) { return Utils::StyleHelper::dpiSpecificImageFile(id); - } else if (AssetsLibraryModel::supportedTexture3DSuffixes().contains(suffix)) { + } else if (asset.isTexture3D()) { return HdrImage{id}.toPixmap(); } else { QString type; - if (AssetsLibraryModel::supportedShaderSuffixes().contains(suffix)) + if (asset.isShader()) type = "shader"; - else if (AssetsLibraryModel::supportedAudioSuffixes().contains(suffix)) + else if (asset.isAudio()) type = "sound"; - else if (AssetsLibraryModel::supportedVideoSuffixes().contains(suffix)) + else if (asset.isVideo()) type = "video"; - else if (AssetsLibraryModel::supportedEffectMakerSuffixes().contains(suffix)) + else if (asset.isEffect()) type = QmlDesigner::ModelNodeOperations::getEffectIcon(id); QString pathTemplate = QString(":/AssetsLibrary/images/asset_%1%2.png").arg(type); diff --git a/src/plugins/qmldesigner/components/assetslibrary/assetslibrarymodel.cpp b/src/plugins/qmldesigner/components/assetslibrary/assetslibrarymodel.cpp index e52f168f73d..0953eff9eb1 100644 --- a/src/plugins/qmldesigner/components/assetslibrary/assetslibrarymodel.cpp +++ b/src/plugins/qmldesigner/components/assetslibrary/assetslibrarymodel.cpp @@ -1,13 +1,13 @@ -// Copyright (C) 2021 The Qt Company Ltd. +// Copyright (C) 2022 The Qt Company Ltd. // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0+ OR GPL-3.0 WITH Qt-GPL-exception-1.0 #include #include #include -#include #include #include +#include "asset.h" #include "assetslibrarymodel.h" #include @@ -158,9 +158,7 @@ bool AssetsLibraryModel::deleteFolderRecursively(const QModelIndex &folderIndex) bool AssetsLibraryModel::allFilePathsAreImages(const QStringList &filePaths) const { return Utils::allOf(filePaths, [](const QString &path) { - const QString suffix = "*." + path.split('.').last().toLower(); - - return AssetsLibraryModel::supportedImageSuffixes().contains(suffix); + return Asset(path).isImage(); }); } @@ -299,7 +297,7 @@ void AssetsLibraryModel::setRootPath(const QString &newPath) m_rootPath = newPath; m_sourceFsModel->setRootPath(newPath); - m_sourceFsModel->setNameFilters(supportedSuffixes().values()); + m_sourceFsModel->setNameFilters(Asset::supportedSuffixes().values()); m_sourceFsModel->setNameFilterDisables(false); endResetModel(); @@ -374,80 +372,4 @@ QString AssetsLibraryModel::parentDirPath(const QString &path) const return filePath(parentIdx); } -const QStringList &AssetsLibraryModel::supportedImageSuffixes() -{ - static QStringList retList; - if (retList.isEmpty()) { - const QList suffixes = QImageReader::supportedImageFormats(); - for (const QByteArray &suffix : suffixes) - retList.append("*." + QString::fromUtf8(suffix)); - } - return retList; -} - -const QStringList &AssetsLibraryModel::supportedFragmentShaderSuffixes() -{ - static const QStringList retList {"*.frag", "*.glsl", "*.glslf", "*.fsh"}; - return retList; -} - -const QStringList &AssetsLibraryModel::supportedShaderSuffixes() -{ - static const QStringList retList {"*.frag", "*.vert", - "*.glsl", "*.glslv", "*.glslf", - "*.vsh", "*.fsh"}; - return retList; -} - -const QStringList &AssetsLibraryModel::supportedFontSuffixes() -{ - static const QStringList retList {"*.ttf", "*.otf"}; - return retList; -} - -const QStringList &AssetsLibraryModel::supportedAudioSuffixes() -{ - static const QStringList retList {"*.wav", "*.mp3"}; - return retList; -} - -const QStringList &AssetsLibraryModel::supportedVideoSuffixes() -{ - static const QStringList retList {"*.mp4"}; - return retList; -} - -const QStringList &AssetsLibraryModel::supportedTexture3DSuffixes() -{ - // These are file types only supported by 3D textures - static QStringList retList {"*.hdr", "*.ktx"}; - return retList; -} - -const QStringList &AssetsLibraryModel::supportedEffectMakerSuffixes() -{ - // These are file types only supported by Effect Maker - static QStringList retList {"*.qep"}; - return retList; -} - -const QSet &AssetsLibraryModel::supportedSuffixes() -{ - static QSet allSuffixes; - if (allSuffixes.isEmpty()) { - auto insertSuffixes = [](const QStringList &suffixes) { - for (const auto &suffix : suffixes) - allSuffixes.insert(suffix); - }; - insertSuffixes(supportedImageSuffixes()); - insertSuffixes(supportedShaderSuffixes()); - insertSuffixes(supportedFontSuffixes()); - insertSuffixes(supportedAudioSuffixes()); - insertSuffixes(supportedVideoSuffixes()); - insertSuffixes(supportedTexture3DSuffixes()); - insertSuffixes(supportedEffectMakerSuffixes()); - } - return allSuffixes; -} - } // namespace QmlDesigner diff --git a/src/plugins/qmldesigner/components/assetslibrary/assetslibrarymodel.h b/src/plugins/qmldesigner/components/assetslibrary/assetslibrarymodel.h index b6cadd4eceb..0538bedf42b 100644 --- a/src/plugins/qmldesigner/components/assetslibrary/assetslibrarymodel.h +++ b/src/plugins/qmldesigner/components/assetslibrary/assetslibrarymodel.h @@ -1,14 +1,14 @@ -// Copyright (C) 2021 The Qt Company Ltd. +// Copyright (C) 2022 The Qt Company Ltd. // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0+ OR GPL-3.0 WITH Qt-GPL-exception-1.0 #pragma once +#include #include #include -#include -#include #include +#include namespace QmlDesigner { @@ -60,16 +60,6 @@ public: bool haveFiles() const { return m_haveFiles; } - static const QStringList &supportedImageSuffixes(); - static const QStringList &supportedFragmentShaderSuffixes(); - static const QStringList &supportedShaderSuffixes(); - static const QStringList &supportedFontSuffixes(); - static const QStringList &supportedAudioSuffixes(); - static const QStringList &supportedVideoSuffixes(); - static const QStringList &supportedTexture3DSuffixes(); - static const QStringList &supportedEffectMakerSuffixes(); - static const QSet &supportedSuffixes(); - signals: void directoryLoaded(const QString &path); void rootPathChanged(); diff --git a/src/plugins/qmldesigner/components/assetslibrary/assetslibrarywidget.cpp b/src/plugins/qmldesigner/components/assetslibrary/assetslibrarywidget.cpp index 05ebb729869..cc9263eda46 100644 --- a/src/plugins/qmldesigner/components/assetslibrary/assetslibrarywidget.cpp +++ b/src/plugins/qmldesigner/components/assetslibrary/assetslibrarywidget.cpp @@ -3,8 +3,9 @@ #include "assetslibrarywidget.h" -#include "assetslibrarymodel.h" +#include "asset.h" #include "assetslibraryiconprovider.h" +#include "assetslibrarymodel.h" #include @@ -229,7 +230,7 @@ QSet AssetsLibraryWidget::supportedAssetSuffixes(bool complex) QSet suffixes; for (const AddResourceHandler &handler : handlers) { - if (AssetsLibraryModel::supportedSuffixes().contains(handler.filter) != complex) + if (Asset(handler.filter).isSupported() != complex) suffixes.insert(handler.filter); } @@ -290,32 +291,32 @@ void AssetsLibraryWidget::startDragAsset(const QStringList &assetPaths, const QP QPair AssetsLibraryWidget::getAssetTypeAndData(const QString &assetPath) { - QString suffix = "*." + assetPath.split('.').last().toLower(); - if (!suffix.isEmpty()) { - if (AssetsLibraryModel::supportedImageSuffixes().contains(suffix)) { + Asset asset(assetPath); + if (asset.hasSuffix()) { + if (asset.isImage()) { // Data: Image format (suffix) - return {Constants::MIME_TYPE_ASSET_IMAGE, suffix.toUtf8()}; - } else if (AssetsLibraryModel::supportedFontSuffixes().contains(suffix)) { + return {Constants::MIME_TYPE_ASSET_IMAGE, asset.suffix().toUtf8()}; + } else if (asset.isFont()) { // Data: Font family name QRawFont font(assetPath, 10); QString fontFamily = font.isValid() ? font.familyName() : ""; return {Constants::MIME_TYPE_ASSET_FONT, fontFamily.toUtf8()}; - } else if (AssetsLibraryModel::supportedShaderSuffixes().contains(suffix)) { + } else if (asset.isShader()) { // Data: shader type, frament (f) or vertex (v) return {Constants::MIME_TYPE_ASSET_SHADER, - AssetsLibraryModel::supportedFragmentShaderSuffixes().contains(suffix) ? "f" : "v"}; - } else if (AssetsLibraryModel::supportedAudioSuffixes().contains(suffix)) { + asset.isFragmentShader() ? "f" : "v"}; + } else if (asset.isAudio()) { // No extra data for sounds return {Constants::MIME_TYPE_ASSET_SOUND, {}}; - } else if (AssetsLibraryModel::supportedVideoSuffixes().contains(suffix)) { + } else if (asset.isVideo()) { // No extra data for videos return {Constants::MIME_TYPE_ASSET_VIDEO, {}}; - } else if (AssetsLibraryModel::supportedTexture3DSuffixes().contains(suffix)) { + } else if (asset.isTexture3D()) { // Data: Image format (suffix) - return {Constants::MIME_TYPE_ASSET_TEXTURE3D, suffix.toUtf8()}; - } else if (AssetsLibraryModel::supportedEffectMakerSuffixes().contains(suffix)) { + return {Constants::MIME_TYPE_ASSET_TEXTURE3D, asset.suffix().toUtf8()}; + } else if (asset.isEffect()) { // Data: Effect Maker format (suffix) - return {Constants::MIME_TYPE_ASSET_EFFECT, suffix.toUtf8()}; + return {Constants::MIME_TYPE_ASSET_EFFECT, asset.suffix().toUtf8()}; } } return {}; diff --git a/src/plugins/qmldesigner/components/propertyeditor/propertyeditorimageprovider.cpp b/src/plugins/qmldesigner/components/propertyeditor/propertyeditorimageprovider.cpp index e7ff48450d1..fc5a09818ca 100644 --- a/src/plugins/qmldesigner/components/propertyeditor/propertyeditorimageprovider.cpp +++ b/src/plugins/qmldesigner/components/propertyeditor/propertyeditorimageprovider.cpp @@ -2,7 +2,7 @@ // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0+ OR GPL-3.0 WITH Qt-GPL-exception-1.0 #include "propertyeditorimageprovider.h" -#include "assetslibrarymodel.h" +#include "asset.h" #include #include @@ -16,12 +16,12 @@ namespace QmlDesigner { QQuickImageResponse *PropertyEditorImageProvider::requestImageResponse(const QString &id, const QSize &requestedSize) { - const QString suffix = "*." + id.split('.').last().toLower(); + Asset asset(id); - if (suffix == "*.mesh") + if (asset.suffix() == "*.mesh") return m_smallImageCacheProvider.requestImageResponse(id, requestedSize); - if (suffix == "*.builtin") + if (asset.suffix() == "*.builtin") return m_smallImageCacheProvider.requestImageResponse("#" + id.split('.').first(), requestedSize); @@ -29,15 +29,15 @@ QQuickImageResponse *PropertyEditorImageProvider::requestImageResponse(const QSt QMetaObject::invokeMethod( response.get(), - [response = QPointer(response.get()), suffix, id, requestedSize] { - if (AssetsLibraryModel::supportedImageSuffixes().contains(suffix)) { - QImage image = QImage(Utils::StyleHelper::dpiSpecificImageFile(id)); + [response = QPointer(response.get()), asset, requestedSize] { + if (asset.isImage()) { + QImage image = QImage(Utils::StyleHelper::dpiSpecificImageFile(asset.id())); if (!image.isNull()) { response->setImage(image.scaled(requestedSize, Qt::KeepAspectRatio)); return; } - } else if (AssetsLibraryModel::supportedTexture3DSuffixes().contains(suffix)) { - HdrImage hdr{id}; + } else if (asset.isTexture3D()) { + HdrImage hdr{asset.id()}; if (!hdr.image().isNull()) { response->setImage(hdr.image().scaled(requestedSize, Qt::KeepAspectRatio)); return; diff --git a/src/plugins/qmldesigner/utils/asset.cpp b/src/plugins/qmldesigner/utils/asset.cpp new file mode 100644 index 00000000000..6a0a4658429 --- /dev/null +++ b/src/plugins/qmldesigner/utils/asset.cpp @@ -0,0 +1,182 @@ +// Copyright (C) 2022 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0+ OR GPL-3.0 WITH Qt-GPL-exception-1.0 + +#include + +#include "asset.h" + +namespace QmlDesigner { + +Asset::Asset(const QString &filePath) + : m_filePath(filePath) +{ + m_suffix = "*." + filePath.split('.').last().toLower(); +} + + +const QStringList &Asset::supportedImageSuffixes() +{ + static QStringList retList; + if (retList.isEmpty()) { + const QList suffixes = QImageReader::supportedImageFormats(); + for (const QByteArray &suffix : suffixes) + retList.append("*." + QString::fromUtf8(suffix)); + } + return retList; +} + +const QStringList &Asset::supportedFragmentShaderSuffixes() +{ + static const QStringList retList {"*.frag", "*.glsl", "*.glslf", "*.fsh"}; + return retList; +} + +const QStringList &Asset::supportedShaderSuffixes() +{ + static const QStringList retList {"*.frag", "*.vert", + "*.glsl", "*.glslv", "*.glslf", + "*.vsh", "*.fsh"}; + return retList; +} + +const QStringList &Asset::supportedFontSuffixes() +{ + static const QStringList retList {"*.ttf", "*.otf"}; + return retList; +} + +const QStringList &Asset::supportedAudioSuffixes() +{ + static const QStringList retList {"*.wav", "*.mp3"}; + return retList; +} + +const QStringList &Asset::supportedVideoSuffixes() +{ + static const QStringList retList {"*.mp4"}; + return retList; +} + +const QStringList &Asset::supportedTexture3DSuffixes() +{ + // These are file types only supported by 3D textures + static QStringList retList {"*.hdr", "*.ktx"}; + return retList; +} + +const QStringList &Asset::supportedEffectMakerSuffixes() +{ + // These are file types only supported by Effect Maker + static QStringList retList {"*.qep"}; + return retList; +} + +const QSet &Asset::supportedSuffixes() +{ + static QSet allSuffixes; + if (allSuffixes.isEmpty()) { + auto insertSuffixes = [](const QStringList &suffixes) { + for (const auto &suffix : suffixes) + allSuffixes.insert(suffix); + }; + insertSuffixes(supportedImageSuffixes()); + insertSuffixes(supportedShaderSuffixes()); + insertSuffixes(supportedFontSuffixes()); + insertSuffixes(supportedAudioSuffixes()); + insertSuffixes(supportedVideoSuffixes()); + insertSuffixes(supportedTexture3DSuffixes()); + insertSuffixes(supportedEffectMakerSuffixes()); + } + return allSuffixes; +} + +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; +} + +bool Asset::isImage() const +{ + return type() == Asset::Type::Image; +} + +bool Asset::isFragmentShader() const +{ + return type() == Asset::Type::FragmentShader; +} + +bool Asset::isShader() const +{ + return type() == Asset::Type::Shader; +} + +bool Asset::isFont() const +{ + return type() == Asset::Type::Font; +} + +bool Asset::isAudio() const +{ + return type() == Asset::Type::Audio; +} + +bool Asset::isVideo() const +{ + return type() == Asset::Type::Video; +} + +bool Asset::isTexture3D() const +{ + return type() == Asset::Type::Texture3D; +} + +bool Asset::isEffect() const +{ + return type() == Asset::Type::Effect; +} + +const QString Asset::suffix() const +{ + return m_suffix; +} + +const QString Asset::id() const +{ + return m_filePath; +} + +bool Asset::isSupported() const +{ + return supportedSuffixes().contains(m_filePath); +} + +bool Asset::hasSuffix() const +{ + return !m_suffix.isEmpty(); +} + +} // namespace QmlDesigner diff --git a/src/plugins/qmldesigner/utils/asset.h b/src/plugins/qmldesigner/utils/asset.h new file mode 100644 index 00000000000..279edb93e1b --- /dev/null +++ b/src/plugins/qmldesigner/utils/asset.h @@ -0,0 +1,45 @@ +// Copyright (C) 2022 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0+ OR GPL-3.0 WITH Qt-GPL-exception-1.0 + +#pragma once + +namespace QmlDesigner { + +class Asset +{ +public: + enum Type { Unknown, Image, FragmentShader, Font, Audio, Video, Texture3D, Effect, Shader }; + + Asset(const QString &filePath); + + static const QStringList &supportedImageSuffixes(); + static const QStringList &supportedFragmentShaderSuffixes(); + static const QStringList &supportedShaderSuffixes(); + static const QStringList &supportedFontSuffixes(); + static const QStringList &supportedAudioSuffixes(); + static const QStringList &supportedVideoSuffixes(); + static const QStringList &supportedTexture3DSuffixes(); + static const QStringList &supportedEffectMakerSuffixes(); + static const QSet &supportedSuffixes(); + + const QString suffix() const; + const QString id() const; + bool hasSuffix() const; + + Type type() const; + bool isImage() const; + bool isFragmentShader() const; + bool isShader() const; + bool isFont() const; + bool isAudio() const; + bool isVideo() const; + bool isTexture3D() const; + bool isEffect() const; + bool isSupported() const; + +private: + QString m_filePath; + QString m_suffix; +}; + +} // namespace QmlDesigner diff --git a/src/tools/qml2puppet/qml2puppet/instances/qt5informationnodeinstanceserver.cpp b/src/tools/qml2puppet/qml2puppet/instances/qt5informationnodeinstanceserver.cpp index c2ef586c031..c806edcdb65 100644 --- a/src/tools/qml2puppet/qml2puppet/instances/qt5informationnodeinstanceserver.cpp +++ b/src/tools/qml2puppet/qml2puppet/instances/qt5informationnodeinstanceserver.cpp @@ -921,7 +921,7 @@ void Qt5InformationNodeInstanceServer::updateActiveSceneToEditView3D([[maybe_unu if (toolStates.contains("syncBackgroundColor")) { bool sync = toolStates["syncBackgroundColor"].toBool(); if (sync) { - QList colors = {helper->sceneEnvironmentColor(sceneId)}; + QList colors{helper->sceneEnvironmentColor(sceneId)}; View3DActionCommand cmd(View3DActionType::SelectBackgroundColor, QVariant::fromValue(colors)); view3DAction(cmd); @@ -2274,7 +2274,7 @@ void Qt5InformationNodeInstanceServer::setSceneEnvironmentColor(const PropertyVa if (toolStates.contains("syncBackgroundColor")) { bool sync = toolStates["syncBackgroundColor"].toBool(); - QList colors = {color}; + QList colors{color}; if (sync) { View3DActionCommand cmd(View3DActionType::SelectBackgroundColor, QVariant::fromValue(colors));