1
0
Fork 0
flightgear/src/GUI/PopupWindowTracker.cxx

68 lines
1.5 KiB
C++
Raw Normal View History

#include "PopupWindowTracker.hxx"
#include <QGuiApplication>
#include <QMouseEvent>
#include <QWindow>
2018-03-09 10:07:14 +00:00
#include <QDebug>
PopupWindowTracker::PopupWindowTracker(QObject *parent) : QObject(parent)
{
2018-03-09 10:07:14 +00:00
connect(qGuiApp, &QGuiApplication::applicationStateChanged,
this, &PopupWindowTracker::onApplicationStateChanged);
}
PopupWindowTracker::~PopupWindowTracker()
{
if (m_window) {
qApp->removeEventFilter(this);
}
}
void PopupWindowTracker::setWindow(QWindow *window)
{
if (m_window == window)
return;
if (m_window) {
qApp->removeEventFilter(this);
}
m_window = window;
if (m_window) {
qApp->installEventFilter(this);
}
emit windowChanged(m_window);
}
2018-03-09 10:07:14 +00:00
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)
return false;
if (event->type() == QEvent::MouseButtonPress) {
QMouseEvent* me = static_cast<QMouseEvent*>(event);
2018-03-09 10:07:14 +00:00
QRect globalGeometry(m_window->mapToGlobal(QPoint(0,0)), m_window->size());
2018-03-09 10:07:14 +00:00
if (globalGeometry.contains(me->globalPos())) {
// click inside the window, process as normal fall through
} else {
m_window->close();
setWindow(nullptr);
2018-04-09 18:41:44 +00:00
// still fall through
2018-03-09 10:07:14 +00:00
}
}
return false;
}