| 1 | /**************************************************************************** | 
|---|
| 2 | ** | 
|---|
| 3 | ** Copyright (C) 2016 The Qt Company Ltd. | 
|---|
| 4 | ** Contact: https://www.qt.io/licensing/ | 
|---|
| 5 | ** | 
|---|
| 6 | ** This file is part of the QtWidgets module of the Qt Toolkit. | 
|---|
| 7 | ** | 
|---|
| 8 | ** $QT_BEGIN_LICENSE:LGPL$ | 
|---|
| 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 https://www.qt.io/terms-conditions. For further | 
|---|
| 15 | ** information use the contact form at https://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.LGPL3 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-3.0.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 (at your option) the GNU General | 
|---|
| 28 | ** Public license version 3 or any later version approved by the KDE Free | 
|---|
| 29 | ** Qt Foundation. The licenses are as published by the Free Software | 
|---|
| 30 | ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 | 
|---|
| 31 | ** included in the packaging of this file. Please review the following | 
|---|
| 32 | ** information to ensure the GNU General Public License requirements will | 
|---|
| 33 | ** be met: https://www.gnu.org/licenses/gpl-2.0.html and | 
|---|
| 34 | ** https://www.gnu.org/licenses/gpl-3.0.html. | 
|---|
| 35 | ** | 
|---|
| 36 | ** $QT_END_LICENSE$ | 
|---|
| 37 | ** | 
|---|
| 38 | ****************************************************************************/ | 
|---|
| 39 |  | 
|---|
| 40 | #include "qshortcut.h" | 
|---|
| 41 | #include "private/qshortcut_p.h" | 
|---|
| 42 |  | 
|---|
| 43 | #include "private/qwidget_p.h" | 
|---|
| 44 |  | 
|---|
| 45 | #include <qevent.h> | 
|---|
| 46 | #if QT_CONFIG(whatsthis) | 
|---|
| 47 | #include <qwhatsthis.h> | 
|---|
| 48 | #endif | 
|---|
| 49 | #if QT_CONFIG(menu) | 
|---|
| 50 | #include <qmenu.h> | 
|---|
| 51 | #endif | 
|---|
| 52 | #if QT_CONFIG(menubar) | 
|---|
| 53 | #include <qmenubar.h> | 
|---|
| 54 | #endif | 
|---|
| 55 | #include <qapplication.h> | 
|---|
| 56 | #include <private/qapplication_p.h> | 
|---|
| 57 | #include <private/qshortcutmap_p.h> | 
|---|
| 58 | #if QT_CONFIG(action) | 
|---|
| 59 | #  include <private/qaction_p.h> | 
|---|
| 60 | #endif | 
|---|
| 61 | #include <private/qwidgetwindow_p.h> | 
|---|
| 62 | #include <qpa/qplatformmenu.h> | 
|---|
| 63 |  | 
|---|
| 64 | QT_BEGIN_NAMESPACE | 
|---|
| 65 |  | 
|---|
| 66 | static bool correctWidgetContext(Qt::ShortcutContext context, QWidget *w, QWidget *active_window); | 
|---|
| 67 | #if QT_CONFIG(graphicsview) | 
|---|
| 68 | static bool correctGraphicsWidgetContext(Qt::ShortcutContext context, QGraphicsWidget *w, QWidget *active_window); | 
|---|
| 69 | #endif | 
|---|
| 70 | #if QT_CONFIG(action) | 
|---|
| 71 | static bool correctActionContext(Qt::ShortcutContext context, QAction *a, QWidget *active_window); | 
|---|
| 72 | #endif | 
|---|
| 73 |  | 
|---|
| 74 |  | 
|---|
| 75 | /*! \internal | 
|---|
| 76 | Returns \c true if the widget \a w is a logical sub window of the current | 
|---|
| 77 | top-level widget. | 
|---|
| 78 | */ | 
|---|
| 79 | bool qWidgetShortcutContextMatcher(QObject *object, Qt::ShortcutContext context) | 
|---|
| 80 | { | 
|---|
| 81 | Q_ASSERT_X(object, "QShortcutMap", "Shortcut has no owner. Illegal map state!"); | 
|---|
| 82 |  | 
|---|
| 83 | QWidget *active_window = QApplication::activeWindow(); | 
|---|
| 84 |  | 
|---|
| 85 | // popups do not become the active window, | 
|---|
| 86 | // so we fake it here to get the correct context | 
|---|
| 87 | // for the shortcut system. | 
|---|
| 88 | if (QApplication::activePopupWidget()) | 
|---|
| 89 | active_window = QApplication::activePopupWidget(); | 
|---|
| 90 |  | 
|---|
| 91 | if (!active_window) { | 
|---|
| 92 | QWindow *qwindow = QGuiApplication::focusWindow(); | 
|---|
| 93 | if (qwindow && qwindow->isActive()) { | 
|---|
| 94 | while (qwindow) { | 
|---|
| 95 | if (auto widgetWindow = qobject_cast<QWidgetWindow *>(qwindow)) { | 
|---|
| 96 | active_window = widgetWindow->widget(); | 
|---|
| 97 | break; | 
|---|
| 98 | } | 
|---|
| 99 | qwindow = qwindow->parent(); | 
|---|
| 100 | } | 
|---|
| 101 | } | 
|---|
| 102 | } | 
|---|
| 103 |  | 
|---|
| 104 | if (!active_window) | 
|---|
| 105 | return false; | 
|---|
| 106 |  | 
|---|
| 107 | #if QT_CONFIG(action) | 
|---|
| 108 | if (auto a = qobject_cast<QAction *>(object)) | 
|---|
| 109 | return correctActionContext(context, a, active_window); | 
|---|
| 110 | #endif | 
|---|
| 111 |  | 
|---|
| 112 | #if QT_CONFIG(graphicsview) | 
|---|
| 113 | if (auto gw = qobject_cast<QGraphicsWidget *>(object)) | 
|---|
| 114 | return correctGraphicsWidgetContext(context, gw, active_window); | 
|---|
| 115 | #endif | 
|---|
| 116 |  | 
|---|
| 117 | auto w = qobject_cast<QWidget *>(object); | 
|---|
| 118 | if (!w) { | 
|---|
| 119 | if (auto s = qobject_cast<QShortcut *>(object)) | 
|---|
| 120 | w = s->parentWidget(); | 
|---|
| 121 | } | 
|---|
| 122 |  | 
|---|
| 123 | if (!w) { | 
|---|
| 124 | auto qwindow = qobject_cast<QWindow *>(object); | 
|---|
| 125 | while (qwindow) { | 
|---|
| 126 | if (auto widget_window = qobject_cast<QWidgetWindow *>(qwindow)) { | 
|---|
| 127 | w = widget_window->widget(); | 
|---|
| 128 | break; | 
|---|
| 129 | } | 
|---|
| 130 | qwindow = qwindow->parent(); | 
|---|
| 131 | } | 
|---|
| 132 | } | 
|---|
| 133 |  | 
|---|
| 134 | if (!w) | 
|---|
| 135 | return false; | 
|---|
| 136 |  | 
|---|
| 137 | return correctWidgetContext(context, w, active_window); | 
|---|
| 138 | } | 
|---|
| 139 |  | 
|---|
| 140 | static bool correctWidgetContext(Qt::ShortcutContext context, QWidget *w, QWidget *active_window) | 
|---|
| 141 | { | 
|---|
| 142 | bool visible = w->isVisible(); | 
|---|
| 143 | #if QT_CONFIG(menubar) | 
|---|
| 144 | if (auto  = qobject_cast<QMenuBar *>(w)) { | 
|---|
| 145 | if (auto *pmb = menuBar->platformMenuBar()) { | 
|---|
| 146 | if (menuBar->parentWidget()) { | 
|---|
| 147 | visible = true; | 
|---|
| 148 | } else { | 
|---|
| 149 | if (auto *ww = qobject_cast<QWidgetWindow *>(pmb->parentWindow())) | 
|---|
| 150 | w = ww->widget(); // Good enough since we only care about the window | 
|---|
| 151 | else | 
|---|
| 152 | return false; // This is not a QWidget window. We won't deliver | 
|---|
| 153 | } | 
|---|
| 154 | } | 
|---|
| 155 | } | 
|---|
| 156 | #endif | 
|---|
| 157 |  | 
|---|
| 158 | if (!visible || !w->isEnabled()) | 
|---|
| 159 | return false; | 
|---|
| 160 |  | 
|---|
| 161 | if (context == Qt::ApplicationShortcut) | 
|---|
| 162 | return QApplicationPrivate::tryModalHelper(w, nullptr); // true, unless w is shadowed by a modal dialog | 
|---|
| 163 |  | 
|---|
| 164 | if (context == Qt::WidgetShortcut) | 
|---|
| 165 | return w == QApplication::focusWidget(); | 
|---|
| 166 |  | 
|---|
| 167 | if (context == Qt::WidgetWithChildrenShortcut) { | 
|---|
| 168 | const QWidget *tw = QApplication::focusWidget(); | 
|---|
| 169 | while (tw && tw != w && (tw->windowType() == Qt::Widget || tw->windowType() == Qt::Popup || tw->windowType() == Qt::SubWindow)) | 
|---|
| 170 | tw = tw->parentWidget(); | 
|---|
| 171 | return tw == w; | 
|---|
| 172 | } | 
|---|
| 173 |  | 
|---|
| 174 | // Below is Qt::WindowShortcut context | 
|---|
| 175 | QWidget *tlw = w->window(); | 
|---|
| 176 | #if QT_CONFIG(graphicsview) | 
|---|
| 177 | if (auto topData = static_cast<QWidgetPrivate *>(QObjectPrivate::get(tlw))->extra.get()) { | 
|---|
| 178 | if (topData->proxyWidget) { | 
|---|
| 179 | bool res = correctGraphicsWidgetContext(context, topData->proxyWidget, active_window); | 
|---|
| 180 | return res; | 
|---|
| 181 | } | 
|---|
| 182 | } | 
|---|
| 183 | #endif | 
|---|
| 184 |  | 
|---|
| 185 | if (active_window && active_window != tlw) { | 
|---|
| 186 | /* if a floating tool window is active, keep shortcuts on the parent working. | 
|---|
| 187 | * and if a popup window is active (f.ex a completer), keep shortcuts on the | 
|---|
| 188 | * focus proxy working */ | 
|---|
| 189 | if (active_window->windowType() == Qt::Tool && active_window->parentWidget()) { | 
|---|
| 190 | active_window = active_window->parentWidget()->window(); | 
|---|
| 191 | } else if (active_window->windowType() == Qt::Popup && active_window->focusProxy()) { | 
|---|
| 192 | active_window = active_window->focusProxy()->window(); | 
|---|
| 193 | } | 
|---|
| 194 | } | 
|---|
| 195 |  | 
|---|
| 196 | if (active_window != tlw) { | 
|---|
| 197 | #if QT_CONFIG(menubar) | 
|---|
| 198 | // If the tlw is a QMenuBar then we allow it to proceed as this indicates that | 
|---|
| 199 | // the QMenuBar is a parentless one and is therefore used for multiple top level | 
|---|
| 200 | // windows in the application. This is common on macOS platforms for example. | 
|---|
| 201 | if (!qobject_cast<QMenuBar *>(tlw)) | 
|---|
| 202 | #endif | 
|---|
| 203 | return false; | 
|---|
| 204 | } | 
|---|
| 205 |  | 
|---|
| 206 | /* if we live in a MDI subwindow, ignore the event if we are | 
|---|
| 207 | not the active document window */ | 
|---|
| 208 | const QWidget* sw = w; | 
|---|
| 209 | while (sw && !(sw->windowType() == Qt::SubWindow) && !sw->isWindow()) | 
|---|
| 210 | sw = sw->parentWidget(); | 
|---|
| 211 | if (sw && (sw->windowType() == Qt::SubWindow)) { | 
|---|
| 212 | QWidget *focus_widget = QApplication::focusWidget(); | 
|---|
| 213 | while (focus_widget && focus_widget != sw) | 
|---|
| 214 | focus_widget = focus_widget->parentWidget(); | 
|---|
| 215 | return sw == focus_widget; | 
|---|
| 216 | } | 
|---|
| 217 |  | 
|---|
| 218 | #if defined(DEBUG_QSHORTCUTMAP) | 
|---|
| 219 | qDebug().nospace() << "..true [Pass-through]"; | 
|---|
| 220 | #endif | 
|---|
| 221 | return QApplicationPrivate::tryModalHelper(w, nullptr); | 
|---|
| 222 | } | 
|---|
| 223 |  | 
|---|
| 224 | #if QT_CONFIG(graphicsview) | 
|---|
| 225 | static bool correctGraphicsWidgetContext(Qt::ShortcutContext context, QGraphicsWidget *w, QWidget *active_window) | 
|---|
| 226 | { | 
|---|
| 227 | bool visible = w->isVisible(); | 
|---|
| 228 | #if defined(Q_OS_DARWIN) && QT_CONFIG(menubar) | 
|---|
| 229 | if (!QCoreApplication::testAttribute(Qt::AA_DontUseNativeMenuBar) && qobject_cast<QMenuBar *>(w)) | 
|---|
| 230 | visible = true; | 
|---|
| 231 | #endif | 
|---|
| 232 |  | 
|---|
| 233 | if (!visible || !w->isEnabled() || !w->scene()) | 
|---|
| 234 | return false; | 
|---|
| 235 |  | 
|---|
| 236 | if (context == Qt::ApplicationShortcut) { | 
|---|
| 237 | // Applicationwide shortcuts are always reachable unless their owner | 
|---|
| 238 | // is shadowed by modality. In QGV there's no modality concept, but we | 
|---|
| 239 | // must still check if all views are shadowed. | 
|---|
| 240 | const auto &views = w->scene()->views(); | 
|---|
| 241 | for (auto view : views) { | 
|---|
| 242 | if (QApplicationPrivate::tryModalHelper(view, nullptr)) | 
|---|
| 243 | return true; | 
|---|
| 244 | } | 
|---|
| 245 | return false; | 
|---|
| 246 | } | 
|---|
| 247 |  | 
|---|
| 248 | if (context == Qt::WidgetShortcut) | 
|---|
| 249 | return static_cast<QGraphicsItem *>(w) == w->scene()->focusItem(); | 
|---|
| 250 |  | 
|---|
| 251 | if (context == Qt::WidgetWithChildrenShortcut) { | 
|---|
| 252 | const QGraphicsItem *ti = w->scene()->focusItem(); | 
|---|
| 253 | if (ti && ti->isWidget()) { | 
|---|
| 254 | const auto *tw = static_cast<const QGraphicsWidget *>(ti); | 
|---|
| 255 | while (tw && tw != w && (tw->windowType() == Qt::Widget || tw->windowType() == Qt::Popup)) | 
|---|
| 256 | tw = tw->parentWidget(); | 
|---|
| 257 | return tw == w; | 
|---|
| 258 | } | 
|---|
| 259 | return false; | 
|---|
| 260 | } | 
|---|
| 261 |  | 
|---|
| 262 | // Below is Qt::WindowShortcut context | 
|---|
| 263 |  | 
|---|
| 264 | // Find the active view (if any). | 
|---|
| 265 | const auto &views = w->scene()->views(); | 
|---|
| 266 | QGraphicsView *activeView = nullptr; | 
|---|
| 267 | for (auto view : views) { | 
|---|
| 268 | if (view->window() == active_window) { | 
|---|
| 269 | activeView = view; | 
|---|
| 270 | break; | 
|---|
| 271 | } | 
|---|
| 272 | } | 
|---|
| 273 | if (!activeView) | 
|---|
| 274 | return false; | 
|---|
| 275 |  | 
|---|
| 276 | // The shortcut is reachable if owned by a windowless widget, or if the | 
|---|
| 277 | // widget's window is the same as the focus item's window. | 
|---|
| 278 | QGraphicsWidget *a = w->scene()->activeWindow(); | 
|---|
| 279 | return !w->window() || a == w->window(); | 
|---|
| 280 | } | 
|---|
| 281 | #endif | 
|---|
| 282 |  | 
|---|
| 283 | #if QT_CONFIG(action) | 
|---|
| 284 | static bool correctActionContext(Qt::ShortcutContext context, QAction *a, QWidget *active_window) | 
|---|
| 285 | { | 
|---|
| 286 | const QObjectList associatedObjects = a->associatedObjects(); | 
|---|
| 287 | #if defined(DEBUG_QSHORTCUTMAP) | 
|---|
| 288 | if (associatedObjects.isEmpty()) | 
|---|
| 289 | qDebug() << a << "not connected to any widgets; won't trigger"; | 
|---|
| 290 | #endif | 
|---|
| 291 | for (auto object : associatedObjects) { | 
|---|
| 292 | #if QT_CONFIG(menu) | 
|---|
| 293 | if (auto  = qobject_cast<QMenu *>(object)) { | 
|---|
| 294 | #ifdef Q_OS_DARWIN | 
|---|
| 295 | // On Mac, menu item shortcuts are processed before reaching any window. | 
|---|
| 296 | // That means that if a menu action shortcut has not been already processed | 
|---|
| 297 | // (and reaches this point), then the menu item itself has been disabled. | 
|---|
| 298 | // This occurs at the QPA level on Mac, where we disable all the Cocoa menus | 
|---|
| 299 | // when showing a modal window. (Notice that only the QPA menu is disabled, | 
|---|
| 300 | // not the QMenu.) Since we can also reach this code by climbing the menu | 
|---|
| 301 | // hierarchy (see below), or when the shortcut is not a key-equivalent, we | 
|---|
| 302 | // need to check whether the QPA menu is actually disabled. | 
|---|
| 303 | // When there is no QPA menu, there will be no QCocoaMenuDelegate checking | 
|---|
| 304 | // for the actual shortcuts. We can then fallback to our own logic. | 
|---|
| 305 | QPlatformMenu *pm = menu->platformMenu(); | 
|---|
| 306 | if (pm && !pm->isEnabled()) | 
|---|
| 307 | continue; | 
|---|
| 308 | #endif | 
|---|
| 309 | QAction *a = menu->menuAction(); | 
|---|
| 310 | if (correctActionContext(context, a, active_window)) | 
|---|
| 311 | return true; | 
|---|
| 312 | } else | 
|---|
| 313 | #endif | 
|---|
| 314 | if (auto widget = qobject_cast<QWidget*>(object)) { | 
|---|
| 315 | if (correctWidgetContext(context, widget, active_window)) | 
|---|
| 316 | return true; | 
|---|
| 317 | } | 
|---|
| 318 | #if QT_CONFIG(graphicsview) | 
|---|
| 319 | else if (auto graphicsWidget = qobject_cast<QGraphicsWidget*>(object)) { | 
|---|
| 320 | if (correctGraphicsWidgetContext(context, graphicsWidget, active_window)) | 
|---|
| 321 | return true; | 
|---|
| 322 | } | 
|---|
| 323 | #endif | 
|---|
| 324 | } | 
|---|
| 325 |  | 
|---|
| 326 | return false; | 
|---|
| 327 | } | 
|---|
| 328 | #endif // QT_CONFIG(action) | 
|---|
| 329 |  | 
|---|
| 330 | class QtWidgetsShortcutPrivate : public QShortcutPrivate | 
|---|
| 331 | { | 
|---|
| 332 | Q_DECLARE_PUBLIC(QShortcut) | 
|---|
| 333 | public: | 
|---|
| 334 | QtWidgetsShortcutPrivate() = default; | 
|---|
| 335 |  | 
|---|
| 336 | QShortcutMap::ContextMatcher contextMatcher() const override | 
|---|
| 337 | { return qWidgetShortcutContextMatcher; } | 
|---|
| 338 |  | 
|---|
| 339 | bool handleWhatsThis() override; | 
|---|
| 340 | }; | 
|---|
| 341 |  | 
|---|
| 342 | bool QtWidgetsShortcutPrivate::handleWhatsThis() | 
|---|
| 343 | { | 
|---|
| 344 | #if QT_CONFIG(whatsthis) | 
|---|
| 345 | if (QWhatsThis::inWhatsThisMode()) { | 
|---|
| 346 | QWhatsThis::showText(QCursor::pos(), sc_whatsthis); | 
|---|
| 347 | return true; | 
|---|
| 348 | } | 
|---|
| 349 | #endif | 
|---|
| 350 | return false; | 
|---|
| 351 | } | 
|---|
| 352 |  | 
|---|
| 353 | QShortcutPrivate *QApplicationPrivate::createShortcutPrivate() const | 
|---|
| 354 | { | 
|---|
| 355 | return new QtWidgetsShortcutPrivate; | 
|---|
| 356 | } | 
|---|
| 357 |  | 
|---|
| 358 | QT_END_NAMESPACE | 
|---|
| 359 |  | 
|---|