ProductDependencyTreeModel.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  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 <native/ui/ProductDependencyTreeModel.h>
  9. #include <native/ui/ProductDependencyTreeItemData.h>
  10. #include <native/ui/AssetTreeItem.h>
  11. #include <native/ui/ProductAssetTreeItemData.h>
  12. #include <QIcon>
  13. #include <QItemSelection>
  14. #include <AzCore/IO/Path/Path.h>
  15. #include <AzFramework/StringFunc/StringFunc.h>
  16. #include <AzCore/std/smart_ptr/make_shared.h>
  17. namespace AssetProcessor
  18. {
  19. ProductDependencyTreeModel::ProductDependencyTreeModel(
  20. AZStd::shared_ptr<AzToolsFramework::AssetDatabase::AssetDatabaseConnection> sharedDbConnection,
  21. AssetTreeFilterModel* productFilterModel,
  22. DependencyTreeType treeType,
  23. QObject* parent)
  24. : QAbstractItemModel(parent)
  25. , m_sharedDbConnection(sharedDbConnection)
  26. , m_productFilterModel(productFilterModel)
  27. , m_treeType(treeType)
  28. , m_fileIcon(QIcon(QStringLiteral(":/AssetProcessor_goto.svg")))
  29. {
  30. m_root.reset(new ProductDependencyTreeItem(AZStd::make_shared<ProductDependencyTreeItemData>("", "")));
  31. }
  32. ProductDependencyTreeModel::~ProductDependencyTreeModel()
  33. {
  34. }
  35. QModelIndex ProductDependencyTreeModel::index(int row, int column, const QModelIndex& parent) const
  36. {
  37. if (!hasIndex(row, column, parent))
  38. {
  39. return QModelIndex();
  40. }
  41. ProductDependencyTreeItem* parentItem = nullptr;
  42. if (!parent.isValid())
  43. {
  44. parentItem = m_root.get();
  45. }
  46. else
  47. {
  48. parentItem = static_cast<ProductDependencyTreeItem*>(parent.internalPointer());
  49. }
  50. if (!parentItem)
  51. {
  52. return QModelIndex();
  53. }
  54. ProductDependencyTreeItem* childItem = parentItem->GetChild(row);
  55. if (childItem)
  56. {
  57. QModelIndex index = createIndex(row, column, childItem);
  58. if (checkIndex(index))
  59. {
  60. return index;
  61. }
  62. }
  63. return QModelIndex();
  64. }
  65. int ProductDependencyTreeModel::rowCount(const QModelIndex& parent) const
  66. {
  67. if (parent.column() > 0)
  68. {
  69. return 0;
  70. }
  71. ProductDependencyTreeItem* parentItem = nullptr;
  72. if (!parent.isValid())
  73. {
  74. parentItem = m_root.get();
  75. }
  76. else
  77. {
  78. parentItem = static_cast<ProductDependencyTreeItem*>(parent.internalPointer());
  79. }
  80. if (!parentItem)
  81. {
  82. return 0;
  83. }
  84. return parentItem->getChildCount();
  85. }
  86. int ProductDependencyTreeModel::columnCount([[maybe_unused]] const QModelIndex& parent) const
  87. {
  88. return static_cast<int>(ProductDependencyTreeColumns::Max);
  89. }
  90. QVariant ProductDependencyTreeModel::data(const QModelIndex& index, int role) const
  91. {
  92. if (!index.isValid())
  93. {
  94. return QVariant();
  95. }
  96. ProductDependencyTreeItem* item = static_cast<ProductDependencyTreeItem*>(index.internalPointer());
  97. switch (role)
  98. {
  99. case Qt::DisplayRole:
  100. return item->GetDataForColumn(index.column());
  101. case Qt::DecorationRole:
  102. // Only show the icon in the name column
  103. if (index.column() == static_cast<int>(AssetTreeColumns::Name))
  104. {
  105. return m_fileIcon;
  106. }
  107. break;
  108. case Qt::ToolTipRole:
  109. // Purposely return an empty string, so mousing over rows clear out.
  110. return QString("");
  111. }
  112. return QVariant();
  113. }
  114. bool ProductDependencyTreeModel::setData(
  115. [[maybe_unused]] const QModelIndex& index, [[maybe_unused]] const QVariant& value, [[maybe_unused]] int role)
  116. {
  117. return false;
  118. }
  119. Qt::ItemFlags ProductDependencyTreeModel::flags(const QModelIndex& index) const
  120. {
  121. return Qt::ItemIsSelectable | QAbstractItemModel::flags(index);
  122. }
  123. QModelIndex ProductDependencyTreeModel::parent(const QModelIndex& index) const
  124. {
  125. if (!index.isValid())
  126. {
  127. return QModelIndex();
  128. }
  129. ProductDependencyTreeItem* childItem = static_cast<ProductDependencyTreeItem*>(index.internalPointer());
  130. ProductDependencyTreeItem* parentItem = childItem->GetParent();
  131. if (parentItem == m_root.get() || parentItem == nullptr)
  132. {
  133. return QModelIndex();
  134. }
  135. QModelIndex parentIndex = createIndex(parentItem->GetRow(), 0, parentItem);
  136. if (checkIndex(parentIndex))
  137. {
  138. return parentIndex;
  139. }
  140. return QModelIndex();
  141. }
  142. bool ProductDependencyTreeModel::hasChildren(const QModelIndex& parent) const
  143. {
  144. ProductDependencyTreeItem* parentItem = nullptr;
  145. if (!parent.isValid())
  146. {
  147. parentItem = m_root.get();
  148. }
  149. else
  150. {
  151. parentItem = static_cast<ProductDependencyTreeItem*>(parent.internalPointer());
  152. }
  153. if (!parentItem)
  154. {
  155. return false;
  156. }
  157. return parentItem->getChildCount() > 0;
  158. }
  159. void ProductDependencyTreeModel::AssetDataSelectionChanged(const QItemSelection& selected, [[maybe_unused]] const QItemSelection& deselected)
  160. {
  161. // Even if multi-select is enabled, only display the first selected item.
  162. if (selected.indexes().count() == 0 || !selected.indexes()[0].isValid())
  163. {
  164. // ResetText();
  165. return;
  166. }
  167. QModelIndex productModelIndex = m_productFilterModel->mapToSource(selected.indexes()[0]);
  168. if (!productModelIndex.isValid())
  169. {
  170. return;
  171. }
  172. const AssetTreeItem* assetTreeItem = static_cast<const AssetTreeItem*>(productModelIndex.internalPointer());
  173. const AZStd::shared_ptr<const ProductAssetTreeItemData> productItemData =
  174. AZStd::rtti_pointer_cast<const ProductAssetTreeItemData>(assetTreeItem->GetData());
  175. beginResetModel();
  176. ProductDependencyTreeItem* productDependencies =
  177. new ProductDependencyTreeItem(AZStd::make_shared<ProductDependencyTreeItemData>(productItemData->m_name, ""));
  178. m_root.reset(productDependencies);
  179. m_trackedProductIds.clear();
  180. createIndex(0, 0, productDependencies);
  181. m_trackedProductIds.insert(productItemData->m_databaseInfo.m_productID);
  182. switch (m_treeType)
  183. {
  184. case DependencyTreeType::Outgoing:
  185. PopulateOutgoingProductDependencies(productDependencies, productItemData->m_databaseInfo.m_productID, {});
  186. break;
  187. case DependencyTreeType::Incoming:
  188. PopulateIncomingProductDependencies(productDependencies, productItemData->m_databaseInfo.m_productID, {});
  189. break;
  190. }
  191. endResetModel();
  192. }
  193. struct ProductDependencyChild
  194. {
  195. ProductDependencyChild(ProductDependencyTreeItem* treeItem, AZ::s64 productId)
  196. : m_treeItem(treeItem)
  197. , m_productId(productId)
  198. {
  199. }
  200. ProductDependencyTreeItem* m_treeItem;
  201. AZ::s64 m_productId;
  202. };
  203. void ProductDependencyTreeModel::PopulateIncomingProductDependencies(ProductDependencyTreeItem* parent, AZ::s64 parentProductId, QSet<AZ::s64> visitedDependencies)
  204. {
  205. AZ::Data::AssetId assetId;
  206. m_sharedDbConnection->QueryProductByProductID(
  207. parentProductId,
  208. [&](AzToolsFramework::AssetDatabase::ProductDatabaseEntry& productEntry)
  209. {
  210. assetId.m_subId = productEntry.m_subID;
  211. return true;
  212. });
  213. m_sharedDbConnection->QuerySourceByProductID(
  214. parentProductId,
  215. [&](AzToolsFramework::AssetDatabase::SourceDatabaseEntry& sourceEntry)
  216. {
  217. assetId.m_guid = sourceEntry.m_sourceGuid;
  218. return true;
  219. });
  220. AZStd::string platform;
  221. m_sharedDbConnection->QueryJobByProductID(
  222. parentProductId,
  223. [&](AzToolsFramework::AssetDatabase::JobDatabaseEntry& jobEntry)
  224. {
  225. platform = jobEntry.m_platform;
  226. return true;
  227. });
  228. AZStd::vector<ProductDependencyChild> pendingChildren;
  229. m_sharedDbConnection->QueryDirectReverseProductDependenciesBySourceGuidSubId(
  230. assetId.m_guid, assetId.m_subId,
  231. [&](AzToolsFramework::AssetDatabase::ProductDatabaseEntry& incomingDependency)
  232. {
  233. // Make sure we haven't already encountered this product before
  234. // If we have, that means there's a dependency loop, so just abort
  235. if (visitedDependencies.contains(incomingDependency.m_productID))
  236. {
  237. return true;
  238. }
  239. bool platformMatches = false;
  240. m_sharedDbConnection->QueryJobByJobID(
  241. incomingDependency.m_jobPK,
  242. [&](AzToolsFramework::AssetDatabase::JobDatabaseEntry& jobEntry)
  243. {
  244. if (platform.compare(jobEntry.m_platform) == 0)
  245. {
  246. platformMatches = true;
  247. }
  248. return true;
  249. });
  250. if (!platformMatches)
  251. {
  252. return true;
  253. }
  254. visitedDependencies.insert(incomingDependency.m_productID);
  255. AZ::IO::Path productNamePath(incomingDependency.m_productName, AZ::IO::PosixPathSeparator);
  256. const AZ::IO::PathView filename = productNamePath.Filename();
  257. AZStd::shared_ptr<ProductDependencyTreeItemData> productDepTreeItemData = AZStd::make_shared<ProductDependencyTreeItemData>(
  258. AZ::IO::FixedMaxPathString(filename.Native()).c_str(), incomingDependency.m_productName);
  259. ProductDependencyTreeItem* child = parent->CreateChild(productDepTreeItemData);
  260. pendingChildren.push_back(ProductDependencyChild(child, incomingDependency.m_productID));
  261. m_trackedProductIds.insert(incomingDependency.m_productID);
  262. return true;
  263. });
  264. for (auto& child : pendingChildren)
  265. {
  266. PopulateIncomingProductDependencies(child.m_treeItem, child.m_productId, visitedDependencies);
  267. }
  268. }
  269. void ProductDependencyTreeModel::PopulateOutgoingProductDependencies(ProductDependencyTreeItem* parent, AZ::s64 parentProductId, QSet<AZ::s64> visitedDependencies)
  270. {
  271. AZStd::string platform;
  272. m_sharedDbConnection->QueryJobByProductID(
  273. parentProductId,
  274. [&](AzToolsFramework::AssetDatabase::JobDatabaseEntry& jobEntry)
  275. {
  276. platform = jobEntry.m_platform;
  277. return true;
  278. });
  279. AZStd::vector<ProductDependencyChild> pendingChildren;
  280. m_sharedDbConnection->QueryProductDependencyByProductId(
  281. parentProductId,
  282. [&](AzToolsFramework::AssetDatabase::ProductDependencyDatabaseEntry& dependency)
  283. {
  284. if (dependency.m_dependencySourceGuid.IsNull())
  285. {
  286. return true;
  287. }
  288. m_sharedDbConnection->QueryProductBySourceGuidSubID(
  289. dependency.m_dependencySourceGuid, dependency.m_dependencySubID,
  290. [&](AzToolsFramework::AssetDatabase::ProductDatabaseEntry& product)
  291. {
  292. // Make sure we haven't already encountered this product before
  293. // If we have, that means there's a dependency loop, so just abort
  294. if (visitedDependencies.contains(product.m_productID))
  295. {
  296. return true;
  297. }
  298. bool platformMatches = false;
  299. m_sharedDbConnection->QueryJobByJobID(
  300. product.m_jobPK,
  301. [&](AzToolsFramework::AssetDatabase::JobDatabaseEntry& jobEntry)
  302. {
  303. if (platform.compare(jobEntry.m_platform) == 0)
  304. {
  305. platformMatches = true;
  306. }
  307. return true;
  308. });
  309. if (!platformMatches)
  310. {
  311. return true;
  312. }
  313. visitedDependencies.insert(product.m_productID);
  314. AZ::IO::Path productNamePath(product.m_productName, AZ::IO::PosixPathSeparator);
  315. const AZ::IO::PathView filename = productNamePath.Filename();
  316. AZStd::shared_ptr<ProductDependencyTreeItemData> productDepTreeItemData =
  317. AZStd::make_shared<ProductDependencyTreeItemData>(
  318. AZ::IO::FixedMaxPathString(filename.Native()).c_str(), product.m_productName);
  319. ProductDependencyTreeItem* child = parent->CreateChild(productDepTreeItemData);
  320. pendingChildren.push_back(ProductDependencyChild(child, product.m_productID));
  321. m_trackedProductIds.insert(product.m_productID);
  322. return true;
  323. });
  324. return true;
  325. });
  326. for (auto& child : pendingChildren)
  327. {
  328. PopulateOutgoingProductDependencies(child.m_treeItem, child.m_productId, visitedDependencies);
  329. }
  330. }
  331. } // namespace AssetProcessor