ProjectsScreen.cpp 35 KB

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