From fa72d8dd064f1a9c481d6bc4a2160246599e06b1 Mon Sep 17 00:00:00 2001 From: James Turner Date: Fri, 9 Mar 2018 10:07:14 +0000 Subject: [PATCH] Popup window tracking helper hooked up --- src/GUI/LauncherMainWindow.cxx | 2 ++ src/GUI/PopupWindowTracker.cxx | 24 ++++++++++++++++++++---- src/GUI/PopupWindowTracker.hxx | 2 ++ src/GUI/qml/AircraftVariantChoice.qml | 6 +++++- src/GUI/qml/PopupChoice.qml | 6 +++++- 5 files changed, 34 insertions(+), 6 deletions(-) diff --git a/src/GUI/LauncherMainWindow.cxx b/src/GUI/LauncherMainWindow.cxx index af3c248da..fbfc23829 100644 --- a/src/GUI/LauncherMainWindow.cxx +++ b/src/GUI/LauncherMainWindow.cxx @@ -48,6 +48,7 @@ #include "QmlAircraftInfo.hxx" #include "LauncherArgumentTokenizer.hxx" #include "PathUrlHelper.hxx" +#include "PopupWindowTracker.hxx" #include "ui_Launcher.h" @@ -245,6 +246,7 @@ void LauncherMainWindow::initQML() qmlRegisterType("FlightGear.Launcher", 1, 0, "FlickableExtentQuery"); qmlRegisterType("FlightGear.Launcher", 1, 0, "AircraftInfo"); + qmlRegisterType("FlightGear.Launcher", 1, 0, "PopupWindowTracker"); m_config = new LaunchConfig(this); connect(m_config, &LaunchConfig::collect, this, &LauncherMainWindow::collectAircraftArgs); diff --git a/src/GUI/PopupWindowTracker.cxx b/src/GUI/PopupWindowTracker.cxx index a49a27b36..355400595 100644 --- a/src/GUI/PopupWindowTracker.cxx +++ b/src/GUI/PopupWindowTracker.cxx @@ -3,10 +3,12 @@ #include #include #include +#include PopupWindowTracker::PopupWindowTracker(QObject *parent) : QObject(parent) { - + connect(qGuiApp, &QGuiApplication::applicationStateChanged, + this, &PopupWindowTracker::onApplicationStateChanged); } PopupWindowTracker::~PopupWindowTracker() @@ -35,6 +37,14 @@ void PopupWindowTracker::setWindow(QWindow *window) emit windowChanged(m_window); } +void PopupWindowTracker::onApplicationStateChanged(Qt::ApplicationState as) +{ + if (m_window && (as != Qt::ApplicationActive)) { + m_window->close(); + setWindow(nullptr); + } +} + bool PopupWindowTracker::eventFilter(QObject *watched, QEvent *event) { if (!m_window) @@ -42,10 +52,16 @@ bool PopupWindowTracker::eventFilter(QObject *watched, QEvent *event) if (event->type() == QEvent::MouseButtonPress) { QMouseEvent* me = static_cast(event); - QPoint windowPos = m_window->mapFromGlobal(me->globalPos()); - } + QRect globalGeometry(m_window->mapToGlobal(QPoint(0,0)), m_window->size()); - // also check for app loosing focus + if (globalGeometry.contains(me->globalPos())) { + // click inside the window, process as normal fall through + } else { + m_window->close(); + setWindow(nullptr); + return true; + } + } return false; } diff --git a/src/GUI/PopupWindowTracker.hxx b/src/GUI/PopupWindowTracker.hxx index 3fbe6c72e..2eeb72860 100644 --- a/src/GUI/PopupWindowTracker.hxx +++ b/src/GUI/PopupWindowTracker.hxx @@ -29,6 +29,8 @@ signals: public slots: void setWindow(QWindow* window); + void onApplicationStateChanged(Qt::ApplicationState as); + protected: bool eventFilter(QObject *watched, QEvent *event) override; }; diff --git a/src/GUI/qml/AircraftVariantChoice.qml b/src/GUI/qml/AircraftVariantChoice.qml index 4012c1fb7..96bd433aa 100644 --- a/src/GUI/qml/AircraftVariantChoice.qml +++ b/src/GUI/qml/AircraftVariantChoice.qml @@ -73,6 +73,7 @@ Rectangle { popupFrame.x = screenPos.x; popupFrame.y = screenPos.y; popupFrame.visible = true + tracker.window = popupFrame } } @@ -81,10 +82,13 @@ Rectangle { id: aircraftInfo } + PopupWindowTracker { + id: tracker + } + Window { id: popupFrame - modality: Qt.WindowModal width: root.width flags: Qt.Popup height: choicesColumn.childrenRect.height diff --git a/src/GUI/qml/PopupChoice.qml b/src/GUI/qml/PopupChoice.qml index 8438c6ed1..b77d78170 100644 --- a/src/GUI/qml/PopupChoice.qml +++ b/src/GUI/qml/PopupChoice.qml @@ -111,13 +111,17 @@ Item { popupFrame.x = screenPos.x; popupFrame.y = screenPos.y; popupFrame.visible = true + tracker.window = popupFrame } } + PopupWindowTracker { + id: tracker + } + Window { id: popupFrame - modality: Qt.WindowModal flags: Qt.Popup height: choicesColumn.childrenRect.height + Style.margin * 2 width: choicesColumn.childrenRect.width + Style.margin * 2