添加了序列化的接口和基本实现

TaoZhang-Branch
karlis 2023-03-06 19:43:01 +08:00
parent 6188d6a413
commit c1ad6a61f0
10 changed files with 79 additions and 0 deletions

View File

@ -31,6 +31,7 @@ EditorWidget::EditorWidget(QWidget *parent) : QWidget(parent)
QJsonObject source = jsonDoc.object(); QJsonObject source = jsonDoc.object();
elementManager = new ElementManager(source,previewWindow->getRenderer()); elementManager = new ElementManager(source,previewWindow->getRenderer());
layerManager = new LayerManager(source, elementManager); layerManager = new LayerManager(source, elementManager);
qDebug() << layerManager->toJson();
previewWindow->initialize(layerManager,QSize(jsonDoc.object().value("width").toDouble(),jsonDoc.object().value("height").toDouble())); previewWindow->initialize(layerManager,QSize(jsonDoc.object().value("width").toDouble(),jsonDoc.object().value("height").toDouble()));
if (layerManager->getRoot() != nullptr) if (layerManager->getRoot() != nullptr)
{ {

View File

@ -3,6 +3,7 @@ ElementManager::ElementManager(QJsonObject source,Renderer::ElementRenderer* ren
{ {
auto elementsJson = source.value("elements").toArray(); auto elementsJson = source.value("elements").toArray();
qDebug() << elementsJson.size(); qDebug() << elementsJson.size();
int index = 0;
for (auto elementJson : elementsJson) for (auto elementJson : elementsJson)
{ {
if (elementJson.toObject().value("type").toString() == "group") if (elementJson.toObject().value("type").toString() == "group")
@ -12,6 +13,8 @@ ElementManager::ElementManager(QJsonObject source,Renderer::ElementRenderer* ren
(*elements.rbegin())->renderer = renderer; (*elements.rbegin())->renderer = renderer;
} }
for (auto element : elements)
element->index = index++;
} }
void ElementManager::addElement(GraphicElement *element) void ElementManager::addElement(GraphicElement *element)
@ -33,3 +36,13 @@ GraphicElement *ElementManager::getElementById(int index)
ElementManager::~ElementManager() ElementManager::~ElementManager()
{ {
} }
QJsonObject ElementManager::toJson() const
{
QJsonArray elementsJson;
for (auto element : elements)
elementsJson.push_back(element->toJson());
QJsonObject result;
result.insert("elements", elementsJson);
return result;
}

View File

@ -19,6 +19,7 @@ class ElementManager
~ElementManager(); ~ElementManager();
void addElement(GraphicElement *element); void addElement(GraphicElement *element);
void removeElement(GraphicElement *pElement); void removeElement(GraphicElement *pElement);
QJsonObject toJson()const;
/** /**
* only used in initialization * only used in initialization
*/ */

View File

@ -76,3 +76,10 @@ PixelPath GroupElement::getPaintObject(std::vector<Renderer::ElementStyleStrokeD
// rawPath.addPath(path); // rawPath.addPath(path);
// } // }
//} //}
//TODO : Ìí¼Óϸ½Ú
QJsonObject GraphicElement::toJson() const
{
QJsonObject result;
result.insert("name", name);
return result;
}

View File

@ -22,7 +22,9 @@ class GraphicElement
public: public:
Renderer::ElementRenderer *renderer; Renderer::ElementRenderer *renderer;
QString name = ""; QString name = "";
int index;
// TODO: ¸ÄΪBitmapPath // TODO: ¸ÄΪBitmapPath
virtual QJsonObject toJson() const;
virtual PixelPath getPaintObject() const = 0; virtual PixelPath getPaintObject() const = 0;
virtual PixelPath getPaintObject(std::vector<Renderer::ElementStyleStrokeDemo>) const = 0; virtual PixelPath getPaintObject(std::vector<Renderer::ElementStyleStrokeDemo>) const = 0;
}; };

View File

@ -62,11 +62,20 @@ bool LayerManager::changeParent(FolderLayerWrapper *newParent) const
selectedLayers[0]->setParent(newParent); selectedLayers[0]->setParent(newParent);
return true; return true;
} }
void LayerManager::addLayer(LayerWrapper *layer) void LayerManager::addLayer(LayerWrapper *layer)
{ {
layerSet.insert(layer); layerSet.insert(layer);
} }
void LayerManager::removeLayer(LayerWrapper *layer) void LayerManager::removeLayer(LayerWrapper *layer)
{ {
layerSet.erase(layer); layerSet.erase(layer);
} }
QJsonObject LayerManager::toJson() const
{
QJsonObject result;
result.insert("root-layer", root->toJson());
return result;
}

View File

@ -31,6 +31,7 @@ class LayerManager
LayerWrapper *getRoot() const; LayerWrapper *getRoot() const;
LayerManager() = default; LayerManager() = default;
LayerManager(QJsonObject source, ElementManager* elementManager); LayerManager(QJsonObject source, ElementManager* elementManager);
QJsonObject toJson() const;
void paint(QPainter *painter, QSize size) const; void paint(QPainter *painter, QSize size) const;
bool rename(QString newName) const; bool rename(QString newName) const;
bool combine() const; bool combine() const;

View File

@ -53,6 +53,7 @@ FolderLayerWrapper::FolderLayerWrapper(QJsonObject json, ElementManager *element
auto p = reinterpret_cast<GroupElement *>(elementManager->getElementById(referencedJson.toInt())); auto p = reinterpret_cast<GroupElement *>(elementManager->getElementById(referencedJson.toInt()));
if (p != nullptr) if (p != nullptr)
p->setSourceLayer(this); p->setSourceLayer(this);
this->referencedBy = referencedJson.toInt();
} }
for (auto childJson : childrenJson) for (auto childJson : childrenJson)
{ {
@ -181,3 +182,40 @@ QTreeWidgetItem* FolderLayerWrapper::getQTreeItem()
} }
return LayerWrapper::getQTreeItem(); return LayerWrapper::getQTreeItem();
} }
//TODO: add effects
QJsonObject LayerWrapper::toJson() const
{
QJsonObject json;
json.insert("name", property.name);
QJsonObject transformJson;
transformJson.insert("offset", QJsonObject({ {"x", property.offset.x()}, {"y", property.offset.y()} }));
transformJson.insert("scale", QJsonObject({ {"x", property.scale.x()}, {"y", property.scale.y()} }));
transformJson.insert("rotation", property.rotation);
json.insert("transform", transformJson);
return json;
}
QJsonObject FolderLayerWrapper::toJson() const
{
QJsonObject json = LayerWrapper::toJson();
QJsonArray childrenJson;
for (auto& child : children)
childrenJson.push_back(child->toJson());
json.insert("children", childrenJson);
json.insert("is-folder", true);
if(this->referencedBy != -1)
json.insert("referenced-by", this->referencedBy);
else
json.insert("referenced-by", QJsonValue());
return json;
}
QJsonObject LeafLayerWrapper::toJson() const
{
QJsonObject json = LayerWrapper::toJson();
json.insert("element", wrappedElement->index);
json.insert("is-folder", false);
return json;
}

