#include "viewer/QtOsgViewWidget.h" #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include "config.h" #include "app/Application.h" #include "common/SpdLogger.h" #include "viewer/OsgView.h" #include "viewer/OsgViewer.h" #include "viewer/OsgCameraManipulator.h" #include "viewer/CameraControlManipulator.h" #include "scene/OsgScene.h" #include "workspace/WorkSpace.h" #include "workspace/WorkSpaceManager.h" #include "ui/MainFrame.h" QtOsgViewWidget::QtOsgViewWidget(QWidget* parent) : QWidget(parent) { LOG_INFO("actor, self={}", fmt::ptr(this)); setAttribute(Qt::WA_PaintOnScreen, false); setAttribute(Qt::WA_StaticContents, false); setAttribute(Qt::WA_NoSystemBackground, false); setAttribute(Qt::WA_OpaquePaintEvent, false); setAttribute(Qt::WA_DontCreateNativeAncestors, false); setFocusPolicy(Qt::StrongFocus); Initialize(); } QtOsgViewWidget::~QtOsgViewWidget() { LOG_INFO("dctor, self={}", fmt::ptr(this)); } void QtOsgViewWidget::keyPressEvent(QKeyEvent* event) { assert(nullptr != view_); view_->KeyPress(event); } void QtOsgViewWidget::keyReleaseEvent(QKeyEvent* event) { assert(nullptr != view_); view_->KeyRelease(event); } void QtOsgViewWidget::Initialize(void) { if (view_) { LOG_INFO("view is created"); return; } WId handle= winId(); double pixelRatio = screen()->devicePixelRatio(); view_ = OsgViewer::Get().CreateView(x(), y(), width() * pixelRatio, height() * pixelRatio, reinterpret_cast(handle)); if (nullptr == view_) { LOG_ERROR("view is nullptr"); return; } setMouseTracking(true); ////OsgSceneManipulator* manipulator = new OsgSceneManipulator; //CameraControlManipulator* manipulator = new CameraControlManipulator; ////osg::Vec3f eye(-256.63, 48.57, 82.19); ////osg::Vec3f center(-164.45, 30.05, 48.14); ////osg::Vec3f up(0.33, -0.07, 0.94); //osg::Vec3 eye(0.0f, 300.f, 60.0f); //osg::Vec3 center(0.0f, 60.0f, 0.0f); //osg::Vec3 up(0.0f, 0.0f, 1.0f); //manipulator->setHomePosition(eye, center, up); //manipulator->setManipulatorMode(CameraControlManipulator::Roam_ManipulatorMode); //manipulator->setVerticalAxisFixed(true); //manipulator->setRoamCamera(eye, center, up); //manipulator->setDistance(650.0); osgEarth::Util::EarthManipulator* manipulator = new osgEarth::Util::EarthManipulator; osgEarth::Viewpoint vp("home", 107.85, 32.35, 100.0, -2.50, -90.0, 1.5e7); manipulator->setHomeViewpoint(vp, 3.0); OsgCameraManipulator* cameraManipulator = new OsgCameraManipulator(manipulator, this); view_->Initialize(cameraManipulator); //auto pScene = Scenes::SceneManager::GetSingleton().GetCurrentScene(); //if (nullptr == pScene) { // pScene = Core::Application::GetSingleton().GetEngine()->CreateScene("Untitled"); // Scenes::SceneManager::GetSingleton().SetCurrentScene(pScene); //} //view_->SetScense(pScene); /* scene_ = new OsgScene; scene_->UseShadows(false); scene_->ChangeScene(OsgScene::CLOUDY); scene_->AttachView(view_); scene_->InitEventHandle(view_);*/ //scene_->TestBuildModel(); const QString path = QString("%1/default2.dyt").arg(Application::GetWorkSpacePath()); OnLoadDyt(path); /*WorkSpace* workspace = WorkSpaceManager::Get().GetOrCreate("defaultDyt"); workspace->AddScene("default", scene_, true); workspace_ = workspace;*/ //WorkSpaceManager::Get(). } void QtOsgViewWidget::Uninitialize(void) { /*if (nullptr != scene_) { delete scene_; scene_ = nullptr; }*/ if (nullptr != view_) { view_->Uninitialize(); OsgViewer::Get().DestroyView(view_); } } void QtOsgViewWidget::OnLoadDyt(const QString& path) { LOG_INFO("load dyt path:{}", path.toStdString()); WorkSpace* workSpace = WorkSpaceManager::Get().LoadDyt(path, view_); if (nullptr == workSpace) { QMessageBox::warning(&MainFrame::Get(), tr("notify"), 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(); } void QtOsgViewWidget::resizeEvent(QResizeEvent* event) { if (nullptr == view_) { return; } double pixelRatio = screen()->devicePixelRatio(); const QSize& size = event->size(); view_->Resize(0, 0, size.width() * pixelRatio, size.height() * pixelRatio); if (nullptr != workspace_) { workspace_->ViewReize(size.width() * pixelRatio, size.height() * pixelRatio); } }