From ae804357d884835946b37ee1a980c54ff929618c Mon Sep 17 00:00:00 2001 From: Brad Davis Date: Thu, 23 Apr 2015 14:10:54 -0700 Subject: [PATCH 1/2] Attempting to fix backspace issue in new UI --- interface/src/Application.cpp | 5 +++ libraries/render-utils/src/OffscreenUi.cpp | 44 +++++++++++++++++++--- libraries/render-utils/src/OffscreenUi.h | 1 + 3 files changed, 45 insertions(+), 5 deletions(-) diff --git a/interface/src/Application.cpp b/interface/src/Application.cpp index 1df771f127..9ae54ac83e 100644 --- a/interface/src/Application.cpp +++ b/interface/src/Application.cpp @@ -1028,6 +1028,11 @@ bool Application::event(QEvent* event) { bool Application::eventFilter(QObject* object, QEvent* event) { if (event->type() == QEvent::ShortcutOverride) { + if (DependencyManager::get()->shouldSwallowShortcut(event)) { + event->accept(); + return true; + } + // Filter out captured keys before they're used for shortcut actions. if (_controllerScriptingInterface.isKeyCaptured(static_cast(event))) { event->accept(); diff --git a/libraries/render-utils/src/OffscreenUi.cpp b/libraries/render-utils/src/OffscreenUi.cpp index 969d3052cd..837affab59 100644 --- a/libraries/render-utils/src/OffscreenUi.cpp +++ b/libraries/render-utils/src/OffscreenUi.cpp @@ -89,7 +89,15 @@ void OffscreenUi::create(QOpenGLContext* shareContext) { // is needed too). connect(_renderControl, &QQuickRenderControl::renderRequested, this, &OffscreenUi::requestRender); connect(_renderControl, &QQuickRenderControl::sceneChanged, this, &OffscreenUi::requestUpdate); - _quickWindow->focusObject(); + +#ifdef DEBUG + connect(_quickWindow, &QQuickWindow::focusObjectChanged, [this]{ + qDebug() << "New focus item " << _quickWindow->focusObject(); + }); + connect(_quickWindow, &QQuickWindow::activeFocusItemChanged, [this] { + qDebug() << "New active focus item " << _quickWindow->activeFocusItem(); + }); +#endif _qmlComponent = new QQmlComponent(_qmlEngine); // Initialize the render control and our OpenGL resources. @@ -257,6 +265,24 @@ QPointF OffscreenUi::mapWindowToUi(const QPointF& sourcePosition, QObject* sourc return QPointF(offscreenPosition.x, offscreenPosition.y); } +// This hack allows the QML UI to work with keys that are also bound as +// shortcuts at the application level. However, it seems as though the +// bound actions are still getting triggered. At least for backspace. +// Not sure why. +// +// However, the problem may go away once we switch to the new menu system, +// so I think it's OK for the time being. +bool OffscreenUi::shouldSwallowShortcut(QEvent * event) { + Q_ASSERT(event->type() == QEvent::ShortcutOverride); + QObject * focusObject = _quickWindow->focusObject(); + if (focusObject != _quickWindow && focusObject != _rootItem) { + //qDebug() << "Swallowed shortcut " << static_cast(event)->key(); + event->accept(); + return true; + } + return false; +} + /////////////////////////////////////////////////////// // // Event handling customization @@ -268,11 +294,17 @@ bool OffscreenUi::eventFilter(QObject* originalDestination, QEvent* event) { return false; } + +#ifdef DEBUG // Don't intercept our own events, or we enter an infinite recursion - if (originalDestination == _quickWindow) { - return false; + QObject * recurseTest = originalDestination; + while (recurseTest) { + Q_ASSERT(recurseTest != _rootItem && recurseTest != _quickWindow); + recurseTest = recurseTest->parent(); } - +#endif + + switch (event->type()) { case QEvent::Resize: { QResizeEvent* resizeEvent = static_cast(event); @@ -280,12 +312,14 @@ bool OffscreenUi::eventFilter(QObject* originalDestination, QEvent* event) { if (widget) { this->resize(resizeEvent->size()); } - return false; + break; } case QEvent::KeyPress: case QEvent::KeyRelease: { event->ignore(); + //if (_quickWindow->activeFocusItem()) { + // _quickWindow->sendEvent(_quickWindow->activeFocusItem(), event); if (QCoreApplication::sendEvent(_quickWindow, event)) { return event->isAccepted(); } diff --git a/libraries/render-utils/src/OffscreenUi.h b/libraries/render-utils/src/OffscreenUi.h index d86f99554e..c64d0d833c 100644 --- a/libraries/render-utils/src/OffscreenUi.h +++ b/libraries/render-utils/src/OffscreenUi.h @@ -73,6 +73,7 @@ public: void resume(); bool isPaused() const; void setProxyWindow(QWindow* window); + bool shouldSwallowShortcut(QEvent* event); QPointF mapWindowToUi(const QPointF& sourcePosition, QObject* sourceObject); virtual bool eventFilter(QObject* originalDestination, QEvent* event); void setMouseTranslator(MouseTranslator mouseTranslator) { From 0a9398808f2e140f77e4d631fecf78c517e9aa42 Mon Sep 17 00:00:00 2001 From: Brad Davis Date: Fri, 24 Apr 2015 13:25:30 -0700 Subject: [PATCH 2/2] CR comments --- libraries/render-utils/src/OffscreenUi.cpp | 2 -- 1 file changed, 2 deletions(-) diff --git a/libraries/render-utils/src/OffscreenUi.cpp b/libraries/render-utils/src/OffscreenUi.cpp index 837affab59..ac78317703 100644 --- a/libraries/render-utils/src/OffscreenUi.cpp +++ b/libraries/render-utils/src/OffscreenUi.cpp @@ -318,8 +318,6 @@ bool OffscreenUi::eventFilter(QObject* originalDestination, QEvent* event) { case QEvent::KeyPress: case QEvent::KeyRelease: { event->ignore(); - //if (_quickWindow->activeFocusItem()) { - // _quickWindow->sendEvent(_quickWindow->activeFocusItem(), event); if (QCoreApplication::sendEvent(_quickWindow, event)) { return event->isAccepted(); }