Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qssgassetimportmanager.cpp
Go to the documentation of this file.
1// Copyright (C) 2019 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
3// Qt-Security score:significant reason:default
4
5
7
10
11#include <QtCore/QFile>
12#include <QtCore/QDebug>
13#include <QtCore/QHash>
14#include <QtCore/QMap>
15#include <QtCore/QList>
16#include <QtCore/QString>
17
19
20QSSGAssetImportManager::QSSGAssetImportManager(QObject *parent) : QObject(parent)
21{
22 // load assetimporters
23 const QStringList keys = QSSGAssetImporterFactory::keys();
24 for (const auto &key : keys) {
25 auto importer = QSSGAssetImporterFactory::create(key, QStringList());
26 if (importer) {
27 m_assetImporters.append(importer);
28 // Add to extension map
29 for (const auto &extension : importer->inputExtensions()) {
30 m_extensionsMap.insert(extension, importer);
31 }
32 } else {
33 qWarning() << "Failed to load asset import plugin with key: " << key;
34 }
35 }
36}
37
38QSSGAssetImportManager::~QSSGAssetImportManager()
39{
40 for (auto importer : m_assetImporters) {
41 delete importer;
42 }
43}
44
45// Compatibility with old API
46QSSGAssetImportManager::ImportState QSSGAssetImportManager::importFile(const QString &filename,
47 const QDir &outputPath,
48 QString *error)
49{
50 return importFile(filename, outputPath, QJsonObject(), error);
51}
52
53QSSGAssetImportManager::ImportState QSSGAssetImportManager::importFile(const QString &filename,
54 const QDir &outputPath,
55 const QJsonObject &options,
56 QString *error)
57{
58 QFileInfo fileInfo(filename);
59
60 // Is this a real file?
61 if (!fileInfo.exists()) {
62 if (error)
63 *error = QStringLiteral("file does not exist");
64 return ImportState::IoError;
65 }
66
67 // Do we have a importer to load the file?
68 const auto extension = fileInfo.suffix().toLower();
69 auto importer = m_extensionsMap.value(extension, nullptr);
70 if (!importer) {
71 if (error)
72 *error = QStringLiteral("unsupported file extension %1").arg(extension);
73 return ImportState::Unsupported;
74 }
75
76 QStringList generatedFiles;
77 auto errorString = importer->import(fileInfo.absoluteFilePath(), outputPath, options, &generatedFiles);
78
79 if (!errorString.isEmpty()) {
80 if (error) {
81 *error = QStringLiteral("%1").arg(errorString);
82 }
83
84 return ImportState::IoError;
85 }
86
87 // debug output
88 for (const auto &file : generatedFiles)
89 qDebug() << "generated file: " << file;
90
91 return ImportState::Success;
92}
93
94QSSGAssetImportManager::ImportState QSSGAssetImportManager::importFile(const QUrl &url,
95 QSSGSceneDesc::Scene &scene,
96 QString *error)
97{
98 return importFile(url, scene, {}, error);
99}
100
101QSSGAssetImportManager::ImportState QSSGAssetImportManager::importFile(const QUrl &url,
102 QSSGSceneDesc::Scene &scene,
103 const QJsonObject &options,
104 QString *error)
105{
106 auto importState = ImportState::Unsupported;
107 auto it = m_assetImporters.cbegin();
108 const auto end = m_assetImporters.cend();
109 for (; it != end; ++it) {
110 if ((*it)->name() == QLatin1String("assimp"))
111 break;
112 }
113
114 if (it != end) {
115 const auto &importer = *it;
116 const auto ret = importer->import(url, options, scene);
117 if (!ret.isEmpty()) {
118 if (error)
119 *error = ret;
120 importState = ImportState::IoError;
121 } else {
122 importState = ImportState::Success;
123 }
124 }
125
126 return importState;
127}
128
129QJsonObject QSSGAssetImportManager::getOptionsForFile(const QString &filename)
130{
131 QFileInfo fileInfo(filename);
132
133 QJsonObject options;
134
135 // Is this a real file?
136 if (fileInfo.exists()) {
137 // Do we have a importer to load the file?
138 const auto extension = fileInfo.suffix().toLower();
139 auto importer = m_extensionsMap.value(extension, nullptr);
140 if (importer)
141 options = importer->importOptions();
142 }
143
144 return options;
145}
146
147QSSGAssetImportManager::PluginOptionMaps QSSGAssetImportManager::getAllOptions() const
148{
149 PluginOptionMaps options;
150 for (const auto importer : m_assetImporters)
151 options.insert(importer->inputExtensions().join(QChar::fromLatin1(':')), importer->importOptions());
152 return options;
153}
154
155QHash<QString, QStringList> QSSGAssetImportManager::getSupportedExtensions() const
156{
157 QHash<QString, QStringList> extensionMap;
158 for (const auto importer : std::as_const(m_assetImporters))
159 extensionMap.insert(importer->typeDescription(), importer->inputExtensions());
160 return extensionMap;
161}
162
163QList<QSSGAssetImporterPluginInfo> QSSGAssetImportManager::getImporterPluginInfos() const
164{
165 QList<QSSGAssetImporterPluginInfo> output;
166
167 for (const QSSGAssetImporter *importer : m_assetImporters) {
168 QSSGAssetImporterPluginInfo plugin;
169 plugin.name = importer->name();
170 plugin.inputExtensions = importer->inputExtensions();
171 plugin.outputExtension = importer->outputExtension();
172 plugin.type = importer->type();
173 plugin.importOptions = importer->importOptions();
174 plugin.typeDescription = importer->typeDescription();
175 output.push_back(plugin);
176 }
177
178 return output;
179}
180
181QT_END_NAMESPACE
Combined button and popup list for selecting options.