2017-12-15 15:42:36 +00:00
|
|
|
#include "PopupWindowTracker.hxx"
|
|
|
|
|
|
|
|
#include <QGuiApplication>
|
|
|
|
#include <QMouseEvent>
|
|
|
|
#include <QWindow>
|
2018-03-09 10:07:14 +00:00
|
|
|
#include <QDebug>
|
2017-12-15 15:42:36 +00:00
|
|
|
|
|
|
|
PopupWindowTracker::PopupWindowTracker(QObject *parent) : QObject(parent)
|
|
|
|
{
|
2018-03-09 10:07:14 +00:00
|
|
|
connect(qGuiApp, &QGuiApplication::applicationStateChanged,
|
|
|
|
this, &PopupWindowTracker::onApplicationStateChanged);
|
2017-12-15 15:42:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-15 15:42:36 +00:00
|
|
|
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());
|
2017-12-15 15:42:36 +00:00
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
2017-12-15 15:42:36 +00:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|