LODSceneGraphWidget.cpp 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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 <QStandardItemModel>
  9. #include <QtWidgets/QCheckBox>
  10. #include <QtWidgets/QTreeView>
  11. #include <SceneAPI/SceneCore/Containers/Scene.h>
  12. #include <SceneAPI/SceneCore/Containers/Views/PairIterator.h>
  13. #include <SceneAPI/SceneCore/Containers/Views/SceneGraphDownwardsIterator.h>
  14. #include <SceneAPI/SceneCore/DataTypes/IGraphObject.h>
  15. #include <SceneAPI/SceneCore/Containers/Scene.h>
  16. #include <SceneAPI/SceneCore/DataTypes/IGraphObject.h>
  17. #include <Editor/PropertyWidgets/LODSceneGraphWidget.h>
  18. #include <Editor/PropertyWidgets/PropertyWidgetAllocator.h>
  19. #include <SceneAPIExt/Utilities/LODSelector.h>
  20. namespace EMotionFX
  21. {
  22. namespace Pipeline
  23. {
  24. namespace UI
  25. {
  26. AZ_CLASS_ALLOCATOR_IMPL(LODSceneGraphWidget, EMotionFX::PropertyWidgetAllocator)
  27. LODSceneGraphWidget::LODSceneGraphWidget(const SceneContainers::Scene& scene, const SceneDataTypes::ISceneNodeSelectionList& targetList,
  28. QWidget* parent)
  29. : SceneGraphWidget(scene, targetList, parent)
  30. , m_hideUncheckableItem(false)
  31. {
  32. }
  33. void LODSceneGraphWidget::HideUncheckableItem(bool hide)
  34. {
  35. m_hideUncheckableItem = hide;
  36. }
  37. void LODSceneGraphWidget::Build()
  38. {
  39. setUpdatesEnabled(false);
  40. QSignalBlocker blocker(m_treeModel.data());
  41. const SceneContainers::SceneGraph& graph = m_scene.GetGraph();
  42. m_selectedCount = 0;
  43. m_totalCount = 0;
  44. m_treeModel->clear();
  45. m_treeItems.clear();
  46. m_treeItems = AZStd::vector<QStandardItem*>(graph.GetNodeCount(), nullptr);
  47. if (m_checkableOption == CheckableOption::NoneCheckable)
  48. {
  49. GetQCheckBox()->hide();
  50. }
  51. else
  52. {
  53. GetQCheckBox()->show();
  54. }
  55. auto sceneGraphView = SceneContainers::Views::MakePairView(graph.GetNameStorage(), graph.GetContentStorage());
  56. auto sceneGraphDownwardsIteratorView = SceneContainers::Views::MakeSceneGraphDownwardsView<SceneContainers::Views::BreadthFirst>(
  57. graph, graph.GetRoot(), sceneGraphView.begin(), true);
  58. // Some importer implementations may write an empty node to force collection all items under a common root
  59. // If that is the case, we're going to skip it so we don't show the user an empty node root
  60. auto startIterator = sceneGraphDownwardsIteratorView.begin();
  61. if (startIterator->first.GetPathLength() == 0 && !startIterator->second)
  62. {
  63. ++startIterator;
  64. }
  65. // 1. First find all the items to add to this widget.
  66. AZStd::unordered_map<SceneContainers::SceneGraph::NodeIndex::IndexType, bool> itemsToAdd;
  67. for (auto iterator = startIterator; iterator != sceneGraphDownwardsIteratorView.end(); ++iterator)
  68. {
  69. SceneContainers::SceneGraph::HierarchyStorageConstIterator hierarchy = iterator.GetHierarchyIterator();
  70. SceneContainers::SceneGraph::NodeIndex currentIndex = graph.ConvertToNodeIndex(hierarchy);
  71. AZ_Assert(currentIndex.IsValid(), "While iterating through the Scene Graph an unexpected invalid entry was found.");
  72. AZStd::shared_ptr<const SceneDataTypes::IGraphObject> currentItem = iterator->second;
  73. if (hierarchy->IsEndPoint())
  74. {
  75. switch (m_endPointOption)
  76. {
  77. case EndPointOption::AlwaysShow:
  78. break;
  79. case EndPointOption::NeverShow:
  80. continue;
  81. case EndPointOption::OnlyShowFilterTypes:
  82. if (IsFilteredType(currentItem, currentIndex))
  83. {
  84. break;
  85. }
  86. else
  87. {
  88. continue;
  89. }
  90. default:
  91. AZ_Assert(false, "Unsupported type %i for end point option.", m_endPointOption);
  92. break;
  93. }
  94. }
  95. bool isCheckable = false;
  96. switch (m_checkableOption)
  97. {
  98. case CheckableOption::AllCheckable:
  99. isCheckable = true;
  100. break;
  101. case CheckableOption::NoneCheckable:
  102. isCheckable = false;
  103. break;
  104. case CheckableOption::OnlyFilterTypesCheckable:
  105. isCheckable = IsFilteredType(currentItem, currentIndex);
  106. break;
  107. default:
  108. AZ_Assert(false, "Unsupported type %i for checkable option.", m_checkableOption);
  109. isCheckable = false;
  110. break;
  111. }
  112. // Add an option to skip the uncheckable item in tree widget.
  113. if (m_hideUncheckableItem && !isCheckable)
  114. {
  115. continue;
  116. }
  117. itemsToAdd.emplace(currentIndex.AsNumber(), isCheckable);
  118. // We want to add all parent item to this item as well.
  119. SceneContainers::SceneGraph::NodeIndex parentIndex = graph.GetNodeParent(currentIndex);
  120. while (parentIndex.IsValid())
  121. {
  122. if (itemsToAdd.contains(parentIndex.AsNumber()))
  123. {
  124. break;
  125. }
  126. itemsToAdd.emplace(parentIndex.AsNumber(), true);
  127. parentIndex = graph.GetNodeParent(parentIndex);
  128. }
  129. }
  130. // 2. Add all the items following the scene graph order.
  131. for (auto iterator = startIterator; iterator != sceneGraphDownwardsIteratorView.end(); ++iterator)
  132. {
  133. SceneContainers::SceneGraph::HierarchyStorageConstIterator hierarchy = iterator.GetHierarchyIterator();
  134. SceneContainers::SceneGraph::NodeIndex currentIndex = graph.ConvertToNodeIndex(hierarchy);
  135. if (!itemsToAdd.contains(currentIndex.AsNumber()))
  136. {
  137. continue;
  138. }
  139. AZStd::shared_ptr<const SceneDataTypes::IGraphObject> currentItem = iterator->second;
  140. const bool isCheckable = itemsToAdd[currentIndex.AsNumber()];
  141. QStandardItem* treeItem = BuildTreeItem(currentItem, iterator->first, isCheckable, hierarchy->IsEndPoint());
  142. if (isCheckable)
  143. {
  144. if (IsSelected(iterator->first))
  145. {
  146. treeItem->setCheckState(Qt::CheckState::Checked);
  147. m_selectedCount++;
  148. }
  149. m_totalCount++;
  150. }
  151. m_treeItems[currentIndex.AsNumber()] = treeItem;
  152. SceneContainers::SceneGraph::NodeIndex parentIndex = graph.GetNodeParent(currentIndex);
  153. if (parentIndex.IsValid() && m_treeItems[parentIndex.AsNumber()])
  154. {
  155. m_treeItems[parentIndex.AsNumber()]->appendRow(treeItem);
  156. }
  157. else
  158. {
  159. m_treeModel->appendRow(treeItem);
  160. }
  161. }
  162. GetQTreeView()->expandAll();
  163. UpdateSelectAllStatus();
  164. setUpdatesEnabled(true);
  165. }
  166. }
  167. }
  168. }