1 | // Copyright (C) 2016 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 "loadproxydevicejob_p.h" |
5 | #include <Qt3DCore/private/qaspectmanager_p.h> |
6 | #include <Qt3DInput/private/qabstractphysicaldeviceproxy_p.h> |
7 | #include <Qt3DInput/private/qabstractphysicaldeviceproxy_p_p.h> |
8 | #include <Qt3DInput/private/inputhandler_p.h> |
9 | #include <Qt3DInput/private/inputmanagers_p.h> |
10 | #include <Qt3DInput/private/job_common_p.h> |
11 | |
12 | QT_BEGIN_NAMESPACE |
13 | |
14 | namespace Qt3DInput { |
15 | namespace Input { |
16 | |
17 | class LoadProxyDeviceJobPrivate : public Qt3DCore::QAspectJobPrivate |
18 | { |
19 | public: |
20 | LoadProxyDeviceJobPrivate() { } |
21 | ~LoadProxyDeviceJobPrivate() override { } |
22 | |
23 | void postFrame(Qt3DCore::QAspectManager *manager) override; |
24 | |
25 | QList<QPair<Qt3DCore::QNodeId, QAbstractPhysicalDevice *>> updates; |
26 | }; |
27 | |
28 | LoadProxyDeviceJob::LoadProxyDeviceJob() |
29 | : Qt3DCore::QAspectJob(*new LoadProxyDeviceJobPrivate) |
30 | , m_inputHandler(nullptr) |
31 | { |
32 | SET_JOB_RUN_STAT_TYPE(this, JobTypes::DeviceProxyLoading, 0) |
33 | } |
34 | |
35 | LoadProxyDeviceJob::~LoadProxyDeviceJob() |
36 | { |
37 | } |
38 | |
39 | void LoadProxyDeviceJob::setProxiesToLoad(const QList<Qt3DCore::QNodeId> &wrappers) |
40 | { |
41 | m_proxies = wrappers; |
42 | } |
43 | |
44 | void LoadProxyDeviceJob::setInputHandler(InputHandler *handler) |
45 | { |
46 | m_inputHandler = handler; |
47 | } |
48 | |
49 | InputHandler *LoadProxyDeviceJob::inputHandler() const |
50 | { |
51 | return m_inputHandler; |
52 | } |
53 | |
54 | QList<Qt3DCore::QNodeId> LoadProxyDeviceJob::proxies() const |
55 | { |
56 | return m_proxies; |
57 | } |
58 | |
59 | void LoadProxyDeviceJob::run() |
60 | { |
61 | Q_D(LoadProxyDeviceJob); |
62 | d->updates.reserve(asize: m_proxies.size()); |
63 | |
64 | Q_ASSERT(m_inputHandler); |
65 | for (const Qt3DCore::QNodeId &id : std::as_const(t&: m_proxies)) { |
66 | PhysicalDeviceProxy *proxy = m_inputHandler->physicalDeviceProxyManager()->lookupResource(id); |
67 | QAbstractPhysicalDevice *device = m_inputHandler->createPhysicalDevice(name: proxy->deviceName()); |
68 | if (device != nullptr) |
69 | proxy->setDevice(device); |
70 | } |
71 | } |
72 | |
73 | void LoadProxyDeviceJobPrivate::postFrame(Qt3DCore::QAspectManager *manager) |
74 | { |
75 | for (const auto &res : std::as_const(t&: updates)) { |
76 | QAbstractPhysicalDeviceProxy *node = qobject_cast<QAbstractPhysicalDeviceProxy *>(object: manager->lookupNode(id: res.first)); |
77 | if (!node) |
78 | continue; |
79 | |
80 | auto *device = res.second; |
81 | QAbstractPhysicalDeviceProxyPrivate *dnode = static_cast<QAbstractPhysicalDeviceProxyPrivate *>(QAbstractPhysicalDeviceProxyPrivate::get(q: node)); |
82 | QAbstractPhysicalDevice *oldDevice = dnode->m_device; |
83 | dnode->setDevice(device); |
84 | // Delete the old device if it existed |
85 | delete oldDevice; |
86 | } |
87 | |
88 | updates.clear(); |
89 | } |
90 | |
91 | |
92 | } // namespace Input |
93 | } // namespace Qt3DInput |
94 | |
95 | QT_END_NAMESPACE |
96 |