AssetListTableModel.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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/AssetListTableModel.h>
  9. #include <source/utils/utils.h>
  10. #include <AzFramework/StringFunc/StringFunc.h>
  11. namespace AssetBundler
  12. {
  13. //////////////////////////////////////////////////////////////////////////////////////////////////
  14. // AssetListTableModel
  15. //////////////////////////////////////////////////////////////////////////////////////////////////
  16. AssetListTableModel::AssetListTableModel(QObject* parent, const AZStd::string& absolutePath, const AZStd::string& platform)
  17. : QAbstractTableModel(parent)
  18. {
  19. m_seedListManager.reset(new AzToolsFramework::AssetSeedManager());
  20. if (absolutePath.empty() || platform.empty())
  21. {
  22. return;
  23. }
  24. AZ::Outcome<AzToolsFramework::AssetFileInfoList, AZStd::string> outcome = m_seedListManager->LoadAssetFileInfo(absolutePath);
  25. if (!outcome.IsSuccess())
  26. {
  27. AZ_Error(AssetBundler::AppWindowName, false, "Failed to load the asset file info for %s", absolutePath.c_str());
  28. return;
  29. }
  30. m_assetFileInfoList = outcome.TakeValue();
  31. m_platformId = static_cast<AzFramework::PlatformId>(AzFramework::PlatformHelper::GetPlatformIndexFromName(platform.c_str()));
  32. }
  33. int AssetListTableModel::rowCount(const QModelIndex& parent) const
  34. {
  35. return parent.isValid() ? 0 : static_cast<int>(m_assetFileInfoList.m_fileInfoList.size());
  36. }
  37. int AssetListTableModel::columnCount(const QModelIndex& parent) const
  38. {
  39. return parent.isValid() ? 0 : Column::Max;
  40. }
  41. QVariant AssetListTableModel::headerData(int section, Qt::Orientation orientation, int role) const
  42. {
  43. if (role == Qt::DisplayRole && orientation == Qt::Horizontal)
  44. {
  45. switch (section)
  46. {
  47. case Column::ColumnAssetName:
  48. return QString("Asset Name");
  49. case Column::ColumnRelativePath:
  50. return QString("Relative Path");
  51. case Column::ColumnAssetId:
  52. return QString("Asset ID");
  53. default:
  54. break;
  55. }
  56. }
  57. return QVariant();
  58. }
  59. QVariant AssetListTableModel::data(const QModelIndex& index, int role) const
  60. {
  61. auto assetFileInfoOutcome = GetAssetFileInfo(index);
  62. if (!assetFileInfoOutcome.IsSuccess())
  63. {
  64. return QVariant();
  65. }
  66. switch (role)
  67. {
  68. case Qt::DisplayRole:
  69. {
  70. switch (index.column())
  71. {
  72. case Column::ColumnAssetName:
  73. {
  74. AZStd::string fileName = assetFileInfoOutcome.GetValue().get().m_assetRelativePath;
  75. AzFramework::StringFunc::Path::GetFullFileName(fileName.c_str(), fileName);
  76. return fileName.c_str();
  77. }
  78. case Column::ColumnRelativePath:
  79. {
  80. return assetFileInfoOutcome.GetValue().get().m_assetRelativePath.c_str();
  81. }
  82. case Column::ColumnAssetId:
  83. {
  84. AZStd::string assetIdStr;
  85. assetFileInfoOutcome.GetValue().get().m_assetId.ToString(assetIdStr);
  86. return assetIdStr.c_str();
  87. }
  88. default:
  89. break;
  90. }
  91. }
  92. default:
  93. break;
  94. }
  95. return QVariant();
  96. }
  97. AZ::Outcome<AZStd::reference_wrapper<const AzToolsFramework::AssetFileInfo>, AZStd::string> AssetListTableModel::GetAssetFileInfo(const QModelIndex& index) const
  98. {
  99. int row = index.row();
  100. int col = index.column();
  101. if (row >= rowCount() || row < 0 || col >= columnCount() || col < 0)
  102. {
  103. return AZ::Failure(AZStd::string::format("Selected index (%i, %i) is out of range", row, col));
  104. }
  105. return m_assetFileInfoList.m_fileInfoList.at(row);
  106. }
  107. }