QmlDesigner: Add necessary QML imports when importing 3d asset

Necessary missing import statements are generated when a 3D asset is
imported to a project.

Task-number: QDS-3683
Change-Id: I93da8ddaf5691d8ef66e167ea8b0fefd7e1c6c91
Reviewed-by: Mahmoud Badri <mahmoud.badri@qt.io>
Reviewed-by: Thomas Hartmann <thomas.hartmann@qt.io>
This commit is contained in:
Miikka Heikkinen
2021-02-04 14:18:29 +02:00
parent ee85d7f9ca
commit dcc105bb21
2 changed files with 43 additions and 0 deletions

View File

@@ -29,6 +29,8 @@
#include "rewriterview.h"
#include "model.h"
#include "puppetcreator.h"
#include "rewritertransaction.h"
#include "rewritingexception.h"
#include <QDir>
#include <QDirIterator>
@@ -194,6 +196,7 @@ void ItemLibraryAssetImporter::reset()
m_qmlPuppetCount = 0;
m_qmlImportFinishedCount = 0;
m_parseData.clear();
m_requiredImports.clear();
}
void ItemLibraryAssetImporter::parseFiles(const QStringList &filePaths,
@@ -313,6 +316,9 @@ void ItemLibraryAssetImporter::postParseQuick3DAsset(const ParseData &pd)
qmlInfo.append(".");
qmlInfo.append(pd.assetName);
qmlInfo.append('\n');
m_requiredImports.append(Import::createLibraryImport(
QStringLiteral("%1.%2").arg(pd.targetDir.dirName(),
pd.assetName), version));
while (qmlIt.hasNext()) {
qmlIt.next();
QFileInfo fi = QFileInfo(qmlIt.filePath());
@@ -344,6 +350,24 @@ void ItemLibraryAssetImporter::postParseQuick3DAsset(const ParseData &pd)
out << "canBeDroppedInFormEditor: false" << Qt::endl;
out << "canBeDroppedInView3D: true" << Qt::endl;
file.close();
// Add quick3D import unless it is already added
if (m_requiredImports.first().url() != "QtQuick3D") {
QByteArray import3dStr{"import QtQuick3D"};
int importIdx = content.indexOf(import3dStr);
if (importIdx != -1 && importIdx < braceIdx) {
importIdx += import3dStr.size();
int nlIdx = content.indexOf('\n', importIdx);
QByteArray versionStr = content.mid(importIdx, nlIdx - importIdx).trimmed();
// There could be 'as abc' after version, so just take first part
QList<QByteArray> parts = versionStr.split(' ');
QString impVersion;
if (parts.size() >= 1)
impVersion = QString::fromUtf8(parts[0]);
m_requiredImports.prepend(Import::createLibraryImport(
"QtQuick3D", impVersion));
}
}
}
if (startIconProcess(24, iconFileName, qmlIt.filePath())) {
// Since icon is generated by external process, the file won't be
@@ -563,6 +587,24 @@ void ItemLibraryAssetImporter::finalizeQuick3DImport()
if (counter == 10) {
model->rewriterView()->textModifier()->replace(0, 0, {});
} else if (counter == 19) {
try {
const QList<Import> currentImports = model->imports();
QList<Import> newImportsToAdd;
for (auto &imp : qAsConst(m_requiredImports)) {
if (!currentImports.contains(imp))
newImportsToAdd.append(imp);
}
if (!newImportsToAdd.isEmpty()) {
RewriterTransaction transaction
= model->rewriterView()->beginRewriterTransaction(
QByteArrayLiteral("ItemLibraryAssetImporter::finalizeQuick3DImport"));
model->changeImports(newImportsToAdd, {});
transaction.commit();
}
} catch (const RewritingException &e) {
addError(tr("Failed to update imports: %1").arg(e.description()));
}
doc->updateSubcomponentManager();
} else if (counter >= 20) {
if (!m_overwrittenImports.isEmpty())

View File

@@ -117,5 +117,6 @@ private:
int m_importIdCounter = 1000000; // Use ids in range unlikely to clash with any normal process exit codes
QHash<int, ParseData> m_parseData;
QString m_progressTitle;
QList<Import> m_requiredImports;
};
} // QmlDesigner