AssetTreeModel.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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 "AssetTreeModel.h"
  9. #include "AssetTreeItem.h"
  10. #include <AzCore/Component/TickBus.h>
  11. #include <AzCore/std/smart_ptr/make_shared.h>
  12. namespace AssetProcessor
  13. {
  14. AssetTreeModel::AssetTreeModel(AZStd::shared_ptr<AzToolsFramework::AssetDatabase::AssetDatabaseConnection> sharedDbConnection, QObject *parent) :
  15. QAbstractItemModel(parent),
  16. m_sharedDbConnection(sharedDbConnection)
  17. , m_errorIcon(QStringLiteral(":/stylesheet/img/logging/error.svg"))
  18. , m_folderIcon(QIcon(QStringLiteral(":/Gallery/Asset_Folder.svg")))
  19. , m_fileIcon(QIcon(QStringLiteral(":/Gallery/Asset_File.svg")))
  20. {
  21. m_folderIcon.addFile(QStringLiteral(":/Gallery/Asset_Folder.svg"), QSize(), QIcon::Selected);
  22. ApplicationManagerNotifications::Bus::Handler::BusConnect();
  23. AzToolsFramework::AssetDatabase::AssetDatabaseNotificationBus::Handler::BusConnect();
  24. }
  25. AssetTreeModel::~AssetTreeModel()
  26. {
  27. AzToolsFramework::AssetDatabase::AssetDatabaseNotificationBus::Handler::BusDisconnect();
  28. ApplicationManagerNotifications::Bus::Handler::BusDisconnect();
  29. }
  30. void AssetTreeModel::ApplicationShutdownRequested()
  31. {
  32. AzToolsFramework::AssetDatabase::AssetDatabaseNotificationBus::Handler::BusDisconnect();
  33. // AssetTreeModels can queue functions on the systemTickBus for processing on the main thread in response to asset changes
  34. // We need to clear out any left pending before we go away
  35. AZ::SystemTickBus::ExecuteQueuedEvents();
  36. }
  37. void AssetTreeModel::Reset()
  38. {
  39. beginResetModel();
  40. m_root.reset(new AssetTreeItem(
  41. AZStd::make_shared<AssetTreeItemData>("", "", true, AZ::Uuid::CreateNull(), AzToolsFramework::AssetDatabase::InvalidEntryId),
  42. m_errorIcon,
  43. m_folderIcon,
  44. m_fileIcon));
  45. ResetModel();
  46. endResetModel();
  47. }
  48. int AssetTreeModel::rowCount(const QModelIndex &parent) const
  49. {
  50. if (parent.column() > 0)
  51. {
  52. return 0;
  53. }
  54. AssetTreeItem* parentItem = nullptr;
  55. if (!parent.isValid())
  56. {
  57. parentItem = m_root.get();
  58. }
  59. else
  60. {
  61. parentItem = static_cast<AssetTreeItem*>(parent.internalPointer());
  62. }
  63. if (!parentItem)
  64. {
  65. return 0;
  66. }
  67. return parentItem->getChildCount();
  68. }
  69. int AssetTreeModel::columnCount(const QModelIndex &parent) const
  70. {
  71. if (parent.isValid())
  72. {
  73. return static_cast<AssetTreeItem*>(parent.internalPointer())->GetColumnCount();
  74. }
  75. if (m_root)
  76. {
  77. return m_root->GetColumnCount();
  78. }
  79. return 0;
  80. }
  81. QVariant AssetTreeModel::data(const QModelIndex &index, int role) const
  82. {
  83. if (!index.isValid())
  84. {
  85. return QVariant();
  86. }
  87. AssetTreeItem* item = static_cast<AssetTreeItem*>(index.internalPointer());
  88. switch (role)
  89. {
  90. case Qt::DisplayRole:
  91. return item->GetDataForColumn(index.column());
  92. case Qt::DecorationRole:
  93. // Only show the icon in the name column
  94. if (index.column() == static_cast<int>(AssetTreeColumns::Name))
  95. {
  96. return item->GetIcon();
  97. }
  98. break;
  99. case Qt::ToolTipRole:
  100. {
  101. QString toolTip = item->GetData()->m_unresolvedIssuesTooltip;
  102. if (!toolTip.isEmpty())
  103. {
  104. return toolTip;
  105. }
  106. // Purposely return an empty string, so mousing over rows clear out.
  107. return QString("");
  108. }
  109. }
  110. return QVariant();
  111. }
  112. QVariant AssetTreeModel::headerData(int section, Qt::Orientation orientation, int role) const
  113. {
  114. if (orientation != Qt::Horizontal || role != Qt::DisplayRole)
  115. {
  116. return QVariant();
  117. }
  118. if (section < 0 || section >= static_cast<int>(AssetTreeColumns::Max))
  119. {
  120. return QVariant();
  121. }
  122. switch (section)
  123. {
  124. case static_cast<int>(AssetTreeColumns::Name):
  125. return tr("Name");
  126. case static_cast<int>(AssetTreeColumns::Extension):
  127. return tr("Extension");
  128. default:
  129. AZ_Warning("AssetProcessor", false, "Unhandled AssetTree section %d", section);
  130. break;
  131. }
  132. return QVariant();
  133. }
  134. QModelIndex AssetTreeModel::index(int row, int column, const QModelIndex &parent) const
  135. {
  136. if (!hasIndex(row, column, parent))
  137. {
  138. return QModelIndex();
  139. }
  140. AssetTreeItem* parentItem = nullptr;
  141. if (!parent.isValid())
  142. {
  143. parentItem = m_root.get();
  144. }
  145. else
  146. {
  147. parentItem = static_cast<AssetTreeItem*>(parent.internalPointer());
  148. }
  149. if (!parentItem)
  150. {
  151. return QModelIndex();
  152. }
  153. AssetTreeItem* childItem = parentItem->GetChild(row);
  154. if (childItem)
  155. {
  156. QModelIndex index = createIndex(row, column, childItem);
  157. Q_ASSERT(checkIndex(index));
  158. return index;
  159. }
  160. return QModelIndex();
  161. }
  162. bool AssetTreeModel::setData(const QModelIndex &/*index*/, const QVariant &/*value*/, int /*role*/)
  163. {
  164. return false;
  165. }
  166. Qt::ItemFlags AssetTreeModel::flags(const QModelIndex &index) const
  167. {
  168. return Qt::ItemIsSelectable | QAbstractItemModel::flags(index);
  169. }
  170. QModelIndex AssetTreeModel::parent(const QModelIndex &index) const
  171. {
  172. if (!index.isValid())
  173. {
  174. return QModelIndex();
  175. }
  176. AssetTreeItem* childItem = static_cast<AssetTreeItem*>(index.internalPointer());
  177. AssetTreeItem* parentItem = childItem->GetParent();
  178. if (parentItem == m_root.get() || parentItem == nullptr)
  179. {
  180. return QModelIndex();
  181. }
  182. QModelIndex parentIndex = createIndex(parentItem->GetRow(), 0, parentItem);
  183. Q_ASSERT(checkIndex(parentIndex));
  184. return parentIndex;
  185. }
  186. bool AssetTreeModel::hasChildren(const QModelIndex &parent) const
  187. {
  188. AssetTreeItem* parentItem = nullptr;
  189. if (!parent.isValid())
  190. {
  191. parentItem = m_root.get();
  192. }
  193. else
  194. {
  195. parentItem = static_cast<AssetTreeItem*>(parent.internalPointer());
  196. }
  197. if (!parentItem)
  198. {
  199. return false;
  200. }
  201. return parentItem->getChildCount() > 0;
  202. }
  203. QFlags<QItemSelectionModel::SelectionFlag> AssetTreeModel::GetAssetTreeSelectionFlags()
  204. {
  205. return QItemSelectionModel::ClearAndSelect | QItemSelectionModel::Rows | QItemSelectionModel::QItemSelectionModel::Current;
  206. }
  207. }