GemModel.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. #pragma once
  9. #if !defined(Q_MOC_RUN)
  10. #include <GemCatalog/GemInfo.h>
  11. #include <TagWidget.h>
  12. #include <QAbstractItemModel>
  13. #include <QStandardItemModel>
  14. #include <QItemSelectionModel>
  15. #endif
  16. namespace O3DE::ProjectManager
  17. {
  18. class GemModel
  19. : public QStandardItemModel
  20. {
  21. Q_OBJECT
  22. public:
  23. explicit GemModel(QObject* parent = nullptr);
  24. QItemSelectionModel* GetSelectionModel() const;
  25. enum UserRole
  26. {
  27. RoleName = Qt::UserRole,
  28. RoleDisplayName,
  29. RoleWasPreviouslyAdded,
  30. RoleWasPreviouslyAddedDependency,
  31. RoleIsAdded,
  32. RoleIsAddedDependency,
  33. RoleDependingGems,
  34. RoleVersion, // the current version
  35. RoleNewVersion, // the new version the user wants to use
  36. RoleDownloadStatus,
  37. RoleGemInfoVersions
  38. };
  39. QPersistentModelIndex AddGem(const GemInfo& gemInfo);
  40. QVector<QPersistentModelIndex> AddGems(const QVector<GemInfo>& gemInfos, bool updateExisting = false);
  41. void ActivateGems(const QHash<QString, QString>& enabledGemNames);
  42. void RemoveGem(const QModelIndex& modelIndex);
  43. void RemoveGem(const QString& gemName, const QString& version = "", const QString& path = "");
  44. void Clear();
  45. void UpdateGemDependencies();
  46. QPersistentModelIndex FindIndexByNameString(const QString& nameString) const;
  47. QVector<Tag> GetDependingGemTags(const QModelIndex& modelIndex);
  48. bool HasDependentGems(const QModelIndex& modelIndex) const;
  49. /*
  50. * Get the GemInfo for the currently displayed item if no version or path is specified,
  51. * otherwise, return the gem info for the requested version and/or path
  52. * @param modelIndex The model index for the gem
  53. * @param version The optional version to find
  54. * @param path The optional path to find, this is often preferred over version because it is possible the user
  55. * has multiple instances of the same gem registered
  56. */
  57. static const GemInfo GetGemInfo(const QPersistentModelIndex& modelIndex, const QString& version = "", const QString& path = "");
  58. static const QList<QVariant> GetGemVersions(const QModelIndex& modelIndex);
  59. static QString GetName(const QModelIndex& modelIndex);
  60. static QString GetDisplayName(const QModelIndex& modelIndex);
  61. static GemInfo::DownloadStatus GetDownloadStatus(const QModelIndex& modelIndex);
  62. static QString GetVersion(const QModelIndex& modelIndex);
  63. static QString GetNewVersion(const QModelIndex& modelIndex);
  64. static GemModel* GetSourceModel(QAbstractItemModel* model);
  65. static const GemModel* GetSourceModel(const QAbstractItemModel* model);
  66. static bool IsAdded(const QModelIndex& modelIndex);
  67. static bool IsAddedDependency(const QModelIndex& modelIndex);
  68. static void SetIsAdded(QAbstractItemModel& model, const QModelIndex& modelIndex, bool isAdded, const QString& version = "");
  69. static void SetIsAddedDependency(QAbstractItemModel& model, const QModelIndex& modelIndex, bool isAdded);
  70. //! Set the version the user confirms they want to use
  71. static void SetNewVersion(QAbstractItemModel& model, const QModelIndex& modelIndex, const QString& version);
  72. static void SetWasPreviouslyAdded(QAbstractItemModel& model, const QModelIndex& modelIndex, bool wasAdded);
  73. static bool WasPreviouslyAdded(const QModelIndex& modelIndex);
  74. static void SetWasPreviouslyAddedDependency(QAbstractItemModel& model, const QModelIndex& modelIndex, bool wasAdded);
  75. static bool WasPreviouslyAddedDependency(const QModelIndex& modelIndex);
  76. static bool NeedsToBeAdded(const QModelIndex& modelIndex, bool includeDependencies = false);
  77. static bool NeedsToBeRemoved(const QModelIndex& modelIndex, bool includeDependencies = false);
  78. static bool HasRequirement(const QModelIndex& modelIndex);
  79. static bool HasUpdates(const QModelIndex& modelIndex, bool compatibleOnly = true);
  80. static bool IsCompatible(const QModelIndex& modelIndex);
  81. static bool IsAddedMissing(const QModelIndex& modelIndex);
  82. static void UpdateDependencies(QAbstractItemModel& model, const QString& gemName, bool isAdded);
  83. static void UpdateWithVersion(
  84. QAbstractItemModel& model, const QPersistentModelIndex& modelIndex, const QString& version, const QString& path = "");
  85. static void DeactivateDependentGems(QAbstractItemModel& model, const QModelIndex& modelIndex);
  86. static void SetDownloadStatus(QAbstractItemModel& model, const QModelIndex& modelIndex, GemInfo::DownloadStatus status);
  87. bool DoGemsToBeAddedHaveRequirements() const;
  88. bool HasDependentGemsToRemove() const;
  89. QVector<QPersistentModelIndex> GatherGemDependencies(const QPersistentModelIndex& modelIndex) const;
  90. QVector<QPersistentModelIndex> GatherDependentGems(const QPersistentModelIndex& modelIndex, bool addedOnly = false) const;
  91. QVector<QModelIndex> GatherGemsToBeAdded(bool includeDependencies = false) const;
  92. QVector<QModelIndex> GatherGemsToBeRemoved(bool includeDependencies = false) const;
  93. int TotalAddedGems(bool includeDependencies = false) const;
  94. void ShowCompatibleGems();
  95. signals:
  96. void gemStatusChanged(const QString& gemName, uint32_t numChangedDependencies);
  97. void dependencyGemStatusChanged(const QString& gemName);
  98. protected slots:
  99. void OnRowsAboutToBeRemoved(const QModelIndex& parent, int first, int last);
  100. void OnRowsRemoved(const QModelIndex& parent, int first, int last);
  101. private:
  102. void GetAllDependingGems(const QModelIndex& modelIndex, QSet<QPersistentModelIndex>& inOutGems);
  103. QStringList GetDependingGems(const QModelIndex& modelIndex);
  104. QString GetMostCompatibleVersion(const QModelIndex& modelIndex);
  105. bool VersionIsCompatible(const QModelIndex& modelIndex, const QString& version);
  106. bool ShouldUpdateItemDataFromGemInfo(const QModelIndex& modelIndex, const GemInfo& gemInfo);
  107. QHash<QString, QPersistentModelIndex> m_nameToIndexMap;
  108. QItemSelectionModel* m_selectionModel = nullptr;
  109. QHash<QString, QSet<QPersistentModelIndex>> m_gemDependencyMap;
  110. QHash<QString, QSet<QPersistentModelIndex>> m_gemReverseDependencyMap;
  111. };
  112. } // namespace O3DE::ProjectManager