ProjectsScreen.cpp 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864
  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 <ProjectsScreen.h>
  9. #include <ProjectManagerDefs.h>
  10. #include <ProjectButtonWidget.h>
  11. #include <PythonBindingsInterface.h>
  12. #include <ProjectUtils.h>
  13. #include <ProjectBuilderController.h>
  14. #include <ScreensCtrl.h>
  15. #include <SettingsInterface.h>
  16. #include <AddRemoteProjectDialog.h>
  17. #include <AzCore/std/ranges/ranges_algorithm.h>
  18. #include <AzQtComponents/Components/FlowLayout.h>
  19. #include <AzCore/Platform.h>
  20. #include <AzCore/IO/SystemFile.h>
  21. #include <AzFramework/AzFramework_Traits_Platform.h>
  22. #include <AzFramework/Process/ProcessCommon.h>
  23. #include <AzFramework/Process/ProcessWatcher.h>
  24. #include <AzCore/Utils/Utils.h>
  25. #include <AzCore/std/sort.h>
  26. #include <QVBoxLayout>
  27. #include <QHBoxLayout>
  28. #include <QLabel>
  29. #include <QPushButton>
  30. #include <QMenu>
  31. #include <QListView>
  32. #include <QSpacerItem>
  33. #include <QListWidget>
  34. #include <QListWidgetItem>
  35. #include <QScrollArea>
  36. #include <QStackedWidget>
  37. #include <QFrame>
  38. #include <QIcon>
  39. #include <QPixmap>
  40. #include <QSettings>
  41. #include <QMessageBox>
  42. #include <QTimer>
  43. #include <QQueue>
  44. #include <QDir>
  45. #include <QGuiApplication>
  46. #include <QFileSystemWatcher>
  47. namespace O3DE::ProjectManager
  48. {
  49. ProjectsScreen::ProjectsScreen(DownloadController* downloadController, QWidget* parent)
  50. : ScreenWidget(parent)
  51. , m_downloadController(downloadController)
  52. {
  53. QVBoxLayout* vLayout = new QVBoxLayout();
  54. vLayout->setAlignment(Qt::AlignTop);
  55. vLayout->setContentsMargins(s_contentMargins, 0, s_contentMargins, 0);
  56. setLayout(vLayout);
  57. m_fileSystemWatcher = new QFileSystemWatcher(this);
  58. connect(m_fileSystemWatcher, &QFileSystemWatcher::fileChanged, this, &ProjectsScreen::HandleProjectFilePathChanged);
  59. m_stack = new QStackedWidget(this);
  60. m_firstTimeContent = CreateFirstTimeContent();
  61. m_stack->addWidget(m_firstTimeContent);
  62. m_projectsContent = CreateProjectsContent();
  63. m_stack->addWidget(m_projectsContent);
  64. vLayout->addWidget(m_stack);
  65. connect(reinterpret_cast<ScreensCtrl*>(parent), &ScreensCtrl::NotifyBuildProject, this, &ProjectsScreen::SuggestBuildProject);
  66. connect(m_downloadController, &DownloadController::Done, this, &ProjectsScreen::HandleDownloadResult);
  67. connect(m_downloadController, &DownloadController::ObjectDownloadProgress, this, &ProjectsScreen::HandleDownloadProgress);
  68. }
  69. ProjectsScreen::~ProjectsScreen() = default;
  70. QFrame* ProjectsScreen::CreateFirstTimeContent()
  71. {
  72. QFrame* frame = new QFrame(this);
  73. frame->setObjectName("firstTimeContent");
  74. {
  75. QVBoxLayout* layout = new QVBoxLayout();
  76. layout->setContentsMargins(0, 0, 0, 0);
  77. layout->setAlignment(Qt::AlignTop);
  78. frame->setLayout(layout);
  79. QLabel* titleLabel = new QLabel(tr("Ready? Set. Create!"), this);
  80. titleLabel->setObjectName("titleLabel");
  81. layout->addWidget(titleLabel);
  82. QLabel* introLabel = new QLabel(this);
  83. introLabel->setObjectName("introLabel");
  84. introLabel->setText(tr("Welcome to O3DE! Start something new by creating a project."));
  85. layout->addWidget(introLabel);
  86. QHBoxLayout* buttonLayout = new QHBoxLayout();
  87. buttonLayout->setAlignment(Qt::AlignLeft);
  88. buttonLayout->setSpacing(s_spacerSize);
  89. // use a newline to force the text up
  90. QPushButton* createProjectButton = new QPushButton(tr("Create a project\n"), this);
  91. createProjectButton->setObjectName("createProjectButton");
  92. buttonLayout->addWidget(createProjectButton);
  93. QPushButton* addProjectButton = new QPushButton(tr("Open a project\n"), this);
  94. addProjectButton->setObjectName("addProjectButton");
  95. buttonLayout->addWidget(addProjectButton);
  96. #ifdef ADD_REMOTE_PROJECT_ENABLED
  97. QPushButton* addRemoteProjectButton = new QPushButton(tr("Add a remote project\n"), this);
  98. addRemoteProjectButton->setObjectName("addRemoteProjectButton");
  99. buttonLayout->addWidget(addRemoteProjectButton);
  100. #endif
  101. connect(createProjectButton, &QPushButton::clicked, this, &ProjectsScreen::HandleNewProjectButton);
  102. connect(addProjectButton, &QPushButton::clicked, this, &ProjectsScreen::HandleAddProjectButton);
  103. #ifdef ADD_REMOTE_PROJECT_ENABLED
  104. connect(addRemoteProjectButton, &QPushButton::clicked, this, &ProjectsScreen::HandleAddRemoteProjectButton);
  105. #endif
  106. layout->addLayout(buttonLayout);
  107. }
  108. return frame;
  109. }
  110. QFrame* ProjectsScreen::CreateProjectsContent()
  111. {
  112. QFrame* frame = new QFrame(this);
  113. frame->setObjectName("projectsContent");
  114. {
  115. QVBoxLayout* layout = new QVBoxLayout();
  116. layout->setAlignment(Qt::AlignTop);
  117. layout->setContentsMargins(0, 0, 0, 0);
  118. frame->setLayout(layout);
  119. QFrame* header = new QFrame(frame);
  120. QHBoxLayout* headerLayout = new QHBoxLayout();
  121. {
  122. QLabel* titleLabel = new QLabel(tr("My Projects"), this);
  123. titleLabel->setObjectName("titleLabel");
  124. headerLayout->addWidget(titleLabel);
  125. QMenu* newProjectMenu = new QMenu(this);
  126. m_createNewProjectAction = newProjectMenu->addAction("Create New Project");
  127. m_addExistingProjectAction = newProjectMenu->addAction("Open Existing Project");
  128. #ifdef ADD_REMOTE_PROJECT_ENABLED
  129. m_addRemoteProjectAction = newProjectMenu->addAction("Add Remote Project");
  130. #endif
  131. connect(m_createNewProjectAction, &QAction::triggered, this, &ProjectsScreen::HandleNewProjectButton);
  132. connect(m_addExistingProjectAction, &QAction::triggered, this, &ProjectsScreen::HandleAddProjectButton);
  133. #ifdef ADD_REMOTE_PROJECT_ENABLED
  134. connect(m_addRemoteProjectAction, &QAction::triggered, this, &ProjectsScreen::HandleAddRemoteProjectButton);
  135. #endif
  136. QPushButton* newProjectMenuButton = new QPushButton(tr("New Project..."), this);
  137. newProjectMenuButton->setObjectName("newProjectButton");
  138. newProjectMenuButton->setMenu(newProjectMenu);
  139. newProjectMenuButton->setDefault(true);
  140. headerLayout->addWidget(newProjectMenuButton);
  141. }
  142. header->setLayout(headerLayout);
  143. layout->addWidget(header);
  144. QScrollArea* projectsScrollArea = new QScrollArea(this);
  145. QWidget* scrollWidget = new QWidget();
  146. m_projectsFlowLayout = new FlowLayout(0, s_spacerSize, s_spacerSize);
  147. scrollWidget->setLayout(m_projectsFlowLayout);
  148. projectsScrollArea->setWidget(scrollWidget);
  149. projectsScrollArea->setWidgetResizable(true);
  150. ResetProjectsContent();
  151. layout->addWidget(projectsScrollArea);
  152. }
  153. return frame;
  154. }
  155. ProjectButton* ProjectsScreen::CreateProjectButton(const ProjectInfo& project, const EngineInfo& engine)
  156. {
  157. ProjectButton* projectButton = new ProjectButton(project, engine, this);
  158. m_projectButtons.insert({ project.m_path.toUtf8().constData(), projectButton });
  159. m_projectsFlowLayout->addWidget(projectButton);
  160. connect(projectButton, &ProjectButton::OpenProject, this, &ProjectsScreen::HandleOpenProject);
  161. connect(projectButton, &ProjectButton::EditProject, this, &ProjectsScreen::HandleEditProject);
  162. connect(projectButton, &ProjectButton::EditProjectGems, this, &ProjectsScreen::HandleEditProjectGems);
  163. connect(projectButton, &ProjectButton::CopyProject, this, &ProjectsScreen::HandleCopyProject);
  164. connect(projectButton, &ProjectButton::RemoveProject, this, &ProjectsScreen::HandleRemoveProject);
  165. connect(projectButton, &ProjectButton::DeleteProject, this, &ProjectsScreen::HandleDeleteProject);
  166. connect(projectButton, &ProjectButton::BuildProject, this, &ProjectsScreen::QueueBuildProject);
  167. connect(projectButton, &ProjectButton::OpenCMakeGUI, this,
  168. [this](const ProjectInfo& projectInfo)
  169. {
  170. AZ::Outcome result = ProjectUtils::OpenCMakeGUI(projectInfo.m_path);
  171. if (!result)
  172. {
  173. QMessageBox::critical(this, tr("Failed to open CMake GUI"), result.GetError(), QMessageBox::Ok);
  174. }
  175. });
  176. return projectButton;
  177. }
  178. void ProjectsScreen::ResetProjectsContent()
  179. {
  180. RemoveInvalidProjects();
  181. // Get all projects and sort so that building and queued projects appear first
  182. // followed by the remaining projects in alphabetical order
  183. auto projectsResult = PythonBindingsInterface::Get()->GetProjects();
  184. if (projectsResult.IsSuccess() && !projectsResult.GetValue().isEmpty())
  185. {
  186. QVector<ProjectInfo> projects = projectsResult.GetValue();
  187. // additional
  188. auto remoteProjectsResult = PythonBindingsInterface::Get()->GetProjectsForAllRepos();
  189. if (remoteProjectsResult.IsSuccess() && !remoteProjectsResult.GetValue().isEmpty())
  190. {
  191. const QVector<ProjectInfo>& remoteProjects = remoteProjectsResult.GetValue();
  192. for (const ProjectInfo& remoteProject : remoteProjects)
  193. {
  194. auto foundProject = AZStd::ranges::find_if(projects,
  195. [&remoteProject](const ProjectInfo& value)
  196. {
  197. return remoteProject.m_id == value.m_id;
  198. });
  199. if (foundProject== projects.end())
  200. {
  201. projects.append(remoteProject);
  202. }
  203. }
  204. }
  205. // If a project path is in this set then the button for it will be kept
  206. AZStd::unordered_set<AZ::IO::Path> keepProject;
  207. for (const ProjectInfo& project : projects)
  208. {
  209. keepProject.insert(project.m_path.toUtf8().constData());
  210. }
  211. // Remove buttons from flow layout and delete buttons for removed projects
  212. auto projectButtonsIter = m_projectButtons.begin();
  213. while (projectButtonsIter != m_projectButtons.end())
  214. {
  215. const auto button = projectButtonsIter->second;
  216. m_projectsFlowLayout->removeWidget(button);
  217. if (!keepProject.contains(projectButtonsIter->first))
  218. {
  219. m_fileSystemWatcher->removePath(QDir::toNativeSeparators(button->GetProjectInfo().m_path + "/project.json"));
  220. button->deleteLater();
  221. projectButtonsIter = m_projectButtons.erase(projectButtonsIter);
  222. }
  223. else
  224. {
  225. ++projectButtonsIter;
  226. }
  227. }
  228. AZ::IO::Path buildProjectPath;
  229. if (m_currentBuilder)
  230. {
  231. buildProjectPath = AZ::IO::Path(m_currentBuilder->GetProjectInfo().m_path.toUtf8().constData());
  232. }
  233. // Put currently building project in front, then queued projects, then sorts alphabetically
  234. AZStd::sort(projects.begin(), projects.end(), [buildProjectPath, this](const ProjectInfo& arg1, const ProjectInfo& arg2)
  235. {
  236. if (AZ::IO::Path(arg1.m_path.toUtf8().constData()) == buildProjectPath)
  237. {
  238. return true;
  239. }
  240. else if (AZ::IO::Path(arg2.m_path.toUtf8().constData()) == buildProjectPath)
  241. {
  242. return false;
  243. }
  244. bool arg1InBuildQueue = BuildQueueContainsProject(arg1.m_path);
  245. bool arg2InBuildQueue = BuildQueueContainsProject(arg2.m_path);
  246. if (arg1InBuildQueue && !arg2InBuildQueue)
  247. {
  248. return true;
  249. }
  250. else if (!arg1InBuildQueue && arg2InBuildQueue)
  251. {
  252. return false;
  253. }
  254. else
  255. {
  256. return arg1.m_displayName.toLower() < arg2.m_displayName.toLower();
  257. }
  258. });
  259. // Add all project buttons, restoring buttons to default state
  260. for (const ProjectInfo& project : projects)
  261. {
  262. ProjectButton* currentButton = nullptr;
  263. const AZ::IO::Path projectPath { project.m_path.toUtf8().constData() };
  264. auto projectButtonIter = m_projectButtons.find(projectPath);
  265. EngineInfo engine{};
  266. if (auto result = PythonBindingsInterface::Get()->GetProjectEngine(project.m_path); result)
  267. {
  268. engine = result.GetValue<EngineInfo>();
  269. }
  270. if (projectButtonIter == m_projectButtons.end())
  271. {
  272. currentButton = CreateProjectButton(project, engine);
  273. m_projectButtons.insert({ projectPath, currentButton });
  274. m_fileSystemWatcher->addPath(QDir::toNativeSeparators(project.m_path + "/project.json"));
  275. }
  276. else
  277. {
  278. currentButton = projectButtonIter->second;
  279. currentButton->SetEngine(engine);
  280. currentButton->SetProject(project);
  281. currentButton->SetState(ProjectButtonState::ReadyToLaunch);
  282. }
  283. // Check whether project manager has successfully built the project
  284. if (currentButton)
  285. {
  286. m_projectsFlowLayout->addWidget(currentButton);
  287. bool projectBuiltSuccessfully = false;
  288. SettingsInterface::Get()->GetProjectBuiltSuccessfully(projectBuiltSuccessfully, project);
  289. if (!projectBuiltSuccessfully)
  290. {
  291. currentButton->SetState(ProjectButtonState::NeedsToBuild);
  292. }
  293. if (project.m_remote)
  294. {
  295. currentButton->SetState(ProjectButtonState::NotDownloaded);
  296. currentButton->SetProjectButtonAction(
  297. tr("Download Project"),
  298. [this, currentButton, project]
  299. {
  300. m_downloadController->AddObjectDownload(project.m_projectName, DownloadController::DownloadObjectType::Project);
  301. currentButton->SetState(ProjectButtonState::Downloading);
  302. });
  303. }
  304. }
  305. }
  306. // Setup building button again
  307. auto buildProjectIter = m_projectButtons.find(buildProjectPath);
  308. if (buildProjectIter != m_projectButtons.end())
  309. {
  310. m_currentBuilder->SetProjectButton(buildProjectIter->second);
  311. }
  312. for (const ProjectInfo& project : m_buildQueue)
  313. {
  314. auto projectIter = m_projectButtons.find(project.m_path.toUtf8().constData());
  315. if (projectIter != m_projectButtons.end())
  316. {
  317. projectIter->second->SetProjectButtonAction(
  318. tr("Cancel queued build"),
  319. [this, project]
  320. {
  321. UnqueueBuildProject(project);
  322. SuggestBuildProjectMsg(project, false);
  323. });
  324. }
  325. }
  326. for (const ProjectInfo& project : m_requiresBuild)
  327. {
  328. auto projectIter = m_projectButtons.find(project.m_path.toUtf8().constData());
  329. if (projectIter != m_projectButtons.end())
  330. {
  331. // If project is not currently or about to build
  332. if (!m_currentBuilder || m_currentBuilder->GetProjectInfo() != project)
  333. {
  334. if (project.m_buildFailed)
  335. {
  336. projectIter->second->SetBuildLogsLink(project.m_logUrl);
  337. projectIter->second->SetState(ProjectButtonState::BuildFailed);
  338. }
  339. else
  340. {
  341. projectIter->second->SetState(ProjectButtonState::NeedsToBuild);
  342. }
  343. }
  344. }
  345. }
  346. }
  347. if (m_projectsContent)
  348. {
  349. m_stack->setCurrentWidget(m_projectsContent);
  350. }
  351. m_projectsFlowLayout->update();
  352. // Will focus whatever button it finds so the Project tab is not focused on start-up
  353. QTimer::singleShot(0, this, [this]
  354. {
  355. QPushButton* foundButton = m_stack->currentWidget()->findChild<QPushButton*>();
  356. if (foundButton)
  357. {
  358. foundButton->setFocus();
  359. }
  360. });
  361. }
  362. void ProjectsScreen::HandleProjectFilePathChanged(const QString& /*path*/)
  363. {
  364. // QFileWatcher automatically stops watching the path if it was removed so we will just refresh our view
  365. ResetProjectsContent();
  366. }
  367. ProjectManagerScreen ProjectsScreen::GetScreenEnum()
  368. {
  369. return ProjectManagerScreen::Projects;
  370. }
  371. bool ProjectsScreen::IsTab()
  372. {
  373. return true;
  374. }
  375. QString ProjectsScreen::GetTabText()
  376. {
  377. return tr("Projects");
  378. }
  379. void ProjectsScreen::paintEvent([[maybe_unused]] QPaintEvent* event)
  380. {
  381. // we paint the background here because qss does not support background cover scaling
  382. QPainter painter(this);
  383. const QSize winSize = size();
  384. const float pixmapRatio = (float)m_background.width() / m_background.height();
  385. const float windowRatio = (float)winSize.width() / winSize.height();
  386. QRect backgroundRect;
  387. if (pixmapRatio > windowRatio)
  388. {
  389. const int newWidth = (int)(winSize.height() * pixmapRatio);
  390. const int offset = (newWidth - winSize.width()) / -2;
  391. backgroundRect = QRect(offset, 0, newWidth, winSize.height());
  392. }
  393. else
  394. {
  395. const int newHeight = (int)(winSize.width() / pixmapRatio);
  396. backgroundRect = QRect(0, 0, winSize.width(), newHeight);
  397. }
  398. // Draw the background image.
  399. painter.drawPixmap(backgroundRect, m_background);
  400. // Draw a semi-transparent overlay to darken down the colors.
  401. // Use SourceOver, DestinationIn will make background transparent on Mac
  402. painter.setCompositionMode (QPainter::CompositionMode_SourceOver);
  403. const float overlayTransparency = 0.3f;
  404. painter.fillRect(backgroundRect, QColor(0, 0, 0, static_cast<int>(255.0f * overlayTransparency)));
  405. }
  406. void ProjectsScreen::HandleNewProjectButton()
  407. {
  408. emit ResetScreenRequest(ProjectManagerScreen::CreateProject);
  409. emit ChangeScreenRequest(ProjectManagerScreen::CreateProject);
  410. }
  411. void ProjectsScreen::HandleAddProjectButton()
  412. {
  413. if (ProjectUtils::AddProjectDialog(this))
  414. {
  415. emit ChangeScreenRequest(ProjectManagerScreen::Projects);
  416. }
  417. }
  418. void ProjectsScreen::HandleAddRemoteProjectButton()
  419. {
  420. AddRemoteProjectDialog* addRemoteProjectDialog = new AddRemoteProjectDialog(this);
  421. connect(addRemoteProjectDialog, &AddRemoteProjectDialog::StartObjectDownload, this, &ProjectsScreen::StartProjectDownload);
  422. if (addRemoteProjectDialog->exec() == QDialog::DialogCode::Accepted)
  423. {
  424. QString repoUri = addRemoteProjectDialog->GetRepoPath();
  425. if (repoUri.isEmpty())
  426. {
  427. QMessageBox::warning(this, tr("No Input"), tr("Please provide a repo Uri."));
  428. return;
  429. }
  430. }
  431. }
  432. void ProjectsScreen::HandleOpenProject(const QString& projectPath)
  433. {
  434. if (!projectPath.isEmpty())
  435. {
  436. if (!WarnIfInBuildQueue(projectPath))
  437. {
  438. AZ::IO::FixedMaxPath fixedProjectPath = projectPath.toUtf8().constData();
  439. AZ::IO::FixedMaxPath editorExecutablePath = ProjectUtils::GetEditorExecutablePath(fixedProjectPath);
  440. if (editorExecutablePath.empty())
  441. {
  442. AZ_Error("ProjectManager", false, "Failed to locate editor");
  443. QMessageBox::critical(
  444. this, tr("Error"), tr("Failed to locate the Editor, please verify that it is built."));
  445. return;
  446. }
  447. AzFramework::ProcessLauncher::ProcessLaunchInfo processLaunchInfo;
  448. processLaunchInfo.m_commandlineParameters = AZStd::vector<AZStd::string>{
  449. editorExecutablePath.String(),
  450. AZStd::string::format(R"(--regset="/Amazon/AzCore/Bootstrap/project_path=%s")", fixedProjectPath.c_str())
  451. };
  452. ;
  453. bool launchSucceeded = AzFramework::ProcessLauncher::LaunchUnwatchedProcess(processLaunchInfo);
  454. if (!launchSucceeded)
  455. {
  456. AZ_Error("ProjectManager", false, "Failed to launch editor");
  457. QMessageBox::critical(
  458. this, tr("Error"), tr("Failed to launch the Editor, please verify the project settings are valid."));
  459. }
  460. else
  461. {
  462. // prevent the user from accidentally pressing the button while the editor is launching
  463. // and let them know what's happening
  464. ProjectButton* button = qobject_cast<ProjectButton*>(sender());
  465. if (button)
  466. {
  467. button->SetState(ProjectButtonState::Launching);
  468. }
  469. // enable the button after 3 seconds
  470. constexpr int waitTimeInMs = 3000;
  471. QTimer::singleShot(
  472. waitTimeInMs, this,
  473. [button]
  474. {
  475. if (button)
  476. {
  477. button->SetState(ProjectButtonState::ReadyToLaunch);
  478. }
  479. });
  480. }
  481. }
  482. }
  483. else
  484. {
  485. AZ_Error("ProjectManager", false, "Cannot open editor because an empty project path was provided");
  486. QMessageBox::critical( this, tr("Error"), tr("Failed to launch the Editor because the project path is invalid."));
  487. }
  488. }
  489. void ProjectsScreen::HandleEditProject(const QString& projectPath)
  490. {
  491. if (!WarnIfInBuildQueue(projectPath))
  492. {
  493. emit NotifyCurrentProject(projectPath);
  494. emit ChangeScreenRequest(ProjectManagerScreen::UpdateProject);
  495. }
  496. }
  497. void ProjectsScreen::HandleEditProjectGems(const QString& projectPath)
  498. {
  499. if (!WarnIfInBuildQueue(projectPath))
  500. {
  501. emit NotifyCurrentProject(projectPath);
  502. emit ChangeScreenRequest(ProjectManagerScreen::ProjectGemCatalog);
  503. }
  504. }
  505. void ProjectsScreen::HandleCopyProject(const ProjectInfo& projectInfo)
  506. {
  507. if (!WarnIfInBuildQueue(projectInfo.m_path))
  508. {
  509. ProjectInfo newProjectInfo(projectInfo);
  510. // Open file dialog and choose location for copied project then register copy with O3DE
  511. if (ProjectUtils::CopyProjectDialog(projectInfo.m_path, newProjectInfo, this))
  512. {
  513. emit NotifyBuildProject(newProjectInfo);
  514. emit ChangeScreenRequest(ProjectManagerScreen::Projects);
  515. }
  516. }
  517. }
  518. void ProjectsScreen::HandleRemoveProject(const QString& projectPath)
  519. {
  520. if (!WarnIfInBuildQueue(projectPath))
  521. {
  522. // Unregister Project from O3DE and reload projects
  523. if (ProjectUtils::UnregisterProject(projectPath))
  524. {
  525. emit ChangeScreenRequest(ProjectManagerScreen::Projects);
  526. }
  527. }
  528. }
  529. void ProjectsScreen::HandleDeleteProject(const QString& projectPath)
  530. {
  531. if (!WarnIfInBuildQueue(projectPath))
  532. {
  533. QMessageBox::StandardButton warningResult = QMessageBox::warning(this,
  534. tr("Delete Project"),
  535. tr("Are you sure?\nProject will be unregistered from O3DE and project directory will be deleted from your disk."),
  536. QMessageBox::No | QMessageBox::Yes);
  537. if (warningResult == QMessageBox::Yes)
  538. {
  539. QGuiApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
  540. // Remove project from O3DE and delete from disk
  541. HandleRemoveProject(projectPath);
  542. ProjectUtils::DeleteProjectFiles(projectPath);
  543. QGuiApplication::restoreOverrideCursor();
  544. }
  545. }
  546. }
  547. void ProjectsScreen::SuggestBuildProjectMsg(const ProjectInfo& projectInfo, bool showMessage)
  548. {
  549. if (RequiresBuildProjectIterator(projectInfo.m_path) == m_requiresBuild.end() || projectInfo.m_buildFailed)
  550. {
  551. m_requiresBuild.append(projectInfo);
  552. }
  553. ResetProjectsContent();
  554. if (showMessage)
  555. {
  556. QMessageBox::information(this,
  557. tr("Project should be rebuilt."),
  558. projectInfo.GetProjectDisplayName() + tr(" project likely needs to be rebuilt."));
  559. }
  560. }
  561. void ProjectsScreen::SuggestBuildProject(const ProjectInfo& projectInfo)
  562. {
  563. SuggestBuildProjectMsg(projectInfo, true);
  564. }
  565. void ProjectsScreen::QueueBuildProject(const ProjectInfo& projectInfo)
  566. {
  567. auto requiredIter = RequiresBuildProjectIterator(projectInfo.m_path);
  568. if (requiredIter != m_requiresBuild.end())
  569. {
  570. m_requiresBuild.erase(requiredIter);
  571. }
  572. if (!BuildQueueContainsProject(projectInfo.m_path))
  573. {
  574. if (m_buildQueue.empty() && !m_currentBuilder)
  575. {
  576. StartProjectBuild(projectInfo);
  577. // Projects Content is already reset in function
  578. }
  579. else
  580. {
  581. m_buildQueue.append(projectInfo);
  582. ResetProjectsContent();
  583. }
  584. }
  585. }
  586. void ProjectsScreen::UnqueueBuildProject(const ProjectInfo& projectInfo)
  587. {
  588. m_buildQueue.removeAll(projectInfo);
  589. ResetProjectsContent();
  590. }
  591. void ProjectsScreen::StartProjectDownload(const QString& projectName)
  592. {
  593. m_downloadController->AddObjectDownload(projectName, DownloadController::DownloadObjectType::Project);
  594. auto foundButton = AZStd::ranges::find_if(m_projectButtons,
  595. [&projectName](const AZStd::unordered_map<AZ::IO::Path, ProjectButton*>::value_type& value)
  596. {
  597. return (value.second->GetProjectInfo().m_projectName == projectName);
  598. });
  599. if (foundButton != m_projectButtons.end())
  600. {
  601. (*foundButton).second->SetState(ProjectButtonState::Downloading);
  602. }
  603. }
  604. void ProjectsScreen::HandleDownloadResult(const QString& /*projectName*/, bool /*succeeded*/)
  605. {
  606. ResetProjectsContent();
  607. }
  608. void ProjectsScreen::HandleDownloadProgress(const QString& projectName, DownloadController::DownloadObjectType objectType, int bytesDownloaded, int totalBytes)
  609. {
  610. if (objectType != DownloadController::DownloadObjectType::Project)
  611. {
  612. return;
  613. }
  614. //Find button for project name
  615. auto foundButton = AZStd::ranges::find_if(m_projectButtons,
  616. [&projectName](const AZStd::unordered_map<AZ::IO::Path, ProjectButton*>::value_type& value)
  617. {
  618. return (value.second->GetProjectInfo().m_projectName == projectName);
  619. });
  620. if (foundButton != m_projectButtons.end())
  621. {
  622. float percentage = static_cast<float>(bytesDownloaded) / totalBytes;
  623. (*foundButton).second->SetProgressBarPercentage(percentage);
  624. }
  625. }
  626. void ProjectsScreen::NotifyCurrentScreen()
  627. {
  628. if (ShouldDisplayFirstTimeContent())
  629. {
  630. m_background.load(":/Backgrounds/FtueBackground.jpg");
  631. m_stack->setCurrentWidget(m_firstTimeContent);
  632. }
  633. else
  634. {
  635. m_background.load(":/Backgrounds/DefaultBackground.jpg");
  636. ResetProjectsContent();
  637. }
  638. }
  639. bool ProjectsScreen::ShouldDisplayFirstTimeContent()
  640. {
  641. auto projectsResult = PythonBindingsInterface::Get()->GetProjects();
  642. if (!projectsResult.IsSuccess() || projectsResult.GetValue().isEmpty())
  643. {
  644. return true;
  645. }
  646. QSettings settings;
  647. bool displayFirstTimeContent = settings.value("displayFirstTimeContent", true).toBool();
  648. if (displayFirstTimeContent)
  649. {
  650. settings.setValue("displayFirstTimeContent", false);
  651. }
  652. return displayFirstTimeContent;
  653. }
  654. bool ProjectsScreen::RemoveInvalidProjects()
  655. {
  656. return PythonBindingsInterface::Get()->RemoveInvalidProjects();
  657. }
  658. bool ProjectsScreen::StartProjectBuild(const ProjectInfo& projectInfo)
  659. {
  660. if (ProjectUtils::FindSupportedCompiler(this))
  661. {
  662. QMessageBox::StandardButton buildProject = QMessageBox::information(
  663. this,
  664. tr("Building \"%1\"").arg(projectInfo.GetProjectDisplayName()),
  665. tr("Ready to build \"%1\"?").arg(projectInfo.GetProjectDisplayName()),
  666. QMessageBox::No | QMessageBox::Yes);
  667. if (buildProject == QMessageBox::Yes)
  668. {
  669. m_currentBuilder = new ProjectBuilderController(projectInfo, nullptr, this);
  670. ResetProjectsContent();
  671. connect(m_currentBuilder, &ProjectBuilderController::Done, this, &ProjectsScreen::ProjectBuildDone);
  672. connect(m_currentBuilder, &ProjectBuilderController::NotifyBuildProject, this, &ProjectsScreen::SuggestBuildProject);
  673. m_currentBuilder->Start();
  674. }
  675. else
  676. {
  677. SuggestBuildProjectMsg(projectInfo, false);
  678. return false;
  679. }
  680. return true;
  681. }
  682. return false;
  683. }
  684. void ProjectsScreen::ProjectBuildDone(bool success)
  685. {
  686. ProjectInfo currentBuilderProject;
  687. if (!success)
  688. {
  689. currentBuilderProject = m_currentBuilder->GetProjectInfo();
  690. }
  691. delete m_currentBuilder;
  692. m_currentBuilder = nullptr;
  693. if (!success)
  694. {
  695. SuggestBuildProjectMsg(currentBuilderProject, false);
  696. }
  697. if (!m_buildQueue.empty())
  698. {
  699. while (!StartProjectBuild(m_buildQueue.front()) && m_buildQueue.size() > 1)
  700. {
  701. m_buildQueue.pop_front();
  702. }
  703. m_buildQueue.pop_front();
  704. }
  705. ResetProjectsContent();
  706. }
  707. QList<ProjectInfo>::iterator ProjectsScreen::RequiresBuildProjectIterator(const QString& projectPath)
  708. {
  709. QString nativeProjPath(QDir::toNativeSeparators(projectPath));
  710. auto projectIter = m_requiresBuild.begin();
  711. for (; projectIter != m_requiresBuild.end(); ++projectIter)
  712. {
  713. if (QDir::toNativeSeparators(projectIter->m_path) == nativeProjPath)
  714. {
  715. break;
  716. }
  717. }
  718. return projectIter;
  719. }
  720. bool ProjectsScreen::BuildQueueContainsProject(const QString& projectPath)
  721. {
  722. const AZ::IO::PathView path { projectPath.toUtf8().constData() };
  723. for (const ProjectInfo& project : m_buildQueue)
  724. {
  725. if (AZ::IO::PathView(project.m_path.toUtf8().constData()) == path)
  726. {
  727. return true;
  728. }
  729. }
  730. return false;
  731. }
  732. bool ProjectsScreen::WarnIfInBuildQueue(const QString& projectPath)
  733. {
  734. if (BuildQueueContainsProject(projectPath))
  735. {
  736. QMessageBox::warning(
  737. this,
  738. tr("Action Temporarily Disabled!"),
  739. tr("Action not allowed on projects in build queue."));
  740. return true;
  741. }
  742. return false;
  743. }
  744. } // namespace O3DE::ProjectManager