You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
o3de/Code/Sandbox/Editor/Util/ModalWindowDismisser.cpp

79 lines
2.2 KiB
C++

/*
* All or portions of this file Copyright (c) Amazon.com, Inc. or its affiliates or
* its licensors.
*
* For complete copyright and license terms please see the LICENSE at the root of this
* distribution (the "License"). All use of this software is governed by the License,
* or, if provided, by the license below or the license accompanying this file. Do not
* remove or modify any license notices. This file is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*
*/
// Description : Utility for dismissing every modal windows
#include "EditorDefs.h"
#include "ModalWindowDismisser.h"
// Qt
#include <QDialog>
#include <QTimer>
ModalWindowDismisser::ModalWindowDismisser()
{
qApp->installEventFilter(this);
}
ModalWindowDismisser::~ModalWindowDismisser()
{
if (qApp)
{
qApp->removeEventFilter(this);
}
}
void ModalWindowDismisser::DismissWindows()
{
for (QDialog* dialog : m_windows)
{
dialog->close();
}
m_windows.clear();
m_dissmiss = false;
}
bool ModalWindowDismisser::eventFilter(QObject* object, QEvent* event)
{
if (QDialog* dialog = qobject_cast<QDialog*>(object))
{
if (dialog->isModal())
{
if (event->type() == QEvent::Show)
{
auto it = AZStd::find(m_windows.begin(), m_windows.end(), dialog);
if (it == m_windows.end())
{
m_windows.push_back(dialog);
}
if (!m_dissmiss)
{
// Closing the window at the same moment is opened leads to crashes and is unstable,
// so do it after a long 1 ms
QTimer::singleShot(1, this, &ModalWindowDismisser::DismissWindows);
m_dissmiss = true;
}
}
else if (event->type() == QEvent::Close)
{
auto it = AZStd::find(m_windows.begin(), m_windows.end(), dialog);
if (it != m_windows.end())
{
m_windows.erase(it);
}
}
}
}
return false;
}