DYTSrouce/src/workspace/WorkSpaceXMLWrite.cpp

160 lines
5.1 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);
2025-11-10 23:16:13 +00:00
SaveTimeStep(scene);
2025-01-04 04:12:51 +00:00
SaveChart(scene, &doc);
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;
}
2025-11-10 23:16:13 +00:00
// Removed: SaveTimeStep — timestep is no longer file-based
// 新增:保存 Timestep 的手动范围与步长倍率
2025-01-04 04:12:51 +00:00
bool WorkSpaceXMLWrite::SaveTimeStep(tinyxml2::XMLElement* scene) {
2025-11-10 23:16:13 +00:00
if (nullptr == workSpace_) {
2025-01-04 04:12:51 +00:00
return false;
}
2025-11-10 23:16:13 +00:00
auto* t = workSpace_->GetTimestep();
if (!t) {
// 若尚未创建,确保存在一个默认实例,以保证文件的一致性
workSpace_->EnsureTimestep();
t = workSpace_->GetTimestep();
}
if (!t) {
return false;
}
double start = 0.0, end = 0.0, step = 1.0;
t->GetRange(start, end, step);
tinyxml2::XMLElement* timestep = scene->InsertNewChildElement("timestep");
timestep->SetAttribute("manual", t->HasManualRange() ? 1 : 0);
timestep->SetAttribute("start", start);
timestep->SetAttribute("end", end);
timestep->SetAttribute("step", step);
2025-01-04 04:12:51 +00:00
return true;
}
bool WorkSpaceXMLWrite::SaveLamp(tinyxml2::XMLElement* scene) {
2025-10-29 09:11:36 +00:00
LampStatus *pLampStatus = workSpace_->GetLampStatus();
if (pLampStatus)
{
const QString lampPath = pLampStatus->GetPath();
if (!lampPath.isEmpty()) {
tinyxml2::XMLElement* lamp = scene->InsertNewChildElement("lamp");
lamp->SetAttribute("path", lampPath.toStdString().c_str());
return true;
}
}
return false;
2025-10-14 16:41:12 +00:00
}
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)
{
2025-11-02 09:47:54 +00:00
// charts section retained for compatibility, but deprecated entries (Wave, Report, RD, SimMatlab) are no longer saved
tinyxml2::XMLElement* charts = doc->NewElement("charts");
2025-01-04 04:12:51 +00:00
scene->LinkEndChild(charts);
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);
2025-10-27 00:35:04 +00:00
// Call SaveFiles method for each FileEntry to save detailed data
for (const auto& fileEntry : vec) {
if (fileEntry) {
fileEntry->SaveFiles(typeElem, doc);
}
}
2025-10-12 14:14:16 +00:00
}
return true;
}