2
0

AssetListFileTableModel.cpp 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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/AssetListFileTableModel.h>
  9. #include <source/models/AssetListTableModel.h>
  10. #include <source/utils/utils.h>
  11. #include <AzCore/Outcome/Outcome.h>
  12. #include <AzFramework/IO/LocalFileIO.h>
  13. #include <AzFramework/Platform/PlatformDefaults.h>
  14. #include <AzFramework/StringFunc/StringFunc.h>
  15. #include <AzToolsFramework/Asset/AssetBundler.h>
  16. #include <QFileInfo>
  17. #include <QFont>
  18. namespace AssetBundler
  19. {
  20. //////////////////////////////////////////////////////////////////////////////////////////////////
  21. // AssetListFileInfo
  22. //////////////////////////////////////////////////////////////////////////////////////////////////
  23. AssetListFileInfo::AssetListFileInfo(const AZStd::string& absolutePath, const QString& fileName, const AZStd::string& platform)
  24. : m_absolutePath(absolutePath)
  25. , m_fileName(fileName)
  26. , m_platform(QString(platform.c_str()))
  27. {
  28. AzFramework::StringFunc::Path::Normalize(m_absolutePath);
  29. // Modification time will either give us the time the file was last overwritten
  30. // (or the time it was created if it has never been overwritten)
  31. QFileInfo fileInfo(m_absolutePath.c_str());
  32. m_fileCreationTime = fileInfo.fileTime(QFileDevice::FileModificationTime);
  33. // Load the contents of the asset list file into memory
  34. m_assetListModel.reset(new AssetListTableModel(nullptr, absolutePath, platform));
  35. }
  36. //////////////////////////////////////////////////////////////////////////////////////////////////
  37. // AssetListFileTableModel
  38. //////////////////////////////////////////////////////////////////////////////////////////////////
  39. AssetListFileTableModel::AssetListFileTableModel()
  40. : AssetBundlerAbstractFileTableModel()
  41. {
  42. }
  43. QSharedPointer<AssetListTableModel> AssetListFileTableModel::GetAssetListFileContents(const QModelIndex& index)
  44. {
  45. auto assetListFileInfoOutcome = GetAssetFileInfo(index);
  46. if (!assetListFileInfoOutcome.IsSuccess())
  47. {
  48. return QSharedPointer<AssetListTableModel>();
  49. }
  50. return assetListFileInfoOutcome.GetValue()->m_assetListModel;
  51. }
  52. bool AssetListFileTableModel::DeleteFile(const QModelIndex& index)
  53. {
  54. auto assetFileInfoOutcome = GetAssetFileInfo(index);
  55. if (!assetFileInfoOutcome.IsSuccess())
  56. {
  57. return false;
  58. }
  59. AZStd::string key = AssetBundler::GenerateKeyFromAbsolutePath(assetFileInfoOutcome.GetValue()->m_absolutePath);
  60. if (m_assetListFileInfoMap.find(key) == m_assetListFileInfoMap.end())
  61. {
  62. return false;
  63. }
  64. AssetListFileInfoPtr assetFileInfo = m_assetListFileInfoMap[key];
  65. // Remove file from disk
  66. const char* absolutePath = assetFileInfo->m_absolutePath.c_str();
  67. if (AZ::IO::FileIOBase::GetInstance()->Exists(absolutePath))
  68. {
  69. if (AZ::IO::FileIOBase::GetInstance()->IsReadOnly(absolutePath))
  70. {
  71. AZ_Error(AssetBundler::AppWindowName, false, ReadOnlyFileErrorMessage, absolutePath);
  72. return false;
  73. }
  74. auto deleteResult = AZ::IO::FileIOBase::GetInstance()->Remove(absolutePath);
  75. if (!deleteResult)
  76. {
  77. AZ_Error(AssetBundler::AppWindowName, false,
  78. "Unable to delete (%s). Result code: %u", absolutePath, deleteResult.GetResultCode());
  79. return false;
  80. }
  81. }
  82. // Remove file from model
  83. m_assetListFileInfoMap.erase(key);
  84. RemoveFileKey(index);
  85. return true;
  86. }
  87. void AssetListFileTableModel::LoadFile(
  88. const AZStd::string& absoluteFilePath,
  89. const AZStd::string& /*projectName*/,
  90. bool /*isDefaultFile*/)
  91. {
  92. AZStd::string fullFileName;
  93. AzFramework::StringFunc::Path::GetFullFileName(absoluteFilePath.c_str(), fullFileName);
  94. // Get the file name without the platform for display purposes
  95. AZStd::string baseFileName;
  96. AZStd::string platformIdentifier;
  97. AzToolsFramework::SplitFilename(fullFileName, baseFileName, platformIdentifier);
  98. // Read the AssetListFile into memory and store it
  99. AZStd::string key = AssetBundler::GenerateKeyFromAbsolutePath(absoluteFilePath);
  100. m_assetListFileInfoMap[key].reset(new AssetListFileInfo(absoluteFilePath, QString(baseFileName.c_str()), platformIdentifier));
  101. AddFileKey(key);
  102. }
  103. bool AssetListFileTableModel::WriteToDisk(const AZStd::string& /*key*/)
  104. {
  105. return true;
  106. }
  107. AZStd::string AssetListFileTableModel::GetFileAbsolutePath(const QModelIndex& index) const
  108. {
  109. auto assetFileInfoOutcome = GetAssetFileInfo(index);
  110. if (!assetFileInfoOutcome.IsSuccess())
  111. {
  112. // Error has already been thrown
  113. return AZStd::string();
  114. }
  115. return assetFileInfoOutcome.GetValue()->m_absolutePath;
  116. }
  117. int AssetListFileTableModel::GetFileNameColumnIndex() const
  118. {
  119. return Column::ColumnFileName;
  120. }
  121. int AssetListFileTableModel::GetTimeStampColumnIndex() const
  122. {
  123. return Column::ColumnFileCreationTime;
  124. }
  125. int AssetListFileTableModel::columnCount(const QModelIndex& parent) const
  126. {
  127. return parent.isValid() ? 0 : Column::Max;
  128. }
  129. QVariant AssetListFileTableModel::headerData(int section, Qt::Orientation orientation, int role) const
  130. {
  131. if (role == Qt::DisplayRole && orientation == Qt::Horizontal)
  132. {
  133. switch (section)
  134. {
  135. case Column::ColumnFileName:
  136. return QString(tr("Asset List File"));
  137. case Column::ColumnPlatform:
  138. return QString(tr("Platform"));
  139. case Column::ColumnFileCreationTime:
  140. return QString(tr("Creation Time"));
  141. default:
  142. break;
  143. }
  144. }
  145. return QVariant();
  146. }
  147. QVariant AssetListFileTableModel::data(const QModelIndex& index, int role) const
  148. {
  149. auto assetListFileInfoOutcome = GetAssetFileInfo(index);
  150. if (!assetListFileInfoOutcome.IsSuccess())
  151. {
  152. return QVariant();
  153. }
  154. int col = index.column();
  155. switch (role)
  156. {
  157. case Qt::DisplayRole:
  158. [[fallthrough]];
  159. case SortRole:
  160. {
  161. if (col == Column::ColumnFileName)
  162. {
  163. return assetListFileInfoOutcome.GetValue()->m_fileName;
  164. }
  165. else if (col == Column::ColumnPlatform)
  166. {
  167. return assetListFileInfoOutcome.GetValue()->m_platform;
  168. }
  169. else if (col == Column::ColumnFileCreationTime)
  170. {
  171. if (role == SortRole)
  172. {
  173. return assetListFileInfoOutcome.GetValue()->m_fileCreationTime;
  174. }
  175. else
  176. {
  177. return assetListFileInfoOutcome.GetValue()->m_fileCreationTime.toString(DateTimeFormat);
  178. }
  179. }
  180. else
  181. {
  182. // Returning an empty QString will ensure the checkboxes do not have any text displayed next to them
  183. return QString();
  184. }
  185. }
  186. case Qt::FontRole:
  187. {
  188. if (col == Column::ColumnFileName)
  189. {
  190. QFont boldFont;
  191. boldFont.setBold(true);
  192. return boldFont;
  193. }
  194. }
  195. default:
  196. break;
  197. }
  198. return QVariant();
  199. }
  200. AZ::Outcome<AssetListFileInfoPtr, void> AssetListFileTableModel::GetAssetFileInfo(const QModelIndex& index) const
  201. {
  202. AZStd::string key = GetFileKey(index);
  203. if (key.empty())
  204. {
  205. // Error has already been thrown
  206. return AZ::Failure();
  207. }
  208. if (m_assetListFileInfoMap.find(key) != m_assetListFileInfoMap.end())
  209. {
  210. return AZ::Success(m_assetListFileInfoMap.at(key));
  211. }
  212. else
  213. {
  214. AZ_Error(AssetBundler::AppWindowName, false, "Cannot find Asset List File Info");
  215. return AZ::Failure();
  216. }
  217. }
  218. } // namespace AssetBundler