CommandHierarchyItemCreate.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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 "EditorCommon.h"
  9. #define UICANVASEDITOR_ELEMENT_NAME_DEFAULT "Element"
  10. CommandHierarchyItemCreate::CommandHierarchyItemCreate(UndoStack* stack,
  11. HierarchyWidget* hierarchy,
  12. const EntityHelpers::EntityIdList& parents,
  13. int childIndex,
  14. PostCreationCallback postCreationCB)
  15. : QUndoCommand()
  16. , m_stack(stack)
  17. , m_hierarchy(hierarchy)
  18. , m_parents(parents)
  19. , m_childIndex(childIndex)
  20. , m_entries()
  21. , m_postCreationCB(postCreationCB)
  22. {
  23. setText(QString("create element") + (m_parents.empty() ? "" : "s"));
  24. }
  25. void CommandHierarchyItemCreate::undo()
  26. {
  27. UndoStackExecutionScope s(m_stack);
  28. HierarchyHelpers::Delete(m_hierarchy, m_entries);
  29. }
  30. void CommandHierarchyItemCreate::redo()
  31. {
  32. UndoStackExecutionScope s(m_stack);
  33. if (m_entries.empty())
  34. {
  35. // This is the first call to redo().
  36. HierarchyItemRawPtrList items;
  37. for (auto& parentEntityId : m_parents)
  38. {
  39. // Find a unique name for the new element
  40. AZStd::string uniqueName;
  41. UiCanvasBus::EventResult(
  42. uniqueName,
  43. m_hierarchy->GetEditorWindow()->GetCanvas(),
  44. &UiCanvasBus::Events::GetUniqueChildName,
  45. parentEntityId,
  46. UICANVASEDITOR_ELEMENT_NAME_DEFAULT,
  47. nullptr);
  48. // Create a new hierarchy item which will create a new entity
  49. QTreeWidgetItem* parent = HierarchyHelpers::ElementToItem(m_hierarchy, parentEntityId, true);
  50. AZ_Assert(parent, "No parent widget item found for parent entity");
  51. HierarchyItem* hierarchyItem = new HierarchyItem(m_hierarchy->GetEditorWindow(),
  52. *parent,
  53. m_childIndex,
  54. QString(uniqueName.c_str()),
  55. nullptr);
  56. items.push_back(hierarchyItem);
  57. AZ::Entity* element = items.back()->GetElement();
  58. m_postCreationCB(element);
  59. }
  60. // true: Put the serialized data in m_undoXml.
  61. HierarchyClipboard::Serialize(m_hierarchy, m_hierarchy->selectedItems(), &items, m_entries, true);
  62. AZ_Assert(!m_entries.empty(), "Failed to serialize");
  63. }
  64. else
  65. {
  66. HierarchyHelpers::CreateItemsAndElements(m_hierarchy, m_entries);
  67. }
  68. HierarchyHelpers::ExpandParents(m_hierarchy, m_entries);
  69. m_hierarchy->clearSelection();
  70. HierarchyHelpers::SetSelectedItems(m_hierarchy, &m_entries);
  71. }
  72. void CommandHierarchyItemCreate::Push(UndoStack* stack,
  73. HierarchyWidget* hierarchy,
  74. const QTreeWidgetItemRawPtrQList& selectedItems,
  75. int childIndex,
  76. PostCreationCallback postCreationCB)
  77. {
  78. if (stack->GetIsExecuting())
  79. {
  80. // This is a redundant Qt notification.
  81. // Nothing else to do.
  82. return;
  83. }
  84. stack->push(new CommandHierarchyItemCreate(stack,
  85. hierarchy,
  86. SelectionHelpers::GetSelectedElementIds(hierarchy,
  87. selectedItems,
  88. true
  89. ),
  90. childIndex,
  91. postCreationCB));
  92. }