ProjectsScreen.cpp 34 KB

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