NodeTreeSelectionWidget.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  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 <QLabel>
  9. #include <QToolButton>
  10. #include <AzCore/std/bind/bind.h>
  11. #include <RowWidgets/ui_NodeTreeSelectionWidget.h>
  12. #include <SceneAPI/SceneCore/Containers/Scene.h>
  13. #include <SceneAPI/SceneCore/DataTypes/IGraphObject.h>
  14. #include <SceneAPI/SceneCore/DataTypes/ManifestBase/ISceneNodeSelectionList.h>
  15. #include <SceneAPI/SceneCore/Events/GraphMetaInfoBus.h>
  16. #include <SceneAPI/SceneCore/Utilities/SceneGraphSelector.h>
  17. #include <SceneAPI/SceneUI/RowWidgets/NodeTreeSelectionWidget.h>
  18. #include <SceneAPI/SceneUI/SceneWidgets/ManifestWidget.h>
  19. #include <SceneAPI/SceneUI/CommonWidgets/OverlayWidget.h>
  20. namespace AZ
  21. {
  22. namespace SceneAPI
  23. {
  24. namespace UI
  25. {
  26. AZ_CLASS_ALLOCATOR_IMPL(NodeTreeSelectionWidget, SystemAllocator);
  27. NodeTreeSelectionWidget::NodeTreeSelectionWidget(QWidget* parent)
  28. : QWidget(parent)
  29. , ui(new Ui::NodeTreeSelectionWidget())
  30. , m_narrowSelection(false)
  31. , m_filterName("nodes")
  32. {
  33. ui->setupUi(this);
  34. ui->m_selectButton->setIcon(QIcon(":/SceneUI/Manifest/TreeIcon.png"));
  35. connect(ui->m_selectButton, &QToolButton::clicked, this, &NodeTreeSelectionWidget::SelectButtonClicked);
  36. }
  37. NodeTreeSelectionWidget::~NodeTreeSelectionWidget() = default;
  38. void NodeTreeSelectionWidget::SetList(const DataTypes::ISceneNodeSelectionList& list)
  39. {
  40. m_list = list.Copy();
  41. }
  42. void NodeTreeSelectionWidget::CopyListTo(DataTypes::ISceneNodeSelectionList& target)
  43. {
  44. if (m_list)
  45. {
  46. m_list->CopyTo(target);
  47. }
  48. }
  49. void NodeTreeSelectionWidget::SetFilterName(const AZStd::string& name)
  50. {
  51. ui->m_selectButton->setToolTip(QString::asprintf("Select %s", name.c_str()));
  52. m_filterName = name;
  53. }
  54. void NodeTreeSelectionWidget::SetFilterName(AZStd::string&& name)
  55. {
  56. ui->m_selectButton->setToolTip(QString::asprintf("Select %s", name.c_str()));
  57. m_filterName = AZStd::move(name);
  58. }
  59. void NodeTreeSelectionWidget::AddFilterType(const Uuid& idProperty)
  60. {
  61. if (m_filterTypes.find(idProperty) == m_filterTypes.end())
  62. {
  63. m_filterTypes.insert(idProperty);
  64. }
  65. }
  66. void NodeTreeSelectionWidget::AddFilterVirtualType(Crc32 name)
  67. {
  68. if (m_filterVirtualTypes.find(name) == m_filterVirtualTypes.end())
  69. {
  70. m_filterVirtualTypes.insert(name);
  71. }
  72. }
  73. void NodeTreeSelectionWidget::UseNarrowSelection(bool enable)
  74. {
  75. m_narrowSelection = enable;
  76. }
  77. void NodeTreeSelectionWidget::SelectButtonClicked()
  78. {
  79. AZ_Assert(!m_treeWidget, "Node tree already active, NodeTreeSelectionWidget button pressed multiple times.");
  80. AZ_Assert(m_list, "Requested updating of selection list before it was set.");
  81. ManifestWidget* root = ManifestWidget::FindRoot(this);
  82. AZ_Assert(root, "NodeTreeSelectionWidget is not a child of a ManifestWidget.");
  83. if (!m_list || !root)
  84. {
  85. return;
  86. }
  87. AZStd::shared_ptr<Containers::Scene> scene = root->GetScene();
  88. if (!scene)
  89. {
  90. return;
  91. }
  92. OverlayWidgetButtonList buttons;
  93. OverlayWidgetButton acceptButton;
  94. acceptButton.m_text = "Select";
  95. acceptButton.m_callback = AZStd::bind(&NodeTreeSelectionWidget::ListChangesAccepted, this);
  96. acceptButton.m_triggersPop = true;
  97. OverlayWidgetButton cancelButton;
  98. cancelButton.m_text = "Cancel";
  99. cancelButton.m_callback = AZStd::bind(&NodeTreeSelectionWidget::ListChangesCanceled, this);
  100. cancelButton.m_triggersPop = true;
  101. cancelButton.m_isCloseButton = true;
  102. buttons.push_back(&acceptButton);
  103. buttons.push_back(&cancelButton);
  104. ResetNewTreeWidget(*scene);
  105. for (const Uuid& filterType : m_filterTypes)
  106. {
  107. m_treeWidget->AddFilterType(filterType);
  108. }
  109. for (Crc32 virtualTypeName : m_filterVirtualTypes)
  110. {
  111. m_treeWidget->AddVirtualFilterType(virtualTypeName);
  112. }
  113. if (m_narrowSelection)
  114. {
  115. m_treeWidget->MakeCheckable(SceneGraphWidget::CheckableOption::OnlyFilterTypesCheckable);
  116. }
  117. m_treeWidget->Build();
  118. QLabel* label = new QLabel("Finish selecting nodes to continue editing settings.");
  119. label->setAlignment(Qt::AlignCenter);
  120. OverlayWidget::PushLayerToContainingOverlay(this, label, m_treeWidget.get(), "Select nodes", buttons);
  121. }
  122. void NodeTreeSelectionWidget::ResetNewTreeWidget(const Containers::Scene& scene)
  123. {
  124. m_treeWidget.reset(aznew SceneGraphWidget(scene, *m_list));
  125. }
  126. void NodeTreeSelectionWidget::ListChangesAccepted()
  127. {
  128. m_list = m_treeWidget->ClaimTargetList();
  129. m_treeWidget.reset();
  130. emit valueChanged();
  131. }
  132. void NodeTreeSelectionWidget::ListChangesCanceled()
  133. {
  134. m_treeWidget.reset();
  135. }
  136. void NodeTreeSelectionWidget::UpdateSelectionLabel()
  137. {
  138. if (m_list)
  139. {
  140. size_t selected = CalculateSelectedCount();
  141. size_t total = CalculateTotalCount();
  142. AZ_Assert(selected <= total, "Selected count of nodes should not be greater than the total count");
  143. if (total == 0)
  144. {
  145. ui->m_statusLabel->setText("Default selection");
  146. }
  147. else if (selected == total)
  148. {
  149. ui->m_statusLabel->setText(QString::asprintf("All %s selected", m_filterName.c_str()));
  150. }
  151. else
  152. {
  153. ui->m_statusLabel->setText(
  154. QString("%1 of %2 %3 selected").arg(selected).arg(total).arg(m_filterName.c_str()));
  155. }
  156. }
  157. else
  158. {
  159. ui->m_statusLabel->setText("No list assigned");
  160. }
  161. }
  162. size_t NodeTreeSelectionWidget::CalculateSelectedCount()
  163. {
  164. if (!m_list)
  165. {
  166. return 0;
  167. }
  168. const ManifestWidget* root = ManifestWidget::FindRoot(this);
  169. AZ_Assert(root, "NodeTreeSelectionWidget is not a child of a ManifestWidget.");
  170. if (!m_list || !root)
  171. {
  172. return 0;
  173. }
  174. const Containers::SceneGraph& graph = root->GetScene()->GetGraph();
  175. size_t result = 0;
  176. AZStd::unique_ptr<DataTypes::ISceneNodeSelectionList> tempList(m_list->Copy());
  177. Utilities::SceneGraphSelector::UpdateNodeSelection(graph, *tempList);
  178. tempList->EnumerateSelectedNodes(
  179. [&](const AZStd::string& nodeName)
  180. {
  181. Containers::SceneGraph::NodeIndex index = graph.Find(nodeName);
  182. if (!index.IsValid())
  183. {
  184. return true;
  185. }
  186. AZStd::shared_ptr<const DataTypes::IGraphObject> object = graph.GetNodeContent(index);
  187. if (!object)
  188. {
  189. return true;
  190. }
  191. if (m_filterTypes.empty() && m_filterVirtualTypes.empty())
  192. {
  193. result++;
  194. return true;
  195. }
  196. bool foundType = false;
  197. for (const Uuid& type : m_filterTypes)
  198. {
  199. if (object->RTTI_IsTypeOf(type))
  200. {
  201. result++;
  202. foundType = true;
  203. break;
  204. }
  205. }
  206. if (foundType)
  207. {
  208. return true;
  209. }
  210. // Check if the object is one of the registered virtual types.
  211. Events::GraphMetaInfo::VirtualTypesSet virtualTypes;
  212. Events::GraphMetaInfoBus::Broadcast(
  213. &Events::GraphMetaInfo::GetVirtualTypes, virtualTypes, *root->GetScene(), index);
  214. for (Crc32 name : virtualTypes)
  215. {
  216. if (m_filterVirtualTypes.contains(name))
  217. {
  218. result++;
  219. break;
  220. }
  221. }
  222. return true;
  223. });
  224. return result;
  225. }
  226. size_t NodeTreeSelectionWidget::CalculateTotalCount()
  227. {
  228. const ManifestWidget* root = ManifestWidget::FindRoot(this);
  229. AZ_Assert(root, "NodeTreeSelectionWidget is not a child of a ManifestWidget.");
  230. if (!m_list || !root)
  231. {
  232. return 0;
  233. }
  234. const Containers::SceneGraph& graph = root->GetScene()->GetGraph();
  235. size_t total = 0;
  236. if (m_filterTypes.empty() && m_filterVirtualTypes.empty())
  237. {
  238. Containers::SceneGraph::HierarchyStorageConstData view = graph.GetHierarchyStorage();
  239. if (!graph.GetNodeContent(graph.GetRoot()) && graph.GetNodeName(graph.GetRoot()).GetPathLength() == 0)
  240. {
  241. view = Containers::SceneGraph::HierarchyStorageConstData(view.begin() + 1, view.end());
  242. }
  243. for (auto& it : view)
  244. {
  245. if (!it.IsEndPoint())
  246. {
  247. total++;
  248. }
  249. }
  250. }
  251. else
  252. {
  253. for (auto it = graph.GetContentStorage().begin(); it != graph.GetContentStorage().end(); ++it)
  254. {
  255. if (!(*it))
  256. {
  257. continue;
  258. }
  259. Containers::SceneGraph::NodeIndex index = graph.ConvertToNodeIndex(it);
  260. bool foundType = false;
  261. for (const Uuid& type : m_filterTypes)
  262. {
  263. if ((*it)->RTTI_IsTypeOf(type))
  264. {
  265. total++;
  266. foundType = true;
  267. break;
  268. }
  269. }
  270. if (foundType)
  271. {
  272. continue;
  273. }
  274. // Check if the object is one of the registered virtual types.
  275. Events::GraphMetaInfo::VirtualTypesSet virtualTypes;
  276. Events::GraphMetaInfoBus::Broadcast(&Events::GraphMetaInfo::GetVirtualTypes, virtualTypes, *root->GetScene(), index);
  277. for (Crc32 name : virtualTypes)
  278. {
  279. if (m_filterVirtualTypes.find(name) != m_filterVirtualTypes.end())
  280. {
  281. total++;
  282. break;
  283. }
  284. }
  285. }
  286. }
  287. return total;
  288. }
  289. } // namespace UI
  290. } // namespace SceneAPI
  291. } // namespace AZ
  292. #include <RowWidgets/moc_NodeTreeSelectionWidget.cpp>