ProjectBuilderController.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project.
  3. * For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0 OR MIT
  6. *
  7. */
  8. #include <ProjectBuilderController.h>
  9. #include <ProjectBuilderWorker.h>
  10. #include <ProjectButtonWidget.h>
  11. #include <SettingsInterface.h>
  12. #include <QMessageBox>
  13. #include <QDesktopServices>
  14. #include <QUrl>
  15. namespace O3DE::ProjectManager
  16. {
  17. ProjectBuilderController::ProjectBuilderController(const ProjectInfo& projectInfo, ProjectButton* projectButton, QWidget* parent)
  18. : QObject()
  19. , m_projectInfo(projectInfo)
  20. , m_projectButton(projectButton)
  21. , m_lastProgress(0)
  22. , m_parent(parent)
  23. {
  24. m_worker = new ProjectBuilderWorker(m_projectInfo);
  25. m_worker->moveToThread(&m_workerThread);
  26. // Remove key here in case Project Manager crashing while building that causes HandleResults to not be called
  27. SettingsInterface::Get()->SetProjectBuiltSuccessfully(m_projectInfo, false);
  28. connect(&m_workerThread, &QThread::finished, m_worker, &ProjectBuilderWorker::deleteLater);
  29. connect(&m_workerThread, &QThread::started, m_worker, &ProjectBuilderWorker::BuildProject);
  30. connect(m_worker, &ProjectBuilderWorker::Done, this, &ProjectBuilderController::HandleResults);
  31. connect(m_worker, &ProjectBuilderWorker::UpdateProgress, this, &ProjectBuilderController::UpdateUIProgress);
  32. }
  33. ProjectBuilderController::~ProjectBuilderController()
  34. {
  35. m_workerThread.requestInterruption();
  36. m_workerThread.quit();
  37. m_workerThread.wait();
  38. }
  39. void ProjectBuilderController::Start()
  40. {
  41. m_workerThread.start();
  42. UpdateUIProgress(0);
  43. }
  44. void ProjectBuilderController::SetProjectButton(ProjectButton* projectButton)
  45. {
  46. m_projectButton = projectButton;
  47. if (projectButton)
  48. {
  49. projectButton->SetProjectBuilding();
  50. projectButton->SetProjectButtonAction(tr("Cancel Build"), [this] { HandleCancel(); });
  51. if (m_lastProgress != 0)
  52. {
  53. UpdateUIProgress(m_lastProgress);
  54. }
  55. }
  56. }
  57. const ProjectInfo& ProjectBuilderController::GetProjectInfo() const
  58. {
  59. return m_projectInfo;
  60. }
  61. void ProjectBuilderController::UpdateUIProgress(int progress)
  62. {
  63. m_lastProgress = progress;
  64. if (m_projectButton)
  65. {
  66. m_projectButton->SetButtonOverlayText(QString("%1 (%2%)<br>%3<br>").arg(tr("Building Project..."), QString::number(progress), tr("Click to <a href=\"logs\">view logs</a>.")));
  67. m_projectButton->SetProgressBarValue(progress);
  68. m_projectButton->SetBuildLogsLink(m_worker->GetLogFilePath());
  69. }
  70. }
  71. void ProjectBuilderController::HandleResults(const QString& result)
  72. {
  73. if (!result.isEmpty())
  74. {
  75. if (result.contains(tr("log")))
  76. {
  77. QMessageBox::StandardButton openLog = QMessageBox::critical(
  78. m_parent,
  79. tr("Project Failed to Build!"),
  80. result + tr("\n\nWould you like to view log?"),
  81. QMessageBox::No | QMessageBox::Yes);
  82. if (openLog == QMessageBox::Yes)
  83. {
  84. // Open application assigned to this file type
  85. QDesktopServices::openUrl(QUrl("file:///" + m_worker->GetLogFilePath()));
  86. }
  87. m_projectInfo.m_buildFailed = true;
  88. m_projectInfo.m_logUrl = QUrl("file:///" + m_worker->GetLogFilePath());
  89. emit NotifyBuildProject(m_projectInfo);
  90. }
  91. else
  92. {
  93. QMessageBox::critical(m_parent, tr("Project Failed to Build!"), result);
  94. m_projectInfo.m_buildFailed = true;
  95. m_projectInfo.m_logUrl = QUrl("file:///" + m_worker->GetLogFilePath());
  96. emit NotifyBuildProject(m_projectInfo);
  97. }
  98. SettingsInterface::Get()->SetProjectBuiltSuccessfully(m_projectInfo, false);
  99. emit Done(false);
  100. return;
  101. }
  102. else
  103. {
  104. m_projectInfo.m_buildFailed = false;
  105. SettingsInterface::Get()->SetProjectBuiltSuccessfully(m_projectInfo, true);
  106. }
  107. emit Done(true);
  108. }
  109. void ProjectBuilderController::HandleCancel()
  110. {
  111. m_workerThread.quit();
  112. emit Done(false);
  113. }
  114. } // namespace O3DE::ProjectManager