完成了两个Manager的构建逻辑

dev-VirtualTexture
白封羽 2022-12-16 21:05:21 +08:00
parent c6a6b68687
commit d5a6c3e3ee
9 changed files with 93 additions and 22 deletions

View File

@ -4,11 +4,9 @@ EditorWidget::EditorWidget(QWidget *parent) : QWidget(parent)
{ {
ui.setupUi(this); ui.setupUi(this);
previewWindow = ui.Preview; previewWindow = ui.Preview;
QJsonArray elements; QJsonObject source;
QJsonArray layers; elementManager = new ElementManager(source);
elementManager = new ElementManager(elements); layerManager = new LayerManager(source, elementManager);
layerManager = new LayerManager(layers, elementManager);
elementManager->initialize(layerManager);
previewWindow->initialize(layerManager); previewWindow->initialize(layerManager);
} }

View File

@ -1,11 +1,14 @@
#include "ElementManager.h" #include "ElementManager.h"
ElementManager::ElementManager(QJsonArray jsonElementArray) ElementManager::ElementManager(QJsonObject source)
{ {
elements.resize(jsonElementArray.size()); 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::initialize(LayerManager *layerManager)
{
} }
void ElementManager::addElement(GraphicElement *element) 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()) if (index < elements.size())
return &elements[index]; return elements[index];
else else
return nullptr; return nullptr;
} }
// TODO: undone
ElementManager::~ElementManager()
{
}

View File

@ -5,6 +5,7 @@
#include <vector> #include <vector>
using std::vector; using std::vector;
class LayerManager; class LayerManager;
class GraphicElement;
class ElementManager class ElementManager
{ {
@ -12,12 +13,12 @@ class ElementManager
vector<GraphicElement *> elements; vector<GraphicElement *> elements;
public: public:
ElementManager(QJsonArray jsonElementArray); ElementManager(QJsonObject source);
void initialize(LayerManager *layerManager); ~ElementManager();
void addElement(GraphicElement *element); void addElement(GraphicElement *element);
void removeElement(GraphicElement *pElement); void removeElement(GraphicElement *pElement);
/** /**
* only used in initialization * only used in initialization
*/ */
GraphicElement **getElementById(int index); GraphicElement *getElementById(int index);
}; };

View File

@ -72,6 +72,10 @@ GroupElement::GroupElement(FolderLayerWrapper *sourceLayer)
{ {
this->sourceLayer = sourceLayer; this->sourceLayer = sourceLayer;
} }
void GroupElement::setSourceLayer(FolderLayerWrapper *sourceLayer)
{
this->sourceLayer = sourceLayer;
}
QPainterPath GroupElement::getPaintObject() const QPainterPath GroupElement::getPaintObject() const
{ {
if (sourceLayer != nullptr) if (sourceLayer != nullptr)

View File

@ -51,7 +51,9 @@ class GroupElement : public GraphicElement
FolderLayerWrapper *sourceLayer; FolderLayerWrapper *sourceLayer;
public: public:
GroupElement() = default;
GroupElement(FolderLayerWrapper *mSourceLayer); GroupElement(FolderLayerWrapper *mSourceLayer);
~GroupElement() = default; ~GroupElement() = default;
QPainterPath getPaintObject() const override; QPainterPath getPaintObject() const override;
void setSourceLayer(FolderLayerWrapper *sourceLayer);
}; };

View File

@ -1,6 +1,11 @@
#include "LayerManager.h" #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 void LayerManager::paint(QPainter *painter) const
{ {

View File

@ -8,6 +8,7 @@
using std::pair; using std::pair;
using std::vector; using std::vector;
class ElementManager; class ElementManager;
class LayerWrapper;
class LayerManager class LayerManager
{ {
@ -18,6 +19,6 @@ class LayerManager
LayerPtrs involvedLeafLayersCache; LayerPtrs involvedLeafLayersCache;
public: public:
LayerManager(QJsonArray layers, ElementManager *elementManager); LayerManager(QJsonObject source, ElementManager *elementManager);
void paint(QPainter *painter) const; void paint(QPainter *painter) const;
}; };

View File

@ -1,14 +1,57 @@
#include "LayerWrapper.h" #include "LayerWrapper.h"
using namespace std;
void FolderLayerWrapper::addChild(LayerWrapper *child) void FolderLayerWrapper::addChild(LayerWrapper *child)
{ {
for (auto &chi : children)
if (chi.get() == child)
return;
children.push_back(shared_ptr<LayerWrapper>(child));
} }
void FolderLayerWrapper::removeChild(LayerWrapper *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 QPainterPath LayerWrapper::getCache() const
{ {
return cache; 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<GroupElement *>(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<LayerWrapper>(new FolderLayerWrapper(childJson.toObject(), elementManager, this)));
else
children.push_back(
shared_ptr<LayerWrapper>(new LeafLayerWrapper(childJson.toObject(), elementManager, this)));
}
}
#include <iostream>
LeafLayerWrapper::LeafLayerWrapper(QJsonObject json, ElementManager *elementManager, LayerWrapper *parent)
: LayerWrapper(json, parent)
{
int elementIndex = json.value("element").toInt();
wrappedElement = elementManager->getElementById(elementIndex);
}

View File

@ -1,9 +1,11 @@
#pragma once #pragma once
#include "ElementManager.h"
#include "GraphicElement.h" #include "GraphicElement.h"
#include "LayerStyle.h" #include "LayerStyle.h"
#include <QGraphicsItem> #include <QGraphicsItem>
#include <QGraphicsItemGroup> #include <QGraphicsItemGroup>
#include <QGraphicsScene> #include <QGraphicsScene>
#include <QJSonObject>
#include <QLine> #include <QLine>
#include <QPoint> #include <QPoint>
#include <memory> #include <memory>
@ -13,6 +15,7 @@ using std::vector;
class GraphicElement; class GraphicElement;
class SimpleElement; class SimpleElement;
class GroupElement; class GroupElement;
class ElementManager;
class LayerWrapper class LayerWrapper
{ {
@ -24,7 +27,8 @@ class LayerWrapper
public: public:
QPainterPath getCache() const; // invoke by manager, then invoke parent's applyStyles 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 class FolderLayerWrapper : public LayerWrapper
@ -33,6 +37,8 @@ class FolderLayerWrapper : public LayerWrapper
vector<shared_ptr<LayerWrapper>> children; vector<shared_ptr<LayerWrapper>> children;
public: public:
FolderLayerWrapper() = default;
FolderLayerWrapper(QJsonObject json, ElementManager *elementManager, LayerWrapper *parent);
void addChild(LayerWrapper *child); void addChild(LayerWrapper *child);
void removeChild(LayerWrapper *child); void removeChild(LayerWrapper *child);
}; };
@ -40,5 +46,9 @@ class FolderLayerWrapper : public LayerWrapper
class LeafLayerWrapper : public LayerWrapper class LeafLayerWrapper : public LayerWrapper
{ {
private: private:
shared_ptr<SimpleElement> wrappedElement; GraphicElement *wrappedElement;
public:
LeafLayerWrapper() = default;
LeafLayerWrapper(QJsonObject json, ElementManager *elementManager, LayerWrapper *parent);
}; };