GemCatalogScreen.cpp 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823
  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 <GemCatalog/GemCatalogScreen.h>
  9. #include <AzCore/Dependency/Dependency.h>
  10. #include <PythonBindingsInterface.h>
  11. #include <GemCatalog/GemCatalogHeaderWidget.h>
  12. #include <GemCatalog/GemFilterWidget.h>
  13. #include <GemCatalog/GemListView.h>
  14. #include <GemCatalog/GemInspector.h>
  15. #include <GemCatalog/GemModel.h>
  16. #include <GemCatalog/GemListHeaderWidget.h>
  17. #include <GemCatalog/GemSortFilterProxyModel.h>
  18. #include <GemCatalog/GemUpdateDialog.h>
  19. #include <GemCatalog/GemUninstallDialog.h>
  20. #include <GemCatalog/GemItemDelegate.h>
  21. #include <GemRepo/GemRepoScreen.h>
  22. #include <DownloadController.h>
  23. #include <ProjectUtils.h>
  24. #include <AdjustableHeaderWidget.h>
  25. #include <ScreensCtrl.h>
  26. #include <CreateAGemScreen.h>
  27. #include <EditAGemScreen.h>
  28. #include <QVBoxLayout>
  29. #include <QHBoxLayout>
  30. #include <QTimer>
  31. #include <PythonBindingsInterface.h>
  32. #include <QMessageBox>
  33. #include <QDir>
  34. #include <QFileInfo>
  35. #include <QStandardPaths>
  36. #include <QFileDialog>
  37. #include <QMessageBox>
  38. #include <QHash>
  39. #include <QSet>
  40. #include <QStackedWidget>
  41. namespace O3DE::ProjectManager
  42. {
  43. GemCatalogScreen::GemCatalogScreen(DownloadController* downloadController, bool readOnly, QWidget* parent)
  44. : ScreenWidget(parent)
  45. , m_readOnly(readOnly)
  46. , m_downloadController(downloadController)
  47. {
  48. // The width of either side panel (filters, inspector) in the catalog
  49. constexpr int sidePanelWidth = 240;
  50. // Querying qApp about styling reports the scroll bar being larger than it is so define it manually
  51. constexpr int verticalScrollBarWidth = 8;
  52. setObjectName("GemCatalogScreen");
  53. m_gemModel = new GemModel(this);
  54. m_proxyModel = new GemSortFilterProxyModel(m_gemModel, this);
  55. // default to sort by gem display name
  56. m_proxyModel->setSortRole(GemModel::RoleDisplayName);
  57. m_proxyModel->setSortCaseSensitivity(Qt::CaseInsensitive);
  58. QVBoxLayout* vLayout = new QVBoxLayout();
  59. vLayout->setMargin(0);
  60. vLayout->setSpacing(0);
  61. setLayout(vLayout);
  62. m_headerWidget = new GemCatalogHeaderWidget(m_gemModel, m_proxyModel, m_downloadController);
  63. vLayout->addWidget(m_headerWidget);
  64. connect(m_gemModel, &GemModel::gemStatusChanged, this, &GemCatalogScreen::OnGemStatusChanged);
  65. connect(m_gemModel, &GemModel::dependencyGemStatusChanged, this, &GemCatalogScreen::OnDependencyGemStatusChanged);
  66. connect(m_gemModel->GetSelectionModel(), &QItemSelectionModel::selectionChanged, this, &GemCatalogScreen::ShowInspector);
  67. connect(m_headerWidget, &GemCatalogHeaderWidget::RefreshGems, this, &GemCatalogScreen::Refresh);
  68. connect(m_headerWidget, &GemCatalogHeaderWidget::OpenGemsRepo, this, &GemCatalogScreen::HandleOpenGemRepo);
  69. connect(m_headerWidget, &GemCatalogHeaderWidget::CreateGem, this, &GemCatalogScreen::HandleCreateGem);
  70. connect(m_headerWidget, &GemCatalogHeaderWidget::AddGem, this, &GemCatalogScreen::OnAddGemClicked);
  71. connect(m_headerWidget, &GemCatalogHeaderWidget::UpdateGemCart, this, &GemCatalogScreen::UpdateAndShowGemCart);
  72. connect(m_downloadController, &DownloadController::Done, this, &GemCatalogScreen::OnGemDownloadResult);
  73. ScreensCtrl* screensCtrl = GetScreensCtrl(this);
  74. if (screensCtrl)
  75. {
  76. connect(screensCtrl, &ScreensCtrl::NotifyRemoteContentRefreshed, [this]() { m_needRefresh = true; });
  77. }
  78. QHBoxLayout* hLayout = new QHBoxLayout();
  79. hLayout->setMargin(0);
  80. vLayout->addLayout(hLayout);
  81. m_rightPanelStack = new QStackedWidget(this);
  82. m_rightPanelStack->setFixedWidth(sidePanelWidth);
  83. m_gemInspector = new GemInspector(m_gemModel, m_rightPanelStack, m_readOnly);
  84. connect(
  85. m_gemInspector,
  86. &GemInspector::TagClicked,
  87. [=](const Tag& tag)
  88. {
  89. SelectGem(tag.id);
  90. });
  91. connect(m_gemInspector, &GemInspector::UpdateGem, this, &GemCatalogScreen::UpdateGem);
  92. connect(m_gemInspector, &GemInspector::UninstallGem, this, &GemCatalogScreen::UninstallGem);
  93. connect(m_gemInspector, &GemInspector::EditGem, this, &GemCatalogScreen::HandleEditGem);
  94. connect(m_gemInspector, &GemInspector::DownloadGem, this, &GemCatalogScreen::DownloadGem);
  95. connect(m_gemInspector, &GemInspector::ShowToastNotification, this, &GemCatalogScreen::ShowStandardToastNotification);
  96. QWidget* filterWidget = new QWidget(this);
  97. filterWidget->setFixedWidth(sidePanelWidth);
  98. m_filterWidgetLayout = new QVBoxLayout();
  99. m_filterWidgetLayout->setMargin(0);
  100. m_filterWidgetLayout->setSpacing(0);
  101. filterWidget->setLayout(m_filterWidgetLayout);
  102. m_filterWidget = new GemFilterWidget(m_proxyModel);
  103. m_filterWidgetLayout->addWidget(m_filterWidget);
  104. GemListHeaderWidget* catalogHeaderWidget = new GemListHeaderWidget(m_proxyModel);
  105. connect(catalogHeaderWidget, &GemListHeaderWidget::OnRefresh, this, &GemCatalogScreen::Refresh);
  106. constexpr int previewWidth = GemItemDelegate::s_itemMargins.left() + GemPreviewImageWidth +
  107. AdjustableHeaderWidget::s_headerTextIndent;
  108. constexpr int versionWidth = GemItemDelegate::s_versionSize + GemItemDelegate::s_versionSizeSpacing;
  109. constexpr int statusWidth = GemItemDelegate::s_statusIconSize + GemItemDelegate::s_statusButtonSpacing +
  110. GemItemDelegate::s_buttonWidth + GemItemDelegate::s_contentMargins.right();
  111. constexpr int minHeaderSectionWidth = AZStd::min(previewWidth, AZStd::min(versionWidth, statusWidth));
  112. AdjustableHeaderWidget* listHeaderWidget = new AdjustableHeaderWidget(
  113. QStringList{ tr("Gem Image"), tr("Gem Name"), tr("Gem Summary"), tr("Latest Version"), tr("Status") },
  114. QVector<int>{ previewWidth,
  115. GemItemDelegate::s_defaultSummaryStartX - previewWidth,
  116. 0, // Section is set to stretch to fit
  117. versionWidth,
  118. statusWidth},
  119. minHeaderSectionWidth,
  120. QVector<QHeaderView::ResizeMode>{ QHeaderView::ResizeMode::Fixed,
  121. QHeaderView::ResizeMode::Interactive,
  122. QHeaderView::ResizeMode::Stretch,
  123. QHeaderView::ResizeMode::Fixed,
  124. QHeaderView::ResizeMode::Fixed },
  125. this);
  126. m_gemListView = new GemListView(m_proxyModel, m_proxyModel->GetSelectionModel(), listHeaderWidget, m_readOnly, this);
  127. QHBoxLayout* listHeaderLayout = new QHBoxLayout();
  128. listHeaderLayout->setMargin(0);
  129. listHeaderLayout->setSpacing(0);
  130. listHeaderLayout->addSpacing(GemItemDelegate::s_itemMargins.left());
  131. listHeaderLayout->addWidget(listHeaderWidget);
  132. listHeaderLayout->addSpacing(GemItemDelegate::s_itemMargins.right() + verticalScrollBarWidth);
  133. QVBoxLayout* middleVLayout = new QVBoxLayout();
  134. middleVLayout->setMargin(0);
  135. middleVLayout->setSpacing(0);
  136. middleVLayout->addWidget(catalogHeaderWidget);
  137. middleVLayout->addLayout(listHeaderLayout);
  138. middleVLayout->addWidget(m_gemListView);
  139. hLayout->addWidget(filterWidget);
  140. hLayout->addLayout(middleVLayout);
  141. hLayout->addWidget(m_rightPanelStack);
  142. m_rightPanelStack->addWidget(m_gemInspector);
  143. m_notificationsView = AZStd::make_unique<AzToolsFramework::ToastNotificationsView>(this, AZ_CRC_CE("GemCatalogNotificationsView"));
  144. m_notificationsView->SetOffset(QPoint(10, 70));
  145. m_notificationsView->SetMaxQueuedNotifications(1);
  146. m_notificationsView->SetRejectDuplicates(false); // we want to show notifications if a user repeats actions
  147. }
  148. void GemCatalogScreen::SetUpScreensControl(QWidget* parent)
  149. {
  150. m_screensControl = qobject_cast<ScreensCtrl*>(parent);
  151. if (m_screensControl)
  152. {
  153. ScreenWidget* createGemScreen = m_screensControl->FindScreen(ProjectManagerScreen::CreateGem);
  154. if (createGemScreen)
  155. {
  156. CreateGem* createGem = static_cast<CreateGem*>(createGemScreen);
  157. connect(createGem, &CreateGem::GemCreated, this, &GemCatalogScreen::HandleGemCreated);
  158. }
  159. ScreenWidget* editGemScreen = m_screensControl->FindScreen(ProjectManagerScreen::EditGem);
  160. if (editGemScreen)
  161. {
  162. EditGem* editGem = static_cast<EditGem*>(editGemScreen);
  163. connect(editGem, &EditGem::GemEdited, this, &GemCatalogScreen::HandleGemEdited);
  164. }
  165. }
  166. }
  167. void GemCatalogScreen::NotifyCurrentScreen()
  168. {
  169. if (m_readOnly && m_gemModel->rowCount() == 0)
  170. {
  171. // init the read only catalog the first time it is shown
  172. ReinitForProject(m_projectPath);
  173. }
  174. else if (m_needRefresh)
  175. {
  176. // generally we need to refresh because remote repos were updated
  177. m_needRefresh = false;
  178. Refresh();
  179. }
  180. }
  181. void GemCatalogScreen::NotifyProjectRemoved(const QString& projectPath)
  182. {
  183. // Use QFileInfo because the project path might be the project folder
  184. // or a remote project.json file in the cache
  185. if (QFileInfo(projectPath) == QFileInfo(m_projectPath))
  186. {
  187. m_projectPath = "";
  188. m_gemModel->Clear();
  189. m_gemsToRegisterWithProject.clear();
  190. }
  191. }
  192. void GemCatalogScreen::ReinitForProject(const QString& projectPath)
  193. {
  194. // Avoid slow rebuilding, user can manually refresh if needed
  195. // Use QFileInfo because the project path might be the project folder
  196. // or a remote project.json file in the cache
  197. if (m_gemModel->rowCount() > 0 && QFileInfo(projectPath) == QFileInfo(m_projectPath))
  198. {
  199. return;
  200. }
  201. m_projectPath = projectPath;
  202. m_gemModel->Clear();
  203. m_gemsToRegisterWithProject.clear();
  204. FillModel(projectPath);
  205. m_gemModel->UpdateGemDependencies();
  206. m_proxyModel->sort(/*column=*/0);
  207. m_proxyModel->ResetFilters();
  208. m_filterWidget->UpdateAllFilters(/*clearAllCheckboxes=*/true);
  209. m_headerWidget->ReinitForProject();
  210. QModelIndex firstProxyIndex = m_proxyModel->index(0, 0);
  211. m_proxyModel->GetSelectionModel()->setCurrentIndex(firstProxyIndex, QItemSelectionModel::ClearAndSelect);
  212. m_gemInspector->Update(m_proxyModel->mapToSource(firstProxyIndex));
  213. }
  214. void GemCatalogScreen::OnAddGemClicked()
  215. {
  216. EngineInfo engineInfo;
  217. QString defaultPath;
  218. AZ::Outcome<EngineInfo> engineInfoResult = PythonBindingsInterface::Get()->GetEngineInfo();
  219. if (engineInfoResult.IsSuccess())
  220. {
  221. engineInfo = engineInfoResult.GetValue();
  222. defaultPath = engineInfo.m_defaultGemsFolder;
  223. }
  224. if (defaultPath.isEmpty())
  225. {
  226. defaultPath = QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation);
  227. }
  228. QString directory = QDir::toNativeSeparators(QFileDialog::getExistingDirectory(this, tr("Browse"), defaultPath));
  229. if (!directory.isEmpty())
  230. {
  231. // register the gem to the o3de_manifest.json and to the project after the user confirms
  232. // project creation/update
  233. auto registerResult = PythonBindingsInterface::Get()->RegisterGem(directory);
  234. if(!registerResult)
  235. {
  236. QMessageBox::critical(this, tr("Failed to add gem"), registerResult.GetError().c_str());
  237. }
  238. else
  239. {
  240. m_gemsToRegisterWithProject.insert(directory);
  241. AZ::Outcome<GemInfo> gemInfoResult = PythonBindingsInterface::Get()->GetGemInfo(directory);
  242. if (gemInfoResult)
  243. {
  244. // We added this local gem so set it's status to downloaded
  245. GemInfo& addedGemInfo = gemInfoResult.GetValue<GemInfo>();
  246. addedGemInfo.m_downloadStatus = GemInfo::DownloadStatus::Downloaded;
  247. AddToGemModel(addedGemInfo);
  248. ShowStandardToastNotification(tr("%1 added").arg(addedGemInfo.m_displayName));
  249. }
  250. }
  251. }
  252. }
  253. void GemCatalogScreen::AddToGemModel(const GemInfo& gemInfo)
  254. {
  255. const auto modelIndex = m_gemModel->AddGem(gemInfo);
  256. m_gemModel->UpdateGemDependencies();
  257. m_proxyModel->sort(/*column=*/0);
  258. m_proxyModel->InvalidateFilter();
  259. m_filterWidget->UpdateAllFilters(/*clearAllCheckboxes=*/false);
  260. // attempt to select the newly added gem
  261. if(auto proxyIndex = m_proxyModel->mapFromSource(modelIndex); proxyIndex.isValid())
  262. {
  263. m_proxyModel->GetSelectionModel()->setCurrentIndex(proxyIndex, QItemSelectionModel::ClearAndSelect);
  264. }
  265. }
  266. void GemCatalogScreen::Refresh(bool refreshRemoteRepos)
  267. {
  268. QSet<QPersistentModelIndex> validIndexes;
  269. if (const auto& outcome = PythonBindingsInterface::Get()->GetAllGemInfos(m_projectPath); outcome.IsSuccess())
  270. {
  271. const auto& indexes = m_gemModel->AddGems(outcome.GetValue(), /*updateExisting=*/true);
  272. validIndexes = QSet(indexes.cbegin(), indexes.cend());
  273. }
  274. if(refreshRemoteRepos)
  275. {
  276. PythonBindingsInterface::Get()->RefreshAllGemRepos();
  277. }
  278. if (const auto& outcome = PythonBindingsInterface::Get()->GetGemInfosForAllRepos(); outcome.IsSuccess())
  279. {
  280. const auto& indexes = m_gemModel->AddGems(outcome.GetValue(), /*updateExisting=*/true);
  281. validIndexes.unite(QSet(indexes.cbegin(), indexes.cend()));
  282. }
  283. // remove gems from the model that no longer exist in the hash and are not project dependencies
  284. int i = 0;
  285. while (i < m_gemModel->rowCount())
  286. {
  287. QModelIndex index = m_gemModel->index(i,0);
  288. const bool gemFound = validIndexes.contains(index);
  289. if (!gemFound && !m_gemModel->IsAdded(index) && !m_gemModel->IsAddedDependency(index))
  290. {
  291. m_gemModel->RemoveGem(index);
  292. }
  293. else
  294. {
  295. if (!gemFound && (m_gemModel->IsAdded(index) || m_gemModel->IsAddedDependency(index)))
  296. {
  297. QString gemName = m_gemModel->GetName(index);
  298. const QString error = tr("Gem %1 was removed or unregistered, but is still used by the project.").arg(gemName);
  299. AZ_Warning("Project Manager", false, error.toUtf8().constData());
  300. QMessageBox::warning(this, tr("Gem not found"), error.toUtf8().constData());
  301. }
  302. i++;
  303. }
  304. }
  305. m_gemModel->UpdateGemDependencies();
  306. m_proxyModel->sort(/*column=*/0);
  307. m_proxyModel->InvalidateFilter();
  308. m_filterWidget->UpdateAllFilters(/*clearAllCheckboxes=*/false);
  309. auto selectedIndex = m_proxyModel->GetSelectionModel()->currentIndex();
  310. // be careful to not pass in the proxy model index, we want the source model index
  311. if (selectedIndex.isValid())
  312. {
  313. m_gemInspector->Update(m_proxyModel->mapToSource(selectedIndex));
  314. }
  315. else
  316. {
  317. QModelIndex firstProxyIndex = m_proxyModel->index(0, 0);
  318. m_proxyModel->GetSelectionModel()->setCurrentIndex(firstProxyIndex, QItemSelectionModel::ClearAndSelect);
  319. m_gemInspector->Update(m_proxyModel->mapToSource(firstProxyIndex));
  320. }
  321. }
  322. void GemCatalogScreen::OnGemStatusChanged(const QString& gemName, uint32_t numChangedDependencies)
  323. {
  324. if (m_notificationsEnabled && !m_readOnly)
  325. {
  326. auto modelIndex = m_gemModel->FindIndexByNameString(gemName);
  327. bool added = GemModel::IsAdded(modelIndex);
  328. bool dependency = GemModel::IsAddedDependency(modelIndex);
  329. bool gemStateChanged = (added && !dependency) || (!added && !dependency);
  330. if (!gemStateChanged && !numChangedDependencies)
  331. {
  332. // no actual changes made
  333. return;
  334. }
  335. QString notification;
  336. if (gemStateChanged)
  337. {
  338. const GemInfo& gemInfo = GemModel::GetGemInfo(modelIndex);
  339. const QString& newVersion = GemModel::GetNewVersion(modelIndex);
  340. const QString& version = newVersion.isEmpty() ? gemInfo.m_version : newVersion;
  341. // avoid showing the version twice if it's already in the display name
  342. if (gemInfo.m_isEngineGem || (version.isEmpty() || gemInfo.m_displayName.contains(version) || version.contains("Unknown", Qt::CaseInsensitive)))
  343. {
  344. notification = gemInfo.m_displayName;
  345. }
  346. else
  347. {
  348. notification = QString("%1 %2").arg(gemInfo.m_displayName, version);
  349. }
  350. if (numChangedDependencies > 0)
  351. {
  352. notification += tr(" and ");
  353. }
  354. if (added)
  355. {
  356. if (newVersion.isEmpty() && (GemModel::GetDownloadStatus(modelIndex) == GemInfo::DownloadStatus::NotDownloaded) ||
  357. (GemModel::GetDownloadStatus(modelIndex) == GemInfo::DownloadStatus::DownloadFailed))
  358. {
  359. // download the current version
  360. DownloadGem(modelIndex, gemInfo.m_version, gemInfo.m_path);
  361. }
  362. else if (!newVersion.isEmpty())
  363. {
  364. const GemInfo& newVersionGemInfo = GemModel::GetGemInfo(modelIndex, newVersion);
  365. if (newVersionGemInfo.m_downloadStatus == GemInfo::DownloadStatus::NotDownloaded ||
  366. GemModel::GetDownloadStatus(modelIndex) == GemInfo::DownloadStatus::DownloadFailed)
  367. {
  368. // download the new version
  369. DownloadGem(modelIndex, newVersionGemInfo.m_version, newVersionGemInfo.m_path);
  370. }
  371. }
  372. }
  373. }
  374. if (numChangedDependencies == 1)
  375. {
  376. notification += tr("1 Gem dependency");
  377. }
  378. else if (numChangedDependencies > 1)
  379. {
  380. notification += tr("%1 Gem %2").arg(numChangedDependencies).arg(tr("dependencies"));
  381. }
  382. notification += (added ? tr(" activated") : tr(" deactivated"));
  383. ShowStandardToastNotification(notification);
  384. }
  385. }
  386. void GemCatalogScreen::ShowStandardToastNotification(const QString& notification)
  387. {
  388. AzQtComponents::ToastConfiguration toastConfiguration(AzQtComponents::ToastType::Custom, notification, "");
  389. toastConfiguration.m_customIconImage = ":/gem.svg";
  390. toastConfiguration.m_borderRadius = 4;
  391. toastConfiguration.m_duration = AZStd::chrono::milliseconds(3000);
  392. m_notificationsView->ShowToastNotification(toastConfiguration);
  393. }
  394. void GemCatalogScreen::OnDependencyGemStatusChanged(const QString& gemName)
  395. {
  396. auto modelIndex = m_gemModel->FindIndexByNameString(gemName);
  397. bool added = GemModel::IsAddedDependency(modelIndex);
  398. if (added && (GemModel::GetDownloadStatus(modelIndex) == GemInfo::DownloadStatus::NotDownloaded) ||
  399. (GemModel::GetDownloadStatus(modelIndex) == GemInfo::DownloadStatus::DownloadFailed))
  400. {
  401. m_downloadController->AddObjectDownload(GemModel::GetName(modelIndex), "" , DownloadController::DownloadObjectType::Gem);
  402. GemModel::SetDownloadStatus(*m_gemModel, modelIndex, GemInfo::DownloadStatus::Downloading);
  403. }
  404. }
  405. void GemCatalogScreen::SelectGem(const QString& gemName)
  406. {
  407. auto modelIndex = m_gemModel->FindIndexByNameString(gemName);
  408. if (!m_proxyModel->filterAcceptsRow(modelIndex.row(), QModelIndex()))
  409. {
  410. m_proxyModel->ResetFilters();
  411. m_filterWidget->UpdateAllFilters(/*clearAllCheckboxes=*/true);
  412. }
  413. if (QModelIndex proxyIndex = m_proxyModel->mapFromSource(modelIndex); proxyIndex.isValid())
  414. {
  415. m_proxyModel->GetSelectionModel()->setCurrentIndex(proxyIndex, QItemSelectionModel::ClearAndSelect);
  416. m_gemListView->scrollTo(proxyIndex);
  417. }
  418. ShowInspector();
  419. }
  420. void GemCatalogScreen::UpdateGem(const QModelIndex& modelIndex, const QString& version, const QString& path)
  421. {
  422. const GemInfo& gemInfo = m_gemModel->GetGemInfo(modelIndex, version, path);
  423. if (!gemInfo.m_repoUri.isEmpty())
  424. {
  425. AZ::Outcome<void, AZStd::string> refreshResult = PythonBindingsInterface::Get()->RefreshGemRepo(gemInfo.m_repoUri);
  426. if (refreshResult.IsSuccess())
  427. {
  428. Refresh();
  429. }
  430. else
  431. {
  432. QMessageBox::critical(
  433. this, tr("Operation failed"),
  434. tr("Failed to refresh gem repository %1<br>Error:<br>%2").arg(gemInfo.m_repoUri, refreshResult.GetError().c_str()));
  435. }
  436. }
  437. // If repo uri isn't specified warn user that repo might not be refreshed
  438. else
  439. {
  440. int result = QMessageBox::warning(
  441. this, tr("Gem Repository Unspecified"),
  442. tr("The repo for %1 is unspecfied. Repository cannot be automatically refreshed. "
  443. "Please ensure this gem's repo is refreshed before attempting to update.")
  444. .arg(gemInfo.m_displayName),
  445. QMessageBox::Cancel, QMessageBox::Ok);
  446. // Allow user to cancel update to manually refresh repo
  447. if (result != QMessageBox::Ok)
  448. {
  449. return;
  450. }
  451. }
  452. // include the version if valid
  453. auto outcome = AZ::SemanticVersion::ParseFromString(version.toUtf8().constData());
  454. const QString gemName = outcome ? QString("%1==%2").arg(gemInfo.m_name, version) : gemInfo.m_name;
  455. // Check if there is an update avaliable now that repo is refreshed
  456. bool updateAvaliable = PythonBindingsInterface::Get()->IsGemUpdateAvaliable(gemName, gemInfo.m_lastUpdatedDate);
  457. GemUpdateDialog* confirmUpdateDialog = new GemUpdateDialog(gemInfo.m_name, updateAvaliable, this);
  458. if (confirmUpdateDialog->exec() == QDialog::Accepted)
  459. {
  460. DownloadGem(modelIndex, version, path);
  461. }
  462. }
  463. void GemCatalogScreen::DownloadGem(const QModelIndex& modelIndex, const QString& version, const QString& path)
  464. {
  465. const GemInfo& gemInfo = m_gemModel->GetGemInfo(modelIndex, version, path);
  466. // include the version if valid
  467. auto outcome = AZ::SemanticVersion::ParseFromString(version.toUtf8().constData());
  468. const QString gemName = outcome ? QString("%1==%2").arg(gemInfo.m_name, version) : gemInfo.m_name;
  469. m_downloadController->AddObjectDownload(gemName, "" , DownloadController::DownloadObjectType::Gem);
  470. GemModel::SetDownloadStatus(*m_gemModel, modelIndex, GemInfo::DownloadStatus::Downloading);
  471. }
  472. void GemCatalogScreen::UninstallGem(const QModelIndex& modelIndex, const QString& path)
  473. {
  474. const QString gemDisplayName = m_gemModel->GetDisplayName(modelIndex);
  475. const GemInfo& gemInfo = m_gemModel->GetGemInfo(modelIndex, "", path);
  476. bool confirmed = false;
  477. if (gemInfo.m_gemOrigin == GemInfo::Remote)
  478. {
  479. GemUninstallDialog* confirmUninstallDialog = new GemUninstallDialog(gemDisplayName, this);
  480. confirmed = confirmUninstallDialog->exec() == QDialog::Accepted;
  481. }
  482. else
  483. {
  484. confirmed = QMessageBox::warning(this, tr("Remove Gem"),
  485. tr("Do you want to remove %1?<br>The gem will only be unregistered, and can be re-added. The files will not be removed from disk.").arg(gemDisplayName),
  486. QMessageBox::Ok, QMessageBox::Cancel) == QMessageBox::Ok;
  487. }
  488. if (confirmed)
  489. {
  490. const bool wasAdded = GemModel::WasPreviouslyAdded(modelIndex);
  491. const bool wasAddedDependency = GemModel::WasPreviouslyAddedDependency(modelIndex);
  492. // Remove gem from gems to be added to update any dependencies
  493. GemModel::SetIsAdded(*m_gemModel, modelIndex, false);
  494. GemModel::DeactivateDependentGems(*m_gemModel, modelIndex);
  495. // Unregister the gem
  496. auto unregisterResult = PythonBindingsInterface::Get()->UnregisterGem(path);
  497. if (!unregisterResult)
  498. {
  499. QMessageBox::critical(this, tr("Failed to unregister %1").arg(gemDisplayName), unregisterResult.GetError().c_str());
  500. }
  501. else
  502. {
  503. const QString gemName = m_gemModel->GetName(modelIndex);
  504. m_gemModel->RemoveGem(gemName, /*version=*/"", path);
  505. bool filesDeleted = false;
  506. if (gemInfo.m_gemOrigin == GemInfo::Remote)
  507. {
  508. // Remote gems will have their files deleted
  509. filesDeleted = ProjectUtils::DeleteProjectFiles(path, /*force*/ true);
  510. if (!filesDeleted)
  511. {
  512. QMessageBox::critical(
  513. this, tr("Failed to remove gem directory"), tr("Could not delete gem directory at:<br>%1").arg(path));
  514. }
  515. else
  516. {
  517. ShowStandardToastNotification(tr("%1 uninstalled").arg(gemDisplayName));
  518. }
  519. }
  520. else
  521. {
  522. ShowStandardToastNotification(tr("%1 removed").arg(gemDisplayName));
  523. }
  524. Refresh();
  525. const auto gemIndex = m_gemModel->FindIndexByNameString(gemName);
  526. QModelIndex proxyIndex;
  527. if (gemIndex.isValid())
  528. {
  529. GemModel::SetWasPreviouslyAdded(*m_gemModel, gemIndex, wasAdded);
  530. GemModel::SetWasPreviouslyAddedDependency(*m_gemModel, gemIndex, wasAddedDependency);
  531. if (filesDeleted)
  532. {
  533. GemModel::SetDownloadStatus(*m_gemModel, gemIndex, GemInfo::DownloadStatus::NotDownloaded);
  534. }
  535. proxyIndex = m_proxyModel->mapFromSource(gemIndex);
  536. }
  537. if (!proxyIndex.isValid())
  538. {
  539. // if the gem was removed then we need to pick a new entry
  540. proxyIndex = m_proxyModel->mapFromSource(m_gemModel->index(0, 0));
  541. }
  542. if (proxyIndex.isValid())
  543. {
  544. m_proxyModel->GetSelectionModel()->setCurrentIndex(proxyIndex, QItemSelectionModel::ClearAndSelect);
  545. }
  546. else
  547. {
  548. m_proxyModel->GetSelectionModel()->setCurrentIndex(m_proxyModel->index(0,0), QItemSelectionModel::ClearAndSelect);
  549. }
  550. }
  551. }
  552. }
  553. void GemCatalogScreen::hideEvent(QHideEvent* event)
  554. {
  555. ScreenWidget::hideEvent(event);
  556. m_notificationsView->OnHide();
  557. }
  558. void GemCatalogScreen::showEvent(QShowEvent* event)
  559. {
  560. ScreenWidget::showEvent(event);
  561. m_notificationsView->OnShow();
  562. }
  563. void GemCatalogScreen::resizeEvent(QResizeEvent* event)
  564. {
  565. ScreenWidget::resizeEvent(event);
  566. m_notificationsView->UpdateToastPosition();
  567. }
  568. void GemCatalogScreen::moveEvent(QMoveEvent* event)
  569. {
  570. ScreenWidget::moveEvent(event);
  571. m_notificationsView->UpdateToastPosition();
  572. }
  573. void GemCatalogScreen::FillModel(const QString& projectPath)
  574. {
  575. m_projectPath = projectPath;
  576. const AZ::Outcome<QVector<GemInfo>, AZStd::string>& allGemInfosResult = PythonBindingsInterface::Get()->GetAllGemInfos(projectPath);
  577. if (allGemInfosResult.IsSuccess())
  578. {
  579. m_gemModel->AddGems(allGemInfosResult.GetValue());
  580. const auto& allRepoGemInfosResult = PythonBindingsInterface::Get()->GetGemInfosForAllRepos(projectPath);
  581. if (allRepoGemInfosResult.IsSuccess())
  582. {
  583. m_gemModel->AddGems(allRepoGemInfosResult.GetValue());
  584. }
  585. else
  586. {
  587. QMessageBox::critical(nullptr, tr("Operation failed"), QString("Cannot retrieve gems from repos.<br><br>Error:<br>%1").arg(allRepoGemInfosResult.GetError().c_str()));
  588. }
  589. // we need to update all gem dependencies before activating all the gems for this project
  590. m_gemModel->UpdateGemDependencies();
  591. if (!m_projectPath.isEmpty())
  592. {
  593. constexpr bool includeDependencies = false;
  594. const auto& enabledGemNamesResult = PythonBindingsInterface::Get()->GetEnabledGems(projectPath, includeDependencies);
  595. if (enabledGemNamesResult.IsSuccess())
  596. {
  597. m_gemModel->ActivateGems(enabledGemNamesResult.GetValue());
  598. }
  599. else
  600. {
  601. QMessageBox::critical(nullptr, tr("Operation failed"), QString("Cannot retrieve enabled gems for project %1.<br><br>Error:<br>%2").arg(projectPath, enabledGemNamesResult.GetError().c_str()));
  602. }
  603. }
  604. // sort after activating gems in case the display name for a gem is different for the active version
  605. m_proxyModel->sort(/*column=*/0);
  606. }
  607. else
  608. {
  609. QMessageBox::critical(nullptr, tr("Operation failed"), QString("Cannot retrieve gems for %1.<br><br>Error:<br>%2").arg(projectPath, allGemInfosResult.GetError().c_str()));
  610. }
  611. }
  612. void GemCatalogScreen::ShowInspector()
  613. {
  614. m_rightPanelStack->setCurrentIndex(RightPanelWidgetOrder::Inspector);
  615. m_headerWidget->GemCartShown();
  616. }
  617. void GemCatalogScreen::HandleOpenGemRepo()
  618. {
  619. if (m_readOnly)
  620. {
  621. emit ChangeScreenRequest(ProjectManagerScreen::GemsGemRepos);
  622. }
  623. else
  624. {
  625. emit ChangeScreenRequest(ProjectManagerScreen::GemRepos);
  626. }
  627. }
  628. void GemCatalogScreen::HandleCreateGem()
  629. {
  630. emit ChangeScreenRequest(ProjectManagerScreen::CreateGem);
  631. }
  632. void GemCatalogScreen::HandleEditGem(const QModelIndex& currentModelIndex, const QString& path)
  633. {
  634. if (m_screensControl)
  635. {
  636. auto editGemScreen = qobject_cast<EditGem*>(m_screensControl->FindScreen(ProjectManagerScreen::EditGem));
  637. if (editGemScreen)
  638. {
  639. m_curEditedIndex = currentModelIndex;
  640. editGemScreen->ResetWorkflow(m_gemModel->GetGemInfo(currentModelIndex, /*version=*/"", path));
  641. emit ChangeScreenRequest(ProjectManagerScreen::EditGem);
  642. }
  643. }
  644. }
  645. void GemCatalogScreen::UpdateAndShowGemCart(QWidget* cartWidget)
  646. {
  647. QWidget* previousCart = m_rightPanelStack->widget(RightPanelWidgetOrder::Cart);
  648. if (previousCart)
  649. {
  650. m_rightPanelStack->removeWidget(previousCart);
  651. }
  652. m_rightPanelStack->insertWidget(RightPanelWidgetOrder::Cart, cartWidget);
  653. m_rightPanelStack->setCurrentIndex(RightPanelWidgetOrder::Cart);
  654. }
  655. void GemCatalogScreen::OnGemDownloadResult(const QString& gemName, bool succeeded)
  656. {
  657. QString gemNameWithoutVersionSpecifier = ProjectUtils::GetDependencyName(gemName);
  658. const auto index = m_gemModel->FindIndexByNameString(gemNameWithoutVersionSpecifier);
  659. if (succeeded)
  660. {
  661. Refresh();
  662. if(index.isValid())
  663. {
  664. GemModel::SetDownloadStatus(*m_gemModel, index, GemInfo::DownloadSuccessful);
  665. }
  666. }
  667. else if (index.isValid())
  668. {
  669. GemModel::SetIsAdded(*m_gemModel, index, false);
  670. GemModel::DeactivateDependentGems(*m_gemModel, index);
  671. GemModel::SetDownloadStatus(*m_gemModel, index, GemInfo::DownloadFailed);
  672. }
  673. }
  674. void GemCatalogScreen::HandleGemCreated(const GemInfo& gemInfo)
  675. {
  676. // This signal occurs only upon successful completion of creating a gem. As such, the gemInfo data is assumed to be valid.
  677. // make sure the project gem catalog model is updated
  678. AddToGemModel(gemInfo);
  679. // create Toast Notification for project gem catalog
  680. QString notification = tr("%1 has been created").arg(gemInfo.m_displayName);
  681. ShowStandardToastNotification(notification);
  682. }
  683. void GemCatalogScreen::HandleGemEdited(const GemInfo& newGemInfo)
  684. {
  685. // This signal only occurs upon successful completion of editing a gem. As such, the gemInfo is assumed to be valid
  686. // Make sure to update the current model index in the gem catalog model.
  687. // The current edited index is only set by HandleEdit before editing a gem, and nowhere else.
  688. // As such, the index should be valid.
  689. m_gemModel->RemoveGem(m_curEditedIndex);
  690. m_gemModel->AddGem(newGemInfo);
  691. //gem inspector needs to have its selection updated to the newly added gem
  692. SelectGem(newGemInfo.m_name);
  693. QString notification = tr("%1 was edited").arg(newGemInfo.m_displayName);
  694. ShowStandardToastNotification(notification);
  695. }
  696. ProjectManagerScreen GemCatalogScreen::GetScreenEnum()
  697. {
  698. return ProjectManagerScreen::GemCatalog;
  699. }
  700. QString GemCatalogScreen::GetTabText()
  701. {
  702. return "Gems";
  703. }
  704. bool GemCatalogScreen::IsTab()
  705. {
  706. return true;
  707. }
  708. } // namespace O3DE::ProjectManager