From d5a6c3e3eebdfed14044fb0d9c2ec89ff4d8e02e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E5=B0=81=E7=BE=BD?= <2360164671@qq.com> Date: Fri, 16 Dec 2022 21:05:21 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E6=88=90=E4=BA=86=E4=B8=A4=E4=B8=AAMa?= =?UTF-8?q?nager=E7=9A=84=E6=9E=84=E5=BB=BA=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/Editor/EditorWidget.cpp | 8 ++-- .../src/Editor/ElementManager.cpp | 25 +++++++---- .../src/Editor/ElementManager.h | 7 +-- .../src/Editor/GraphicElement.cpp | 4 ++ .../src/Editor/GraphicElement.h | 2 + .../src/Editor/LayerManager.cpp | 7 ++- .../src/Editor/LayerManager.h | 3 +- .../src/Editor/LayerWrapper.cpp | 45 ++++++++++++++++++- .../src/Editor/LayerWrapper.h | 14 +++++- 9 files changed, 93 insertions(+), 22 deletions(-) diff --git a/ArchitectureColoredPainting/src/Editor/EditorWidget.cpp b/ArchitectureColoredPainting/src/Editor/EditorWidget.cpp index cc00019..60be82e 100644 --- a/ArchitectureColoredPainting/src/Editor/EditorWidget.cpp +++ b/ArchitectureColoredPainting/src/Editor/EditorWidget.cpp @@ -4,11 +4,9 @@ EditorWidget::EditorWidget(QWidget *parent) : QWidget(parent) { ui.setupUi(this); previewWindow = ui.Preview; - QJsonArray elements; - QJsonArray layers; - elementManager = new ElementManager(elements); - layerManager = new LayerManager(layers, elementManager); - elementManager->initialize(layerManager); + QJsonObject source; + elementManager = new ElementManager(source); + layerManager = new LayerManager(source, elementManager); previewWindow->initialize(layerManager); } diff --git a/ArchitectureColoredPainting/src/Editor/ElementManager.cpp b/ArchitectureColoredPainting/src/Editor/ElementManager.cpp index 954a87d..aeaaa1a 100644 --- a/ArchitectureColoredPainting/src/Editor/ElementManager.cpp +++ b/ArchitectureColoredPainting/src/Editor/ElementManager.cpp @@ -1,11 +1,14 @@ #include "ElementManager.h" -ElementManager::ElementManager(QJsonArray jsonElementArray) -{ - elements.resize(jsonElementArray.size()); -} - -void ElementManager::initialize(LayerManager *layerManager) +ElementManager::ElementManager(QJsonObject source) { + auto elementsJson = source.value("elements").toArray(); + for (auto elementJson : elementsJson) + { + if (elementJson.toObject().value("type") == "group") + elements.push_back(new GroupElement()); + else + elements.push_back(new SimpleElement(elementJson.toObject())); + } } void ElementManager::addElement(GraphicElement *element) @@ -16,10 +19,14 @@ void ElementManager::removeElement(GraphicElement *pElement) { } -GraphicElement **ElementManager::getElementById(int index) +GraphicElement *ElementManager::getElementById(int index) { - if (index <= elements.size()) - return &elements[index]; + if (index < elements.size()) + return elements[index]; else return nullptr; } +// TODO: undone +ElementManager::~ElementManager() +{ +} diff --git a/ArchitectureColoredPainting/src/Editor/ElementManager.h b/ArchitectureColoredPainting/src/Editor/ElementManager.h index bd07491..952cb1d 100644 --- a/ArchitectureColoredPainting/src/Editor/ElementManager.h +++ b/ArchitectureColoredPainting/src/Editor/ElementManager.h @@ -5,6 +5,7 @@ #include using std::vector; class LayerManager; +class GraphicElement; class ElementManager { @@ -12,12 +13,12 @@ class ElementManager vector elements; public: - ElementManager(QJsonArray jsonElementArray); - void initialize(LayerManager *layerManager); + ElementManager(QJsonObject source); + ~ElementManager(); void addElement(GraphicElement *element); void removeElement(GraphicElement *pElement); /** * only used in initialization */ - GraphicElement **getElementById(int index); + GraphicElement *getElementById(int index); }; diff --git a/ArchitectureColoredPainting/src/Editor/GraphicElement.cpp b/ArchitectureColoredPainting/src/Editor/GraphicElement.cpp index cc3b7ca..047d53e 100644 --- a/ArchitectureColoredPainting/src/Editor/GraphicElement.cpp +++ b/ArchitectureColoredPainting/src/Editor/GraphicElement.cpp @@ -72,6 +72,10 @@ GroupElement::GroupElement(FolderLayerWrapper *sourceLayer) { this->sourceLayer = sourceLayer; } +void GroupElement::setSourceLayer(FolderLayerWrapper *sourceLayer) +{ + this->sourceLayer = sourceLayer; +} QPainterPath GroupElement::getPaintObject() const { if (sourceLayer != nullptr) diff --git a/ArchitectureColoredPainting/src/Editor/GraphicElement.h b/ArchitectureColoredPainting/src/Editor/GraphicElement.h index ea32b3a..2013c1a 100644 --- a/ArchitectureColoredPainting/src/Editor/GraphicElement.h +++ b/ArchitectureColoredPainting/src/Editor/GraphicElement.h @@ -51,7 +51,9 @@ class GroupElement : public GraphicElement FolderLayerWrapper *sourceLayer; public: + GroupElement() = default; GroupElement(FolderLayerWrapper *mSourceLayer); ~GroupElement() = default; QPainterPath getPaintObject() const override; + void setSourceLayer(FolderLayerWrapper *sourceLayer); }; diff --git a/ArchitectureColoredPainting/src/Editor/LayerManager.cpp b/ArchitectureColoredPainting/src/Editor/LayerManager.cpp index 35ca603..71c66d4 100644 --- a/ArchitectureColoredPainting/src/Editor/LayerManager.cpp +++ b/ArchitectureColoredPainting/src/Editor/LayerManager.cpp @@ -1,6 +1,11 @@ #include "LayerManager.h" -LayerManager::LayerManager(QJsonArray layers, ElementManager *elementManager) +LayerManager::LayerManager(QJsonObject source, ElementManager *elementManager) { + QJsonObject rootJson = source.value("root-layer").toObject(); + if (rootJson.value("is-folder").toBool()) + root = new FolderLayerWrapper(rootJson, elementManager, nullptr); + else + root = new LeafLayerWrapper(rootJson, elementManager, nullptr); } void LayerManager::paint(QPainter *painter) const { diff --git a/ArchitectureColoredPainting/src/Editor/LayerManager.h b/ArchitectureColoredPainting/src/Editor/LayerManager.h index a1fc218..4dc7909 100644 --- a/ArchitectureColoredPainting/src/Editor/LayerManager.h +++ b/ArchitectureColoredPainting/src/Editor/LayerManager.h @@ -8,6 +8,7 @@ using std::pair; using std::vector; class ElementManager; +class LayerWrapper; class LayerManager { @@ -18,6 +19,6 @@ class LayerManager LayerPtrs involvedLeafLayersCache; public: - LayerManager(QJsonArray layers, ElementManager *elementManager); + LayerManager(QJsonObject source, ElementManager *elementManager); void paint(QPainter *painter) const; }; diff --git a/ArchitectureColoredPainting/src/Editor/LayerWrapper.cpp b/ArchitectureColoredPainting/src/Editor/LayerWrapper.cpp index 2c0851b..631f8cd 100644 --- a/ArchitectureColoredPainting/src/Editor/LayerWrapper.cpp +++ b/ArchitectureColoredPainting/src/Editor/LayerWrapper.cpp @@ -1,14 +1,57 @@ #include "LayerWrapper.h" - +using namespace std; void FolderLayerWrapper::addChild(LayerWrapper *child) { + for (auto &chi : children) + if (chi.get() == child) + return; + children.push_back(shared_ptr(child)); } void FolderLayerWrapper::removeChild(LayerWrapper *child) { + for (auto chi = children.begin(); chi != children.end(); chi++) + if ((*chi).get() == child) + { + children.erase(chi); + return; + } } QPainterPath LayerWrapper::getCache() const { return cache; } +// TODO: undone +LayerWrapper::LayerWrapper(QJsonObject json, LayerWrapper *parent) +{ +} + +FolderLayerWrapper::FolderLayerWrapper(QJsonObject json, ElementManager *elementManager, LayerWrapper *parent) + : LayerWrapper(json, parent) +{ + QJsonArray childrenJson = json.value("children").toArray(); + QJsonValue referencedJson = json.value("referenced-by"); + if (!referencedJson.isNull()) + { + auto p = reinterpret_cast(elementManager->getElementById(referencedJson.toInt())); + if (p != nullptr) + p->setSourceLayer(this); + } + for (auto childJson : childrenJson) + { + if (childJson.toObject().value("is-folder").toBool()) + children.push_back( + shared_ptr(new FolderLayerWrapper(childJson.toObject(), elementManager, this))); + else + children.push_back( + shared_ptr(new LeafLayerWrapper(childJson.toObject(), elementManager, this))); + } +} +#include +LeafLayerWrapper::LeafLayerWrapper(QJsonObject json, ElementManager *elementManager, LayerWrapper *parent) + : LayerWrapper(json, parent) +{ + int elementIndex = json.value("element").toInt(); + wrappedElement = elementManager->getElementById(elementIndex); +} diff --git a/ArchitectureColoredPainting/src/Editor/LayerWrapper.h b/ArchitectureColoredPainting/src/Editor/LayerWrapper.h index e1dd524..1ad556b 100644 --- a/ArchitectureColoredPainting/src/Editor/LayerWrapper.h +++ b/ArchitectureColoredPainting/src/Editor/LayerWrapper.h @@ -1,9 +1,11 @@ #pragma once +#include "ElementManager.h" #include "GraphicElement.h" #include "LayerStyle.h" #include #include #include +#include #include #include #include @@ -13,6 +15,7 @@ using std::vector; class GraphicElement; class SimpleElement; class GroupElement; +class ElementManager; class LayerWrapper { @@ -24,7 +27,8 @@ class LayerWrapper public: QPainterPath getCache() const; // invoke by manager, then invoke parent's applyStyles - // todo: provide atomic operations for Events + LayerWrapper(QJsonObject json, LayerWrapper *parent); + LayerWrapper() = default; }; class FolderLayerWrapper : public LayerWrapper @@ -33,6 +37,8 @@ class FolderLayerWrapper : public LayerWrapper vector> children; public: + FolderLayerWrapper() = default; + FolderLayerWrapper(QJsonObject json, ElementManager *elementManager, LayerWrapper *parent); void addChild(LayerWrapper *child); void removeChild(LayerWrapper *child); }; @@ -40,5 +46,9 @@ class FolderLayerWrapper : public LayerWrapper class LeafLayerWrapper : public LayerWrapper { private: - shared_ptr wrappedElement; + GraphicElement *wrappedElement; + + public: + LeafLayerWrapper() = default; + LeafLayerWrapper(QJsonObject json, ElementManager *elementManager, LayerWrapper *parent); };