| 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 "scenemanager_p.h" |
| 5 | #include <Qt3DCore/private/vector_helper_p.h> |
| 6 | |
| 7 | QT_BEGIN_NAMESPACE |
| 8 | |
| 9 | namespace Qt3DRender { |
| 10 | namespace Render { |
| 11 | |
| 12 | SceneManager::SceneManager() |
| 13 | : Qt3DCore::QResourceManager<Scene, |
| 14 | Qt3DCore::QNodeId, |
| 15 | Qt3DCore::ObjectLevelLockingPolicy>() |
| 16 | , m_service(nullptr) |
| 17 | { |
| 18 | } |
| 19 | |
| 20 | SceneManager::~SceneManager() |
| 21 | { |
| 22 | } |
| 23 | |
| 24 | void SceneManager::setDownloadService(Qt3DCore::QDownloadHelperService *service) |
| 25 | { |
| 26 | m_service = service; |
| 27 | } |
| 28 | |
| 29 | void SceneManager::addSceneData(const QUrl &source, |
| 30 | Qt3DCore::QNodeId sceneUuid, |
| 31 | const QByteArray &data) |
| 32 | { |
| 33 | LoadSceneJobPtr newJob(new LoadSceneJob(source, sceneUuid)); |
| 34 | |
| 35 | if (!data.isEmpty()) |
| 36 | newJob->setData(data); |
| 37 | |
| 38 | // We cannot run two jobs that use the same scene loader plugin |
| 39 | // in two different threads at the same time |
| 40 | if (!m_pendingJobs.empty()) |
| 41 | newJob->addDependency(dependency: m_pendingJobs.back()); |
| 42 | |
| 43 | m_pendingJobs.push_back(x: newJob); |
| 44 | } |
| 45 | |
| 46 | std::vector<LoadSceneJobPtr> SceneManager::takePendingSceneLoaderJobs() |
| 47 | { |
| 48 | return Qt3DCore::moveAndClear(data&: m_pendingJobs); |
| 49 | } |
| 50 | |
| 51 | void SceneManager::startSceneDownload(const QUrl &source, Qt3DCore::QNodeId sceneUuid) |
| 52 | { |
| 53 | if (!m_service) |
| 54 | return; |
| 55 | SceneDownloaderPtr request = SceneDownloaderPtr::create(arguments: source, arguments&: sceneUuid, arguments: this); |
| 56 | m_pendingDownloads.push_back(x: request); |
| 57 | m_service->submitRequest(request); |
| 58 | } |
| 59 | |
| 60 | void SceneManager::clearSceneDownload(SceneDownloader *downloader) |
| 61 | { |
| 62 | for (auto it = m_pendingDownloads.begin(); it != m_pendingDownloads.end(); ++it) { |
| 63 | if ((*it).data() == downloader) { |
| 64 | m_pendingDownloads.erase(position: it); |
| 65 | return; |
| 66 | } |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | |
| 71 | SceneDownloader::SceneDownloader(const QUrl &source, Qt3DCore::QNodeId sceneComponent, SceneManager *manager) |
| 72 | : Qt3DCore::QDownloadRequest(source) |
| 73 | , m_sceneComponent(sceneComponent) |
| 74 | , m_manager(manager) |
| 75 | { |
| 76 | |
| 77 | } |
| 78 | |
| 79 | void SceneDownloader::onCompleted() |
| 80 | { |
| 81 | if (!m_manager) |
| 82 | return; |
| 83 | if (succeeded()) |
| 84 | m_manager->addSceneData(source: url(), sceneUuid: m_sceneComponent, data: m_data); |
| 85 | else |
| 86 | qWarning() << "Failed to download scene at" << url(); |
| 87 | m_manager->clearSceneDownload(downloader: this); |
| 88 | } |
| 89 | |
| 90 | } // namespace Render |
| 91 | } // namespace Qt3DRender |
| 92 | |
| 93 | QT_END_NAMESPACE |
| 94 | |