GemRepoModel.cpp 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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 <GemRepo/GemRepoModel.h>
  9. #include <PythonBindings.h>
  10. #include <QItemSelectionModel>
  11. #include <QMessageBox>
  12. #include <QSortFilterProxyModel>
  13. namespace O3DE::ProjectManager
  14. {
  15. GemRepoModel::GemRepoModel(QObject* parent)
  16. : QStandardItemModel(parent)
  17. {
  18. m_selectionModel = new QItemSelectionModel(this, parent);
  19. }
  20. QItemSelectionModel* GemRepoModel::GetSelectionModel() const
  21. {
  22. return m_selectionModel;
  23. }
  24. void SetItemDataSorted(QStandardItem* item, const QSet<QString>& stringSet, int role)
  25. {
  26. auto stringList = QStringList(stringSet.values());
  27. stringList.sort(Qt::CaseInsensitive);
  28. item->setData(stringList, role);
  29. }
  30. void GemRepoModel::AddGemRepo(const GemRepoInfo& gemRepoInfo)
  31. {
  32. QStandardItem* item = new QStandardItem();
  33. item->setFlags(Qt::ItemIsEnabled | Qt::ItemIsSelectable);
  34. item->setData(gemRepoInfo.m_name, RoleName);
  35. item->setData(gemRepoInfo.m_origin, RoleCreator);
  36. item->setData(gemRepoInfo.m_summary, RoleSummary);
  37. item->setData(gemRepoInfo.m_isEnabled, RoleIsEnabled);
  38. item->setData(gemRepoInfo.m_directoryLink, RoleDirectoryLink);
  39. item->setData(gemRepoInfo.m_repoUri, RoleRepoUri);
  40. item->setData(gemRepoInfo.m_lastUpdated, RoleLastUpdated);
  41. item->setData(gemRepoInfo.m_path, RolePath);
  42. item->setData(gemRepoInfo.m_additionalInfo, RoleAdditionalInfo);
  43. item->setData(static_cast<int>(gemRepoInfo.m_badgeType), RoleBadgeType);
  44. appendRow(item);
  45. if (!gemRepoInfo.m_repoUri.isEmpty())
  46. {
  47. // gems - including gems from deactivated repos
  48. const auto& gemInfosResult = PythonBindingsInterface::Get()->GetGemInfosForRepo(gemRepoInfo.m_repoUri, /*enabledOnly*/false);
  49. if (gemInfosResult.IsSuccess())
  50. {
  51. const QVector<GemInfo>& gemInfos = gemInfosResult.GetValue();
  52. if (!gemInfos.isEmpty())
  53. {
  54. // use a set to not include duplicate names because there are multiple versions of a gem
  55. QSet<QString> includedGems;
  56. for (const auto& gemInfo : gemInfos)
  57. {
  58. includedGems.insert(gemInfo.m_displayName.isEmpty() ? gemInfo.m_name : gemInfo.m_displayName);
  59. }
  60. SetItemDataSorted(item, includedGems, RoleIncludedGems);
  61. }
  62. }
  63. else
  64. {
  65. QMessageBox::critical(nullptr, tr("Gems not found"), tr("Cannot find info for gems from repo %1").arg(gemRepoInfo.m_name));
  66. }
  67. // projects - including projects from deactivated repos
  68. const auto& projectInfosResult = PythonBindingsInterface::Get()->GetProjectsForRepo(gemRepoInfo.m_repoUri, /*enabledOnly*/false);
  69. if (projectInfosResult.IsSuccess())
  70. {
  71. const QVector<ProjectInfo>& projectInfos = projectInfosResult.GetValue();
  72. if (!projectInfos.isEmpty())
  73. {
  74. // use a set to not include duplicate names because there are multiple versions of a gem
  75. QSet<QString> includedProjects;
  76. for (const auto& projectInfo : projectInfos)
  77. {
  78. includedProjects.insert(projectInfo.m_displayName.isEmpty() ? projectInfo.m_projectName : projectInfo.m_displayName);
  79. }
  80. SetItemDataSorted(item, includedProjects, RoleIncludedProjects);
  81. }
  82. }
  83. else
  84. {
  85. QMessageBox::critical(nullptr, tr("Projects not found"), tr("Cannot find info for projects from repo %1").arg(gemRepoInfo.m_name));
  86. }
  87. // project templates - including projects from deactivated repos
  88. const auto& projectTemplateInfosResult =
  89. PythonBindingsInterface::Get()->GetProjectTemplatesForRepo(gemRepoInfo.m_repoUri, /*enabledOnly*/false);
  90. if (projectTemplateInfosResult.IsSuccess())
  91. {
  92. const QVector<ProjectTemplateInfo>& projectTemplateInfos = projectTemplateInfosResult.GetValue();
  93. if (!projectTemplateInfos.isEmpty())
  94. {
  95. // use a set to not include duplicate names because there are multiple versions of a gem
  96. QSet<QString> includedProjectTemplates;
  97. for (const auto& projectTemplateInfo : projectTemplateInfos)
  98. {
  99. includedProjectTemplates.insert(projectTemplateInfo.m_displayName.isEmpty() ? projectTemplateInfo.m_name : projectTemplateInfo.m_displayName);
  100. }
  101. SetItemDataSorted(item, includedProjectTemplates, RoleIncludedProjectTemplates);
  102. }
  103. }
  104. else
  105. {
  106. QMessageBox::critical(nullptr, tr("Project templates not found"), tr("Cannot find info for project templates from repo %1").arg(gemRepoInfo.m_name));
  107. }
  108. }
  109. }
  110. void GemRepoModel::Clear()
  111. {
  112. clear();
  113. }
  114. QString GemRepoModel::GetName(const QModelIndex& modelIndex)
  115. {
  116. return modelIndex.data(RoleName).toString();
  117. }
  118. QString GemRepoModel::GetCreator(const QModelIndex& modelIndex)
  119. {
  120. return modelIndex.data(RoleCreator).toString();
  121. }
  122. QString GemRepoModel::GetSummary(const QModelIndex& modelIndex)
  123. {
  124. return modelIndex.data(RoleSummary).toString();
  125. }
  126. QString GemRepoModel::GetAdditionalInfo(const QModelIndex& modelIndex)
  127. {
  128. return modelIndex.data(RoleAdditionalInfo).toString();
  129. }
  130. QString GemRepoModel::GetDirectoryLink(const QModelIndex& modelIndex)
  131. {
  132. return modelIndex.data(RoleDirectoryLink).toString();
  133. }
  134. QString GemRepoModel::GetRepoUri(const QModelIndex& modelIndex)
  135. {
  136. return modelIndex.data(RoleRepoUri).toString();
  137. }
  138. QDateTime GemRepoModel::GetLastUpdated(const QModelIndex& modelIndex)
  139. {
  140. return modelIndex.data(RoleLastUpdated).toDateTime();
  141. }
  142. QString GemRepoModel::GetPath(const QModelIndex& modelIndex)
  143. {
  144. return modelIndex.data(RolePath).toString();
  145. }
  146. GemRepoInfo::BadgeType GemRepoModel::GetBadgeType(const QModelIndex& modelIndex)
  147. {
  148. return static_cast<GemRepoInfo::BadgeType>(modelIndex.data(RoleBadgeType).toInt());
  149. }
  150. QVector<Tag> TagsFromStringList(const QStringList& stringList)
  151. {
  152. if (stringList.isEmpty())
  153. {
  154. return {};
  155. }
  156. QVector<Tag> tags;
  157. tags.reserve(stringList.size());
  158. for (const QString& tagName : stringList)
  159. {
  160. tags.append({ tagName, tagName });
  161. }
  162. return tags;
  163. }
  164. QVector<Tag> GemRepoModel::GetIncludedGemTags(const QModelIndex& modelIndex)
  165. {
  166. return TagsFromStringList(modelIndex.data(RoleIncludedGems).toStringList());
  167. }
  168. QVector<Tag> GemRepoModel::GetIncludedProjectTags(const QModelIndex& modelIndex)
  169. {
  170. return TagsFromStringList(modelIndex.data(RoleIncludedProjects).toStringList());
  171. }
  172. QVector<Tag> GemRepoModel::GetIncludedProjectTemplateTags(const QModelIndex& modelIndex)
  173. {
  174. return TagsFromStringList(modelIndex.data(RoleIncludedProjectTemplates).toStringList());
  175. }
  176. bool GemRepoModel::IsEnabled(const QModelIndex& modelIndex)
  177. {
  178. return modelIndex.data(RoleIsEnabled).toBool();
  179. }
  180. void GemRepoModel::SetEnabled(QAbstractItemModel& model, const QModelIndex& modelIndex, bool isEnabled)
  181. {
  182. QSortFilterProxyModel* proxyModel = qobject_cast<QSortFilterProxyModel*>(&model);
  183. if (proxyModel)
  184. {
  185. GemRepoModel* repoModel = qobject_cast<GemRepoModel*>(proxyModel->sourceModel());
  186. if (repoModel)
  187. {
  188. repoModel->SetRepoEnabled(proxyModel->mapToSource(modelIndex), isEnabled);
  189. }
  190. }
  191. }
  192. void GemRepoModel::SetRepoEnabled(const QModelIndex& modelIndex, bool isEnabled)
  193. {
  194. const QString repoUri = GetRepoUri(modelIndex);
  195. const QString repoName = GetName(modelIndex);
  196. if(PythonBindingsInterface::Get()->SetRepoEnabled(repoUri, isEnabled))
  197. {
  198. if (isEnabled)
  199. {
  200. emit ShowToastNotification(tr("%1 activated").arg(repoName));
  201. }
  202. else
  203. {
  204. emit ShowToastNotification(tr("%1 deactivated").arg(repoName));
  205. }
  206. setData(modelIndex, isEnabled, RoleIsEnabled);
  207. }
  208. else
  209. {
  210. QMessageBox::critical(nullptr, tr("Failed to change repo status"), tr("Failed to change the repo status for %1. The local repo.json cache file could be corrupt or the repo.json was not downloaded").arg(repoName));
  211. }
  212. }
  213. bool GemRepoModel::HasAdditionalInfo(const QModelIndex& modelIndex)
  214. {
  215. return !modelIndex.data(RoleAdditionalInfo).toString().isEmpty();
  216. }
  217. QPersistentModelIndex GemRepoModel::FindModelIndexByRepoUri(const QString& repoUri)
  218. {
  219. // the number of repos should be small enough that we don't need a hash
  220. for (int row = 0; row < rowCount(); ++row)
  221. {
  222. QModelIndex modelIndex = index(row, /*column*/ 0);
  223. if (modelIndex.isValid() && modelIndex.data(RoleRepoUri).toString() == repoUri)
  224. {
  225. return modelIndex;
  226. }
  227. }
  228. return {};
  229. }
  230. } // namespace O3DE::ProjectManager