#include "workspace/WorkSpaceManager.h" #include #include #include "workspace/WorkSpace.h" #include "workspace/Timestep.h" #include "common/SpdLogger.h" template<> WorkSpaceManager* Singleton::instance_ = nullptr; WorkSpaceManager::WorkSpaceManager(QObject* parent) : QObject(parent) { } WorkSpaceManager::~WorkSpaceManager() { } void WorkSpaceManager::OnDestory() { SaveDefaultWorkspace(); } WorkSpace* WorkSpaceManager::LoadDefaultWorkspace(class OsgView* view) { const QString iniFile = GetDefaultWorkSpaceName(); if (!QFile::exists(iniFile)) { LOG_ERROR("default workspace file not exist:{}", iniFile.toStdString()); return nullptr; } QSettings settings(iniFile, QSettings::IniFormat); const QString path = settings.value("workspace/path", "").toString(); if (path.isEmpty()) { LOG_ERROR("default workspace path is empty"); return nullptr; } WorkSpace* workspace = LoadDyt(path, view); if (nullptr == workspace) { LOG_ERROR("load default workspace failed"); return nullptr; } SetCurrent(workspace); return workspace; } void WorkSpaceManager::SaveDefaultWorkspace() { const QString iniFile = GetDefaultWorkSpaceName(); if (!QFile::exists(iniFile)) { LOG_ERROR("default workspace file not exist:{}", iniFile.toStdString()); return; } WorkSpace* current = GetCurrent(); if (nullptr == current) { LOG_ERROR("current workspace is nullptr"); return; } const QString path = current->GetPath(); QSettings settings(iniFile, QSettings::IniFormat); settings.setValue("workspace/path", path); } bool WorkSpaceManager::Contains(const QString& name) const { const auto itor = workSpaces_.find(name); if (workSpaces_.end() != itor) { return true; } return false; } bool WorkSpaceManager::Remove(const QString& name) { auto itor = workSpaces_.find(name); if (workSpaces_.end() == itor) { return true; } WorkSpace* workspace = itor->second; if (nullptr != workspace) { workspace->Unlaod(); workspace->deleteLater(); } workSpaces_.erase(itor); return true; } WorkSpace* WorkSpaceManager::LoadDyt(const QString& dyt, class OsgView* view) { WorkSpace* workspace = new WorkSpace(view, this); if (!workspace->Load(dyt)) { LOG_WARN("load workspace failed"); workspace->deleteLater(); return nullptr; } workSpaces_[dyt] = workspace; return workspace; } WorkSpace* WorkSpaceManager::GetOrCreate(const QString& path) { const auto itor = workSpaces_.find(path); if (workSpaces_.end() != itor) { return itor->second; } LOG_INFO("create workspace name:{}", path.toStdString()); WorkSpace* workspace = new WorkSpace(path, this); workSpaces_[path] = workspace; return workspace; } void WorkSpaceManager::SetCurrent(WorkSpace* workspace) { if (nullptr != current_) { current_->End(); } current_ = workspace; lastTime_ = clock(); emit WorkSpaceChanged(current_); current_->OnLoaded(); } void WorkSpaceManager::OnFrame() { if (nullptr == current_) { return; } constexpr double lockPreSecFre = 1.0 / CLOCKS_PER_SEC; int64_t t = clock(); double dt = static_cast(t - lastTime_) * lockPreSecFre; Timestep* timeStep = current_->GetTimestep(); if (nullptr != timeStep) { timeStep->Update(dt); } lastTime_ = t; } QString WorkSpaceManager::GetDefaultWorkSpaceName() { #if _DEBUG const QString iniFile = QString("%1workspace/config.ini").arg(QString(CONFIG_PATH)).arg(skin); #else const QString appDirPath = QApplication::applicationDirPath(); const QString iniFile = QString("%1/config/workspace/config.ini").arg(appDirPath); #endif return iniFile; }