From 769effe49cd819435d7506f04ba46e00521e4929 Mon Sep 17 00:00:00 2001 From: karlis <2995621482@qq.com> Date: Mon, 20 Mar 2023 22:26:38 +0800 Subject: [PATCH] =?UTF-8?q?FIX:=20=E4=BF=AE=E6=AD=A3=E6=96=87=E4=BB=B6?= =?UTF-8?q?=E8=B7=AF=E5=BE=84=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/Editor/EditorWidgetItem.cpp | 21 ++++++++++++++++++- .../src/Editor/ElementManager.cpp | 16 +++++++++----- .../src/Editor/ElementManager.h | 3 ++- .../src/Editor/GraphicElement.cpp | 8 ++++--- .../src/Editor/GraphicElement.h | 2 +- .../src/Editor/LayerWrapper.cpp | 7 ++++--- .../src/Editor/RightBar/LayerTreeWidget.cpp | 1 + .../src/Editor/util/PaintingUtil.cpp | 3 ++- data.json | 4 ++-- 9 files changed, 48 insertions(+), 17 deletions(-) diff --git a/ArchitectureColoredPainting/src/Editor/EditorWidgetItem.cpp b/ArchitectureColoredPainting/src/Editor/EditorWidgetItem.cpp index 2f5b1e6..529d648 100644 --- a/ArchitectureColoredPainting/src/Editor/EditorWidgetItem.cpp +++ b/ArchitectureColoredPainting/src/Editor/EditorWidgetItem.cpp @@ -3,6 +3,8 @@ #include "DataManager/ProjectDataManager.h" #include #include +#include +#include EditorWidgetItem::EditorWidgetItem(QString filePath,QWidget *parent) : QWidget(parent) { @@ -66,7 +68,7 @@ EditorWidgetItem::EditorWidgetItem(QString filePath,QWidget *parent) : QWidget(p ProjectDataManager::Instance()->addProjectData(data); // end test QJsonObject source = jsonDoc.object(); - elementManager = new ElementManager(source,Renderer::ElementRenderer::instance()); + elementManager = new ElementManager(source, QFileInfo(filePath).absolutePath()); layerManager = new LayerManager(source, elementManager); elementInfoDisplayWidget->setElementManager(elementManager); treeWidget->elementManager = elementManager; @@ -125,6 +127,23 @@ void EditorWidgetItem::save() const void EditorWidgetItem::saveAs(QString filePath) const { saveImpl(filePath); + QString srcHome = QFileInfo(this->filePath).absolutePath(); + if (!QDir(QFileInfo(filePath).absolutePath() + "/svg").exists()) + { + QDir().mkdir(QFileInfo(filePath).absolutePath() + "/svg"); + } + for (auto& ele : elementManager->elements) + { + auto e = dynamic_cast(ele); + if (e != nullptr) + { + QString fileName = e->jsonSource["data"].toObject()["include"].toString(); + QString src = srcHome + fileName; + QString dst = QFileInfo(filePath).absolutePath() + fileName; + QFile::copy(src, dst); + } + } + } void EditorWidgetItem::saveImpl(QString filePath) const diff --git a/ArchitectureColoredPainting/src/Editor/ElementManager.cpp b/ArchitectureColoredPainting/src/Editor/ElementManager.cpp index b711794..2f601aa 100644 --- a/ArchitectureColoredPainting/src/Editor/ElementManager.cpp +++ b/ArchitectureColoredPainting/src/Editor/ElementManager.cpp @@ -1,16 +1,20 @@ #include "ElementManager.h" -ElementManager::ElementManager(QJsonObject source,Renderer::ElementRenderer* renderer) +#include +#include +#include + +ElementManager::ElementManager(QJsonObject source, QString fileHome) { auto elementsJson = source.value("elements").toArray(); + this->fileHome = fileHome; int index = 0; for (auto elementJson : elementsJson) { if (elementJson.toObject().value("type").toString() == "group") elements.push_back(new GroupElement()); else - elements.push_back(new SimpleElement(elementJson.toObject())); + elements.push_back(new SimpleElement(elementJson.toObject(), fileHome)); (*elements.rbegin())->name = elementJson.toObject().value("name").toString(); - (*elements.rbegin())->renderer = renderer; } for (auto element : elements) element->index = index++; @@ -90,9 +94,11 @@ void ElementManager::createGroupElement(QString name, FolderLayerWrapper* source void ElementManager::createSimpleElement(QString name, QString filePath) { QJsonObject json; QJsonObject data; - data.insert("include", filePath); + QFile fileSrc(filePath); + QFile::copy(filePath, QDir::cleanPath(fileHome + "/svg/" + QFileInfo(filePath).fileName())); + data.insert("include", "/svg/"+QFileInfo(filePath).fileName()); json.insert("data", data); - auto element = new SimpleElement(json); + auto element = new SimpleElement(json, fileHome); element->name = name; addElement(element); } \ No newline at end of file diff --git a/ArchitectureColoredPainting/src/Editor/ElementManager.h b/ArchitectureColoredPainting/src/Editor/ElementManager.h index 6c48feb..7cb3839 100644 --- a/ArchitectureColoredPainting/src/Editor/ElementManager.h +++ b/ArchitectureColoredPainting/src/Editor/ElementManager.h @@ -14,9 +14,10 @@ class ElementManager { public: vector elements; + QString fileHome; public: - ElementManager(QJsonObject source,Renderer::ElementRenderer *renderer); + ElementManager(QJsonObject source, QString fileHome); ~ElementManager(); void addElement(GraphicElement *element); void removeElement(GraphicElement *pElement); diff --git a/ArchitectureColoredPainting/src/Editor/GraphicElement.cpp b/ArchitectureColoredPainting/src/Editor/GraphicElement.cpp index 01f6df5..16a7bc8 100644 --- a/ArchitectureColoredPainting/src/Editor/GraphicElement.cpp +++ b/ArchitectureColoredPainting/src/Editor/GraphicElement.cpp @@ -2,6 +2,8 @@ #include "util/SvgFileLoader.h" #include #include +#include + using namespace std; PixelPath SimpleElement::getPaintObject() const @@ -23,13 +25,13 @@ void SimpleElement::loadSvgFile(const QString& filePath) qDebug() << "load svg file success " << painterPath.elementCount() << (isClosed() ? "is" : "not") << "closed"; } -SimpleElement::SimpleElement(QJsonObject jsonSource) : jsonSource(jsonSource) +SimpleElement::SimpleElement(QJsonObject jsonSource, QString fileHome) : jsonSource(jsonSource) { painterPath.clear(); filePath = jsonSource["data"].toObject()["include"].toString(); //loadSvgFile("D:\\Projects\\BigC\\svg\\3.svg"); - QFileInfo info(filePath); - loadSvgFile(filePath); + QFileInfo info(QDir::cleanPath(fileHome + "/" + filePath)); + loadSvgFile(info.absoluteFilePath()); } GroupElement::GroupElement(FolderLayerWrapper* sourceLayer) diff --git a/ArchitectureColoredPainting/src/Editor/GraphicElement.h b/ArchitectureColoredPainting/src/Editor/GraphicElement.h index 8ce9bf2..7415dcc 100644 --- a/ArchitectureColoredPainting/src/Editor/GraphicElement.h +++ b/ArchitectureColoredPainting/src/Editor/GraphicElement.h @@ -46,7 +46,7 @@ public: public: QJsonObject toJson() const override; - SimpleElement(QJsonObject jsonSource); + SimpleElement(QJsonObject jsonSource, QString fileHome); SimpleElement(QString filePath); ~SimpleElement() = default; PixelPath getPaintObject() const override; diff --git a/ArchitectureColoredPainting/src/Editor/LayerWrapper.cpp b/ArchitectureColoredPainting/src/Editor/LayerWrapper.cpp index d2f1453..caf6dc8 100644 --- a/ArchitectureColoredPainting/src/Editor/LayerWrapper.cpp +++ b/ArchitectureColoredPainting/src/Editor/LayerWrapper.cpp @@ -75,7 +75,7 @@ FolderLayerWrapper::FolderLayerWrapper(QJsonObject json, ElementManager *element } LeafLayerWrapper::LeafLayerWrapper(QJsonObject json, ElementManager* elementManager, FolderLayerWrapper* parent) - : LayerWrapper(json, parent), + : LayerWrapper(json, parent, elementManager), wrappedElement(elementManager->getElementById(json.value("element").toInt())), styles(LayerStyleContainer::fromJson(wrappedElement->isClosed(), json.value("styles").toArray())) { @@ -255,7 +255,8 @@ QJsonObject FolderLayerWrapper::toJson() const QJsonObject json = LayerWrapper::toJson(); QJsonArray childrenJson; for (auto& child : children) - childrenJson.push_back(child->toJson()); + if(child != nullptr) + childrenJson.push_back(child->toJson()); json.insert("children", childrenJson); json.insert("is-folder", true); if(this->getReferencedBy() != -1) @@ -268,7 +269,7 @@ QJsonObject FolderLayerWrapper::toJson() const QJsonObject LeafLayerWrapper::toJson() const { QJsonObject json = LayerWrapper::toJson(); - json.insert("element", wrappedElement->index); + json.insert("element", elementManager->getElementIndex(wrappedElement)); json.insert("is-folder", false); json.insert("styles", styles.toJson()); return json; diff --git a/ArchitectureColoredPainting/src/Editor/RightBar/LayerTreeWidget.cpp b/ArchitectureColoredPainting/src/Editor/RightBar/LayerTreeWidget.cpp index 0c5857b..448c6f6 100644 --- a/ArchitectureColoredPainting/src/Editor/RightBar/LayerTreeWidget.cpp +++ b/ArchitectureColoredPainting/src/Editor/RightBar/LayerTreeWidget.cpp @@ -59,6 +59,7 @@ void LayerTreeWidget::popMenu(const QPoint &pos) connect(dialog, &LayerCreateWidget::LayerInfoReturned, this, [this, layer](QJsonObject jsonObj) { auto folderLayer = dynamic_cast(layer); LayerWrapper* newLayer; + qDebug() << this->elementManager; if(jsonObj.value("is-folder").toBool()) newLayer = new FolderLayerWrapper(jsonObj, this->elementManager, folderLayer); else diff --git a/ArchitectureColoredPainting/src/Editor/util/PaintingUtil.cpp b/ArchitectureColoredPainting/src/Editor/util/PaintingUtil.cpp index aa23ce6..f3db226 100644 --- a/ArchitectureColoredPainting/src/Editor/util/PaintingUtil.cpp +++ b/ArchitectureColoredPainting/src/Editor/util/PaintingUtil.cpp @@ -3,6 +3,7 @@ #include #include "PainterPathUtil.h" #include +#include using Renderer::Painting; using Renderer::BaseElement; @@ -37,7 +38,7 @@ Painting PaintingUtil::transfromToPainting(QString jsonFilePath) { glm::bvec2 flip(0, 0); QJsonObject jsonObj = readJsonFile(jsonFilePath); qDebug() << jsonObj; - shared_ptr elementManager = make_shared(jsonObj, Renderer::ElementRenderer::instance()); + shared_ptr elementManager = make_shared(jsonObj, QFileInfo(jsonFilePath).absolutePath()); shared_ptr layerManager = make_shared(jsonObj, elementManager.get()); //qDebug() << elementManager->toJson(); //qDebug() << layerManager->toJson(); diff --git a/data.json b/data.json index 93398a4..141180f 100644 --- a/data.json +++ b/data.json @@ -8,7 +8,7 @@ "name": "ababa", "type": "svg-file", "data": { - "include": "../svg/2.svg" + "include": "/svg/2.svg" } }, { @@ -22,7 +22,7 @@ "name": "ababa2", "type": "svg-file", "data": { - "include": "../svg/0.svg" + "include": "/svg/0.svg" } } ],