实现文件硬合并功能

main
karlis 2023-04-28 19:31:17 +08:00
parent fb85447a2f
commit f4e09a5bc6
6 changed files with 103 additions and 0 deletions

View File

@ -110,6 +110,7 @@
</Link> </Link>
</ItemDefinitionGroup> </ItemDefinitionGroup>
<ItemGroup> <ItemGroup>
<ClCompile Include="src\Editor\util\JsonMerger.cpp" />
<ClCompile Include="src\Editor\Properties\ProjectPropertyWidget.cpp" /> <ClCompile Include="src\Editor\Properties\ProjectPropertyWidget.cpp" />
<ClCompile Include="src\Editor\Properties\ProjectPropertyDialog.cpp" /> <ClCompile Include="src\Editor\Properties\ProjectPropertyDialog.cpp" />
<ClCompile Include="src\Editor\Properties\CanvasPropertyWidget.cpp" /> <ClCompile Include="src\Editor\Properties\CanvasPropertyWidget.cpp" />
@ -221,6 +222,7 @@
<QtMoc Include="src\Editor\Properties\CanvasPropertyWidget.h" /> <QtMoc Include="src\Editor\Properties\CanvasPropertyWidget.h" />
<QtMoc Include="src\Editor\Properties\ProjectPropertyDialog.h" /> <QtMoc Include="src\Editor\Properties\ProjectPropertyDialog.h" />
<QtMoc Include="src\Editor\Properties\ProjectPropertyWidget.h" /> <QtMoc Include="src\Editor\Properties\ProjectPropertyWidget.h" />
<ClInclude Include="src\Editor\util\JsonMerger.h" />
<ClInclude Include="resource.h" /> <ClInclude Include="resource.h" />
<ClInclude Include="src\Editor\Properties\PropertyWidget.h" /> <ClInclude Include="src\Editor\Properties\PropertyWidget.h" />
<ClInclude Include="src\Editor\DataManager\ProjectDataManager.h" /> <ClInclude Include="src\Editor\DataManager\ProjectDataManager.h" />

View File

@ -285,6 +285,9 @@
<ClCompile Include="src\Renderer\Preview\PaintingRenderer.cpp"> <ClCompile Include="src\Renderer\Preview\PaintingRenderer.cpp">
<Filter>Source Files\Renderer\Preview</Filter> <Filter>Source Files\Renderer\Preview</Filter>
</ClCompile> </ClCompile>
<ClCompile Include="src\Editor\util\JsonMerger.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<QtMoc Include="src\Renderer\RendererGLWidget.h"> <QtMoc Include="src\Renderer\RendererGLWidget.h">
@ -576,6 +579,9 @@
<ClInclude Include="src\Renderer\Preview\PaintingRenderer.h"> <ClInclude Include="src\Renderer\Preview\PaintingRenderer.h">
<Filter>Header Files\Renderer\Preview</Filter> <Filter>Header Files\Renderer\Preview</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="src\Editor\util\JsonMerger.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<QtRcc Include="res\MainWindow.qrc"> <QtRcc Include="res\MainWindow.qrc">

View File

