DYTSrouce/src/workspace/WorkSpaceXMLWrite.cpp

156 lines
5.0 KiB
C++
Raw Normal View History

2025-01-04 04:12:51 +00:00
#include "workspace/WorkSpaceXMLWrite.h"
#include "workspace/WorkSpace.h"
#include "entities/EntitiesManager.h"
#include "entities/Entity.h"
#include "workspace/Timestep.h"
#include "workspace/LampStatus.h"
#include "common/SpdLogger.h"
2025-01-05 18:18:16 +00:00
#include "utils/StringUtils.h"
2025-01-04 04:12:51 +00:00
2025-01-05 18:18:16 +00:00
#include "workspace/WorkSpaceManager.h"
2025-10-12 14:14:16 +00:00
#include "workspace/FileEntry.h"
2025-01-04 04:12:51 +00:00
2025-10-14 16:41:12 +00:00
#include <QFileInfo>
2025-01-04 04:12:51 +00:00
WorkSpaceXMLWrite::WorkSpaceXMLWrite(WorkSpace* workspace, QObject* parent) noexcept
: QObject(parent)
, workSpace_(workspace) {}
bool WorkSpaceXMLWrite::Save(const QString& path) {
LOG_INFO("save path: {}", path.toLocal8Bit().constData());
2025-01-04 04:12:51 +00:00
if (nullptr == workSpace_) {
LOG_WARN("worksapce is nullptr");
return false;
}
tinyxml2::XMLDocument doc;
doc.LinkEndChild(doc.NewDeclaration("xml version=\"1.0\" encoding=\"UTF-8\""));
tinyxml2::XMLElement* scene = doc.NewElement("scene");
if (!SaveScene(scene)) {
return false;
}
doc.LinkEndChild(scene);
SaveChart(scene, &doc);
SaveTimeStep(scene);
2025-07-06 16:12:06 +00:00
SaveLamp(scene);
2025-10-14 16:41:12 +00:00
SaveCommond(scene);
2025-10-12 14:14:16 +00:00
SaveFiles(scene, &doc);
2025-01-04 04:12:51 +00:00
tinyxml2::XMLElement* entitiesXml = scene->InsertNewChildElement("entities");
std::vector<Entity*>& entities = workSpace_->GetEntities();
for (auto entity : entities) {
entity->UnSerialize(entitiesXml);
}
tinyxml2::XMLError xmlError = doc.SaveFile(path.toLocal8Bit().constData(), false);
2025-01-04 04:12:51 +00:00
if (tinyxml2::XML_SUCCESS != xmlError) {
LOG_WARN("worksapce is nullptr");
return false;
}
return true;
}
bool WorkSpaceXMLWrite::SaveScene(tinyxml2::XMLElement* scene) {
const QString name = workSpace_->GetName();
scene->SetAttribute("name", name.toStdString().c_str());
scene->SetAttribute("describe", workSpace_->GetDescribe().toStdString().c_str());
scene->SetAttribute("uuid", workSpace_->GetUUid().toStdString().c_str());
2025-01-05 18:18:16 +00:00
scene->SetAttribute("viewpoint", StringUtils::ViewpointToString(workSpace_->GetHomeViewpoint()).c_str());
2025-10-14 16:41:12 +00:00
scene->SetAttribute("commondPath", "");
2025-01-04 04:12:51 +00:00
return true;
}
bool WorkSpaceXMLWrite::SaveTimeStep(tinyxml2::XMLElement* scene) {
Timestep* timestep = workSpace_->GetTimestep();
if (nullptr == timestep) {
return false;
}
tinyxml2::XMLElement* timestepXml = scene->InsertNewChildElement("timestep");
timestepXml->SetAttribute("path", timestep->GetPath().toStdString().c_str());
return true;
}
bool WorkSpaceXMLWrite::SaveLamp(tinyxml2::XMLElement* scene) {
2025-10-14 16:41:12 +00:00
tinyxml2::XMLElement* lamp = scene->InsertNewChildElement("lamp");
const QString lampPath = workSpace_->GetLampStatus()->GetPath();
if (!lampPath.isEmpty()) {
lamp->SetAttribute("path", lampPath.toStdString().c_str());
}
return true;
}
bool WorkSpaceXMLWrite::SaveCommond(tinyxml2::XMLElement* scene) {
const QString commondPath = workSpace_->GetCommondFilePath();
if (!commondPath.isEmpty()) {
tinyxml2::XMLElement* commond = scene->InsertNewChildElement("commond");
// Extract just the filename from the full path for storage
QFileInfo fileInfo(commondPath);
commond->SetAttribute("path", fileInfo.fileName().toStdString().c_str());
2025-01-04 04:12:51 +00:00
}
return true;
}
bool WorkSpaceXMLWrite::SaveEntities(tinyxml2::XMLElement* scene, tinyxml2::XMLDocument* doc) {
tinyxml2::XMLElement* entitics = doc->NewElement("entities");
scene->LinkEndChild(entitics);
return true;
}
bool WorkSpaceXMLWrite::SaveChart(tinyxml2::XMLElement* scene, tinyxml2::XMLDocument* doc)
{
tinyxml2::XMLElement* charts= doc->NewElement("charts");
scene->LinkEndChild(charts);
{
tinyxml2::XMLElement* chart = doc->NewElement("Wave");
charts->LinkEndChild(chart);
2025-01-07 15:45:23 +00:00
chart->SetAttribute("file", workSpace_->GetWavePath().toLocal8Bit().constData());
2025-01-04 04:12:51 +00:00
}
{
tinyxml2::XMLElement* chart = doc->NewElement("Report");
charts->LinkEndChild(chart);
2025-01-07 15:45:23 +00:00
chart->SetAttribute("Report", workSpace_->GetReportPath().toLocal8Bit().constData());
2025-01-04 04:12:51 +00:00
}
{
tinyxml2::XMLElement* chart = doc->NewElement("RD");
charts->LinkEndChild(chart);
2025-01-07 15:45:23 +00:00
chart->SetAttribute("RD", workSpace_->GetRDPath().toLocal8Bit().constData());
2025-01-04 04:12:51 +00:00
}
2025-01-05 14:29:59 +00:00
{
2025-01-07 15:45:23 +00:00
tinyxml2::XMLElement* chart = doc->NewElement("SimMatlab");
2025-01-05 14:29:59 +00:00
charts->LinkEndChild(chart);
2025-07-05 04:07:30 +00:00
chart->SetAttribute("SimMatlab", workSpace_->GetSimMatlabName().toLocal8Bit().constData());
2025-01-07 14:04:19 +00:00
}
2025-01-07 15:45:23 +00:00
2025-01-04 04:12:51 +00:00
return true;
}
2025-10-12 14:14:16 +00:00
bool WorkSpaceXMLWrite::SaveFiles(tinyxml2::XMLElement* scene, tinyxml2::XMLDocument* doc) {
// Persist multi-file entries per type as counts
tinyxml2::XMLElement* files = doc->NewElement("files");
scene->LinkEndChild(files);
for (const auto& kv : workSpace_->files_) {
const FileEntryType type = kv.first;
const auto& vec = kv.second;
tinyxml2::XMLElement* typeElem = doc->NewElement("type");
typeElem->SetAttribute("name", FileEntryTypeToString(type));
typeElem->SetAttribute("count", static_cast<int>(vec.size()));
files->LinkEndChild(typeElem);
}
return true;
}