From 000281fed770b5af96d093724fb1be1347bf5eed Mon Sep 17 00:00:00 2001 From: Vikas Pachdha Date: Fri, 19 Jun 2020 19:43:18 +0200 Subject: [PATCH] AssetExport: Refactor to accommodate asset generation Change-Id: I57b33dc06819b3f0d1269eae10bd1131c5cb911d Reviewed-by: Thomas Hartmann --- .../assetexporterplugin/assetexporter.cpp | 6 ++-- .../assetexporterplugin/componentexporter.cpp | 32 +++++++++++++------ .../assetexporterplugin/componentexporter.h | 23 +++++++------ .../parsers/modelitemnodeparser.cpp | 6 ++-- .../parsers/modelitemnodeparser.h | 3 +- .../parsers/modelnodeparser.h | 7 ++-- .../parsers/textnodeparser.cpp | 5 +-- .../parsers/textnodeparser.h | 4 ++- 8 files changed, 55 insertions(+), 31 deletions(-) diff --git a/src/plugins/qmldesigner/assetexporterplugin/assetexporter.cpp b/src/plugins/qmldesigner/assetexporterplugin/assetexporter.cpp index 27c83ff5d68..e85ac11cabe 100644 --- a/src/plugins/qmldesigner/assetexporterplugin/assetexporter.cpp +++ b/src/plugins/qmldesigner/assetexporterplugin/assetexporter.cpp @@ -88,9 +88,9 @@ bool AssetExporter::isBusy() const void AssetExporter::exportComponent(const ModelNode &rootNode) { qCDebug(loggerInfo) << "Exporting component" << rootNode.id(); - ComponentExporter exporter(rootNode); - QJsonObject json = exporter.exportComponent(); - m_components.append(json); + Component exporter(*this, rootNode); + exporter.exportComponent(); + m_components.append(exporter.json()); } void AssetExporter::notifyLoadError(AssetExporterView::LoadState state) diff --git a/src/plugins/qmldesigner/assetexporterplugin/componentexporter.cpp b/src/plugins/qmldesigner/assetexporterplugin/componentexporter.cpp index a02af72f2ea..819fa3d328f 100644 --- a/src/plugins/qmldesigner/assetexporterplugin/componentexporter.cpp +++ b/src/plugins/qmldesigner/assetexporterplugin/componentexporter.cpp @@ -51,20 +51,31 @@ static void populateLineage(const QmlDesigner::ModelNode &node, QByteArrayList & namespace QmlDesigner { -std::vector> ComponentExporter::m_readers; -ComponentExporter::ComponentExporter(const ModelNode &rootNode): +std::vector> Component::m_readers; +Component::Component(AssetExporter &exporter, const ModelNode &rootNode): + m_exporter(exporter), m_rootNode(rootNode) { } -QJsonObject ComponentExporter::exportComponent() const +QJsonObject Component::json() const { - QTC_ASSERT(m_rootNode.isValid(), return {}); - return nodeToJson(m_rootNode); + return m_json; } -ModelNodeParser *ComponentExporter::createNodeParser(const ModelNode &node) const +AssetExporter &Component::exporter() +{ + return m_exporter; +} + +void Component::exportComponent() +{ + QTC_ASSERT(m_rootNode.isValid(), return); + m_json = nodeToJson(m_rootNode); +} + +ModelNodeParser *Component::createNodeParser(const ModelNode &node) const { QByteArrayList lineage; populateLineage(node, lineage); @@ -80,18 +91,19 @@ ModelNodeParser *ComponentExporter::createNodeParser(const ModelNode &node) cons } } } - if (!reader) { + + if (!reader) qCDebug(loggerInfo()) << "No parser for node" << node; - } + return reader.release(); } -QJsonObject ComponentExporter::nodeToJson(const ModelNode &node) const +QJsonObject Component::nodeToJson(const ModelNode &node) { QJsonObject jsonObject; std::unique_ptr parser(createNodeParser(node)); if (parser) - jsonObject = parser->json(); + jsonObject = parser->json(*this); QJsonArray children; for (const ModelNode &childnode : node.directSubModelNodes()) diff --git a/src/plugins/qmldesigner/assetexporterplugin/componentexporter.h b/src/plugins/qmldesigner/assetexporterplugin/componentexporter.h index 3565b190a7b..3668f372bfb 100644 --- a/src/plugins/qmldesigner/assetexporterplugin/componentexporter.h +++ b/src/plugins/qmldesigner/assetexporterplugin/componentexporter.h @@ -24,11 +24,9 @@ ****************************************************************************/ #pragma once -#include -#include +#include #include -#include #include #include "utils/qtcassert.h" @@ -38,9 +36,9 @@ class QJsonArray; QT_END_NAMESPACE namespace QmlDesigner { -class Model; +class AssetExporter; class ModelNode; -class ComponentExporter; +class Component; class ModelNodeParser; namespace Internal { @@ -50,7 +48,7 @@ public: virtual ~NodeParserCreatorBase() {} protected: virtual ModelNodeParser *instance(const QByteArrayList &, const ModelNode &) const = 0; - friend class QmlDesigner::ComponentExporter; + friend class QmlDesigner::Component; }; template @@ -67,12 +65,15 @@ protected: }; } //Internal -class ComponentExporter +class Component { public: - ComponentExporter(const ModelNode &rootNode); + Component(AssetExporter& exporter, const ModelNode &rootNode); - QJsonObject exportComponent() const; + void exportComponent(); + QJsonObject json() const; + + AssetExporter &exporter(); template static void addNodeParser() { @@ -81,10 +82,12 @@ public: } private: ModelNodeParser* createNodeParser(const ModelNode &node) const; - QJsonObject nodeToJson(const ModelNode &node) const; + QJsonObject nodeToJson(const ModelNode &node); private: + AssetExporter& m_exporter; const ModelNode &m_rootNode; + QJsonObject m_json; static std::vector> m_readers; }; } diff --git a/src/plugins/qmldesigner/assetexporterplugin/parsers/modelitemnodeparser.cpp b/src/plugins/qmldesigner/assetexporterplugin/parsers/modelitemnodeparser.cpp index 08366c4119b..355983f2215 100644 --- a/src/plugins/qmldesigner/assetexporterplugin/parsers/modelitemnodeparser.cpp +++ b/src/plugins/qmldesigner/assetexporterplugin/parsers/modelitemnodeparser.cpp @@ -30,7 +30,8 @@ namespace QmlDesigner { using namespace Constants; -ItemNodeParser::ItemNodeParser(const QByteArrayList &lineage, const ModelNode &node) : +ItemNodeParser::ItemNodeParser(const QByteArrayList &lineage, + const ModelNode &node) : ModelNodeParser(lineage, node) { @@ -41,8 +42,9 @@ bool QmlDesigner::ItemNodeParser::isExportable() const return lineage().contains("QtQuick.Item"); } -QJsonObject QmlDesigner::ItemNodeParser::json() const +QJsonObject QmlDesigner::ItemNodeParser::json(QmlDesigner::Component &component) const { + Q_UNUSED(component); const QmlObjectNode &qmlObjectNode = objectNode(); QJsonObject jsonObject; jsonObject.insert(QmlIdTag, qmlObjectNode.id()); diff --git a/src/plugins/qmldesigner/assetexporterplugin/parsers/modelitemnodeparser.h b/src/plugins/qmldesigner/assetexporterplugin/parsers/modelitemnodeparser.h index 332eada4101..503fb4c2e9b 100644 --- a/src/plugins/qmldesigner/assetexporterplugin/parsers/modelitemnodeparser.h +++ b/src/plugins/qmldesigner/assetexporterplugin/parsers/modelitemnodeparser.h @@ -28,6 +28,7 @@ namespace QmlDesigner { class ModelNode; +class Component; class ItemNodeParser : public ModelNodeParser { @@ -38,6 +39,6 @@ public: int priority() const override { return 100; } bool isExportable() const override; - QJsonObject json() const override; + QJsonObject json(Component &component) const override; }; } diff --git a/src/plugins/qmldesigner/assetexporterplugin/parsers/modelnodeparser.h b/src/plugins/qmldesigner/assetexporterplugin/parsers/modelnodeparser.h index 6b0807d5371..4ca17746e8d 100644 --- a/src/plugins/qmldesigner/assetexporterplugin/parsers/modelnodeparser.h +++ b/src/plugins/qmldesigner/assetexporterplugin/parsers/modelnodeparser.h @@ -30,6 +30,7 @@ #include namespace QmlDesigner { +class Component; class ModelNode; class ModelNodeParser @@ -41,14 +42,16 @@ public: virtual int priority() const = 0; virtual bool isExportable() const = 0; - virtual QJsonObject json() const = 0; + virtual QJsonObject json(Component& component) const = 0; const QByteArrayList& lineage() const { return m_lineage; } const QmlObjectNode& objectNode() const { return m_objectNode; } QVariant propertyValue(const PropertyName &name) const; -private: +protected: const ModelNode &m_node; + +private: QmlObjectNode m_objectNode; QByteArrayList m_lineage; }; diff --git a/src/plugins/qmldesigner/assetexporterplugin/parsers/textnodeparser.cpp b/src/plugins/qmldesigner/assetexporterplugin/parsers/textnodeparser.cpp index 1f5a9e70280..12b73c4506a 100644 --- a/src/plugins/qmldesigner/assetexporterplugin/parsers/textnodeparser.cpp +++ b/src/plugins/qmldesigner/assetexporterplugin/parsers/textnodeparser.cpp @@ -61,9 +61,10 @@ bool TextNodeParser::isExportable() const return lineage().contains("QtQuick.Text"); } -QJsonObject TextNodeParser::json() const +QJsonObject TextNodeParser::json(Component &component) const { - QJsonObject jsonObject = ItemNodeParser::json(); + Q_UNUSED(component); + QJsonObject jsonObject = ItemNodeParser::json(component); QJsonObject textDetails; textDetails.insert(TextContentTag, propertyValue("text").toString()); diff --git a/src/plugins/qmldesigner/assetexporterplugin/parsers/textnodeparser.h b/src/plugins/qmldesigner/assetexporterplugin/parsers/textnodeparser.h index 29c5fbf3c56..c05d5c8f883 100644 --- a/src/plugins/qmldesigner/assetexporterplugin/parsers/textnodeparser.h +++ b/src/plugins/qmldesigner/assetexporterplugin/parsers/textnodeparser.h @@ -27,6 +27,8 @@ #include "modelitemnodeparser.h" namespace QmlDesigner { +class Component; + class TextNodeParser : public ItemNodeParser { public: @@ -35,7 +37,7 @@ public: bool isExportable() const override; int priority() const override { return 200; } - QJsonObject json() const override; + QJsonObject json(Component &component) const override; }; }