diff --git a/Code/Tools/ProjectManager/Source/O3DEObjectDownloadController.cpp b/Code/Tools/ProjectManager/Source/O3DEObjectDownloadController.cpp new file mode 100644 index 0000000000..f3be0fd8e4 --- /dev/null +++ b/Code/Tools/ProjectManager/Source/O3DEObjectDownloadController.cpp @@ -0,0 +1,92 @@ +/* + * Copyright (c) Contributors to the Open 3D Engine Project. + * For complete copyright and license terms please see the LICENSE at the root of this distribution. + * + * SPDX-License-Identifier: Apache-2.0 OR MIT + * + */ + +#include +#include +#include + +#include +#include +#include + + +namespace O3DE::ProjectManager +{ + O3DEObjectDownloadController::O3DEObjectDownloadController(QWidget* parent) + : QObject() + , m_lastProgress(0) + , m_parent(parent) + { + m_worker = new O3DEObjectDownloadWorker(); + m_worker->moveToThread(&m_workerThread); + + connect(&m_workerThread, &QThread::started, m_worker, &O3DEObjectDownloadWorker::StartDownload); + connect(m_worker, &O3DEObjectDownloadWorker::Done, this, &O3DEObjectDownloadController::HandleResults); + connect(m_worker, &O3DEObjectDownloadWorker::UpdateProgress, this, &O3DEObjectDownloadController::UpdateUIProgress); + connect(this, &O3DEObjectDownloadController::StartGemDownload, m_worker, &O3DEObjectDownloadWorker::StartDownload); + } + + O3DEObjectDownloadController::~O3DEObjectDownloadController() + { + connect(&m_workerThread, &QThread::finished, m_worker, &O3DEObjectDownloadWorker::deleteLater); + m_workerThread.requestInterruption(); + m_workerThread.quit(); + m_workerThread.wait(); + } + + void O3DEObjectDownloadController::AddGemDownload(const QString& gemName) + { + m_gemNames.push_back(gemName); + if (m_gemNames.size() == 1) + { + m_worker->SetGemToDownload(m_gemNames[0], false); + m_workerThread.start(); + } + } + + void O3DEObjectDownloadController::Start() + { + + } + + void O3DEObjectDownloadController::UpdateUIProgress(int progress) + { + m_lastProgress = progress; + emit GemDownloadProgress(progress); + } + + void O3DEObjectDownloadController::HandleResults(const QString& result) + { + bool succeeded = true; + + if (!result.isEmpty()) + { + QMessageBox::critical(nullptr, tr("Gem download"), result); + succeeded = false; + } + + m_gemNames.erase(m_gemNames.begin()); + emit Done(succeeded); + + if (!m_gemNames.empty()) + { + emit StartGemDownload(m_gemNames[0]); + } + else + { + m_workerThread.quit(); + m_workerThread.wait(); + } + } + + void O3DEObjectDownloadController::HandleCancel() + { + m_workerThread.quit(); + emit Done(false); + } +} // namespace O3DE::ProjectManager diff --git a/Code/Tools/ProjectManager/Source/O3DEObjectDownloadController.h b/Code/Tools/ProjectManager/Source/O3DEObjectDownloadController.h new file mode 100644 index 0000000000..7cd9220f9f --- /dev/null +++ b/Code/Tools/ProjectManager/Source/O3DEObjectDownloadController.h @@ -0,0 +1,65 @@ +/* + * Copyright (c) Contributors to the Open 3D Engine Project. + * For complete copyright and license terms please see the LICENSE at the root of this distribution. + * + * SPDX-License-Identifier: Apache-2.0 OR MIT + * + */ +#pragma once + +#if !defined(Q_MOC_RUN) +#include +#include +#include +#endif + +QT_FORWARD_DECLARE_CLASS(QProcess) + +namespace O3DE::ProjectManager +{ + QT_FORWARD_DECLARE_CLASS(O3DEObjectDownloadWorker) + + class O3DEObjectDownloadController : public QObject + { + Q_OBJECT + + public: + explicit O3DEObjectDownloadController(QWidget* parent = nullptr); + ~O3DEObjectDownloadController(); + + void AddGemDownload(const QString& m_gemName); + + bool IsDownloadQueueEmpty() + { + return m_gemNames.empty(); + } + + const AZStd::vector& GetDownloadQueue() const + { + return m_gemNames; + } + + const QString& GetCurrentDownloadingGem() const + { + return m_gemNames[0]; + } + public slots: + void Start(); + void UpdateUIProgress(int progress); + void HandleResults(const QString& result); + void HandleCancel(); + + signals: + void StartGemDownload(const QString& gemName); + void Done(bool success = true); + void GemDownloadProgress(int percentage); + + private: + O3DEObjectDownloadWorker* m_worker; + QThread m_workerThread; + QWidget* m_parent; + AZStd::vector m_gemNames; + + int m_lastProgress; + }; +} // namespace O3DE::ProjectManager diff --git a/Code/Tools/ProjectManager/Source/O3DEObjectDownloadWorker.cpp b/Code/Tools/ProjectManager/Source/O3DEObjectDownloadWorker.cpp new file mode 100644 index 0000000000..3462600c40 --- /dev/null +++ b/Code/Tools/ProjectManager/Source/O3DEObjectDownloadWorker.cpp @@ -0,0 +1,52 @@ +/* + * Copyright (c) Contributors to the Open 3D Engine Project. + * For complete copyright and license terms please see the LICENSE at the root of this distribution. + * + * SPDX-License-Identifier: Apache-2.0 OR MIT + * + */ + +#include +#include +#include + +#include +#include +#include + + +namespace O3DE::ProjectManager +{ + O3DEObjectDownloadWorker::O3DEObjectDownloadWorker() + : QObject() + { + } + + void O3DEObjectDownloadWorker::StartDownload() + { + auto gemDownloadProgress = [=](int downloadProgress) + { + m_downloadProgress = downloadProgress; + emit UpdateProgress(downloadProgress); + }; + AZ::Outcome gemInfoResult = PythonBindingsInterface::Get()->DownloadGem(m_gemName, gemDownloadProgress); + if (gemInfoResult.IsSuccess()) + { + emit Done(""); + } + else + { + emit Done(tr("Gem download failed")); + } + } + + void O3DEObjectDownloadWorker::SetGemToDownload(const QString& gemName, bool downloadNow) + { + m_gemName = gemName; + if (downloadNow) + { + StartDownload(); + } + } + +} // namespace O3DE::ProjectManager diff --git a/Code/Tools/ProjectManager/Source/O3DEObjectDownloadWorker.h b/Code/Tools/ProjectManager/Source/O3DEObjectDownloadWorker.h new file mode 100644 index 0000000000..4fff71634f --- /dev/null +++ b/Code/Tools/ProjectManager/Source/O3DEObjectDownloadWorker.h @@ -0,0 +1,51 @@ +/* + * Copyright (c) Contributors to the Open 3D Engine Project. + * For complete copyright and license terms please see the LICENSE at the root of this distribution. + * + * SPDX-License-Identifier: Apache-2.0 OR MIT + * + */ +#pragma once + +#if !defined(Q_MOC_RUN) +#include + +#include +#include +#include +#endif + +QT_FORWARD_DECLARE_CLASS(QProcess) + +namespace O3DE::ProjectManager +{ + class O3DEObjectDownloadWorker : public QObject + { + // QProcess::waitForFinished uses -1 to indicate that the process should not timeout + static constexpr int MaxBuildTimeMSecs = -1; + // Download was cancelled + inline static const QString DownloadCancelled = QObject::tr("Download Cancelled."); + + Q_OBJECT + + public: + explicit O3DEObjectDownloadWorker(); + ~O3DEObjectDownloadWorker() = default; + + public slots: + void StartDownload(); + void SetGemToDownload(const QString& gemName, bool downloadNow = true); + + signals: + void UpdateProgress(int progress); + void Done(QString result = ""); + + private: + + QProcess* m_configProjectProcess = nullptr; + QProcess* m_buildProjectProcess = nullptr; + + QString m_gemName; + int m_downloadProgress; + }; +} // namespace O3DE::ProjectManager