GemCatalogScreen.cpp 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825
  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. [this](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. // Clear the model to avoid duplicated entries that may cause incompatibilities
  270. m_gemModel->Clear();
  271. if (const auto& outcome = PythonBindingsInterface::Get()->GetAllGemInfos(m_projectPath); outcome.IsSuccess())
  272. {
  273. const auto& indexes = m_gemModel->AddGems(outcome.GetValue(), /*updateExisting=*/true);
  274. validIndexes = QSet(indexes.cbegin(), indexes.cend());
  275. }
  276. if(refreshRemoteRepos)
  277. {
  278. PythonBindingsInterface::Get()->RefreshAllGemRepos();
  279. }
  280. if (const auto& outcome = PythonBindingsInterface::Get()->GetGemInfosForAllRepos(); outcome.IsSuccess())
  281. {
  282. const auto& indexes = m_gemModel->AddGems(outcome.GetValue(), /*updateExisting=*/true);
  283. validIndexes.unite(QSet(indexes.cbegin(), indexes.cend()));
  284. }
  285. // remove gems from the model that no longer exist in the hash and are not project dependencies
  286. int i = 0;
  287. while (i < m_gemModel->rowCount())
  288. {
  289. QModelIndex index = m_gemModel->index(i,0);
  290. const bool gemFound = validIndexes.contains(index);
  291. if (!gemFound && !m_gemModel->IsAdded(index) && !m_gemModel->IsAddedDependency(index))
  292. {
  293. m_gemModel->RemoveGem(index);
  294. }
  295. else
  296. {
  297. if (!gemFound && (m_gemModel->IsAdded(index) || m_gemModel->IsAddedDependency(index)))
  298. {
  299. QString gemName = m_gemModel->GetName(index);
  300. const QString error = tr("Gem %1 was removed or unregistered, but is still used by the project.").arg(gemName);
  301. AZ_Warning("Project Manager", false, error.toUtf8().constData());
  302. QMessageBox::warning(this, tr("Gem not found"), error.toUtf8().constData());
  303. }
  304. i++;
  305. }
  306. }
  307. m_gemModel->UpdateGemDependencies();
  308. m_proxyModel->sort(/*column=*/0);
  309. m_proxyModel->InvalidateFilter();
  310. m_filterWidget->UpdateAllFilters(/*clearAllCheckboxes=*/false);
  311. auto selectedIndex = m_proxyModel->GetSelectionModel()->currentIndex();
  312. // be careful to not pass in the proxy model index, we want the source model index
  313. if (selectedIndex.isValid())
  314. {
  315. m_gemInspector->Update(m_proxyModel->mapToSource(selectedIndex));
  316. }
  317. else
  318. {
  319. QModelIndex firstProxyIndex = m_proxyModel->index(0, 0);
  320. m_proxyModel->GetSelectionModel()->setCurrentIndex(firstProxyIndex, QItemSelectionModel::ClearAndSelect);
  321. m_gemInspector->Update(m_proxyModel->mapToSource(firstProxyIndex));
  322. }
  323. }
  324. void GemCatalogScreen::OnGemStatusChanged(const QString& gemName, uint32_t numChangedDependencies)
  325. {
  326. if (m_notificationsEnabled && !m_readOnly)
  327. {
  328. auto modelIndex = m_gemModel->FindIndexByNameString(gemName);
  329. bool added = GemModel::IsAdded(modelIndex);
  330. bool dependency = GemModel::IsAddedDependency(modelIndex);
  331. bool gemStateChanged = (added && !dependency) || (!added && !dependency);
  332. if (!gemStateChanged && !numChangedDependencies)
  333. {
  334. // no actual changes made
  335. return;
  336. }
  337. QString notification;
  338. if (gemStateChanged)
  339. {
  340. const GemInfo& gemInfo = GemModel::GetGemInfo(modelIndex);
  341. const QString& newVersion = GemModel::GetNewVersion(modelIndex);
  342. const QString& version = newVersion.isEmpty() ? gemInfo.m_version : newVersion;
  343. // avoid showing the version twice if it's already in the display name
  344. if (gemInfo.m_isEngineGem || (version.isEmpty() || gemInfo.m_displayName.contains(version) || version.contains("Unknown", Qt::CaseInsensitive)))
  345. {
  346. notification = gemInfo.m_displayName;
  347. }
  348. else
  349. {
  350. notification = QString("%1 %2").arg(gemInfo.m_displayName, version);
  351. }
  352. if (numChangedDependencies > 0)
  353. {
  354. notification += tr(" and ");
  355. }
  356. if (added)
  357. {
  358. if (newVersion.isEmpty() && (GemModel::GetDownloadStatus(modelIndex) == GemInfo::DownloadStatus::NotDownloaded) ||
  359. (GemModel::GetDownloadStatus(modelIndex) == GemInfo::DownloadStatus::DownloadFailed))
  360. {
  361. // download the current version
  362. DownloadGem(modelIndex, gemInfo.m_version, gemInfo.m_path);
  363. }
  364. else if (!newVersion.isEmpty())
  365. {
  366. const GemInfo& newVersionGemInfo = GemModel::GetGemInfo(modelIndex, newVersion);
  367. if (newVersionGemInfo.m_downloadStatus == GemInfo::DownloadStatus::NotDownloaded ||
  368. GemModel::GetDownloadStatus(modelIndex) == GemInfo::DownloadStatus::DownloadFailed)
  369. {
  370. // download the new version
  371. DownloadGem(modelIndex, newVersionGemInfo.m_version, newVersionGemInfo.m_path);
  372. }
  373. }
  374. }
  375. }
  376. if (numChangedDependencies == 1)
  377. {
  378. notification += tr("1 Gem dependency");
  379. }
  380. else if (numChangedDependencies > 1)
  381. {
  382. notification += tr("%1 Gem %2").arg(numChangedDependencies).arg(tr("dependencies"));
  383. }
  384. notification += (added ? tr(" activated") : tr(" deactivated"));
  385. ShowStandardToastNotification(notification);
  386. }
  387. }
  388. void GemCatalogScreen::ShowStandardToastNotification(const QString& notification)
  389. {
  390. AzQtComponents::ToastConfiguration toastConfiguration(AzQtComponents::ToastType::Custom, notification, "");
  391. toastConfiguration.m_customIconImage = ":/gem.svg";
  392. toastConfiguration.m_borderRadius = 4;
  393. toastConfiguration.m_duration = AZStd::chrono::milliseconds(3000);
  394. m_notificationsView->ShowToastNotification(toastConfiguration);
  395. }
  396. void GemCatalogScreen::OnDependencyGemStatusChanged(const QString& gemName)
  397. {
  398. auto modelIndex = m_gemModel->FindIndexByNameString(gemName);
  399. bool added = GemModel::IsAddedDependency(modelIndex);
  400. if (added && (GemModel::GetDownloadStatus(modelIndex) == GemInfo::DownloadStatus::NotDownloaded) ||
  401. (GemModel::GetDownloadStatus(modelIndex) == GemInfo::DownloadStatus::DownloadFailed))
  402. {
  403. m_downloadController->AddObjectDownload(GemModel::GetName(modelIndex), "" , DownloadController::DownloadObjectType::Gem);
  404. GemModel::SetDownloadStatus(*m_gemModel, modelIndex, GemInfo::DownloadStatus::Downloading);
  405. }
  406. }
  407. void GemCatalogScreen::SelectGem(const QString& gemName)
  408. {
  409. auto modelIndex = m_gemModel->FindIndexByNameString(gemName);
  410. if (!m_proxyModel->filterAcceptsRow(modelIndex.row(), QModelIndex()))
  411. {
  412. m_proxyModel->ResetFilters();
  413. m_filterWidget->UpdateAllFilters(/*clearAllCheckboxes=*/true);
  414. }
  415. if (QModelIndex proxyIndex = m_proxyModel->mapFromSource(modelIndex); proxyIndex.isValid())
  416. {
  417. m_proxyModel->GetSelectionModel()->setCurrentIndex(proxyIndex, QItemSelectionModel::ClearAndSelect);
  418. m_gemListView->scrollTo(proxyIndex);
  419. }
  420. ShowInspector();
  421. }
  422. void GemCatalogScreen::UpdateGem(const QModelIndex& modelIndex, const QString& version, const QString& path)
  423. {
  424. const GemInfo& gemInfo = m_gemModel->GetGemInfo(modelIndex, version, path);
  425. if (!gemInfo.m_repoUri.isEmpty())
  426. {
  427. AZ::Outcome<void, AZStd::string> refreshResult = PythonBindingsInterface::Get()->RefreshGemRepo(gemInfo.m_repoUri);
  428. if (refreshResult.IsSuccess())
  429. {
  430. Refresh();
  431. }
  432. else
  433. {
  434. QMessageBox::critical(
  435. this, tr("Operation failed"),
  436. tr("Failed to refresh gem repository %1<br>Error:<br>%2").arg(gemInfo.m_repoUri, refreshResult.GetError().c_str()));
  437. }
  438. }
  439. // If repo uri isn't specified warn user that repo might not be refreshed
  440. else
  441. {
  442. int result = QMessageBox::warning(
  443. this, tr("Gem Repository Unspecified"),
  444. tr("The repo for %1 is unspecfied. Repository cannot be automatically refreshed. "
  445. "Please ensure this gem's repo is refreshed before attempting to update.")
  446. .arg(gemInfo.m_displayName),
  447. QMessageBox::Cancel, QMessageBox::Ok);
  448. // Allow user to cancel update to manually refresh repo
  449. if (result != QMessageBox::Ok)
  450. {
  451. return;
  452. }
  453. }
  454. // include the version if valid
  455. auto outcome = AZ::SemanticVersion::ParseFromString(version.toUtf8().constData());
  456. const QString gemName = outcome ? QString("%1==%2").arg(gemInfo.m_name, version) : gemInfo.m_name;
  457. // Check if there is an update avaliable now that repo is refreshed
  458. bool updateAvaliable = PythonBindingsInterface::Get()->IsGemUpdateAvaliable(gemName, gemInfo.m_lastUpdatedDate);
  459. GemUpdateDialog* confirmUpdateDialog = new GemUpdateDialog(gemInfo.m_name, updateAvaliable, this);
  460. if (confirmUpdateDialog->exec() == QDialog::Accepted)
  461. {
  462. DownloadGem(modelIndex, version, path);
  463. }
  464. }
  465. void GemCatalogScreen::DownloadGem(const QModelIndex& modelIndex, const QString& version, const QString& path)
  466. {
  467. const GemInfo& gemInfo = m_gemModel->GetGemInfo(modelIndex, version, path);
  468. // include the version if valid
  469. auto outcome = AZ::SemanticVersion::ParseFromString(version.toUtf8().constData());
  470. const QString gemName = outcome ? QString("%1==%2").arg(gemInfo.m_name, version) : gemInfo.m_name;
  471. m_downloadController->AddObjectDownload(gemName, "" , DownloadController::DownloadObjectType::Gem);
  472. GemModel::SetDownloadStatus(*m_gemModel, modelIndex, GemInfo::DownloadStatus::Downloading);
  473. }
  474. void GemCatalogScreen::UninstallGem(const QModelIndex& modelIndex, const QString& path)
  475. {
  476. const QString gemDisplayName = m_gemModel->GetDisplayName(modelIndex);
  477. const GemInfo& gemInfo = m_gemModel->GetGemInfo(modelIndex, "", path);
  478. bool confirmed = false;
  479. if (gemInfo.m_gemOrigin == GemInfo::Remote)
  480. {
  481. GemUninstallDialog* confirmUninstallDialog = new GemUninstallDialog(gemDisplayName, this);
  482. confirmed = confirmUninstallDialog->exec() == QDialog::Accepted;
  483. }
  484. else
  485. {
  486. confirmed = QMessageBox::warning(this, tr("Remove Gem"),
  487. 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),
  488. QMessageBox::Ok, QMessageBox::Cancel) == QMessageBox::Ok;
  489. }
  490. if (confirmed)
  491. {
  492. const bool wasAdded = GemModel::WasPreviouslyAdded(modelIndex);
  493. const bool wasAddedDependency = GemModel::WasPreviouslyAddedDependency(modelIndex);
  494. // Remove gem from gems to be added to update any dependencies
  495. GemModel::SetIsAdded(*m_gemModel, modelIndex, false);
  496. GemModel::DeactivateDependentGems(*m_gemModel, modelIndex);
  497. // Unregister the gem
  498. auto unregisterResult = PythonBindingsInterface::Get()->UnregisterGem(path);
  499. if (!unregisterResult)
  500. {
  501. QMessageBox::critical(this, tr("Failed to unregister %1").arg(gemDisplayName), unregisterResult.GetError().c_str());
  502. }
  503. else
  504. {
  505. const QString gemName = m_gemModel->GetName(modelIndex);
  506. m_gemModel->RemoveGem(gemName, /*version=*/"", path);
  507. bool filesDeleted = false;
  508. if (gemInfo.m_gemOrigin == GemInfo::Remote)
  509. {
  510. // Remote gems will have their files deleted
  511. filesDeleted = ProjectUtils::DeleteProjectFiles(path, /*force*/ true);
  512. if (!filesDeleted)
  513. {
  514. QMessageBox::critical(
  515. this, tr("Failed to remove gem directory"), tr("Could not delete gem directory at:<br>%1").arg(path));
  516. }
  517. else
  518. {
  519. ShowStandardToastNotification(tr("%1 uninstalled").arg(gemDisplayName));
  520. }
  521. }
  522. else
  523. {
  524. ShowStandardToastNotification(tr("%1 removed").arg(gemDisplayName));
  525. }
  526. Refresh();
  527. const auto gemIndex = m_gemModel->FindIndexByNameString(gemName);
  528. QModelIndex proxyIndex;
  529. if (gemIndex.isValid())
  530. {
  531. GemModel::SetWasPreviouslyAdded(*m_gemModel, gemIndex, wasAdded);
  532. GemModel::SetWasPreviouslyAddedDependency(*m_gemModel, gemIndex, wasAddedDependency);
  533. if (filesDeleted)
  534. {
  535. GemModel::SetDownloadStatus(*m_gemModel, gemIndex, GemInfo::DownloadStatus::NotDownloaded);
  536. }
  537. proxyIndex = m_proxyModel->mapFromSource(gemIndex);
  538. }
  539. if (!proxyIndex.isValid())
  540. {
  541. // if the gem was removed then we need to pick a new entry
  542. proxyIndex = m_proxyModel->mapFromSource(m_gemModel->index(0, 0));
  543. }
  544. if (proxyIndex.isValid())
  545. {
  546. m_proxyModel->GetSelectionModel()->setCurrentIndex(proxyIndex, QItemSelectionModel::ClearAndSelect);
  547. }
  548. else
  549. {
  550. m_proxyModel->GetSelectionModel()->setCurrentIndex(m_proxyModel->index(0,0), QItemSelectionModel::ClearAndSelect);
  551. }
  552. }
  553. }
  554. }
  555. void GemCatalogScreen::hideEvent(QHideEvent* event)
  556. {
  557. ScreenWidget::hideEvent(event);
  558. m_notificationsView->OnHide();
  559. }
  560. void GemCatalogScreen::showEvent(QShowEvent* event)
  561. {
  562. ScreenWidget::showEvent(event);
  563. m_notificationsView->OnShow();
  564. }
  565. void GemCatalogScreen::resizeEvent(QResizeEvent* event)
  566. {
  567. ScreenWidget::resizeEvent(event);
  568. m_notificationsView->UpdateToastPosition();
  569. }
  570. void GemCatalogScreen::moveEvent(QMoveEvent* event)
  571. {
  572. ScreenWidget::moveEvent(event);
  573. m_notificationsView->UpdateToastPosition();
  574. }
  575. void GemCatalogScreen::FillModel(const QString& projectPath)
  576. {
  577. m_projectPath = projectPath;
  578. const AZ::Outcome<QVector<GemInfo>, AZStd::string>& allGemInfosResult = PythonBindingsInterface::Get()->GetAllGemInfos(projectPath);
  579. if (allGemInfosResult.IsSuccess())
  580. {
  581. m_gemModel->AddGems(allGemInfosResult.GetValue());
  582. const auto& allRepoGemInfosResult = PythonBindingsInterface::Get()->GetGemInfosForAllRepos(projectPath);
  583. if (allRepoGemInfosResult.IsSuccess())
  584. {
  585. m_gemModel->AddGems(allRepoGemInfosResult.GetValue());
  586. }
  587. else
  588. {
  589. QMessageBox::critical(nullptr, tr("Operation failed"), QString("Cannot retrieve gems from repos.<br><br>Error:<br>%1").arg(allRepoGemInfosResult.GetError().c_str()));
  590. }
  591. // we need to update all gem dependencies before activating all the gems for this project
  592. m_gemModel->UpdateGemDependencies();
  593. if (!m_projectPath.isEmpty())
  594. {
  595. constexpr bool includeDependencies = false;
  596. const auto& enabledGemNamesResult = PythonBindingsInterface::Get()->GetEnabledGems(projectPath, includeDependencies);
  597. if (enabledGemNamesResult.IsSuccess())
  598. {
  599. m_gemModel->ActivateGems(enabledGemNamesResult.GetValue());
  600. }
  601. else
  602. {
  603. 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()));
  604. }
  605. }
  606. // sort after activating gems in case the display name for a gem is different for the active version
  607. m_proxyModel->sort(/*column=*/0);
  608. }
  609. else
  610. {
  611. QMessageBox::critical(nullptr, tr("Operation failed"), QString("Cannot retrieve gems for %1.<br><br>Error:<br>%2").arg(projectPath, allGemInfosResult.GetError().c_str()));
  612. }
  613. }
  614. void GemCatalogScreen::ShowInspector()
  615. {
  616. m_rightPanelStack->setCurrentIndex(RightPanelWidgetOrder::Inspector);
  617. m_headerWidget->GemCartShown();
  618. }
  619. void GemCatalogScreen::HandleOpenGemRepo()
  620. {
  621. if (m_readOnly)
  622. {
  623. emit ChangeScreenRequest(ProjectManagerScreen::GemsGemRepos);
  624. }
  625. else
  626. {
  627. emit ChangeScreenRequest(ProjectManagerScreen::GemRepos);
  628. }
  629. }
  630. void GemCatalogScreen::HandleCreateGem()
  631. {
  632. emit ChangeScreenRequest(ProjectManagerScreen::CreateGem);
  633. }
  634. void GemCatalogScreen::HandleEditGem(const QModelIndex& currentModelIndex, const QString& path)
  635. {
  636. if (m_screensControl)
  637. {
  638. auto editGemScreen = qobject_cast<EditGem*>(m_screensControl->FindScreen(ProjectManagerScreen::EditGem));
  639. if (editGemScreen)
  640. {
  641. m_curEditedIndex = currentModelIndex;
  642. editGemScreen->ResetWorkflow(m_gemModel->GetGemInfo(currentModelIndex, /*version=*/"", path));
  643. emit ChangeScreenRequest(ProjectManagerScreen::EditGem);
  644. }
  645. }
  646. }
  647. void GemCatalogScreen::UpdateAndShowGemCart(QWidget* cartWidget)
  648. {
  649. QWidget* previousCart = m_rightPanelStack->widget(RightPanelWidgetOrder::Cart);
  650. if (previousCart)
  651. {
  652. m_rightPanelStack->removeWidget(previousCart);
  653. }
  654. m_rightPanelStack->insertWidget(RightPanelWidgetOrder::Cart, cartWidget);
  655. m_rightPanelStack->setCurrentIndex(RightPanelWidgetOrder::Cart);
  656. }
  657. void GemCatalogScreen::OnGemDownloadResult(const QString& gemName, bool succeeded)
  658. {
  659. QString gemNameWithoutVersionSpecifier = ProjectUtils::GetDependencyName(gemName);
  660. const auto index = m_gemModel->FindIndexByNameString(gemNameWithoutVersionSpecifier);
  661. if (succeeded)
  662. {
  663. Refresh();
  664. if(index.isValid())
  665. {
  666. GemModel::SetDownloadStatus(*m_gemModel, index, GemInfo::DownloadSuccessful);
  667. }
  668. }
  669. else if (index.isValid())
  670. {
  671. GemModel::SetIsAdded(*m_gemModel, index, false);
  672. GemModel::DeactivateDependentGems(*m_gemModel, index);
  673. GemModel::SetDownloadStatus(*m_gemModel, index, GemInfo::DownloadFailed);
  674. }
  675. }
  676. void GemCatalogScreen::HandleGemCreated(const GemInfo& gemInfo)
  677. {
  678. // This signal occurs only upon successful completion of creating a gem. As such, the gemInfo data is assumed to be valid.
  679. // make sure the project gem catalog model is updated
  680. AddToGemModel(gemInfo);
  681. // create Toast Notification for project gem catalog
  682. QString notification = tr("%1 has been created").arg(gemInfo.m_displayName);
  683. ShowStandardToastNotification(notification);
  684. }
  685. void GemCatalogScreen::HandleGemEdited(const GemInfo& newGemInfo)
  686. {
  687. // This signal only occurs upon successful completion of editing a gem. As such, the gemInfo is assumed to be valid
  688. // Make sure to update the current model index in the gem catalog model.
  689. // The current edited index is only set by HandleEdit before editing a gem, and nowhere else.
  690. // As such, the index should be valid.
  691. m_gemModel->RemoveGem(m_curEditedIndex);
  692. m_gemModel->AddGem(newGemInfo);
  693. //gem inspector needs to have its selection updated to the newly added gem
  694. SelectGem(newGemInfo.m_name);
  695. QString notification = tr("%1 was edited").arg(newGemInfo.m_displayName);
  696. ShowStandardToastNotification(notification);
  697. }
  698. ProjectManagerScreen GemCatalogScreen::GetScreenEnum()
  699. {
  700. return ProjectManagerScreen::GemCatalog;
  701. }
  702. QString GemCatalogScreen::GetTabText()
  703. {
  704. return "Gems";
  705. }
  706. bool GemCatalogScreen::IsTab()
  707. {
  708. return true;
  709. }
  710. } // namespace O3DE::ProjectManager