Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
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
5
8
9#include <QtCore/QFile>
10#include <QtCore/QDebug>
11#include <QtCore/QHash>
12#include <QtCore/QMap>
13#include <QtCore/QList>
14#include <QtCore/QString>
15
17
19{
20 // load assetimporters
22 for (const auto &key : keys) {
24 if (importer) {
25 m_assetImporters.append(importer);
26 // Add to extension map
27 for (const auto &extension : importer->inputExtensions()) {
28 m_extensionsMap.insert(extension, importer);
29 }
30 } else {
31 qWarning() << "Failed to load asset import plugin with key: " << key;
32 }
33 }
34}
35
37{
38 for (auto importer : m_assetImporters) {
39 delete importer;
40 }
41}
42
43// Compatibility with old API
45 const QDir &outputPath,
47{
48 return importFile(filename, outputPath, QJsonObject(), error);
49}
50
52 const QDir &outputPath,
53 const QJsonObject &options,
55{
56 QFileInfo fileInfo(filename);
57
58 // Is this a real file?
59 if (!fileInfo.exists()) {
60 if (error)
61 *error = QStringLiteral("file does not exist");
63 }
64
65 // Do we have a importer to load the file?
66 const auto extension = fileInfo.suffix().toLower();
67 auto importer = m_extensionsMap.value(extension, nullptr);
68 if (!importer) {
69 if (error)
70 *error = QStringLiteral("unsupported file extension %1").arg(extension);
72 }
73
74 QStringList generatedFiles;
75 auto errorString = importer->import(fileInfo.absoluteFilePath(), outputPath, options, &generatedFiles);
76
77 if (!errorString.isEmpty()) {
78 if (error) {
79 *error = QStringLiteral("%1").arg(errorString);
80 }
81
83 }
84
85 // debug output
86 for (const auto &file : generatedFiles)
87 qDebug() << "generated file: " << file;
88
90}
91
98
101 const QJsonObject &options,
102 QString *error)
103{
104 auto importState = ImportState::Unsupported;
105 auto it = m_assetImporters.cbegin();
106 const auto end = m_assetImporters.cend();
107 for (; it != end; ++it) {
108 if ((*it)->name() == QLatin1String("assimp"))
109 break;
110 }
111
112 if (it != end) {
113 const auto &importer = *it;
114 const auto ret = importer->import(url, options, scene);
115 if (!ret.isEmpty()) {
116 if (error)
117 *error = ret;
118 importState = ImportState::IoError;
119 } else {
120 importState = ImportState::Success;
121 }
122 }
123
124 return importState;
125}
126
128{
129 QFileInfo fileInfo(filename);
130
131 QJsonObject options;
132
133 // Is this a real file?
134 if (fileInfo.exists()) {
135 // Do we have a importer to load the file?
136 const auto extension = fileInfo.suffix().toLower();
137 auto importer = m_extensionsMap.value(extension, nullptr);
138 if (importer)
139 options = importer->importOptions();
140 }
141
142 return options;
143}
144
146{
147 PluginOptionMaps options;
148 for (const auto importer : m_assetImporters)
149 options.insert(importer->inputExtensions().join(QChar::fromLatin1(':')), importer->importOptions());
150 return options;
151}
152
153QHash<QString, QStringList> QSSGAssetImportManager::getSupportedExtensions() const
154{
155 QHash<QString, QStringList> extensionMap;
156 for (const auto importer : std::as_const(m_assetImporters))
157 extensionMap.insert(importer->typeDescription(), importer->inputExtensions());
158 return extensionMap;
159}
160
161QList<QSSGAssetImporterPluginInfo> QSSGAssetImportManager::getImporterPluginInfos() const
162{
163 QList<QSSGAssetImporterPluginInfo> output;
164
165 for (const QSSGAssetImporter *importer : m_assetImporters) {
167 plugin.name = importer->name();
168 plugin.inputExtensions = importer->inputExtensions();
169 plugin.outputExtension = importer->outputExtension();
170 plugin.type = importer->type();
171 plugin.importOptions = importer->importOptions();
172 plugin.typeDescription = importer->typeDescription();
173 output.push_back(plugin);
174 }
175
176 return output;
177}
178
\inmodule QtCore
Definition qdir.h:20
QString suffix() const
Returns the suffix (extension) of the file.
QString absoluteFilePath() const
bool exists() const
Returns true if the file system entry this QFileInfo refers to exists; otherwise returns false.
iterator insert(const Key &key, const T &value)
Inserts a new item with the key and a value of value.
Definition qhash.h:1303
\inmodule QtCore\reentrant
Definition qjsonobject.h:20
iterator insert(const Key &key, const T &value)
Definition qmap.h:688
T value(const Key &key, const T &defaultValue=T()) const
Definition qmap.h:357
\inmodule QtCore
Definition qobject.h:103
QSSGAssetImportManager(QObject *parent=nullptr)
QList< QSSGAssetImporterPluginInfo > getImporterPluginInfos() const
QJsonObject getOptionsForFile(const QString &filename)
PluginOptionMaps getAllOptions() const
QHash< QString, QJsonObject > PluginOptionMaps
QHash< QString, QStringList > getSupportedExtensions() const
ImportState importFile(const QString &filename, const QDir &outputPath, QString *error=nullptr)
static QStringList keys()
static QSSGAssetImporter * create(const QString &name, const QStringList &args)
\inmodule QtCore
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:129
QString toLower() const &
Definition qstring.h:435
\inmodule QtCore
Definition qurl.h:94
void extension()
[6]
Definition dialogs.cpp:230
QSet< QString >::iterator it
Combined button and popup list for selecting options.
QList< QString > QStringList
Constructs a string list that contains the given string, str.
DBusConnection const char DBusError * error
#define qDebug
[1]
Definition qlogging.h:164
#define qWarning
Definition qlogging.h:166
return ret
GLuint64 key
GLuint GLuint end
#define QStringLiteral(str)
QT_BEGIN_NAMESPACE typedef uchar * output
QFile file
[0]
QStringList keys
QUrl url("example.com")
[constructor-url-reference]
QGraphicsScene scene
[0]