#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); activeScene_ = new OEScene; activeScene_->AttachView(view_); activeScene_->InitEventHandle(view_); osgEarth::Util::EarthManipulator* manipulator = new osgEarth::Util::EarthManipulator; connect(&WorkSpaceManager::Get(), &WorkSpaceManager::WorkSpaceChanged, [](WorkSpace* workspace) { LOG_INFO("WorkSpaceChanged"); if (nullptr == workspace) { return; } OsgCameraManipulator* manipulator = OsgViewer::Get().GetView()->GetCameraManipulator(); if (nullptr == manipulator) { LOG_WARN("manipulator is nullptr"); return; } osgGA::CameraManipulator* gaManipulator = manipulator->GetManipulator(); osgEarth::Util::EarthManipulator* ccm = dynamic_cast(gaManipulator); if (nullptr == ccm) { LOG_WARN("ccm is nullptr"); return; } ccm->setViewpoint(workspace->GetHomeViewpoint(), 3.0); } ); OsgCameraManipulator* cameraManipulator = new OsgCameraManipulator(manipulator, this); view_->Initialize(cameraManipulator); } void QtOsgViewWidget::Uninitialize(void) { /*if (nullptr != scene_) { delete scene_; scene_ = nullptr; }*/ if (nullptr != view_) { view_->Uninitialize(); OsgViewer::Get().DestroyView(view_); } } void QtOsgViewWidget::LoadDefaultScene(void) { dyt_check(nullptr != activeScene_); WorkSpaceManager::Get().LoadDefaultWorkspace(activeScene_); } void QtOsgViewWidget::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("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); }