1 | // Copyright (C) 2017 The Qt Company Ltd. |
2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only |
3 | |
4 | #include "qquickshortcutcontext_p_p.h" |
5 | #include "qquickoverlay_p_p.h" |
6 | #include "qquicktooltip_p.h" |
7 | #include <QtQmlModels/private/qtqmlmodels-config_p.h> |
8 | #if QT_CONFIG(qml_object_model) |
9 | #include "qquickmenu_p.h" |
10 | #include "qquickmenu_p_p.h" |
11 | #endif |
12 | #include "qquickpopup_p.h" |
13 | |
14 | #include <QtCore/qloggingcategory.h> |
15 | #include <QtGui/qguiapplication.h> |
16 | #include <QtGui/private/qguiapplication_p.h> |
17 | #include <QtQuick/qquickrendercontrol.h> |
18 | #include <QtQuickTemplates2/private/qquickpopupwindow_p_p.h> |
19 | |
20 | QT_BEGIN_NAMESPACE |
21 | |
22 | Q_LOGGING_CATEGORY(lcContextMatcher, "qt.quick.controls.shortcutcontext.matcher" ) |
23 | |
24 | static bool (QQuickItem *item) |
25 | { |
26 | if (!item || !item->window()) |
27 | return false; |
28 | |
29 | QQuickOverlay *overlay = QQuickOverlay::overlay(window: item->window()); |
30 | auto = QQuickOverlayPrivate::get(overlay)->stackingOrderPopups(); |
31 | |
32 | for (QWindow * : QGuiApplicationPrivate::popup_list) { |
33 | if (QQuickPopupWindow * = qobject_cast<QQuickPopupWindow *>(object: popupWindow); |
34 | quickPopupWindow && quickPopupWindow->popup()) |
35 | popups += quickPopupWindow->popup(); |
36 | } |
37 | |
38 | for (QQuickPopup * : std::as_const(t&: popups)) { |
39 | if (qobject_cast<QQuickToolTip *>(object: popup)) |
40 | continue; // ignore tooltips (QTBUG-60492) |
41 | if (popup->isModal() || popup->closePolicy() & QQuickPopup::CloseOnEscape) { |
42 | qCDebug(lcContextMatcher) << popup << "is modal or has a CloseOnEscape policy;" |
43 | << "if one of the following is true," << item |
44 | << "will be blocked by it:" << (item != popup->popupItem()) |
45 | << !popup->popupItem()->isAncestorOf(child: item); |
46 | return item != popup->popupItem() && !popup->popupItem()->isAncestorOf(child: item); |
47 | } |
48 | } |
49 | return false; |
50 | } |
51 | |
52 | bool QQuickShortcutContext::matcher(QObject *obj, Qt::ShortcutContext context) |
53 | { |
54 | QQuickItem *item = nullptr; |
55 | switch (context) { |
56 | case Qt::ApplicationShortcut: |
57 | return true; |
58 | case Qt::WindowShortcut: |
59 | while (obj && !obj->isWindowType()) { |
60 | item = qobject_cast<QQuickItem *>(o: obj); |
61 | if (item && item->window()) { |
62 | obj = item->window(); |
63 | break; |
64 | } else if (QQuickPopup * = qobject_cast<QQuickPopup *>(object: obj)) { |
65 | obj = popup->window(); |
66 | item = popup->popupItem(); |
67 | |
68 | #if QT_CONFIG(qml_object_model) |
69 | if (!obj) { |
70 | // The popup has no associated window (yet). However, sub-menus, |
71 | // unlike top-level menus, will not have an associated window |
72 | // until their parent menu is opened. So, check if this is a sub-menu |
73 | // so that actions within it can grab shortcuts. |
74 | if (auto * = qobject_cast<QQuickMenu *>(object: popup)) { |
75 | auto = QQuickMenuPrivate::get(menu)->parentMenu; |
76 | while (!obj && parentMenu) |
77 | obj = parentMenu->window(); |
78 | } |
79 | } |
80 | #endif |
81 | break; |
82 | } |
83 | obj = obj->parent(); |
84 | } |
85 | if (QWindow *renderWindow = QQuickRenderControl::renderWindowFor(win: qobject_cast<QQuickWindow *>(object: obj))) |
86 | obj = renderWindow; |
87 | qCDebug(lcContextMatcher) << "obj" << obj << "item" << item << "focusWindow" << QGuiApplication::focusWindow() |
88 | << "!isBlockedByPopup(item)" << !isBlockedByPopup(item); |
89 | return obj && qobject_cast<QWindow*>(o: obj)->isActive() && !isBlockedByPopup(item); |
90 | default: |
91 | return false; |
92 | } |
93 | } |
94 | |
95 | QT_END_NAMESPACE |
96 | |