| 1 | // Copyright (C) 2014 Klaralvdalens Datakonsult AB (KDAB). |
|---|---|
| 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 "assignkeyboardfocusjob_p.h" |
| 5 | #include <Qt3DCore/private/qaspectmanager_p.h> |
| 6 | #include <Qt3DInput/qkeyboardhandler.h> |
| 7 | #include <Qt3DInput/private/inputhandler_p.h> |
| 8 | #include <Qt3DInput/private/inputmanagers_p.h> |
| 9 | #include <Qt3DInput/private/job_common_p.h> |
| 10 | #include <Qt3DInput/private/keyboarddevice_p.h> |
| 11 | #include <Qt3DInput/private/keyboardhandler_p.h> |
| 12 | |
| 13 | QT_BEGIN_NAMESPACE |
| 14 | |
| 15 | namespace Qt3DInput { |
| 16 | namespace Input { |
| 17 | |
| 18 | class AssignKeyboardFocusJobPrivate : public Qt3DCore::QAspectJobPrivate |
| 19 | { |
| 20 | public: |
| 21 | AssignKeyboardFocusJobPrivate() { } |
| 22 | ~AssignKeyboardFocusJobPrivate() override { } |
| 23 | |
| 24 | void postFrame(Qt3DCore::QAspectManager *manager) override; |
| 25 | |
| 26 | QList<QPair<Qt3DCore::QNodeId, bool>> updates; |
| 27 | }; |
| 28 | |
| 29 | AssignKeyboardFocusJob::AssignKeyboardFocusJob(Qt3DCore::QNodeId keyboardDevice) |
| 30 | : QAspectJob(*new AssignKeyboardFocusJobPrivate()) |
| 31 | , m_inputHandler(nullptr) |
| 32 | , m_keyboardDevice(keyboardDevice) |
| 33 | { |
| 34 | SET_JOB_RUN_STAT_TYPE(this, JobTypes::AssignKeyboardFocus, 0) |
| 35 | } |
| 36 | |
| 37 | void AssignKeyboardFocusJob::setInputHandler(InputHandler *handler) |
| 38 | { |
| 39 | m_inputHandler = handler; |
| 40 | } |
| 41 | |
| 42 | void AssignKeyboardFocusJob::run() |
| 43 | { |
| 44 | Q_D(AssignKeyboardFocusJob); |
| 45 | |
| 46 | KeyboardDevice *keyboardDevice = m_inputHandler->keyboardDeviceManager()->lookupResource(id: m_keyboardDevice); |
| 47 | const auto handles = m_inputHandler->keyboardInputManager()->activeHandles(); |
| 48 | d->updates.reserve(asize: handles.size()); |
| 49 | |
| 50 | for (const HKeyboardHandler &handle : handles) { |
| 51 | KeyboardHandler *input = m_inputHandler->keyboardInputManager()->data(handle); |
| 52 | Q_ASSERT(input); |
| 53 | if (input->keyboardDevice() == m_keyboardDevice) { |
| 54 | bool hasFocus = input->peerId() == keyboardDevice->lastKeyboardInputRequester(); |
| 55 | input->setFocus(hasFocus); |
| 56 | d->updates.push_back(t: {input->peerId(), hasFocus}); |
| 57 | if (hasFocus) |
| 58 | keyboardDevice->setCurrentFocusItem(input->peerId()); |
| 59 | } |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | void AssignKeyboardFocusJobPrivate::postFrame(Qt3DCore::QAspectManager *manager) |
| 64 | { |
| 65 | for (const auto &data: std::as_const(t&: updates)) { |
| 66 | QKeyboardHandler *node = qobject_cast<QKeyboardHandler *>(object: manager->lookupNode(id: data.first)); |
| 67 | if (!node) |
| 68 | continue; |
| 69 | |
| 70 | const bool b = node->blockNotifications(block: true); |
| 71 | node->setFocus(data.second); |
| 72 | node->blockNotifications(block: b); |
| 73 | } |
| 74 | |
| 75 | updates.clear(); |
| 76 | } |
| 77 | |
| 78 | } // namespace Input |
| 79 | } // namespace Qt3DInput |
| 80 | |
| 81 | QT_END_NAMESPACE |
| 82 |
