DYTSrouce/src/workspace/WorkSpace.cpp

302 lines
7.9 KiB
C++
Raw Normal View History

2025-01-04 04:12:51 +00:00
#include "workspace/WorkSpace.h"
#include <QUUID>
2025-01-05 15:12:58 +00:00
#include <QFileInfo>
#include <QDir>
2025-01-04 04:12:51 +00:00
#include "workspace/WorkSpaceXMLParse.h"
#include "workspace/WorkSpaceXMLWrite.h"
#include "workspace/WorkSpaceItem.h"
#include "workspace/Timestep.h"
#include "workspace/LampStatus.h"
#include "xml/tinyxml2.h"
#include "common/SpdLogger.h"
#include "entities/Entity.h"
2025-07-05 04:07:30 +00:00
#include "utils/FileUtils.h"
2025-01-04 04:12:51 +00:00
//#include "workspace/WorkSpaceItemGroup.h"
//#include "workspace/WorkSpaceRiverGroup.h"
//#include "workspace/WorkSpaceRiverNetGroup.h"
Q_DECLARE_METATYPE(WorkSpace*)
2025-01-05 13:49:36 +00:00
WorkSpace::WorkSpace(QObject* parent) noexcept
: QObject(parent) {
2025-01-04 04:12:51 +00:00
uuid_ = QUuid::createUuid().toString();
2025-01-05 18:18:16 +00:00
homeViewpoint_ = osgEarth::Viewpoint("home", 120.000000, 25.000000, 100.000000, -2.500000, -90.000000, 8200000.000000);
2025-01-04 04:12:51 +00:00
}
2025-01-05 13:49:36 +00:00
WorkSpace::WorkSpace(const QString& path, QObject* parent)
2025-01-04 04:12:51 +00:00
: QObject(parent)
2025-01-05 13:49:36 +00:00
, path_(path){
2025-01-04 04:12:51 +00:00
uuid_ = QUuid::createUuid().toString();
2025-01-05 18:18:16 +00:00
homeViewpoint_ = osgEarth::Viewpoint("home", 120.000000, 25.000000, 100.000000, -2.500000, -90.000000, 8200000.000000);
2025-01-04 04:12:51 +00:00
}
2025-01-05 15:12:58 +00:00
const QString WorkSpace::GetDir() const {
QFileInfo info(path_);
return info.absolutePath();
}
2025-01-04 04:12:51 +00:00
2025-10-11 00:32:25 +00:00
void WorkSpace::SetCommondFilePath(const QString& path) {
QFileInfo fileInfo(path);
QString dirPath = QString("%1/%2").arg(GetDir(), fileInfo.fileName());
bool sucess = FileUtils::CopyFileToPath(path, dirPath, true);
LOG_INFO("copy commond file {}: {} to {}",
path.toLocal8Bit().data(),
dirPath.toLocal8Bit().data(),
sucess);
commondPath_ = fileInfo.fileName();
}
const QString WorkSpace::GetCommondFilePath() const {
QString path = QString("%1/%2").arg(GetDir(), commondPath_);
return path;
}
2025-07-05 04:07:30 +00:00
void WorkSpace::SetSimMatlab(const QString& path) {
QFileInfo fileInfo(path);
QString dirPath = QString("%1/%2").arg(GetDir(), fileInfo.fileName());
bool sucess = FileUtils::CopyFileToPath(path, dirPath, true);
LOG_INFO("copy simmatlab file {}: {} to {}",
path.toLocal8Bit().data(),
dirPath.toLocal8Bit().data(),
sucess);
simMatlabPath_ = fileInfo.fileName();
}
const QString WorkSpace::GetSimMatlab() const {
QString path = QString("%1/%2").arg(GetDir(), simMatlabPath_);
return path;
}
void WorkSpace::SetWavePath(const QString& path)
{
QFileInfo fileInfo(path);
QString dirPath = QString("%1/%2").arg(GetDir(), fileInfo.fileName());
bool sucess = FileUtils::CopyFileToPath(path, dirPath, true);
LOG_INFO("copy Wave file {}: {} to {}",
path.toLocal8Bit().data(),
dirPath.toLocal8Bit().data(),
sucess);
waveFile_ = fileInfo.fileName();
}
const QString WorkSpace::GetWavePath() const
{
QString path = QString("%1/%2").arg(GetDir(), waveFile_);
return path;
}
void WorkSpace::SetReportPath(const QString& path)
{
QFileInfo fileInfo(path);
QString dirPath = QString("%1/%2").arg(GetDir(), fileInfo.fileName());
bool sucess = FileUtils::CopyFileToPath(path, dirPath, true);
LOG_INFO("copy Wave file {}: {} to {}",
path.toLocal8Bit().data(),
dirPath.toLocal8Bit().data(),
sucess);
reportFile_ = fileInfo.fileName();
}
const QString WorkSpace::GetReportPath() const
{
QString path = QString("%1/%2").arg(GetDir(), reportFile_);
return path;
}
void WorkSpace::SetRDPath(const QString& path)
{
QFileInfo fileInfo(path);
QString dirPath = QString("%1/%2").arg(GetDir(), fileInfo.fileName());
bool sucess = FileUtils::CopyFileToPath(path, dirPath, true);
LOG_INFO("copy RD file {}: {} to {}",
path.toLocal8Bit().data(),
dirPath.toLocal8Bit().data(),
sucess);
rdFile_ = fileInfo.fileName();
}
const QString WorkSpace::GetRDPath() const
{
QString path = QString("%1/%2").arg(GetDir(), rdFile_);
return path;
}
2025-01-04 04:12:51 +00:00
void WorkSpace::AddEntity(Entity* entity) {
if (nullptr == entity) {
LOG_WARN("entity is nullptr");
return;
}
2025-01-05 03:33:33 +00:00
LOG_INFO("add entity: {}", entity->GetName().toLocal8Bit().constData());
2025-01-04 04:12:51 +00:00
entities_.push_back(entity);
entity->SetWorkspace(this);
emit EntityAdded(entity);
}
void WorkSpace::RemoveEntity(Entity* entity) {
auto itor = std::find_if(entities_.begin(), entities_.end(),
[this, entity](const auto* item) { return item == entity; });
if (itor != entities_.end()) {
emit EntityRemoved(entity);
entities_.erase(itor);
}
}
2025-06-17 18:05:47 +00:00
bool WorkSpace::TrackEntity(Entity* entity) {
dyt_check(nullptr != entity);
if (trackedEntity_ == entity) {
LOG_INFO("entity already tracked: {}", entity->GetName().toStdString());
return true;
}
if (nullptr == scene_) {
LOG_WARN("scene is nullptr, cannot track entity: {}", entity->GetName().toStdString());
return false;
}
if (trackedEntity_ != entity) {
UntrackEntity();
}
trackedEntity_ = entity;
auto mt = entity->GetRootComponent()->GetMatrixTransform();
LOG_INFO("track entity: {}", entity->GetName().toStdString());
return scene_->TrackEntity(mt, true);
}
void WorkSpace::UntrackEntity() {
if (trackedEntity_ != nullptr) {
if (scene_ != nullptr) {
LOG_INFO("untrack entity: {}", trackedEntity_->GetName().toStdString());
auto mt = trackedEntity_->GetRootComponent()->GetMatrixTransform();
scene_->TrackEntity(mt, false);
}
}
trackedEntity_ = nullptr;
}
2025-04-21 00:20:00 +00:00
void WorkSpace::SetActiveScene(OEScene* scene) {
dyt_check(nullptr != scene);
scene_ = scene;
2025-06-17 18:05:47 +00:00
//osgEarth::Viewpoint vp;
//vp.name() = "home";
//vp.focalPoint()->set(scene->GetSrs(), -121.488, 46.2054, 0, osgEarth::AltitudeMode::ALTMODE_ABSOLUTE);
//vp.pitch() = -50.0;
//vp.range() = 100000;
//homeViewpoint_ = vp;
2025-04-21 00:20:00 +00:00
}
2025-01-04 04:12:51 +00:00
bool WorkSpace::SetTimestep(Timestep* timestep) {
if (!timestep) {
return false;
}
if (nullptr != timestep_ && timestep_ != timestep) {
timestep_->deleteLater();
}
timestep_ = timestep;
2025-01-05 11:12:18 +00:00
emit TimestepChanged(timestep_);
2025-01-04 04:12:51 +00:00
return true;
}
bool WorkSpace::SetTimestepPath(const QString& path) {
Timestep* timestep = Timestep::Load(path, this);
return SetTimestep(timestep);
}
bool WorkSpace::SetLampStatus(class LampStatus* lampStatus) {
if (!lampStatus) {
return false;
}
if (nullptr != lampStatus_ && lampStatus_ != lampStatus) {
lampStatus_->deleteLater();
}
lampStatus_ = lampStatus;
2025-01-05 11:12:18 +00:00
emit LampStatusChanged(lampStatus_);
2025-01-04 04:12:51 +00:00
return true;
}
bool WorkSpace::SetLampPath(const QString& path) {
LampStatus* timestep = LampStatus::Load(path, this);
return SetLampStatus(timestep);
}
bool WorkSpace::Save(const QString& path) {
path_ = path;
return Save();
}
bool WorkSpace::Save() {
WorkSpaceXMLWrite xmlWrite(this);
return xmlWrite.Save(path_);
}
bool WorkSpace::Load(const QString& dyt) {
2025-01-05 03:33:33 +00:00
if (leaded_) {
LOG_INFO("dyt {} loaded", dyt.toStdString());
return true;
}
LOG_INFO("dyt {} loading", dyt.toLocal8Bit().constData());
path_ = dyt;
2025-01-04 04:12:51 +00:00
WorkSpaceXMLParse parse(this);
bool success = parse.Load(dyt);
if (!success) {
path_ = "";
2025-01-04 04:12:51 +00:00
}
2025-01-05 03:33:33 +00:00
leaded_ = success;
2025-01-04 04:12:51 +00:00
return success;
}
void WorkSpace::Unlaod() {
2025-01-05 03:33:33 +00:00
if (!leaded_) {
LOG_INFO("dyt {} unloaded", name_.toStdString());
return;
}
while (!entities_.empty()) {
auto entity = entities_.front();
entity->Destory();
2025-01-04 04:12:51 +00:00
}
2025-01-05 03:33:33 +00:00
leaded_ = false;
2025-01-04 04:12:51 +00:00
}
void WorkSpace::Begin() {
for (auto item : entities_) {
item->Begin();
}
}
void WorkSpace::OnFrame(double dt) {
if (nullptr != lampStatus_) {
double current = timestep_->GetCurrent();
lampStatus_->OnFrame(current);
}
for (auto item : entities_) {
item->Update(dt);
}
}
void WorkSpace::End() {
for (auto item : entities_) {
item->End();
}
}
2025-01-05 11:12:18 +00:00
void WorkSpace::OnLoaded() {
if (nullptr != lampStatus_) {
emit LampStatusChanged(lampStatus_);
}
if (nullptr != timestep_) {
emit TimestepChanged(timestep_);
}
}