@ -70,11 +70,13 @@ void EditorWidget::initFileMenu()
auto* actionSave = new QAction(QStringLiteral("±£´æ"), fileMenuButton); auto* actionSave = new QAction(QStringLiteral("±£´æ"), fileMenuButton);
auto* actionSaveAs = new QAction(QStringLiteral("Áí´æΪ"), fileMenuButton); auto* actionSaveAs = new QAction(QStringLiteral("Áí´æΪ"), fileMenuButton);
auto* actionExport = new QAction(QStringLiteral("µ¼³ö"), fileMenuButton); auto* actionExport = new QAction(QStringLiteral("µ¼³ö"), fileMenuButton);
auto* actionMerge = new QAction(QStringLiteral("合并"), fileMenuButton);
fileMenuButton->addMenuAction(actionCreate); fileMenuButton->addMenuAction(actionCreate);
fileMenuButton->addMenuAction(actionOpen); fileMenuButton->addMenuAction(actionOpen);
fileMenuButton->addMenuAction(actionSave); fileMenuButton->addMenuAction(actionSave);
fileMenuButton->addMenuAction(actionSaveAs); fileMenuButton->addMenuAction(actionSaveAs);
fileMenuButton->addMenuAction(actionExport); fileMenuButton->addMenuAction(actionExport);
fileMenuButton->addMenuAction(actionMerge);
connect(actionCreate, &QAction::triggered, [this] { connect(actionCreate, &QAction::triggered, [this] {
static int count = 0; static int count = 0;
const int prevCount = this->tabWidget->count(); const int prevCount = this->tabWidget->count();
@ -117,6 +119,27 @@ void EditorWidget::initFileMenu()
item->exportAs(); item->exportAs();
} }
}); });
connect(actionMerge, &QAction::triggered, this, [this] {
auto files = QFileDialog::getOpenFileNames(this, QStringLiteral("打开需要合并的项目文件"), "", QStringLiteral("JSON文件(*.json)"));
auto target = QFileDialog::getSaveFileName(this, QStringLiteral("保存至"), "", QStringLiteral("JSON文件(*.json)"));
auto load = [&](QString path) {
QFile file(path);
file.open(QFile::ReadOnly);
QByteArray setting = file.readAll().trimmed();
QJsonParseError jError;
QJsonDocument jsonDoc(QJsonDocument::fromJson(setting, &jError));
return jsonDoc.object();
};
QJsonObject result = load(files[0]);
for (int i = 1; i < files.size(); i++)
{
auto obj = load(files[i]);
result = MergeUtil::merge(result, obj);
}
QFile file(target);
file.open(QFile::WriteOnly);
file.write(QJsonDocument(result).toJson());
});
} }
void EditorWidget::initProjectMenu() void EditorWidget::initProjectMenu()

View File

@ -1,6 +1,7 @@
#pragma once #pragma once
#include <qwidget.h> #include <qwidget.h>
#include "../FluentMenuButton.h" #include "../FluentMenuButton.h"
#include "util/JsonMerger.h"
#include "ui_EditorWidget.h" #include "ui_EditorWidget.h"
class EditorWidget : class EditorWidget :

View File

@ -0,0 +1,62 @@
#include "JsonMerger.h"
namespace MergeUtil {
QJsonObject merge(QJsonObject a, QJsonObject b)
{
QJsonObject result = a;
int elementsCount0 = a.find("elements").value().toArray().count();
QJsonArray mergedElements = a.find("elements").value().toArray();
QJsonArray elements1 = b.find("elements").value().toArray();
for (auto v : elements1)
mergedElements.append(v);
result.insert("elements", mergedElements);
QJsonObject mergedLayer;
QJsonObject transform;
QJsonObject offset;
QJsonObject scale;
scale.insert("x", 1);
scale.insert("y", 1);
offset.insert("x", 0);
offset.insert("y", 0);
transform.insert("offset", offset);
transform.insert("scale", scale);
transform.insert("rotation", 0);
mergedLayer.insert("transform", transform);
mergedLayer.insert("referenced-by", QJsonValue());
mergedLayer.insert("name", "root");
mergedLayer.insert("effects", QJsonArray());
mergedLayer.insert("is-folder", true);
QJsonObject root0 = a.find("root-layer").value().toObject();
QJsonObject root1 = layerFixer(b.find("root-layer").value().toObject(), elementsCount0);
QJsonArray children;
children.append(root0);
children.append(root1);
mergedLayer.insert("children", children);
result.insert("root-layer", mergedLayer);
return result;
}
QJsonObject layerFixer(QJsonObject layer, int elementOffset) {
QJsonObject result = layer;
if (layer.find("referenced-by") != layer.end() && !layer.find("referenced-by").value().isNull())
{
result.insert("referenced-by", layer.find("referenced-by").value().toInt() + elementOffset);
}
else if (layer.find("element") != layer.end())
{
result.insert("element", layer.find("element").value().toInt() + elementOffset);
}
if (layer.find("children") != layer.end())
{
auto children = layer.find("children").value().toArray();
QJsonArray newChildren;
for (auto child : children)
{
newChildren.append(layerFixer(child.toObject(), elementOffset));
}
result.insert("children", newChildren);
}
return result;
};
}

View File

@ -0,0 +1,9 @@
#pragma once
#include <QJSonObject>
#include <QJsonArray>
namespace MergeUtil {
QJsonObject merge(QJsonObject a, QJsonObject b);
QJsonObject layerFixer(QJsonObject layer, int elementOffset);
}