From e93cfd3c255f182203800ac0c71d4a0bd3dfd2c6 Mon Sep 17 00:00:00 2001 From: Klaus Basan Date: Sun, 18 Feb 2018 01:04:53 +0100 Subject: [PATCH] Ref T256, added foreign window utility functions --- src/blackgui/foreignwindows.cpp | 64 ++++++++++++++++++++++++++------- src/blackgui/foreignwindows.h | 19 +++++++--- 2 files changed, 65 insertions(+), 18 deletions(-) diff --git a/src/blackgui/foreignwindows.cpp b/src/blackgui/foreignwindows.cpp index 8abfc18c4..e6064515b 100644 --- a/src/blackgui/foreignwindows.cpp +++ b/src/blackgui/foreignwindows.cpp @@ -14,55 +14,93 @@ #include #include +using namespace BlackMisc::Simulation; + namespace BlackGui { QScopedPointer CForeignWindows::m_windowFinder(IWindowFinder::create()); QWindow *CForeignWindows::getFS9Window() { - QWindow *simulatorWindow = nullptr; - simulatorWindow = m_windowFinder->findForeignWindow("", "FS98MAIN"); - + if (!m_windowFinder) { return nullptr; } + QWindow *simulatorWindow = m_windowFinder->findForeignWindow("", "FS98MAIN"); return simulatorWindow; } QWindow *CForeignWindows::getFSXWindow() { - QWindow *simulatorWindow = nullptr; - simulatorWindow = m_windowFinder->findForeignWindow("", "FS98MAIN"); + if (!m_windowFinder) { return nullptr; } + QWindow *simulatorWindow = m_windowFinder->findForeignWindow("", "FS98MAIN"); + return simulatorWindow; + } + QWindow *CForeignWindows::getP3DWindow() + { + if (!m_windowFinder) { return nullptr; } + QWindow *simulatorWindow = m_windowFinder->findForeignWindow("", "FS98MAIN"); return simulatorWindow; } QWindow *CForeignWindows::getXPlaneWindow() { QWindow *simulatorWindow = nullptr; + // FIXME: // Use datarefs Sim/operation/windows/system window via xswiftbus to grab the OS's native window handle // http://www.xsquawkbox.net/xpsdk/mediawiki/sim%252Foperation%252Fwindows%252Fsystem_window // For the time being, use IWindowFinder. // The if condition is to prevent a crash on Linux/MacOS. if (!m_windowFinder.isNull()) simulatorWindow = m_windowFinder->findForeignWindow("X-System", ""); - return simulatorWindow; } - void CForeignWindows::setSimulatorAsParent(QWindow *simulatorWindow, QWidget *child) + QWindow *CForeignWindows::getFirstFoundSimulatorWindow() { - if (!simulatorWindow) return; - if (!child) return; + QWindow *w = CForeignWindows::getP3DWindow(); if (w) { return w; } + w = CForeignWindows::getXPlaneWindow(); if (w) { return w; } + w = CForeignWindows::getFSXWindow(); if (w) { return w; } + w = CForeignWindows::getFS9Window(); + return w; + } - bool isVisible = child->isVisible(); + QWindow *CForeignWindows::getSimulatorWindow(const CSimulatorInfo &simulator) + { + switch (simulator.getSimulator()) + { + case CSimulatorInfo::FS9: return CForeignWindows::getFS9Window(); + case CSimulatorInfo::FSX: return CForeignWindows::getFSXWindow(); + case CSimulatorInfo::P3D: return CForeignWindows::getP3DWindow(); + case CSimulatorInfo::XPLANE: return CForeignWindows::getXPlaneWindow(); + default: break; + } + return nullptr; + } + + bool CForeignWindows::setSimulatorAsParent(QWindow *simulatorWindow, QWidget *child) + { + if (!simulatorWindow) { return false; } + if (!child) {return false; } // If visible, hide it during the reparent. Otherwise setting the parent will have no effect. - if (isVisible) child->hide(); + const bool isVisible = child->isVisible(); + if (isVisible) {child->hide(); } QWindow *childWindow = child->windowHandle(); - Q_ASSERT_X(childWindow, "CForeignWindows::setSimulatorAsParent", "Native resources for child widget have not yet been allocated. Did you call QWidget::show() before?"); + Q_ASSERT_X(childWindow, Q_FUNC_INFO, "Native resources for child widget have not yet been allocated. Did you call QWidget::show() before?"); childWindow->setTransientParent(simulatorWindow); // If it was visible before, make it visible again - if (isVisible) child->show(); + if (isVisible) { child->show(); } + return true; + } + + bool CForeignWindows::unsetSimulatorAsParent(QWidget *child) + { + if (!child) { return false; } + if (!child->windowHandle()) { return false; } + if (!child->windowHandle()->transientParent()) { return false; } + child->windowHandle()->setTransientParent(nullptr); + return true; } } diff --git a/src/blackgui/foreignwindows.h b/src/blackgui/foreignwindows.h index 5c569adcb..0f3a1f2a7 100644 --- a/src/blackgui/foreignwindows.h +++ b/src/blackgui/foreignwindows.h @@ -13,7 +13,7 @@ #define BLACKGUI_FOREIGNWINDOWS_H #include "blackgui/blackguiexport.h" - +#include "blackmisc/simulation/simulatorinfo.h" #include class QWidget; @@ -26,23 +26,32 @@ namespace BlackGui //! Foreign windows class BLACKGUI_EXPORT CForeignWindows { - public: - //! FS9 window static QWindow *getFS9Window(); //! FSX window static QWindow *getFSXWindow(); + //! P3D window + static QWindow *getP3DWindow(); + //! X-Plane window static QWindow *getXPlaneWindow(); + //! First simulator window found + static QWindow *getFirstFoundSimulatorWindow(); + + //! Simulator window + static QWindow *getSimulatorWindow(const BlackMisc::Simulation::CSimulatorInfo &simulator); + //! Set simulator as transient parent for child widget - static void setSimulatorAsParent(QWindow *simulatorWindow, QWidget *child); + static bool setSimulatorAsParent(QWindow *simulatorWindow, QWidget *child); + + //! Unset the parent + static bool unsetSimulatorAsParent(QWidget *child); private: - //! Constructor, use static methods only CForeignWindows() {}