ProjectsScreen.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project. For complete copyright and license terms please see the LICENSE at the root of this distribution.
  3. *
  4. * SPDX-License-Identifier: Apache-2.0 OR MIT
  5. *
  6. */
  7. #include <ProjectsScreen.h>
  8. #include <ProjectManagerDefs.h>
  9. #include <ProjectButtonWidget.h>
  10. #include <PythonBindingsInterface.h>
  11. #include <ProjectUtils.h>
  12. #include <ProjectBuilderController.h>
  13. #include <ScreensCtrl.h>
  14. #include <AzQtComponents/Components/FlowLayout.h>
  15. #include <AzCore/Platform.h>
  16. #include <AzCore/IO/SystemFile.h>
  17. #include <AzFramework/AzFramework_Traits_Platform.h>
  18. #include <AzFramework/Process/ProcessCommon.h>
  19. #include <AzFramework/Process/ProcessWatcher.h>
  20. #include <AzCore/Utils/Utils.h>
  21. #include <QVBoxLayout>
  22. #include <QHBoxLayout>
  23. #include <QLabel>
  24. #include <QPushButton>
  25. #include <QMenu>
  26. #include <QListView>
  27. #include <QSpacerItem>
  28. #include <QListWidget>
  29. #include <QListWidgetItem>
  30. #include <QScrollArea>
  31. #include <QStackedWidget>
  32. #include <QFrame>
  33. #include <QIcon>
  34. #include <QPixmap>
  35. #include <QSettings>
  36. #include <QMessageBox>
  37. #include <QTimer>
  38. #include <QQueue>
  39. #include <QDir>
  40. #include <QGuiApplication>
  41. namespace O3DE::ProjectManager
  42. {
  43. ProjectsScreen::ProjectsScreen(QWidget* parent)
  44. : ScreenWidget(parent)
  45. {
  46. QVBoxLayout* vLayout = new QVBoxLayout();
  47. vLayout->setAlignment(Qt::AlignTop);
  48. vLayout->setContentsMargins(s_contentMargins, 0, s_contentMargins, 0);
  49. setLayout(vLayout);
  50. m_stack = new QStackedWidget(this);
  51. m_firstTimeContent = CreateFirstTimeContent();
  52. m_stack->addWidget(m_firstTimeContent);
  53. m_projectsContent = CreateProjectsContent();
  54. m_stack->addWidget(m_projectsContent);
  55. vLayout->addWidget(m_stack);
  56. connect(reinterpret_cast<ScreensCtrl*>(parent), &ScreensCtrl::NotifyBuildProject, this, &ProjectsScreen::SuggestBuildProject);
  57. }
  58. ProjectsScreen::~ProjectsScreen()
  59. {
  60. delete m_currentBuilder;
  61. }
  62. QFrame* ProjectsScreen::CreateFirstTimeContent()
  63. {
  64. QFrame* frame = new QFrame(this);
  65. frame->setObjectName("firstTimeContent");
  66. {
  67. QVBoxLayout* layout = new QVBoxLayout();
  68. layout->setContentsMargins(0, 0, 0, 0);
  69. layout->setAlignment(Qt::AlignTop);
  70. frame->setLayout(layout);
  71. QLabel* titleLabel = new QLabel(tr("Ready? Set. Create!"), this);
  72. titleLabel->setObjectName("titleLabel");
  73. layout->addWidget(titleLabel);
  74. QLabel* introLabel = new QLabel(this);
  75. introLabel->setObjectName("introLabel");
  76. introLabel->setText(tr("Welcome to O3DE! Start something new by creating a project."));
  77. layout->addWidget(introLabel);
  78. QHBoxLayout* buttonLayout = new QHBoxLayout();
  79. buttonLayout->setAlignment(Qt::AlignLeft);
  80. buttonLayout->setSpacing(s_spacerSize);
  81. // use a newline to force the text up
  82. QPushButton* createProjectButton = new QPushButton(tr("Create a Project\n"), this);
  83. createProjectButton->setObjectName("createProjectButton");
  84. buttonLayout->addWidget(createProjectButton);
  85. QPushButton* addProjectButton = new QPushButton(tr("Add a Project\n"), this);
  86. addProjectButton->setObjectName("addProjectButton");
  87. buttonLayout->addWidget(addProjectButton);
  88. connect(createProjectButton, &QPushButton::clicked, this, &ProjectsScreen::HandleNewProjectButton);
  89. connect(addProjectButton, &QPushButton::clicked, this, &ProjectsScreen::HandleAddProjectButton);
  90. layout->addLayout(buttonLayout);
  91. }
  92. return frame;
  93. }
  94. QFrame* ProjectsScreen::CreateProjectsContent(QString buildProjectPath, ProjectButton** projectButton)
  95. {
  96. RemoveInvalidProjects();
  97. QFrame* frame = new QFrame(this);
  98. frame->setObjectName("projectsContent");
  99. {
  100. QVBoxLayout* layout = new QVBoxLayout();
  101. layout->setAlignment(Qt::AlignTop);
  102. layout->setContentsMargins(0, 0, 0, 0);
  103. frame->setLayout(layout);
  104. QFrame* header = new QFrame(this);
  105. QHBoxLayout* headerLayout = new QHBoxLayout();
  106. {
  107. QLabel* titleLabel = new QLabel(tr("My Projects"), this);
  108. titleLabel->setObjectName("titleLabel");
  109. headerLayout->addWidget(titleLabel);
  110. QMenu* newProjectMenu = new QMenu(this);
  111. m_createNewProjectAction = newProjectMenu->addAction("Create New Project");
  112. m_addExistingProjectAction = newProjectMenu->addAction("Add Existing Project");
  113. connect(m_createNewProjectAction, &QAction::triggered, this, &ProjectsScreen::HandleNewProjectButton);
  114. connect(m_addExistingProjectAction, &QAction::triggered, this, &ProjectsScreen::HandleAddProjectButton);
  115. QPushButton* newProjectMenuButton = new QPushButton(tr("New Project..."), this);
  116. newProjectMenuButton->setObjectName("newProjectButton");
  117. newProjectMenuButton->setMenu(newProjectMenu);
  118. newProjectMenuButton->setDefault(true);
  119. headerLayout->addWidget(newProjectMenuButton);
  120. }
  121. header->setLayout(headerLayout);
  122. layout->addWidget(header);
  123. // Get all projects and create a horizontal scrolling list of them
  124. auto projectsResult = PythonBindingsInterface::Get()->GetProjects();
  125. if (projectsResult.IsSuccess() && !projectsResult.GetValue().isEmpty())
  126. {
  127. QScrollArea* projectsScrollArea = new QScrollArea(this);
  128. QWidget* scrollWidget = new QWidget();
  129. FlowLayout* flowLayout = new FlowLayout(0, s_spacerSize, s_spacerSize);
  130. scrollWidget->setLayout(flowLayout);
  131. projectsScrollArea->setWidget(scrollWidget);
  132. projectsScrollArea->setWidgetResizable(true);
  133. QVector<ProjectInfo> nonProcessingProjects;
  134. buildProjectPath = QDir::fromNativeSeparators(buildProjectPath);
  135. for (auto& project : projectsResult.GetValue())
  136. {
  137. if (projectButton && !*projectButton)
  138. {
  139. if (QDir::fromNativeSeparators(project.m_path) == buildProjectPath)
  140. {
  141. *projectButton = CreateProjectButton(project, flowLayout, true);
  142. continue;
  143. }
  144. }
  145. nonProcessingProjects.append(project);
  146. }
  147. for (auto& project : nonProcessingProjects)
  148. {
  149. ProjectButton* projectButtonWidget = CreateProjectButton(project, flowLayout);
  150. if (BuildQueueContainsProject(project.m_path))
  151. {
  152. projectButtonWidget->SetProjectButtonAction(tr("Cancel Queued Build"),
  153. [this, project]
  154. {
  155. UnqueueBuildProject(project);
  156. SuggestBuildProjectMsg(project, false);
  157. });
  158. }
  159. else if (RequiresBuildProjectIterator(project.m_path) != m_requiresBuild.end())
  160. {
  161. auto buildProjectIterator = RequiresBuildProjectIterator(project.m_path);
  162. if (buildProjectIterator != m_requiresBuild.end())
  163. {
  164. if (buildProjectIterator->m_buildFailed)
  165. {
  166. projectButtonWidget->ShowBuildFailed(true, buildProjectIterator->m_logUrl);
  167. }
  168. else
  169. {
  170. projectButtonWidget->SetProjectBuildButtonAction();
  171. }
  172. }
  173. }
  174. }
  175. layout->addWidget(projectsScrollArea);
  176. }
  177. }
  178. return frame;
  179. }
  180. ProjectButton* ProjectsScreen::CreateProjectButton(ProjectInfo& project, QLayout* flowLayout, bool processing)
  181. {
  182. ProjectButton* projectButton = new ProjectButton(project, this, processing);
  183. flowLayout->addWidget(projectButton);
  184. if (!processing)
  185. {
  186. connect(projectButton, &ProjectButton::OpenProject, this, &ProjectsScreen::HandleOpenProject);
  187. connect(projectButton, &ProjectButton::EditProject, this, &ProjectsScreen::HandleEditProject);
  188. connect(projectButton, &ProjectButton::CopyProject, this, &ProjectsScreen::HandleCopyProject);
  189. connect(projectButton, &ProjectButton::RemoveProject, this, &ProjectsScreen::HandleRemoveProject);
  190. connect(projectButton, &ProjectButton::DeleteProject, this, &ProjectsScreen::HandleDeleteProject);
  191. }
  192. connect(projectButton, &ProjectButton::BuildProject, this, &ProjectsScreen::QueueBuildProject);
  193. return projectButton;
  194. }
  195. void ProjectsScreen::ResetProjectsContent()
  196. {
  197. // refresh the projects content by re-creating it for now
  198. if (m_projectsContent)
  199. {
  200. m_stack->removeWidget(m_projectsContent);
  201. m_projectsContent->deleteLater();
  202. }
  203. m_background.load(":/Backgrounds/DefaultBackground.jpg");
  204. // Make sure to update builder with latest Project Button
  205. if (m_currentBuilder)
  206. {
  207. ProjectButton* projectButtonPtr = nullptr;
  208. m_projectsContent = CreateProjectsContent(m_currentBuilder->GetProjectInfo().m_path, &projectButtonPtr);
  209. m_currentBuilder->SetProjectButton(projectButtonPtr);
  210. }
  211. else
  212. {
  213. m_projectsContent = CreateProjectsContent();
  214. }
  215. m_stack->addWidget(m_projectsContent);
  216. m_stack->setCurrentWidget(m_projectsContent);
  217. }
  218. ProjectManagerScreen ProjectsScreen::GetScreenEnum()
  219. {
  220. return ProjectManagerScreen::Projects;
  221. }
  222. bool ProjectsScreen::IsTab()
  223. {
  224. return true;
  225. }
  226. QString ProjectsScreen::GetTabText()
  227. {
  228. return tr("Projects");
  229. }
  230. void ProjectsScreen::paintEvent([[maybe_unused]] QPaintEvent* event)
  231. {
  232. // we paint the background here because qss does not support background cover scaling
  233. QPainter painter(this);
  234. const QSize winSize = size();
  235. const float pixmapRatio = (float)m_background.width() / m_background.height();
  236. const float windowRatio = (float)winSize.width() / winSize.height();
  237. QRect backgroundRect;
  238. if (pixmapRatio > windowRatio)
  239. {
  240. const int newWidth = (int)(winSize.height() * pixmapRatio);
  241. const int offset = (newWidth - winSize.width()) / -2;
  242. backgroundRect = QRect(offset, 0, newWidth, winSize.height());
  243. }
  244. else
  245. {
  246. const int newHeight = (int)(winSize.width() / pixmapRatio);
  247. backgroundRect = QRect(0, 0, winSize.width(), newHeight);
  248. }
  249. // Draw the background image.
  250. painter.drawPixmap(backgroundRect, m_background);
  251. // Draw a semi-transparent overlay to darken down the colors.
  252. painter.setCompositionMode (QPainter::CompositionMode_DestinationIn);
  253. const float overlayTransparency = 0.7f;
  254. painter.fillRect(backgroundRect, QColor(0, 0, 0, static_cast<int>(255.0f * overlayTransparency)));
  255. }
  256. void ProjectsScreen::HandleNewProjectButton()
  257. {
  258. emit ResetScreenRequest(ProjectManagerScreen::CreateProject);
  259. emit ChangeScreenRequest(ProjectManagerScreen::CreateProject);
  260. }
  261. void ProjectsScreen::HandleAddProjectButton()
  262. {
  263. if (ProjectUtils::AddProjectDialog(this))
  264. {
  265. ResetProjectsContent();
  266. emit ChangeScreenRequest(ProjectManagerScreen::Projects);
  267. }
  268. }
  269. void ProjectsScreen::HandleOpenProject(const QString& projectPath)
  270. {
  271. if (!projectPath.isEmpty())
  272. {
  273. if (!WarnIfInBuildQueue(projectPath))
  274. {
  275. AZ::IO::FixedMaxPath executableDirectory = AZ::Utils::GetExecutableDirectory();
  276. AZStd::string executableFilename = "Editor";
  277. AZ::IO::FixedMaxPath editorExecutablePath = executableDirectory / (executableFilename + AZ_TRAIT_OS_EXECUTABLE_EXTENSION);
  278. auto cmdPath = AZ::IO::FixedMaxPathString::format(
  279. "%s -regset=\"/Amazon/AzCore/Bootstrap/project_path=%s\"", editorExecutablePath.c_str(),
  280. projectPath.toStdString().c_str());
  281. AzFramework::ProcessLauncher::ProcessLaunchInfo processLaunchInfo;
  282. processLaunchInfo.m_commandlineParameters = cmdPath;
  283. bool launchSucceeded = AzFramework::ProcessLauncher::LaunchUnwatchedProcess(processLaunchInfo);
  284. if (!launchSucceeded)
  285. {
  286. AZ_Error("ProjectManager", false, "Failed to launch editor");
  287. QMessageBox::critical(
  288. this, tr("Error"), tr("Failed to launch the Editor, please verify the project settings are valid."));
  289. }
  290. else
  291. {
  292. // prevent the user from accidentally pressing the button while the editor is launching
  293. // and let them know what's happening
  294. ProjectButton* button = qobject_cast<ProjectButton*>(sender());
  295. if (button)
  296. {
  297. button->SetLaunchButtonEnabled(false);
  298. button->SetButtonOverlayText(tr("Opening Editor..."));
  299. }
  300. // enable the button after 3 seconds
  301. constexpr int waitTimeInMs = 3000;
  302. QTimer::singleShot(
  303. waitTimeInMs, this,
  304. [this, button]
  305. {
  306. if (button)
  307. {
  308. button->SetLaunchButtonEnabled(true);
  309. }
  310. });
  311. }
  312. }
  313. }
  314. else
  315. {
  316. AZ_Error("ProjectManager", false, "Cannot open editor because an empty project path was provided");
  317. QMessageBox::critical( this, tr("Error"), tr("Failed to launch the Editor because the project path is invalid."));
  318. }
  319. }
  320. void ProjectsScreen::HandleEditProject(const QString& projectPath)
  321. {
  322. if (!WarnIfInBuildQueue(projectPath))
  323. {
  324. emit NotifyCurrentProject(projectPath);
  325. emit ChangeScreenRequest(ProjectManagerScreen::UpdateProject);
  326. }
  327. }
  328. void ProjectsScreen::HandleCopyProject(const QString& projectPath)
  329. {
  330. if (!WarnIfInBuildQueue(projectPath))
  331. {
  332. // Open file dialog and choose location for copied project then register copy with O3DE
  333. if (ProjectUtils::CopyProjectDialog(projectPath, this))
  334. {
  335. ResetProjectsContent();
  336. emit ChangeScreenRequest(ProjectManagerScreen::Projects);
  337. }
  338. }
  339. }
  340. void ProjectsScreen::HandleRemoveProject(const QString& projectPath)
  341. {
  342. if (!WarnIfInBuildQueue(projectPath))
  343. {
  344. // Unregister Project from O3DE and reload projects
  345. if (ProjectUtils::UnregisterProject(projectPath))
  346. {
  347. ResetProjectsContent();
  348. emit ChangeScreenRequest(ProjectManagerScreen::Projects);
  349. }
  350. }
  351. }
  352. void ProjectsScreen::HandleDeleteProject(const QString& projectPath)
  353. {
  354. if (!WarnIfInBuildQueue(projectPath))
  355. {
  356. QMessageBox::StandardButton warningResult = QMessageBox::warning(this,
  357. tr("Delete Project"),
  358. tr("Are you sure?\nProject will be unregistered from O3DE and project directory will be deleted from your disk."),
  359. QMessageBox::No | QMessageBox::Yes);
  360. if (warningResult == QMessageBox::Yes)
  361. {
  362. QGuiApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
  363. // Remove project from O3DE and delete from disk
  364. HandleRemoveProject(projectPath);
  365. ProjectUtils::DeleteProjectFiles(projectPath);
  366. QGuiApplication::restoreOverrideCursor();
  367. }
  368. }
  369. }
  370. void ProjectsScreen::SuggestBuildProjectMsg(const ProjectInfo& projectInfo, bool showMessage)
  371. {
  372. if (RequiresBuildProjectIterator(projectInfo.m_path) == m_requiresBuild.end() || projectInfo.m_buildFailed)
  373. {
  374. m_requiresBuild.append(projectInfo);
  375. }
  376. ResetProjectsContent();
  377. if (showMessage)
  378. {
  379. QMessageBox::information(this,
  380. tr("Project Should be rebuilt."),
  381. projectInfo.GetProjectDisplayName() + tr(" project likely needs to be rebuilt."));
  382. }
  383. }
  384. void ProjectsScreen::SuggestBuildProject(const ProjectInfo& projectInfo)
  385. {
  386. SuggestBuildProjectMsg(projectInfo, true);
  387. }
  388. void ProjectsScreen::QueueBuildProject(const ProjectInfo& projectInfo)
  389. {
  390. auto requiredIter = RequiresBuildProjectIterator(projectInfo.m_path);
  391. if (requiredIter != m_requiresBuild.end())
  392. {
  393. m_requiresBuild.erase(requiredIter);
  394. }
  395. if (!BuildQueueContainsProject(projectInfo.m_path))
  396. {
  397. if (m_buildQueue.empty() && !m_currentBuilder)
  398. {
  399. StartProjectBuild(projectInfo);
  400. // Projects Content is already reset in fuction
  401. }
  402. else
  403. {
  404. m_buildQueue.append(projectInfo);
  405. ResetProjectsContent();
  406. }
  407. }
  408. }
  409. void ProjectsScreen::UnqueueBuildProject(const ProjectInfo& projectInfo)
  410. {
  411. m_buildQueue.removeAll(projectInfo);
  412. ResetProjectsContent();
  413. }
  414. void ProjectsScreen::NotifyCurrentScreen()
  415. {
  416. if (ShouldDisplayFirstTimeContent())
  417. {
  418. m_background.load(":/Backgrounds/FtueBackground.jpg");
  419. m_stack->setCurrentWidget(m_firstTimeContent);
  420. }
  421. else
  422. {
  423. ResetProjectsContent();
  424. }
  425. }
  426. bool ProjectsScreen::ShouldDisplayFirstTimeContent()
  427. {
  428. auto projectsResult = PythonBindingsInterface::Get()->GetProjects();
  429. if (!projectsResult.IsSuccess() || projectsResult.GetValue().isEmpty())
  430. {
  431. return true;
  432. }
  433. QSettings settings;
  434. bool displayFirstTimeContent = settings.value("displayFirstTimeContent", true).toBool();
  435. if (displayFirstTimeContent)
  436. {
  437. settings.setValue("displayFirstTimeContent", false);
  438. }
  439. return displayFirstTimeContent;
  440. }
  441. bool ProjectsScreen::RemoveInvalidProjects()
  442. {
  443. return PythonBindingsInterface::Get()->RemoveInvalidProjects();
  444. }
  445. bool ProjectsScreen::StartProjectBuild(const ProjectInfo& projectInfo)
  446. {
  447. if (ProjectUtils::FindSupportedCompiler(this))
  448. {
  449. QMessageBox::StandardButton buildProject = QMessageBox::information(
  450. this,
  451. tr("Building \"%1\"").arg(projectInfo.GetProjectDisplayName()),
  452. tr("Ready to build \"%1\"?").arg(projectInfo.GetProjectDisplayName()),
  453. QMessageBox::No | QMessageBox::Yes);
  454. if (buildProject == QMessageBox::Yes)
  455. {
  456. m_currentBuilder = new ProjectBuilderController(projectInfo, nullptr, this);
  457. ResetProjectsContent();
  458. connect(m_currentBuilder, &ProjectBuilderController::Done, this, &ProjectsScreen::ProjectBuildDone);
  459. connect(m_currentBuilder, &ProjectBuilderController::NotifyBuildProject, this, &ProjectsScreen::SuggestBuildProject);
  460. m_currentBuilder->Start();
  461. }
  462. else
  463. {
  464. SuggestBuildProjectMsg(projectInfo, false);
  465. return false;
  466. }
  467. return true;
  468. }
  469. return false;
  470. }
  471. void ProjectsScreen::ProjectBuildDone(bool success)
  472. {
  473. ProjectInfo currentBuilderProject;
  474. if (!success)
  475. {
  476. currentBuilderProject = m_currentBuilder->GetProjectInfo();
  477. }
  478. delete m_currentBuilder;
  479. m_currentBuilder = nullptr;
  480. if (!success)
  481. {
  482. SuggestBuildProjectMsg(currentBuilderProject, false);
  483. }
  484. if (!m_buildQueue.empty())
  485. {
  486. while (!StartProjectBuild(m_buildQueue.front()) && m_buildQueue.size() > 1)
  487. {
  488. m_buildQueue.pop_front();
  489. }
  490. m_buildQueue.pop_front();
  491. }
  492. ResetProjectsContent();
  493. }
  494. QList<ProjectInfo>::iterator ProjectsScreen::RequiresBuildProjectIterator(const QString& projectPath)
  495. {
  496. QString nativeProjPath(QDir::toNativeSeparators(projectPath));
  497. auto projectIter = m_requiresBuild.begin();
  498. for (; projectIter != m_requiresBuild.end(); ++projectIter)
  499. {
  500. if (QDir::toNativeSeparators(projectIter->m_path) == nativeProjPath)
  501. {
  502. break;
  503. }
  504. }
  505. return projectIter;
  506. }
  507. bool ProjectsScreen::BuildQueueContainsProject(const QString& projectPath)
  508. {
  509. QString nativeProjPath(QDir::toNativeSeparators(projectPath));
  510. for (const ProjectInfo& project : m_buildQueue)
  511. {
  512. if (QDir::toNativeSeparators(project.m_path) == nativeProjPath)
  513. {
  514. return true;
  515. }
  516. }
  517. return false;
  518. }
  519. bool ProjectsScreen::WarnIfInBuildQueue(const QString& projectPath)
  520. {
  521. if (BuildQueueContainsProject(projectPath))
  522. {
  523. QMessageBox::warning(
  524. this,
  525. tr("Action Temporarily Disabled!"),
  526. tr("Action not allowed on projects in build queue."));
  527. return true;
  528. }
  529. return false;
  530. }
  531. } // namespace O3DE::ProjectManager