View File

@ -60,13 +60,16 @@ class LayerWrapper
// virtual void deleteAll() const = 0; // virtual void deleteAll() const = 0;
virtual void del(); virtual void del();
virtual void delSelf(); virtual void delSelf();
virtual QJsonObject toJson() const;
~LayerWrapper() = default; ~LayerWrapper() = default;
}; };
class FolderLayerWrapper : public LayerWrapper class FolderLayerWrapper : public LayerWrapper
{ {
public: public:
vector<shared_ptr<LayerWrapper>> children; vector<shared_ptr<LayerWrapper>> children;
int referencedBy = -1;
public: public:
@ -80,6 +83,7 @@ class FolderLayerWrapper : public LayerWrapper
void del() override; void del() override;
void delSelf() override; void delSelf() override;
QTreeWidgetItem* getQTreeItem() override; QTreeWidgetItem* getQTreeItem() override;
QJsonObject toJson() const override;
}; };
class LeafLayerWrapper : public LayerWrapper class LeafLayerWrapper : public LayerWrapper
@ -93,6 +97,7 @@ class LeafLayerWrapper : public LayerWrapper
void refresh() override; void refresh() override;
LeafLayerWrapper() = default; LeafLayerWrapper() = default;
LeafLayerWrapper(QJsonObject json, ElementManager *elementManager, FolderLayerWrapper*parent); LeafLayerWrapper(QJsonObject json, ElementManager *elementManager, FolderLayerWrapper*parent);
QJsonObject toJson() const override;
}; };
Q_DECLARE_METATYPE(LayerWrapper *) Q_DECLARE_METATYPE(LayerWrapper *)

View File

@ -34,6 +34,7 @@
"is-folder": true, "is-folder": true,
"referenced-by": null, "referenced-by": null,
"children": [ "children": [
{ {
"name": "GroupFolderExample", "name": "GroupFolderExample",
"transform": { "transform": {
@ -87,6 +88,7 @@
} }
] ]
}, },
{ {
"name": "ReferencingGroupLayer", "name": "ReferencingGroupLayer",
"transform": { "transform": {