ProjectsScreen.cpp 37 KB

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