/* * 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 "StdAfx.h" #include "ModalWindowDismisser.h" #include #include #include 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(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; }