完成了两个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);
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);
}

View File

@ -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()
{
}

View File

@ -5,6 +5,7 @@
#include <vector>
using std::vector;
class LayerManager;
class GraphicElement;
class ElementManager
{
@ -12,12 +13,12 @@ class ElementManager
vector<GraphicElement *> 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);
};

View File

@ -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)

View File

@ -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);
};

View File

@ -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
{

View File

@ -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;
};

View File

@ -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<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
{
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
#include "ElementManager.h"
#include "GraphicElement.h"
#include "LayerStyle.h"
#include <QGraphicsItem>
#include <QGraphicsItemGroup>
#include <QGraphicsScene>
#include <QJSonObject>
#include <QLine>
#include <QPoint>
#include <memory>
@ -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<shared_ptr<LayerWrapper>> 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<SimpleElement> wrappedElement;
GraphicElement *wrappedElement;
public:
LeafLayerWrapper() = default;
LeafLayerWrapper(QJsonObject json, ElementManager *elementManager, LayerWrapper *parent);
};