实现保存流程

TaoZhang-Branch
karlis 2023-03-07 17:56:00 +08:00
parent 8ca036fafb
commit 33475f6f57
5 changed files with 152 additions and 16 deletions

View File

@ -120,12 +120,12 @@
<property name="currentIndex"> <property name="currentIndex">
<number>1</number> <number>1</number>
</property> </property>
<widget class="EditorWidgetItem" name="tab"> <widget class="QWidget" name="tab">
<attribute name="title"> <attribute name="title">
<string>Tab 1</string> <string>Tab 1</string>
</attribute> </attribute>
</widget> </widget>
<widget class="EditorWidgetItem" name="tab_2"> <widget class="QWidget" name="tab_2">
<attribute name="title"> <attribute name="title">
<string>Tab 2</string> <string>Tab 2</string>
</attribute> </attribute>
@ -136,14 +136,6 @@
</item> </item>
</layout> </layout>
</widget> </widget>
<customwidgets>
<customwidget>
<class>EditorWidgetItem</class>
<extends>QWidget</extends>
<header>EditorWidgetItem.h</header>
<container>1</container>
</customwidget>
</customwidgets>
<resources/> <resources/>
<connections/> <connections/>
</ui> </ui>

View File

@ -18,16 +18,24 @@ EditorWidget::EditorWidget(QWidget* parent) : QWidget(parent)
} }
connect(this->createButton, &QPushButton::clicked, this, [this]() { connect(this->createButton, &QPushButton::clicked, this, [this]() {
static int count = 0; static int count = 0;
this->tabWidget->addTab(new EditorWidgetItem(this),"untitled"+QString::number(count++)); this->tabWidget->addTab(new EditorWidgetItem("../data.json",this), "untitled" + QString::number(count++));
}); });
connect(this->closeButton, &QPushButton::clicked, this, [this]() { connect(this->closeButton, &QPushButton::clicked, this, [this]() {
this->tabWidget->removeTab(this->tabWidget->currentIndex()); this->tabWidget->removeTab(this->tabWidget->currentIndex());
}); });
connect(this->saveButton, &QPushButton::clicked, this, [this]() { connect(this->saveButton, &QPushButton::clicked, this, [this]() {
EditorWidgetItem* item = dynamic_cast<EditorWidgetItem*>(this->tabWidget->currentWidget());
if (item != nullptr)
{
item->saveAs("../data.back.json");
}
}); });
connect(this->saveAsButton, &QPushButton::clicked, this, [this]() { connect(this->saveAsButton, &QPushButton::clicked, this, [this]() {
EditorWidgetItem* item = dynamic_cast<EditorWidgetItem*>(this->tabWidget->currentWidget());
if (item != nullptr)
{
item->saveAs("../data.back.json");
}
}); });
} }

View File

