QmlDesigner: Add unimportFinished signal to BundleImporter

Change-Id: I6d7ab5716f1bf6fe48f454d0c95c29ebd5d181de
Reviewed-by: Mahmoud Badri <mahmoud.badri@qt.io>
Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org>
This commit is contained in:
Miikka Heikkinen
2022-10-13 16:25:23 +03:00
parent 519b6fefe1
commit 7a967d385e
2 changed files with 39 additions and 15 deletions

View File

@@ -97,9 +97,10 @@ QString BundleImporter::importComponent(const QString &qmlFile,
FilePath qmlSourceFile = bundleImportPath.resolvePath(FilePath::fromString(qmlFile)); FilePath qmlSourceFile = bundleImportPath.resolvePath(FilePath::fromString(qmlFile));
const bool qmlFileExists = qmlSourceFile.exists(); const bool qmlFileExists = qmlSourceFile.exists();
const QString qmlType = qmlSourceFile.baseName(); const QString qmlType = qmlSourceFile.baseName();
m_pendingTypes.append(QStringLiteral("%1.%2.%3") const QString fullTypeName = QStringLiteral("%1.%2.%3")
.arg(QLatin1String(Constants::COMPONENT_BUNDLES_FOLDER).mid(1), .arg(QLatin1String(Constants::COMPONENT_BUNDLES_FOLDER).mid(1), m_bundleId, qmlType);
m_bundleId, qmlType)); if (m_pendingTypes.contains(fullTypeName) && !m_pendingTypes[fullTypeName])
return QStringLiteral("Unable to import while unimporting the same type: '%1'").arg(fullTypeName);
if (!qmldirContent.contains(qmlFile)) { if (!qmldirContent.contains(qmlFile)) {
qmldirContent.append(qmlType); qmldirContent.append(qmlType);
qmldirContent.append(" 1.0 "); qmldirContent.append(" 1.0 ");
@@ -162,6 +163,7 @@ QString BundleImporter::importComponent(const QString &qmlFile,
m_importAddPending = true; m_importAddPending = true;
} }
} }
m_pendingTypes.insert(fullTypeName, true);
m_importTimerCount = 0; m_importTimerCount = 0;
m_importTimer.start(); m_importTimer.start();
@@ -175,8 +177,16 @@ void BundleImporter::handleImportTimer()
m_fullReset = false; m_fullReset = false;
m_importAddPending = false; m_importAddPending = false;
m_importTimerCount = 0; m_importTimerCount = 0;
m_pendingTypes.clear();
emit importFinished({}); // Emit dummy finished signals for all pending types
const QStringList pendingTypes = m_pendingTypes.keys();
for (const QString &pendingType : pendingTypes) {
m_pendingTypes.remove(pendingType);
if (m_pendingTypes[pendingType])
emit importFinished({});
else
emit unimportFinished({});
}
}; };
auto doc = QmlDesignerPlugin::instance()->currentDesignDocument(); auto doc = QmlDesignerPlugin::instance()->currentDesignDocument();
@@ -210,12 +220,17 @@ void BundleImporter::handleImportTimer()
} }
// Detect when the code model has the new material(s) fully available // Detect when the code model has the new material(s) fully available
const QStringList pendingTypes = m_pendingTypes; const QStringList pendingTypes = m_pendingTypes.keys();
for (const QString &pendingType : pendingTypes) { for (const QString &pendingType : pendingTypes) {
NodeMetaInfo metaInfo = model->metaInfo(pendingType.toUtf8()); NodeMetaInfo metaInfo = model->metaInfo(pendingType.toUtf8());
if (metaInfo.isValid() && !metaInfo.superClasses().isEmpty()) { const bool isImport = m_pendingTypes[pendingType];
m_pendingTypes.removeAll(pendingType); const bool typeComplete = metaInfo.isValid() && !metaInfo.superClasses().isEmpty();
emit importFinished(metaInfo); if (isImport == typeComplete) {
m_pendingTypes.remove(pendingType);
if (isImport)
emit importFinished(metaInfo);
else
emit unimportFinished(metaInfo);
} }
} }
@@ -257,14 +272,14 @@ QString BundleImporter::unimportComponent(const QString &qmlFile)
{ {
FilePath bundleImportPath = resolveBundleImportPath(); FilePath bundleImportPath = resolveBundleImportPath();
if (bundleImportPath.isEmpty()) if (bundleImportPath.isEmpty())
return "Failed to resolve bundle import folder"; return QStringLiteral("Failed to resolve bundle import folder for: '%1'").arg(qmlFile);
if (!bundleImportPath.exists()) if (!bundleImportPath.exists())
return {}; return QStringLiteral("Unable to find bundle path: '%1'").arg(bundleImportPath.toString());
FilePath qmlFilePath = bundleImportPath.resolvePath(qmlFile); FilePath qmlFilePath = bundleImportPath.resolvePath(qmlFile);
if (!qmlFilePath.exists()) if (!qmlFilePath.exists())
return {}; return QStringLiteral("Unable to find specified file: '%1'").arg(qmlFilePath.toString());
QStringList removedFiles; QStringList removedFiles;
removedFiles.append(qmlFile); removedFiles.append(qmlFile);
@@ -272,9 +287,15 @@ QString BundleImporter::unimportComponent(const QString &qmlFile)
FilePath qmldirPath = bundleImportPath.resolvePath(QStringLiteral("qmldir")); FilePath qmldirPath = bundleImportPath.resolvePath(QStringLiteral("qmldir"));
QByteArray qmldirContent = qmldirPath.fileContents(); QByteArray qmldirContent = qmldirPath.fileContents();
QByteArray newContent; QByteArray newContent;
QString qmlType = qmlFilePath.baseName();
const QString fullTypeName = QStringLiteral("%1.%2.%3")
.arg(QLatin1String(Constants::COMPONENT_BUNDLES_FOLDER).mid(1), m_bundleId, qmlType);
if (m_pendingTypes.contains(fullTypeName) && m_pendingTypes[fullTypeName])
return QStringLiteral("Unable to unimport while importing the same type: '%1'").arg(fullTypeName);
if (!qmldirContent.isEmpty()) { if (!qmldirContent.isEmpty()) {
QByteArray qmlType = qmlFilePath.baseName().toUtf8(); int typeIndex = qmldirContent.indexOf(qmlType.toUtf8());
int typeIndex = qmldirContent.indexOf(qmlType);
if (typeIndex != -1) { if (typeIndex != -1) {
int newLineIndex = qmldirContent.indexOf('\n', typeIndex); int newLineIndex = qmldirContent.indexOf('\n', typeIndex);
newContent = qmldirContent.left(typeIndex); newContent = qmldirContent.left(typeIndex);
@@ -287,6 +308,8 @@ QString BundleImporter::unimportComponent(const QString &qmlFile)
} }
} }
m_pendingTypes.insert(fullTypeName, false);
QVariantHash assetRefMap = loadAssetRefMap(bundleImportPath); QVariantHash assetRefMap = loadAssetRefMap(bundleImportPath);
bool writeAssetRefs = false; bool writeAssetRefs = false;
const auto keys = assetRefMap.keys(); const auto keys = assetRefMap.keys();

View File

@@ -57,6 +57,7 @@ signals:
// asynchronous part of the import. In this case all remaining pending imports have been // asynchronous part of the import. In this case all remaining pending imports have been
// terminated, and will not receive separate importFinished notifications. // terminated, and will not receive separate importFinished notifications.
void importFinished(const QmlDesigner::NodeMetaInfo &metaInfo); void importFinished(const QmlDesigner::NodeMetaInfo &metaInfo);
void unimportFinished(const QmlDesigner::NodeMetaInfo &metaInfo);
private: private:
void handleImportTimer(); void handleImportTimer();
@@ -72,7 +73,7 @@ private:
int m_importTimerCount = 0; int m_importTimerCount = 0;
bool m_importAddPending = false; bool m_importAddPending = false;
bool m_fullReset = false; bool m_fullReset = false;
QStringList m_pendingTypes; QHash<QString, bool> m_pendingTypes; // <type, isImport>
}; };
} // namespace QmlDesigner::Internal } // namespace QmlDesigner::Internal