SeedListTableModel.cpp 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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 <source/models/SeedListTableModel.h>
  9. #include <source/utils/utils.h>
  10. #include <AzCore/Outcome/Outcome.h>
  11. #include <AzCore/std/smart_ptr/make_shared.h>
  12. #include <AzFramework/IO/LocalFileIO.h>
  13. #include <AzFramework/StringFunc/StringFunc.h>
  14. #include <AzToolsFramework/AssetCatalog/PlatformAddressedAssetCatalog.h>
  15. #include <QFont>
  16. namespace AssetBundler
  17. {
  18. //////////////////////////////////////////////////////////////////////////////////////////////////
  19. // AdditionalSeedInfo
  20. //////////////////////////////////////////////////////////////////////////////////////////////////
  21. AdditionalSeedInfo::AdditionalSeedInfo(const QString& relativePath, const QString& platformList)
  22. : m_relativePath(relativePath)
  23. , m_platformList(platformList)
  24. {
  25. }
  26. //////////////////////////////////////////////////////////////////////////////////////////////////
  27. // SeedListTableModel
  28. //////////////////////////////////////////////////////////////////////////////////////////////////
  29. SeedListTableModel::SeedListTableModel(
  30. QObject* parent,
  31. const AZStd::string& absolutePath,
  32. const AZStd::vector<AZStd::string>& defaultSeeds,
  33. const AzFramework::PlatformFlags& platforms)
  34. : QAbstractTableModel(parent)
  35. {
  36. m_seedListManager.reset(new AzToolsFramework::AssetSeedManager());
  37. if (absolutePath.empty() && defaultSeeds.empty())
  38. {
  39. return;
  40. }
  41. if (!defaultSeeds.empty())
  42. {
  43. for (const AZStd::string& seed : defaultSeeds)
  44. {
  45. m_seedListManager->AddSeedAssetForValidPlatforms(seed, platforms);
  46. }
  47. m_isFileOnDisk = false;
  48. }
  49. else
  50. {
  51. m_seedListManager->Load(absolutePath);
  52. }
  53. AZ::Data::AssetInfo assetInfo;
  54. QString platformList;
  55. for (const auto& seed : m_seedListManager->GetAssetSeedList())
  56. {
  57. assetInfo = AzToolsFramework::AssetSeedManager::GetAssetInfoById(
  58. seed.m_assetId,
  59. AzFramework::PlatformHelper::GetPlatformIndicesInterpreted(seed.m_platformFlags)[0],
  60. absolutePath,
  61. seed.m_assetRelativePath);
  62. platformList = QString(m_seedListManager->GetReadablePlatformList(seed).c_str());
  63. m_additionalSeedInfoMap[seed.m_assetId].reset(new AdditionalSeedInfo(assetInfo.m_relativePath.c_str(), platformList));
  64. }
  65. }
  66. AZ::Outcome<AzFramework::PlatformFlags, void> SeedListTableModel::GetSeedPlatforms(const QModelIndex& index) const
  67. {
  68. auto seedOutcome = GetSeedInfo(index);
  69. if (!seedOutcome.IsSuccess())
  70. {
  71. // Error has already been thrown
  72. return AZ::Failure();
  73. }
  74. return AZ::Success(seedOutcome.GetValue().m_platformFlags);
  75. }
  76. bool SeedListTableModel::Save(const AZStd::string& absolutePath)
  77. {
  78. if (!HasUnsavedChanges())
  79. {
  80. // There are no changes, so there is nothing to save
  81. return true;
  82. }
  83. SetHasUnsavedChanges(!m_seedListManager->Save(absolutePath));
  84. return !HasUnsavedChanges();
  85. }
  86. bool SeedListTableModel::SetSeedPlatforms(const QModelIndex& index, const AzFramework::PlatformFlags& platforms)
  87. {
  88. auto seedOutcome = GetSeedInfo(index);
  89. if (!seedOutcome.IsSuccess())
  90. {
  91. // Error has already been thrown
  92. return false;
  93. }
  94. if (platforms == AzFramework::PlatformFlags::Platform_NONE)
  95. {
  96. AZ_Error(AssetBundler::AppWindowName, false, "Cannot Edit Platforms: No platforms were selected");
  97. return false;
  98. }
  99. auto setPlatformOutcome = m_seedListManager->SetSeedPlatformFlags(index.row(), platforms);
  100. if (!setPlatformOutcome.IsSuccess())
  101. {
  102. AZ_Error(AssetBundler::AppWindowName, false, setPlatformOutcome.GetError().c_str());
  103. return false;
  104. }
  105. // Update the cached display info
  106. auto additionalSeedInfo = m_additionalSeedInfoMap.find(seedOutcome.GetValue().m_assetId);
  107. if (additionalSeedInfo == m_additionalSeedInfoMap.end())
  108. {
  109. AZ_Error(AssetBundler::AppWindowName, false, "Unable to find additional Seed info");
  110. return false;
  111. }
  112. additionalSeedInfo->second->m_platformList =
  113. QString(AzFramework::PlatformHelper::GetCommaSeparatedPlatformList(platforms).c_str());
  114. SetHasUnsavedChanges(true);
  115. // Update the display
  116. QModelIndex firstChangedIndex = QAbstractTableModel::index(index.row(), Column::ColumnRelativePath);
  117. QModelIndex lastChangedIndex = QAbstractTableModel::index(index.row(), Column::Max - 1);
  118. emit dataChanged(firstChangedIndex, lastChangedIndex, { Qt::DisplayRole });
  119. return true;
  120. }
  121. bool SeedListTableModel::AddSeed(const AZStd::string& seedRelativePath, const AzFramework::PlatformFlags& platforms)
  122. {
  123. AZStd::pair<AZ::Data::AssetId, AzFramework::PlatformFlags> addSeedsResult =
  124. m_seedListManager->AddSeedAssetForValidPlatforms(seedRelativePath, platforms);
  125. if (!addSeedsResult.first.IsValid() || addSeedsResult.second == AzFramework::PlatformFlags::Platform_NONE)
  126. {
  127. // Error has already been thrown
  128. return false;
  129. }
  130. QString platformList = QString(AzFramework::PlatformHelper::GetCommaSeparatedPlatformList(addSeedsResult.second).c_str());
  131. int lastRowIndex = AZStd::max(rowCount() - 1, 0);
  132. beginInsertRows(QModelIndex(), lastRowIndex, lastRowIndex);
  133. m_additionalSeedInfoMap[addSeedsResult.first].reset(new AdditionalSeedInfo(QString(seedRelativePath.c_str()), platformList));
  134. endInsertRows();
  135. SetHasUnsavedChanges(true);
  136. return true;
  137. }
  138. bool SeedListTableModel::RemoveSeed(const QModelIndex& seedIndex)
  139. {
  140. auto seedOutcome = GetSeedInfo(seedIndex);
  141. if (!seedOutcome.IsSuccess())
  142. {
  143. // Error has already been thrown
  144. return false;
  145. }
  146. int row = seedIndex.row();
  147. beginRemoveRows(QModelIndex(), row, row);
  148. m_seedListManager->RemoveSeedAsset(seedOutcome.GetValue().m_assetId, seedOutcome.GetValue().m_platformFlags);
  149. m_additionalSeedInfoMap.erase(seedOutcome.GetValue().m_assetId);
  150. endRemoveRows();
  151. SetHasUnsavedChanges(true);
  152. return true;
  153. }
  154. int SeedListTableModel::rowCount(const QModelIndex& parent) const
  155. {
  156. return parent.isValid() ? 0 : static_cast<int>(m_additionalSeedInfoMap.size());
  157. }
  158. int SeedListTableModel::columnCount(const QModelIndex& parent) const
  159. {
  160. return parent.isValid() ? 0 : Column::Max;
  161. }
  162. QVariant SeedListTableModel::headerData(int section, Qt::Orientation orientation, int role) const
  163. {
  164. if (role == Qt::DisplayRole && orientation == Qt::Horizontal)
  165. {
  166. switch (section)
  167. {
  168. case Column::ColumnRelativePath:
  169. return QString("Seed");
  170. case Column::ColumnPlatformList:
  171. return QString("Platforms");
  172. default:
  173. break;
  174. }
  175. }
  176. return QVariant();
  177. }
  178. QVariant SeedListTableModel::data(const QModelIndex& index, int role) const
  179. {
  180. auto additionalSeedInfoOutcome = GetAdditionalSeedInfo(index);
  181. if (!additionalSeedInfoOutcome.IsSuccess())
  182. {
  183. return QVariant();
  184. }
  185. switch (role)
  186. {
  187. case Qt::DisplayRole:
  188. {
  189. if (index.column() == Column::ColumnRelativePath)
  190. {
  191. return additionalSeedInfoOutcome.GetValue()->m_relativePath;
  192. }
  193. else if (index.column() == Column::ColumnPlatformList)
  194. {
  195. return additionalSeedInfoOutcome.GetValue()->m_platformList;
  196. }
  197. }
  198. default:
  199. break;
  200. }
  201. return QVariant();
  202. }
  203. AZ::Outcome<AzFramework::SeedInfo&, void> SeedListTableModel::GetSeedInfo(const QModelIndex& index) const
  204. {
  205. int row = index.row();
  206. int col = index.column();
  207. if (row >= rowCount() || row < 0 || col >= columnCount() || col < 0)
  208. {
  209. AZ_Error(AssetBundler::AppWindowName, false, "Selected index (%i, %i) is out of range", row, col);
  210. return AZ::Failure();
  211. }
  212. return AZ::Success(m_seedListManager->GetAssetSeedList().at(row));
  213. }
  214. AZ::Outcome<AdditionalSeedInfoPtr, void> SeedListTableModel::GetAdditionalSeedInfo(const QModelIndex& index) const
  215. {
  216. auto seedInfoOutcome = GetSeedInfo(index);
  217. if (!seedInfoOutcome.IsSuccess())
  218. {
  219. // Error has already been thrown
  220. return AZ::Failure();
  221. }
  222. auto additionalSeedInfoIt = m_additionalSeedInfoMap.find(seedInfoOutcome.GetValue().m_assetId);
  223. if (additionalSeedInfoIt == m_additionalSeedInfoMap.end())
  224. {
  225. return AZ::Failure();
  226. }
  227. return AZ::Success(additionalSeedInfoIt->second);
  228. }
  229. } // namespace AssetBundler