@ -1,6 +1,6 @@
#include "EditorWidgetItem.h" #include "EditorWidgetItem.h"
EditorWidgetItem::EditorWidgetItem(QWidget *parent) : QWidget(parent) EditorWidgetItem::EditorWidgetItem(QString filePath,QWidget *parent) : QWidget(parent)
{ {
QImage x; QImage x;
displayLayer = nullptr; displayLayer = nullptr;
@ -9,6 +9,7 @@ EditorWidgetItem::EditorWidgetItem(QWidget *parent) : QWidget(parent)
previewWindow = ui.Preview; previewWindow = ui.Preview;
treeWidget = ui.LayerTree; treeWidget = ui.LayerTree;
tabWidget = ui.DisplayTab; tabWidget = ui.DisplayTab;
this->filePath = filePath;
layerInfoDisplayWidget = dynamic_cast<InfoDisplayWidget *>(tabWidget->widget(0)); layerInfoDisplayWidget = dynamic_cast<InfoDisplayWidget *>(tabWidget->widget(0));
elementInfoDisplayWidget = dynamic_cast<InfoDisplayWidget *>(tabWidget->widget(1)); elementInfoDisplayWidget = dynamic_cast<InfoDisplayWidget *>(tabWidget->widget(1));
connect(treeWidget, &LayerTreeWidget::displayLayerChange, this, &EditorWidgetItem::onLayerChange); connect(treeWidget, &LayerTreeWidget::displayLayerChange, this, &EditorWidgetItem::onLayerChange);
@ -20,7 +21,8 @@ EditorWidgetItem::EditorWidgetItem(QWidget *parent) : QWidget(parent)
// &EditorWidget::triggerRefreshPreview); // &EditorWidget::triggerRefreshPreview);
// test // test
QFile settingFile; QFile settingFile;
settingFile.setFileName("../data.json"); //settingFile.setFileName("../data.json");
settingFile.setFileName(filePath);
settingFile.open(QFile::ReadOnly); settingFile.open(QFile::ReadOnly);
QByteArray setting = settingFile.readAll().trimmed(); QByteArray setting = settingFile.readAll().trimmed();
QJsonParseError jError; QJsonParseError jError;
@ -61,3 +63,29 @@ void EditorWidgetItem::triggerRefreshPreview()
{ {
previewWindow->update(); previewWindow->update();
} }
void EditorWidgetItem::save() const
{
saveImpl(this->filePath);
}
void EditorWidgetItem::saveAs(QString filePath) const
{
saveImpl(filePath);
}
void EditorWidgetItem::saveImpl(QString filePath) const
{
QJsonObject source1 = layerManager->toJson();
QJsonObject source2 = elementManager->toJson();
QJsonObject json;
json.insert("width", 1080);
json.insert("height", 1080);
json.insert("root-layer", source1.value("root-layer"));
json.insert("elements", source2.value("elements"));
QJsonDocument doc(json);
QFile file(filePath);
file.open(QIODevice::WriteOnly);
file.write(doc.toJson());
file.close();
}

View File

@ -27,11 +27,15 @@ class EditorWidgetItem : public QWidget
// QT DATA PART // QT DATA PART
LayerWrapper *displayLayer; LayerWrapper *displayLayer;
GraphicElement *displayElement; GraphicElement *displayElement;
QString filePath;
void saveImpl(QString filePath)const;
public: public:
EditorWidgetItem(QWidget *parent = nullptr); EditorWidgetItem(QString filePath, QWidget *parent = nullptr);
~EditorWidgetItem(); ~EditorWidgetItem();
void paintEvent(QPaintEvent *event) override; void paintEvent(QPaintEvent *event) override;
void save() const;
void saveAs(QString filePath)const;
private slots: private slots:
void onLayerChange(LayerWrapper *layer); void onLayerChange(LayerWrapper *layer);

104
data.back.json Normal file
View File

@ -0,0 +1,104 @@
{
"elements": [
{
"name": "ababa",
"type": "svg-file",
"data": {
"include": "./svg/2.svg"
}
},
{
"name": "ababa-group",
"type": "group",
"data": {
"reference-layer": "0.0"
}
}
],
"height": 1080,
"root-layer": {
"children": [
{
"children": [
{
"element": 0,
"is-folder": false,
"name": "Leaf1",
"transform": {
"offset": {
"x": 0,
"y": 0
},
"rotation": 0,
"scale": {
"x": 1,
"y": 1
}
}
},
{
"element": 0,
"is-folder": false,
"name": "Leaf2",
"transform": {
"offset": {
"x": 150,
"y": 0
},
"rotation": 0,
"scale": {
"x": 1.5,
"y": 1.5
}
}
}
],
"is-folder": true,
"name": "GroupFolderExample",
"referenced-by": 1,
"transform": {
"offset": {
"x": 50,
"y": 50
},
"rotation": 0,
"scale": {
"x": 1,
"y": 1
}
}
},
{
"element": 1,
"is-folder": false,
"name": "ReferencingGroupLayer",
"transform": {
"offset": {
"x": 100,
"y": 0
},
"rotation": 45,
"scale": {
"x": 1,
"y": 1
}
}
}
],
"is-folder": true,
"name": "root",
"referenced-by": null,
"transform": {
"offset": {
"x": 0,
"y": 0
},
"rotation": 0,
"scale": {
"x": 1,
"y": 1
}
}
},
"width": 1080
}