diff --git a/src/main.cpp b/src/main.cpp index e8e64d26..5c9b28ef 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,14 +1,6 @@ #include "app/Application.h" -#include "common/SpdLogger.h" -#include "common/RecourceHelper.h" -#include "common/CrashHandler.h" - -#include "ui/MainFrame.h" -#include "ui/MainWindow.h" -#include "viewer/OsgWidget.h" - #include #include #include @@ -17,19 +9,14 @@ #include #include -#include -#include +#include "common/SpdLogger.h" +#include "common/RecourceHelper.h" +#include "common/CrashHandler.h" -#include +#include "workspace/WorkspaceManager.h" -#include -#include -#include -#include +#include "ui/MainFrame.h" -#include "osgqt/GraphicsWindowQt.h" -#include "scene/ui/CompositeWidgetManager.h" -#include "scene/ui/QueryElevationWidget.h" #ifndef LC #define LC "DYT" @@ -75,6 +62,7 @@ int main(int argc, char* argv[]) { mainWindow.showMaximized(); splash.finish(&mainWindow); + int ret = app.exec(); // app.Uninit(); Sleep(200); diff --git a/src/scene/OEScene.cpp b/src/scene/OEScene.cpp index b2a4d40d..a1f7082d 100644 --- a/src/scene/OEScene.cpp +++ b/src/scene/OEScene.cpp @@ -8,6 +8,7 @@ #include #include #include +#include #include #include @@ -34,51 +35,77 @@ OEScene::OEScene() { Init(); } +OEScene::~OEScene() { + +} + void OEScene::AttachView(osgViewer::View* view) { + if (!view) return; + if (curentView_ == view) return; + dyt_check(nullptr != earthRootNode_); - skyDome_ = osgEarth::Util::SkyNode::create(earthMapNode_); + if (!earthMapNode_) { - LOG_WARN("eart map node is nullptr"); - return; + LOG_WARN("eart map node is nullptr"); + return; } - dyt_check(nullptr != earthManipulator_); - view->setCameraManipulator(earthManipulator_); + if (skyDome_.valid()) { + removeChild(skyDome_.get()); + skyDome_ = nullptr; + } + + skyDome_ = osgEarth::Util::SkyNode::create(earthMapNode_); skyDome_->attach(view); skyDome_->getSunLight()->setAmbient(osg::Vec4(0.5,0.5,0.5,1.0)); addChild(skyDome_); - skyDome_->setDateTime(osgEarth::DateTime(2024, 12, 24, 3)); + dyt_check(nullptr != earthManipulator_); + view->setCameraManipulator(earthManipulator_); + view->setSceneData(this); curentView_ = view; } void OEScene::DetachView(osgViewer::View* view) { if (!view) return; + if (view != curentView_) return; - // Remove sky dome from this group if present; SkyNode has no detach in osgEarth 2.8 if (skyDome_.valid()) { - // Proactively disable lighting/effects to reduce interactions with the View skyDome_->setLighting(osg::StateAttribute::OFF); skyDome_->setSunVisible(false); skyDome_->setMoonVisible(false); skyDome_->setStarsVisible(false); skyDome_->setAtmosphereVisible(false); - removeChild(skyDome_.get()); - skyDome_ = nullptr; } - // Clear scene data - view->setSceneData(nullptr); - - // Clear manipulator association if (earthManipulator_.valid()) { view->setCameraManipulator(nullptr); } - // Clear internal view pointer - curentView_ = nullptr; + if (skyDome_.valid()) { + removeChild(skyDome_.get()); + skyDome_ = nullptr; + } + + if (entityRoot_.valid()) { + removeChild(entityRoot_); + entityRoot_ = nullptr; + } + + if (earthRootNode_.valid()) { + removeChild(earthRootNode_); + earthRootNode_ = nullptr; + } + + view->setSceneData(nullptr); + + // 清理内部视图指针 + if (curentView_ == view) { + curentView_ = nullptr; + } + } bool OEScene::AddToScene(osg::Node* node) const { @@ -87,6 +114,7 @@ bool OEScene::AddToScene(osg::Node* node) const { } void OEScene::SetHomeViewpoint(const osgEarth::Viewpoint& viewpoint, double duration_s) { + return; dyt_check(nullptr != earthManipulator_); earthManipulator_->setViewpoint(viewpoint, duration_s); @@ -198,18 +226,19 @@ void OEScene::Init() { std::string earthPath(earthFile.toLocal8Bit().constData()); LOG_INFO("Loading earth file from: {}", earthPath); - // earthRootNode_ = osgDB::readNodeFile("triton.earth"); + + osg::ref_ptr myReadOptions = osgEarth::Registry::cloneOrCreateOptions(); earthRootNode_ = osgDB::readNodeFile(earthPath); dyt_check(nullptr != earthRootNode_); - addChild(earthRootNode_); - earthMapNode_ = osgEarth::MapNode::get(earthRootNode_); + earthMapNode_ = osgEarth::MapNode::findMapNode(earthRootNode_); dyt_check(nullptr != earthMapNode_); + addChild(earthMapNode_); + LOG_INFO("earth map node get success: {}", earthMapNode_.valid()); g_srs_ = earthMapNode_->getMapSRS(); - earthManipulator_ = new osgEarth::Util::EarthManipulator(); - earthManipulator_->getSettings()->setMinMaxPitch(-90.0, -10.0); + earthManipulator_ = new osgEarth::Util::EarthManipulator(); entityRoot_ = new osg::Group; diff --git a/src/scene/OEScene.h b/src/scene/OEScene.h index 3f5524f8..4ee20861 100644 --- a/src/scene/OEScene.h +++ b/src/scene/OEScene.h @@ -17,6 +17,7 @@ namespace Util { class OEScene : public osg::Group { public: OEScene(); + ~OEScene(); void AttachView(osgViewer::View* view); void DetachView(osgViewer::View* view); diff --git a/src/scene/ui/OESceneUI.cpp b/src/scene/ui/OESceneUI.cpp index 7ba80e83..a4e4170d 100644 --- a/src/scene/ui/OESceneUI.cpp +++ b/src/scene/ui/OESceneUI.cpp @@ -1,4 +1,4 @@ -#include "scene/ui/OESceneUI.h" +#include "scene/ui/OESceneUI.h" #include "config.h" #include "common/SpdLogger.h" @@ -18,11 +18,10 @@ OESceneUI::OESceneUI(OEScene* oeScene) OESceneUI::~OESceneUI(void) { LOG_INFO("dctor self={}", spdlog::fmt_lib::ptr(this)); - /* if (compositeWidgetManager_) { - compositeWidgetManager_->DetachViewUI(); - delete compositeWidgetManager_; - compositeWidgetManager_ = nullptr; - }*/ + + compositeWidgetManager_ = nullptr; + queryElevationWidget_ = nullptr; + oeScene_ = nullptr; } void OESceneUI::InitUI(OsgViewUI* ui) { @@ -53,12 +52,20 @@ void OESceneUI::UninitUI(OsgViewUI* ui) { LOG_INFO("not init"); return; } + + // 安全清理,不访问可能已销毁的资源 if (compositeWidgetManager_) { - compositeWidgetManager_->DetachViewUI(ui); + // 只在ui有效时才调用DetachViewUI + if (ui) { + compositeWidgetManager_->DetachViewUI(ui); + } compositeWidgetManager_ = nullptr; } if (queryElevationWidget_) { - queryElevationWidget_->DetachViewUI(ui); + // 只在ui有效时才调用DetachViewUI + if (ui) { + queryElevationWidget_->DetachViewUI(ui); + } queryElevationWidget_ = nullptr; } diff --git a/src/ui/MainFrame.cpp b/src/ui/MainFrame.cpp index 4c8ddc1f..e2dc092a 100644 --- a/src/ui/MainFrame.cpp +++ b/src/ui/MainFrame.cpp @@ -123,8 +123,8 @@ void MainFrame::InitUI() { simuRunMenu->SetMainWindow(mainWindow); - OsgWidget* viewWidget = mainWindow->GetViewWidget(); - connect(fileMenu, &FileManagerMenu::LoadDyt, viewWidget, &OsgWidget::OnLoadDyt); + /*OsgWidget* viewWidget = mainWindow->GetViewWidget(); + connect(fileMenu, &FileManagerMenu::LoadDyt, viewWidget, &OsgWidget::OnLoadDyt);*/ //connect(system_, &SystemManagerMenu::signalShowUISetting, mainWindow, &MainWindow::slotShowUISetting); diff --git a/src/ui/MainWindow.cpp b/src/ui/MainWindow.cpp index 078938ba..d4ec800a 100644 --- a/src/ui/MainWindow.cpp +++ b/src/ui/MainWindow.cpp @@ -42,8 +42,6 @@ MainWindow::MainWindow(QWidget* parent) LOG_INFO("MainWindow::MainWindow - Constructor called, about to call InitUI"); InitUI(); LOG_INFO("MainWindow::MainWindow - Constructor completed"); - - QTimer::singleShot(1000.0, this, SLOT(slotUpdateShowMax())); } MainWindow::~MainWindow() { @@ -182,9 +180,7 @@ void MainWindow::InitUI() { } } - //ui->viewWidget->layout()->addWidget(qtOsgViewWidget_); qtOsgViewWidget_->LoadDefaultScene(); - #if 0 MatlabObject::GetInstance()->RunMatlabFile(""); #endif // 1 @@ -206,7 +202,3 @@ void MainWindow::UninitUI() { } } -void MainWindow::slotUpdateShowMax() -{ - showMaximized(); -} \ No newline at end of file diff --git a/src/ui/MainWindow.h b/src/ui/MainWindow.h index 598e2651..c8cbd9db 100644 --- a/src/ui/MainWindow.h +++ b/src/ui/MainWindow.h @@ -36,9 +36,6 @@ public: return dataPanelManager_; } -protected slots: - void slotUpdateShowMax(); - private: void InitUI(); void UninitUI(); diff --git a/src/ui/Menu/FileManagerMenu.cpp b/src/ui/Menu/FileManagerMenu.cpp index 7b480da6..13575a7d 100644 --- a/src/ui/Menu/FileManagerMenu.cpp +++ b/src/ui/Menu/FileManagerMenu.cpp @@ -75,6 +75,7 @@ void FileManagerMenu::OpenWorkSpace() { } LOG_INFO("open workspace: {}", fileName.toLocal8Bit().constData()); + WorkSpaceManager::Get().LoadDyt(fileName); emit LoadDyt(fileName); } diff --git a/src/viewer/OsgWidget.cpp b/src/viewer/OsgWidget.cpp index 26db5d2c..76c4f7ca 100644 --- a/src/viewer/OsgWidget.cpp +++ b/src/viewer/OsgWidget.cpp @@ -47,11 +47,10 @@ static void ConfigureView( osgViewer::View* view ) { OsgWidget::OsgWidget(QWidget* parent, Qt::WindowFlags f) : QWidget(parent, f) { - // setAttribute(Qt::WA_PaintOnScreen, true); - // setAttribute(Qt::WA_StaticContents, true); - // setAttribute(Qt::WA_NoSystemBackground, true); - //setAttribute(Qt::WA_OpaquePaintEvent, true); - // setAttribute(Qt::WA_DontCreateNativeAncestors, false); + //setAttribute(Qt::WA_PaintOnScreen, true); + setAttribute(Qt::WA_StaticContents, true); + setAttribute(Qt::WA_NoSystemBackground, true); + setAttribute(Qt::WA_OpaquePaintEvent, true); setThreadingModel(osgViewer::ViewerBase::SingleThreaded); setMouseTracking(true); @@ -73,16 +72,6 @@ OsgWidget::~OsgWidget() { if (timer_.isActive()) { timer_.stop(); } - - // First unload the workspace while scene/view are still valid, - // so entities can detach cleanly from the scene graph - if (nullptr != workspace_) { - workspace_->Unlaod(); - workspace_ = nullptr; - } - - // Then perform the viewer teardown (detach UI, view, and scene) - Uninitialize(); } void OsgWidget::Initialize() { @@ -109,6 +98,11 @@ void OsgWidget::Initialize() { void OsgWidget::Uninitialize() { LOG_INFO("OsgWidget::Uninitialize"); + + if (nullptr != activeScene_) { + activeScene_->DetachView(view_); + } + if (nullptr != viewUI_) { // Avoid creating UI during teardown; only remove if it exists if (activeScene_.valid()) { @@ -119,8 +113,8 @@ void OsgWidget::Uninitialize() { } viewUI_ = nullptr; } + if (nullptr != activeScene_) { - activeScene_->DetachView(view_); activeScene_ = nullptr; } if (nullptr != view_) { @@ -137,29 +131,6 @@ void OsgWidget::LoadDefaultScene() { } } -void OsgWidget::OnLoadDyt(const QString& path) { - LOG_INFO("load dyt path:{}", path.toStdString()); - WorkSpace* workSpace = WorkSpaceManager::Get().LoadDyt(path); - if (nullptr == workSpace) { - QMessageBox::warning(&MainFrame::Get(), tr("warning"), - tr("open dyt file failed"), QMessageBox::Ok); - return; - } - - const QString& name = workSpace->GetName(); - const QString windowTitle = MainFrame::Get().windowTitle(); - - MainFrame::Get().setWindowTitle(windowTitle + "-" + name); - WorkSpaceManager::Get().SetCurrent(workSpace); - if (nullptr != workspace_ && workspace_ != workSpace) { - workspace_->Unlaod(); - } - - workspace_ = workSpace; - - emit signalResetWorkSpace(); -} - osgQt::GraphicsWindowQt* OsgWidget::createGraphicsWindow(int x, int y, int w, int h, const std::string& name, bool windowDecoration) { osg::DisplaySettings* ds = osg::DisplaySettings::instance().get(); @@ -181,8 +152,7 @@ osgQt::GraphicsWindowQt* OsgWidget::createGraphicsWindow(int x, int y, int w, in QWidget* OsgWidget::addViewWidget(osgQt::GraphicsWindowQt* gw) { view_ = new osgViewer::View; - addView( view_ ); - ConfigureView(view_); + addView(view_); osg::Camera* camera = view_->getCamera(); camera->setGraphicsContext( gw ); @@ -194,6 +164,9 @@ QWidget* OsgWidget::addViewWidget(osgQt::GraphicsWindowQt* gw) { camera->setProjectionMatrixAsPerspective(30.0f, static_cast(traits->width)/static_cast(traits->height), 1.0f, 10000.0f ); activeScene_->AttachView(view_); + + ConfigureView(view_); + return gw->getGLWidget(); } diff --git a/src/viewer/OsgWidget.h b/src/viewer/OsgWidget.h index e2487507..a11cf9a8 100644 --- a/src/viewer/OsgWidget.h +++ b/src/viewer/OsgWidget.h @@ -16,10 +16,8 @@ public: void Initialize(void); void Uninitialize(void); void LoadDefaultScene(void); - void OnLoadDyt(const QString& path); signals: - void signalResetWorkSpace(); void signalScaleInfo(const QString&); protected: @@ -52,7 +50,6 @@ private: osg::ref_ptr activeScene_; osg::ref_ptr viewUI_; - class WorkSpace* workspace_{ nullptr }; osg::ref_ptr view_; // class OsgView* view_{nullptr}; }; diff --git a/src/workspace/WorkSpaceManager.cpp b/src/workspace/WorkSpaceManager.cpp index f890ad9c..b55f4d3a 100644 --- a/src/workspace/WorkSpaceManager.cpp +++ b/src/workspace/WorkSpaceManager.cpp @@ -152,6 +152,7 @@ WorkSpace* WorkSpaceManager::LoadDyt(const QString& dyt) { } workSpaces_[dyt] = workspace; + SetCurrent(workspace); return workspace; }