SceneGraphWidget.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527
  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 <SceneWidgets/ui_SceneGraphWidget.h>
  10. #include <AzCore/std/string/string.h>
  11. #include <AzCore/std/containers/stack.h>
  12. #include <AzToolsFramework/Debug/TraceContext.h>
  13. #include <SceneAPI/SceneCore/Containers/Scene.h>
  14. #include <SceneAPI/SceneCore/Containers/Views/PairIterator.h>
  15. #include <SceneAPI/SceneCore/Containers/Views/SceneGraphDownwardsIterator.h>
  16. #include <SceneAPI/SceneCore/DataTypes/IGraphObject.h>
  17. #include <SceneAPI/SceneCore/DataTypes/ManifestBase/ISceneNodeSelectionList.h>
  18. #include <SceneAPI/SceneCore/Events/GraphMetaInfoBus.h>
  19. #include <SceneAPI/SceneCore/Utilities/SceneGraphSelector.h>
  20. #include <SceneAPI/SceneUI/SceneWidgets/SceneGraphWidget.h>
  21. namespace AZ
  22. {
  23. namespace SceneAPI
  24. {
  25. namespace UI
  26. {
  27. AZ_CLASS_ALLOCATOR_IMPL(SceneGraphWidget, SystemAllocator);
  28. SceneGraphWidget::SceneGraphWidget(const Containers::Scene& scene, QWidget* parent)
  29. : QWidget(parent)
  30. , ui(new Ui::SceneGraphWidget())
  31. , m_treeModel(new QStandardItemModel())
  32. , m_scene(scene)
  33. , m_targetList(nullptr)
  34. , m_selectedCount(0)
  35. , m_totalCount(0)
  36. , m_endPointOption(EndPointOption::AlwaysShow)
  37. , m_checkableOption(CheckableOption::NoneCheckable)
  38. {
  39. SetupUI();
  40. }
  41. SceneGraphWidget::SceneGraphWidget(const Containers::Scene& scene, const DataTypes::ISceneNodeSelectionList& targetList,
  42. QWidget* parent)
  43. : QWidget(parent)
  44. , ui(new Ui::SceneGraphWidget())
  45. , m_treeModel(new QStandardItemModel())
  46. , m_scene(scene)
  47. , m_targetList(targetList.Copy())
  48. , m_selectedCount(0)
  49. , m_totalCount(0)
  50. , m_endPointOption(EndPointOption::OnlyShowFilterTypes)
  51. , m_checkableOption(CheckableOption::AllCheckable)
  52. {
  53. SetupUI();
  54. }
  55. SceneGraphWidget::~SceneGraphWidget() = default;
  56. AZStd::unique_ptr<DataTypes::ISceneNodeSelectionList>&& SceneGraphWidget::ClaimTargetList()
  57. {
  58. return AZStd::move(m_targetList);
  59. }
  60. void SceneGraphWidget::IncludeEndPoints(EndPointOption option)
  61. {
  62. m_endPointOption = option;
  63. }
  64. void SceneGraphWidget::MakeCheckable(CheckableOption option)
  65. {
  66. m_checkableOption = option;
  67. }
  68. void SceneGraphWidget::AddFilterType(const Uuid& id)
  69. {
  70. if (m_filterTypes.find(id) == m_filterTypes.end())
  71. {
  72. m_filterTypes.insert(id);
  73. }
  74. }
  75. void SceneGraphWidget::AddVirtualFilterType(Crc32 name)
  76. {
  77. if (m_filterVirtualTypes.find(name) == m_filterVirtualTypes.end())
  78. {
  79. m_filterVirtualTypes.insert(name);
  80. }
  81. }
  82. void SceneGraphWidget::SetupUI()
  83. {
  84. ui->setupUi(this);
  85. ui->m_selectionTree->setHeaderHidden(true);
  86. ui->m_selectionTree->setModel(m_treeModel.data());
  87. connect(ui->m_selectAllCheckBox, &QCheckBox::stateChanged, this, &SceneGraphWidget::OnSelectAllCheckboxStateChanged);
  88. connect(m_treeModel.data(), &QStandardItemModel::itemChanged, this, &SceneGraphWidget::OnTreeItemStateChanged);
  89. connect(ui->m_selectionTree->selectionModel(), &QItemSelectionModel::currentChanged, this, &SceneGraphWidget::OnTreeItemChanged);
  90. }
  91. void SceneGraphWidget::Build()
  92. {
  93. setUpdatesEnabled(false);
  94. QSignalBlocker blocker(m_treeModel.data());
  95. const Containers::SceneGraph& graph = m_scene.GetGraph();
  96. m_selectedCount = 0;
  97. m_totalCount = 0;
  98. m_treeModel->clear();
  99. m_treeItems.clear();
  100. m_treeItems = AZStd::vector<QStandardItem*>(graph.GetNodeCount(), nullptr);
  101. if (m_checkableOption == CheckableOption::NoneCheckable)
  102. {
  103. ui->m_selectAllCheckBox->hide();
  104. }
  105. else
  106. {
  107. ui->m_selectAllCheckBox->show();
  108. }
  109. auto sceneGraphView = Containers::Views::MakePairView(graph.GetNameStorage(), graph.GetContentStorage());
  110. auto sceneGraphDownardsIteratorView = Containers::Views::MakeSceneGraphDownwardsView<Containers::Views::BreadthFirst>(
  111. graph, graph.GetRoot(), sceneGraphView.begin(), true);
  112. // Some importer implementations may write an empty node to force collection all items under a common root
  113. // If that is the case, we're going to skip it so we don't show the user an empty node root
  114. auto iterator = sceneGraphDownardsIteratorView.begin();
  115. if (iterator->first.GetPathLength() == 0 && !iterator->second)
  116. {
  117. ++iterator;
  118. }
  119. for (; iterator != sceneGraphDownardsIteratorView.end(); ++iterator)
  120. {
  121. Containers::SceneGraph::HierarchyStorageConstIterator hierarchy = iterator.GetHierarchyIterator();
  122. Containers::SceneGraph::NodeIndex currentIndex = graph.ConvertToNodeIndex(hierarchy);
  123. AZ_Assert(currentIndex.IsValid(), "While iterating through the Scene Graph an unexpected invalid entry was found.");
  124. AZStd::shared_ptr<const DataTypes::IGraphObject> currentItem = iterator->second;
  125. if (hierarchy->IsEndPoint())
  126. {
  127. switch (m_endPointOption)
  128. {
  129. case EndPointOption::AlwaysShow:
  130. break;
  131. case EndPointOption::NeverShow:
  132. continue;
  133. case EndPointOption::OnlyShowFilterTypes:
  134. if (IsFilteredType(currentItem, currentIndex))
  135. {
  136. break;
  137. }
  138. else
  139. {
  140. continue;
  141. }
  142. default:
  143. AZ_Assert(false, "Unsupported type %i for end point option.", m_endPointOption);
  144. break;
  145. }
  146. }
  147. bool isCheckable = false;
  148. switch (m_checkableOption)
  149. {
  150. case CheckableOption::AllCheckable:
  151. isCheckable = true;
  152. break;
  153. case CheckableOption::NoneCheckable:
  154. isCheckable = false;
  155. break;
  156. case CheckableOption::OnlyFilterTypesCheckable:
  157. isCheckable = IsFilteredType(currentItem, currentIndex);
  158. break;
  159. default:
  160. AZ_Assert(false, "Unsupported type %i for checkable option.", m_checkableOption);
  161. isCheckable = false;
  162. break;
  163. }
  164. QStandardItem* treeItem = BuildTreeItem(currentItem, iterator->first, isCheckable, hierarchy->IsEndPoint());
  165. if (isCheckable)
  166. {
  167. if (IsSelected(iterator->first, false))
  168. {
  169. treeItem->setCheckState(Qt::CheckState::Checked);
  170. m_selectedCount++;
  171. }
  172. m_totalCount++;
  173. }
  174. m_treeItems[currentIndex.AsNumber()] = treeItem;
  175. Containers::SceneGraph::NodeIndex parentIndex = graph.GetNodeParent(currentIndex);
  176. if (parentIndex.IsValid() && m_treeItems[parentIndex.AsNumber()])
  177. {
  178. m_treeItems[parentIndex.AsNumber()]->appendRow(treeItem);
  179. }
  180. else
  181. {
  182. m_treeModel->appendRow(treeItem);
  183. }
  184. }
  185. ui->m_selectionTree->expandAll();
  186. UpdateSelectAllStatus();
  187. setUpdatesEnabled(true);
  188. }
  189. bool SceneGraphWidget::IsFilteredType(const AZStd::shared_ptr<const DataTypes::IGraphObject>& object,
  190. Containers::SceneGraph::NodeIndex index) const
  191. {
  192. if (!object)
  193. {
  194. return false;
  195. }
  196. for (const Uuid& id : m_filterTypes)
  197. {
  198. if (object->RTTI_IsTypeOf(id))
  199. {
  200. return true;
  201. }
  202. }
  203. if (!m_filterVirtualTypes.empty())
  204. {
  205. Events::GraphMetaInfo::VirtualTypesSet virtualTypes;
  206. EBUS_EVENT(Events::GraphMetaInfoBus, GetVirtualTypes, virtualTypes, m_scene, index);
  207. for (Crc32 name : virtualTypes)
  208. {
  209. if (m_filterVirtualTypes.find(name) != m_filterVirtualTypes.end())
  210. {
  211. return true;
  212. }
  213. }
  214. }
  215. return false;
  216. }
  217. QStandardItem* SceneGraphWidget::BuildTreeItem(const AZStd::shared_ptr<const DataTypes::IGraphObject>& object,
  218. const Containers::SceneGraph::Name& name, bool isCheckable, [[maybe_unused]] bool isEndPoint) const
  219. {
  220. QStandardItem* treeItem = new QStandardItem(name.GetName());
  221. treeItem->setData(QString(name.GetPath()));
  222. treeItem->setEditable(false);
  223. treeItem->setCheckable(isCheckable);
  224. if (object)
  225. {
  226. AZStd::string toolTip;
  227. EBUS_EVENT(Events::GraphMetaInfoBus, GetToolTip, toolTip, object.get());
  228. if (toolTip.empty())
  229. {
  230. treeItem->setToolTip(QString::asprintf("%s\n<%s>", name.GetPath(), object->RTTI_GetTypeName()));
  231. }
  232. else
  233. {
  234. treeItem->setToolTip(QString::asprintf("%s\n\n%s", name.GetPath(), toolTip.c_str()));
  235. }
  236. AZStd::string iconPath;
  237. EBUS_EVENT(Events::GraphMetaInfoBus, GetIconPath, iconPath, object.get());
  238. if (!iconPath.empty())
  239. {
  240. treeItem->setIcon(QIcon(iconPath.c_str()));
  241. }
  242. }
  243. return treeItem;
  244. }
  245. void SceneGraphWidget::OnSelectAllCheckboxStateChanged()
  246. {
  247. setUpdatesEnabled(false);
  248. QSignalBlocker blocker(m_treeModel.data());
  249. Qt::CheckState state = ui->m_selectAllCheckBox->checkState();
  250. if (m_targetList)
  251. {
  252. m_targetList->ClearSelectedNodes();
  253. m_targetList->ClearUnselectedNodes();
  254. for (QStandardItem* item : m_treeItems)
  255. {
  256. if (!item || !item->isCheckable())
  257. {
  258. continue;
  259. }
  260. item->setCheckState(state);
  261. QVariant itemData = item->data();
  262. if (itemData.isValid())
  263. {
  264. AZStd::string fullName = itemData.toString().toUtf8().data();
  265. if (state == Qt::CheckState::Unchecked)
  266. {
  267. m_targetList->RemoveSelectedNode(fullName);
  268. }
  269. else
  270. {
  271. m_targetList->AddSelectedNode(AZStd::move(fullName));
  272. }
  273. }
  274. }
  275. }
  276. else
  277. {
  278. for (QStandardItem* item : m_treeItems)
  279. {
  280. if (item && item->isCheckable())
  281. {
  282. item->setCheckState(state);
  283. }
  284. }
  285. }
  286. m_selectedCount = (state == Qt::CheckState::Unchecked) ? 0 : m_totalCount;
  287. UpdateSelectAllStatus();
  288. setUpdatesEnabled(true);
  289. }
  290. void SceneGraphWidget::OnTreeItemStateChanged(QStandardItem* item)
  291. {
  292. setUpdatesEnabled(false);
  293. QSignalBlocker blocker(m_treeModel.data());
  294. Qt::CheckState state = item->checkState();
  295. bool decrement = (state == Qt::CheckState::Unchecked);
  296. if (decrement)
  297. {
  298. if (!RemoveSelection(item))
  299. {
  300. item->setCheckState(Qt::CheckState::Checked);
  301. return;
  302. }
  303. }
  304. else
  305. {
  306. if (!AddSelection(item))
  307. {
  308. item->setCheckState(Qt::CheckState::Unchecked);
  309. return;
  310. }
  311. }
  312. AZStd::stack<QStandardItem*> children;
  313. int rowCount = item->rowCount();
  314. for (int index = 0; index < rowCount; ++index)
  315. {
  316. children.push(item->child(index));
  317. }
  318. while (!children.empty())
  319. {
  320. QStandardItem* current = children.top();
  321. children.pop();
  322. if (decrement)
  323. {
  324. if (current->checkState() != Qt::CheckState::Unchecked && RemoveSelection(current))
  325. {
  326. current->setCheckState(state);
  327. }
  328. }
  329. else
  330. {
  331. if (current->checkState() == Qt::CheckState::Unchecked && AddSelection(current))
  332. {
  333. current->setCheckState(state);
  334. }
  335. }
  336. int rowCount2 = current->rowCount();
  337. for (int index = 0; index < rowCount2; ++index)
  338. {
  339. children.push(current->child(index));
  340. }
  341. }
  342. UpdateSelectAllStatus();
  343. setUpdatesEnabled(true);
  344. }
  345. void SceneGraphWidget::OnTreeItemChanged(const QModelIndex& current, const QModelIndex& /*previous*/)
  346. {
  347. QStandardItem* item = m_treeModel->itemFromIndex(current);
  348. QVariant itemData = item->data();
  349. if (!itemData.isValid() || itemData.type() != QVariant::Type::String)
  350. {
  351. return;
  352. }
  353. AZStd::string fullName = itemData.toString().toUtf8().data();
  354. AZ_TraceContext("Selected item", fullName);
  355. Containers::SceneGraph::NodeIndex nodeIndex = m_scene.GetGraph().Find(fullName);
  356. AZ_Assert(nodeIndex.IsValid(), "Invalid node added to tree.");
  357. if (!nodeIndex.IsValid())
  358. {
  359. return;
  360. }
  361. Q_EMIT SelectionChanged(m_scene.GetGraph().GetNodeContent(nodeIndex));
  362. }
  363. void SceneGraphWidget::UpdateSelectAllStatus()
  364. {
  365. QSignalBlocker blocker(ui->m_selectAllCheckBox);
  366. if (m_selectedCount == m_totalCount)
  367. {
  368. ui->m_selectAllCheckBox->setText("Unselect all");
  369. ui->m_selectAllCheckBox->setCheckState(Qt::CheckState::Checked);
  370. }
  371. else
  372. {
  373. ui->m_selectAllCheckBox->setText("Select all");
  374. ui->m_selectAllCheckBox->setCheckState(Qt::CheckState::Unchecked);
  375. }
  376. }
  377. bool SceneGraphWidget::IsSelected(const Containers::SceneGraph::Name& name, bool updateNodeSelection) const
  378. {
  379. if (!m_targetList)
  380. {
  381. return false;
  382. }
  383. if (updateNodeSelection)
  384. {
  385. // Use a temp list to get a valid state of the UI here based on selected/unselected nodes
  386. // We use the temp list so that the real list actually keeps track of the user's selection
  387. // Since UpdateNodeSelection will modify selected/unselected node lists for us.
  388. AZStd::unique_ptr<DataTypes::ISceneNodeSelectionList> tempList(m_targetList->Copy());
  389. Utilities::SceneGraphSelector::UpdateNodeSelection(m_scene.GetGraph(), *tempList);
  390. return IsSelectedInSelectionList(name, *tempList);
  391. }
  392. else
  393. {
  394. return IsSelectedInSelectionList(name, *m_targetList);
  395. }
  396. }
  397. bool SceneGraphWidget::IsSelectedInSelectionList(const Containers::SceneGraph::Name& name, const DataTypes::ISceneNodeSelectionList& targetList) const
  398. {
  399. return targetList.IsSelectedNode(name.GetPath());
  400. }
  401. bool SceneGraphWidget::AddSelection(const QStandardItem* item)
  402. {
  403. if (!m_targetList)
  404. {
  405. return true;
  406. }
  407. QVariant itemData = item->data();
  408. if (!itemData.isValid() || itemData.type() != QVariant::Type::String)
  409. {
  410. return false;
  411. }
  412. AZStd::string fullName = itemData.toString().toUtf8().data();
  413. AZ_TraceContext("Item for addition", fullName);
  414. Containers::SceneGraph::NodeIndex nodeIndex = m_scene.GetGraph().Find(fullName);
  415. AZ_Assert(nodeIndex.IsValid(), "Invalid node added to tree.");
  416. if (!nodeIndex.IsValid())
  417. {
  418. return false;
  419. }
  420. m_targetList->AddSelectedNode(fullName);
  421. m_selectedCount++;
  422. AZ_Assert(m_selectedCount <= m_totalCount, "Selected node count exceeds available node count.");
  423. return true;
  424. }
  425. bool SceneGraphWidget::RemoveSelection(const QStandardItem* item)
  426. {
  427. if (!m_targetList)
  428. {
  429. return true;
  430. }
  431. QVariant itemData = item->data();
  432. if (!itemData.isValid() || itemData.type() != QVariant::Type::String)
  433. {
  434. return false;
  435. }
  436. AZStd::string fullName = itemData.toString().toUtf8().data();
  437. AZ_TraceContext("Item for removal", fullName);
  438. Containers::SceneGraph::NodeIndex nodeIndex = m_scene.GetGraph().Find(fullName);
  439. AZ_Assert(nodeIndex.IsValid(), "Invalid node removed from tree.");
  440. if (!nodeIndex.IsValid())
  441. {
  442. return false;
  443. }
  444. m_targetList->RemoveSelectedNode(fullName);
  445. AZ_Assert(m_selectedCount > 0, "Selected node count can not be decremented below zero.");
  446. m_selectedCount--;
  447. return true;
  448. }
  449. QCheckBox* SceneGraphWidget::GetQCheckBox()
  450. {
  451. return ui->m_selectAllCheckBox;
  452. }
  453. QTreeView* SceneGraphWidget::GetQTreeView()
  454. {
  455. return ui->m_selectionTree;
  456. }
  457. } // namespace UI
  458. } // namespace SceneAPI
  459. } // namespace AZ
  460. #include <SceneWidgets/moc_SceneGraphWidget.cpp>