UpdateProjectCtrl.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  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 <ProjectGemCatalogScreen.h>
  9. #include <ProjectUtils.h>
  10. #include <GemRepo/GemRepoScreen.h>
  11. #include <CreateAGemScreen.h>
  12. #include <ProjectManagerDefs.h>
  13. #include <PythonBindingsInterface.h>
  14. #include <ScreenHeaderWidget.h>
  15. #include <ScreensCtrl.h>
  16. #include <UpdateProjectCtrl.h>
  17. #include <UpdateProjectSettingsScreen.h>
  18. #include <ProjectUtils.h>
  19. #include <DownloadController.h>
  20. #include <SettingsInterface.h>
  21. #include <QDialogButtonBox>
  22. #include <QMessageBox>
  23. #include <QPushButton>
  24. #include <QStackedWidget>
  25. #include <QTabWidget>
  26. #include <QVBoxLayout>
  27. #include <QDir>
  28. namespace O3DE::ProjectManager
  29. {
  30. UpdateProjectCtrl::UpdateProjectCtrl(DownloadController* downloadController, QWidget* parent)
  31. : ScreenWidget(parent)
  32. {
  33. QVBoxLayout* vLayout = new QVBoxLayout();
  34. vLayout->setContentsMargins(0, 0, 0, 0);
  35. m_header = new ScreenHeader(this);
  36. m_header->setTitle(tr(""));
  37. m_header->setSubTitle(tr("Edit Project Settings:"));
  38. connect(m_header->backButton(), &QPushButton::clicked, this, &UpdateProjectCtrl::HandleBackButton);
  39. vLayout->addWidget(m_header);
  40. m_updateSettingsScreen = new UpdateProjectSettingsScreen();
  41. m_projectGemCatalogScreen = new ProjectGemCatalogScreen(downloadController, this);
  42. m_gemRepoScreen = new GemRepoScreen(this);
  43. connect(m_projectGemCatalogScreen, &ScreenWidget::ChangeScreenRequest, this, &UpdateProjectCtrl::OnChangeScreenRequest);
  44. connect(static_cast<ScreensCtrl*>(parent), &ScreensCtrl::NotifyProjectRemoved, m_projectGemCatalogScreen, &GemCatalogScreen::NotifyProjectRemoved);
  45. m_stack = new QStackedWidget(this);
  46. m_stack->setObjectName("body");
  47. m_stack->setSizePolicy(QSizePolicy(QSizePolicy::Preferred, QSizePolicy::Expanding));
  48. vLayout->addWidget(m_stack);
  49. QFrame* topBarFrameWidget = new QFrame(this);
  50. topBarFrameWidget->setObjectName("projectSettingsTopFrame");
  51. QHBoxLayout* topBarHLayout = new QHBoxLayout();
  52. topBarHLayout->setContentsMargins(0, 0, 0, 0);
  53. topBarFrameWidget->setLayout(topBarHLayout);
  54. QTabWidget* tabWidget = new QTabWidget();
  55. tabWidget->setObjectName("projectSettingsTab");
  56. tabWidget->tabBar()->setObjectName("projectSettingsTabBar");
  57. tabWidget->tabBar()->setFocusPolicy(Qt::TabFocus);
  58. tabWidget->addTab(m_updateSettingsScreen, tr("General"));
  59. QPushButton* gemsButton = new QPushButton(tr("Configure Gems"), this);
  60. gemsButton->setProperty("secondary", true);
  61. topBarHLayout->addWidget(gemsButton);
  62. tabWidget->setCornerWidget(gemsButton);
  63. topBarHLayout->addWidget(tabWidget);
  64. m_stack->addWidget(topBarFrameWidget);
  65. m_stack->addWidget(m_projectGemCatalogScreen);
  66. m_stack->addWidget(m_gemRepoScreen);
  67. QDialogButtonBox* backNextButtons = new QDialogButtonBox();
  68. backNextButtons->setObjectName("footer");
  69. vLayout->addWidget(backNextButtons);
  70. m_backButton = backNextButtons->addButton(tr("Back"), QDialogButtonBox::RejectRole);
  71. m_backButton->setProperty("secondary", true);
  72. m_nextButton = backNextButtons->addButton(tr("Next"), QDialogButtonBox::ApplyRole);
  73. m_nextButton->setProperty("primary", true);
  74. connect(gemsButton, &QPushButton::clicked, this, &UpdateProjectCtrl::HandleGemsButton);
  75. connect(m_backButton, &QPushButton::clicked, this, &UpdateProjectCtrl::HandleBackButton);
  76. connect(m_nextButton, &QPushButton::clicked, this, &UpdateProjectCtrl::HandleNextButton);
  77. connect(static_cast<ScreensCtrl*>(parent), &ScreensCtrl::NotifyCurrentProject, this, &UpdateProjectCtrl::UpdateCurrentProject);
  78. Update();
  79. setLayout(vLayout);
  80. }
  81. ProjectManagerScreen UpdateProjectCtrl::GetScreenEnum()
  82. {
  83. return ProjectManagerScreen::UpdateProject;
  84. }
  85. bool UpdateProjectCtrl::ContainsScreen(ProjectManagerScreen screen)
  86. {
  87. // Do not include GemRepos because we don't want to advertise jumping to it from all other screens here
  88. return screen == GetScreenEnum() || screen == ProjectManagerScreen::ProjectGemCatalog;
  89. }
  90. void UpdateProjectCtrl::GoToScreen(ProjectManagerScreen screen)
  91. {
  92. OnChangeScreenRequest(screen);
  93. }
  94. // Called when pressing "Edit Project Settings..."
  95. void UpdateProjectCtrl::NotifyCurrentScreen()
  96. {
  97. m_stack->setCurrentIndex(ScreenOrder::Settings);
  98. Update();
  99. }
  100. void UpdateProjectCtrl::OnChangeScreenRequest(ProjectManagerScreen screen)
  101. {
  102. if (screen == ProjectManagerScreen::GemRepos)
  103. {
  104. m_stack->setCurrentWidget(m_gemRepoScreen);
  105. m_gemRepoScreen->NotifyCurrentScreen();
  106. Update();
  107. }
  108. else if (screen == ProjectManagerScreen::ProjectGemCatalog)
  109. {
  110. m_projectGemCatalogScreen->ReinitForProject(m_projectInfo.m_path);
  111. m_projectGemCatalogScreen->NotifyCurrentScreen();
  112. m_stack->setCurrentWidget(m_projectGemCatalogScreen);
  113. Update();
  114. }
  115. else if (screen == ProjectManagerScreen::UpdateProjectSettings)
  116. {
  117. m_stack->setCurrentWidget(m_updateSettingsScreen);
  118. m_updateSettingsScreen->NotifyCurrentScreen();
  119. Update();
  120. }
  121. else
  122. {
  123. emit ChangeScreenRequest(screen);
  124. }
  125. }
  126. void UpdateProjectCtrl::HandleGemsButton()
  127. {
  128. if (UpdateProjectSettings(true))
  129. {
  130. m_projectGemCatalogScreen->ReinitForProject(m_projectInfo.m_path);
  131. m_projectGemCatalogScreen->NotifyCurrentScreen();
  132. m_stack->setCurrentWidget(m_projectGemCatalogScreen);
  133. Update();
  134. }
  135. }
  136. void UpdateProjectCtrl::HandleBackButton()
  137. {
  138. if (m_stack->currentIndex() > 0)
  139. {
  140. m_stack->setCurrentIndex(m_stack->currentIndex() - 1);
  141. if (ScreenWidget* screenWidget = qobject_cast<ScreenWidget*>(m_stack->currentWidget()); screenWidget)
  142. {
  143. screenWidget->NotifyCurrentScreen();
  144. }
  145. Update();
  146. }
  147. else
  148. {
  149. if (UpdateProjectSettings(true))
  150. {
  151. emit GoToPreviousScreenRequest();
  152. }
  153. }
  154. }
  155. void UpdateProjectCtrl::HandleNextButton()
  156. {
  157. bool shouldRebuild = false;
  158. if (m_stack->currentIndex() == ScreenOrder::Settings && m_updateSettingsScreen)
  159. {
  160. if (!UpdateProjectSettings())
  161. {
  162. return;
  163. }
  164. }
  165. else if (m_stack->currentIndex() == ScreenOrder::Gems && m_projectGemCatalogScreen)
  166. {
  167. if (!m_projectGemCatalogScreen->GetDownloadController()->IsDownloadQueueEmpty())
  168. {
  169. QMessageBox::critical(this, tr("Gems downloading"), tr("You must wait for gems to finish downloading before continuing."));
  170. return;
  171. }
  172. // Enable or disable the gems that got adjusted in the gem catalog and apply them to the given project.
  173. const ProjectGemCatalogScreen::ConfiguredGemsResult result = m_projectGemCatalogScreen->ConfigureGemsForProject(m_projectInfo.m_path);
  174. if (result == ProjectGemCatalogScreen::ConfiguredGemsResult::Failed)
  175. {
  176. QMessageBox::critical(this, tr("Failed to configure gems"), tr("Failed to configure gems for project."));
  177. }
  178. if (result != ProjectGemCatalogScreen::ConfiguredGemsResult::Success)
  179. {
  180. return;
  181. }
  182. shouldRebuild = true;
  183. }
  184. if (shouldRebuild)
  185. {
  186. emit NotifyBuildProject(m_projectInfo);
  187. }
  188. emit ChangeScreenRequest(ProjectManagerScreen::Projects);
  189. }
  190. void UpdateProjectCtrl::UpdateCurrentProject(const QString& projectPath)
  191. {
  192. auto projectResult = PythonBindingsInterface::Get()->GetProject(projectPath);
  193. if (projectResult.IsSuccess())
  194. {
  195. m_projectInfo = projectResult.GetValue();
  196. }
  197. Update();
  198. UpdateSettingsScreen();
  199. }
  200. void UpdateProjectCtrl::Update()
  201. {
  202. if (m_stack->currentIndex() == ScreenOrder::GemRepos)
  203. {
  204. m_header->setTitle(QString(tr("Edit Project Settings: \"%1\"")).arg(m_projectInfo.GetProjectDisplayName()));
  205. m_header->setSubTitle(QString(tr("Remote Sources")));
  206. m_nextButton->setVisible(false);
  207. }
  208. else if (m_stack->currentIndex() == ScreenOrder::Gems)
  209. {
  210. m_header->setTitle(QString(tr("Edit Project Settings: \"%1\"")).arg(m_projectInfo.GetProjectDisplayName()));
  211. m_header->setSubTitle(QString(tr("Configure Gems")));
  212. m_nextButton->setText(tr("Save"));
  213. m_nextButton->setVisible(true);
  214. }
  215. else
  216. {
  217. m_header->setTitle("");
  218. m_header->setSubTitle(QString(tr("Edit Project Settings: \"%1\"")).arg(m_projectInfo.GetProjectDisplayName()));
  219. m_nextButton->setText(tr("Save"));
  220. m_nextButton->setVisible(true);
  221. }
  222. }
  223. void UpdateProjectCtrl::UpdateSettingsScreen()
  224. {
  225. m_updateSettingsScreen->SetProjectInfo(m_projectInfo);
  226. }
  227. bool UpdateProjectCtrl::UpdateProjectSettings(bool shouldConfirm)
  228. {
  229. AZ_Assert(m_updateSettingsScreen, "Update settings screen is nullptr.")
  230. ProjectInfo newProjectSettings = m_updateSettingsScreen->GetProjectInfo();
  231. if (m_projectInfo != newProjectSettings)
  232. {
  233. if (shouldConfirm)
  234. {
  235. QMessageBox::StandardButton questionResult = QMessageBox::question(
  236. this,
  237. QObject::tr("Unsaved changes"),
  238. QObject::tr("Would you like to save your changes to project settings?"),
  239. QMessageBox::No | QMessageBox::Yes
  240. );
  241. if (questionResult == QMessageBox::No)
  242. {
  243. return true;
  244. }
  245. }
  246. if (!m_updateSettingsScreen->Validate())
  247. {
  248. QMessageBox::critical(this, tr("Invalid project settings"), tr("Invalid project settings"));
  249. return false;
  250. }
  251. // Move project first to avoid trying to update settings at the new location before it has been moved there
  252. if (QDir(newProjectSettings.m_path) != QDir(m_projectInfo.m_path))
  253. {
  254. if (!ProjectUtils::MoveProject(m_projectInfo.m_path, newProjectSettings.m_path, this))
  255. {
  256. QMessageBox::critical(this, tr("Project move failed"), tr("Failed to move project."));
  257. return false;
  258. }
  259. }
  260. // Check engine compatibility if a new engine was selected
  261. if (QDir(newProjectSettings.m_enginePath) != QDir(m_projectInfo.m_enginePath))
  262. {
  263. auto incompatibleObjectsResult = PythonBindingsInterface::Get()->GetProjectEngineIncompatibleObjects(newProjectSettings.m_path, newProjectSettings.m_enginePath);
  264. AZStd::string errorTitle, generalError, detailedError;
  265. if (!incompatibleObjectsResult)
  266. {
  267. errorTitle = "Failed to check project compatibility";
  268. generalError = incompatibleObjectsResult.GetError().first;
  269. generalError.append("\nDo you still want to save your changes to project settings?");
  270. detailedError = incompatibleObjectsResult.GetError().second;
  271. }
  272. else if (const auto& incompatibleObjects = incompatibleObjectsResult.GetValue(); !incompatibleObjects.isEmpty())
  273. {
  274. // provide a couple more user friendly error messages for uncommon cases
  275. if (incompatibleObjects.at(0).contains(ProjectUtils::EngineJsonFilename.data(), Qt::CaseInsensitive))
  276. {
  277. errorTitle = "Failed to read engine.json";
  278. generalError = "The projects compatibility with the new engine could not be checked because the engine.json could not be read";
  279. }
  280. else if (incompatibleObjects.at(0).contains(ProjectUtils::ProjectJsonFilename.data(), Qt::CaseInsensitive))
  281. {
  282. errorTitle = "Invalid project, failed to read project.json";
  283. generalError = "The projects compatibility with the new engine could not be checked because the project.json could not be read.";
  284. }
  285. else
  286. {
  287. // could be gems, apis or both
  288. errorTitle = "Project may not be compatible with new engine";
  289. generalError = incompatibleObjects.join("\n").toUtf8().constData();
  290. generalError.append("\nDo you still want to save your changes to project settings?");
  291. }
  292. }
  293. if (!generalError.empty())
  294. {
  295. QMessageBox warningDialog(QMessageBox::Warning, errorTitle.c_str(), generalError.c_str(), QMessageBox::Yes | QMessageBox::No, this);
  296. warningDialog.setDetailedText(detailedError.c_str());
  297. if(warningDialog.exec() == QMessageBox::No)
  298. {
  299. return false;
  300. }
  301. AZ_Warning("ProjectManager", false, "Proceeding with saving project settings after engine compatibility check failed.");
  302. }
  303. }
  304. if (auto result = PythonBindingsInterface::Get()->UpdateProject(newProjectSettings); !result.IsSuccess())
  305. {
  306. QMessageBox::critical(this, tr("Project update failed"), tr(result.GetError().c_str()));
  307. return false;
  308. }
  309. if (QDir(newProjectSettings.m_path) != QDir(m_projectInfo.m_path) ||
  310. newProjectSettings.m_projectName != m_projectInfo.m_projectName ||
  311. QDir(newProjectSettings.m_enginePath) != QDir(m_projectInfo.m_enginePath))
  312. {
  313. // Remove project build successfully paths for both old and new projects
  314. // because a full rebuild is required when moving projects
  315. auto settings = SettingsInterface::Get();
  316. settings->SetProjectBuiltSuccessfully(m_projectInfo, false);
  317. settings->SetProjectBuiltSuccessfully(newProjectSettings, false);
  318. emit NotifyBuildProject(newProjectSettings);
  319. }
  320. if (!newProjectSettings.m_newPreviewImagePath.isEmpty())
  321. {
  322. if (!ProjectUtils::ReplaceProjectFile(
  323. QDir(newProjectSettings.m_path).filePath(newProjectSettings.m_iconPath), newProjectSettings.m_newPreviewImagePath))
  324. {
  325. QMessageBox::critical(this, tr("File replace failed"), tr("Failed to replace project preview image."));
  326. return false;
  327. }
  328. m_updateSettingsScreen->ResetProjectPreviewPath();
  329. }
  330. m_projectInfo = newProjectSettings;
  331. }
  332. return true;
  333. }
  334. } // namespace O3DE::ProjectManager