AssetExport: Add generated UUID to qml

This will enable the merge when importing back from Photoshop

Task-number: QDS-1555
Change-Id: I411ad65af1a33a80fcea80206aef93e2d1afa357
Reviewed-by: Thomas Hartmann <thomas.hartmann@qt.io>
This commit is contained in:
Vikas Pachdha
2020-06-25 17:17:05 +02:00
parent 9daf5c130d
commit 3fde0a19ec
5 changed files with 38 additions and 7 deletions

View File

@@ -25,7 +25,9 @@
#include "assetexporter.h"
#include "componentexporter.h"
#include "exportnotification.h"
#include "assetexportpluginconstants.h"
#include "rewriterview.h"
#include "qmlitemnode.h"
#include "qmlobjectnode.h"
#include "utils/qtcassert.h"
@@ -142,12 +144,7 @@ bool AssetExporter::isBusy() const
Utils::FilePath AssetExporter::exportAsset(const QmlObjectNode &node)
{
// TODO: Use this hash as UUID and add to the node.
QByteArray hash;
do {
hash = generateHash(node.id());
} while (m_usedHashes.contains(hash));
m_usedHashes.insert(hash);
QByteArray hash = addNodeUUID(node.modelNode());
Utils::FilePath assetPath = m_exportPath.pathAppended(QString("assets/%1.png")
.arg(QString::fromLatin1(hash)));
m_assetDumper->dumpAsset(node.toQmlItemNode().instanceRenderPixmap(), assetPath);
@@ -189,9 +186,30 @@ void AssetExporter::onQmlFileLoaded()
QTC_ASSERT(m_view && m_view->model(), qCDebug(loggerError) << "Null model"; return);
qCDebug(loggerInfo) << "Qml file load done" << m_view->model()->fileUrl();
exportComponent(m_view->rootModelNode());
QString error;
if (!m_view->saveQmlFile(&error)) {
ExportNotification::addError(tr("Error saving QML file. %1")
.arg(error.isEmpty()? tr("Unknown") : error));
}
triggerLoadNextFile();
}
QByteArray AssetExporter::addNodeUUID(ModelNode node)
{
QByteArray uuid = node.auxiliaryData(Constants::UuidTag).toByteArray();
qDebug() << node.id() << "UUID" << uuid;
if (uuid.isEmpty()) {
// Assign a new hash.
do {
uuid = generateHash(node.id());
} while (m_usedHashes.contains(uuid));
m_usedHashes.insert(uuid);
node.setAuxiliaryData(Constants::UuidAuxTag, QString::fromLatin1(uuid));
node.model()->rewriterView()->writeAuxiliaryData();
}
return uuid;
}
void AssetExporter::triggerLoadNextFile()
{
QTimer::singleShot(0, this, &AssetExporter::loadNextFile);

View File

@@ -87,6 +87,8 @@ private:
void onQmlFileLoaded();
QByteArray addNodeUUID(ModelNode node);
private:
mutable class State {
public:

View File

@@ -62,7 +62,7 @@ bool AssetExporterView::loadQmlFile(const Utils::FilePath &path, uint timeoutSec
setState(LoadState::Busy);
m_retryCount = std::max(MinRetry, static_cast<int>((timeoutSecs * 1000) / RetryIntervalMs));
Core::EditorManager::openEditor(path.toString(), Core::Id(),
m_currentEditor = Core::EditorManager::openEditor(path.toString(), Core::Id(),
Core::EditorManager::DoNotMakeVisible);
Core::ModeManager::activateMode(Core::Constants::MODE_DESIGN);
Core::ModeManager::setFocusToCurrentMode();
@@ -70,6 +70,15 @@ bool AssetExporterView::loadQmlFile(const Utils::FilePath &path, uint timeoutSec
return true;
}
bool AssetExporterView::saveQmlFile(QString *error) const
{
if (!m_currentEditor) {
qCDebug(loggerWarn) << "Saving QML file failed. No editor.";
return false;
}
return m_currentEditor->document()->save(error);
}
void AssetExporterView::modelAttached(Model *model)
{
if (model->rewriterView() && model->rewriterView()->inErrorState())

View File

@@ -54,6 +54,7 @@ public:
AssetExporterView(QObject *parent = nullptr);
bool loadQmlFile(const Utils::FilePath &path, uint timeoutSecs = 10);
bool saveQmlFile(QString *error) const;
void modelAttached(Model *model) override;
void instanceInformationsChanged(const QMultiHash<ModelNode, InformationName> &informationChangeHash) override;

View File

@@ -30,6 +30,7 @@ namespace Constants {
const char EXPORT_QML[] = "Designer.ExportPlugin.ExportQml";
const char TASK_CATEGORY_ASSET_EXPORT[] = "AssetExporter.Export";
const char UuidAuxTag[] = "uuid";
//***************************************************************************
// Metadata tags