1 /****************************************************************************
2 **
3 ** Copyright (C) 2017 The Qt Company Ltd.
4 ** Contact: http://www.qt.io/licensing/
5 **
6 ** This file is part of the Qt Quick Templates 2 module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL3$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see http://www.qt.io/terms-conditions. For further
15 ** information use the contact form at http://www.qt.io/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 3 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPLv3 included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 3 requirements
23 ** will be met: https://www.gnu.org/licenses/lgpl.html.
24 **
25 ** GNU General Public License Usage
26 ** Alternatively, this file may be used under the terms of the GNU
27 ** General Public License version 2.0 or later as published by the Free
28 ** Software Foundation and appearing in the file LICENSE.GPL included in
29 ** the packaging of this file. Please review the following information to
30 ** ensure the GNU General Public License version 2.0 requirements will be
31 ** met: http://www.gnu.org/licenses/gpl-2.0.html.
32 **
33 ** $QT_END_LICENSE$
34 **
35 ****************************************************************************/
36 
37 #include "qquickshortcutcontext_p_p.h"
38 #include "qquickoverlay_p_p.h"
39 #include "qquicktooltip_p.h"
40 #include "qquickpopup_p.h"
41 #include "qquickmenu_p.h"
42 #include "qquickmenubaritem_p.h"
43 
44 #include <QtGui/qguiapplication.h>
45 #include <QtQuick/qquickrendercontrol.h>
46 
47 QT_BEGIN_NAMESPACE
48 
isBlockedByPopup(QQuickItem * item)49 static bool isBlockedByPopup(QQuickItem *item)
50 {
51     if (!item || !item->window())
52         return false;
53 
54     QQuickOverlay *overlay = QQuickOverlay::overlay(item->window());
55     const auto popups = QQuickOverlayPrivate::get(overlay)->stackingOrderPopups();
56     for (QQuickPopup *popup : popups) {
57         if (qobject_cast<QQuickToolTip *>(popup))
58             continue; // ignore tooltips (QTBUG-60492)
59         if (popup->isModal() || popup->closePolicy() & QQuickPopup::CloseOnEscape) {
60             if (QQuickMenu *menu = qobject_cast<QQuickMenu *>(popup)) {
61                 if (qobject_cast<QQuickMenuBarItem *>(menu->parentItem()))
62                     continue;
63             }
64             return item != popup->popupItem() && !popup->popupItem()->isAncestorOf(item);
65         }
66     }
67 
68     return false;
69 }
70 
matcher(QObject * obj,Qt::ShortcutContext context)71 bool QQuickShortcutContext::matcher(QObject *obj, Qt::ShortcutContext context)
72 {
73     QQuickItem *item = nullptr;
74     switch (context) {
75     case Qt::ApplicationShortcut:
76         return true;
77     case Qt::WindowShortcut:
78         while (obj && !obj->isWindowType()) {
79             item = qobject_cast<QQuickItem *>(obj);
80             if (item && item->window()) {
81                 obj = item->window();
82                 break;
83             } else if (QQuickPopup *popup = qobject_cast<QQuickPopup *>(obj)) {
84                 obj = popup->window();
85                 item = popup->popupItem();
86                 break;
87             }
88             obj = obj->parent();
89         }
90         if (QWindow *renderWindow = QQuickRenderControl::renderWindowFor(qobject_cast<QQuickWindow *>(obj)))
91             obj = renderWindow;
92         return obj && obj == QGuiApplication::focusWindow() && !isBlockedByPopup(item);
93     default:
94         return false;
95     }
96 }
97 
98 QT_END_NAMESPACE
99