CommandHierarchyItemToggleIsExpanded.cpp 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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. CommandHierarchyItemToggleIsExpanded::CommandHierarchyItemToggleIsExpanded(UndoStack* stack,
  10. HierarchyWidget* hierarchy,
  11. HierarchyItem* item)
  12. : QUndoCommand()
  13. , m_stack(stack)
  14. , m_hierarchy(hierarchy)
  15. , m_id(item->GetEntityId())
  16. , m_toIsExpanded(item->isExpanded())
  17. {
  18. setText(QString("%1 of \"%2\"").arg((m_toIsExpanded ? "expansion" : "collapse"), item->GetElement()->GetName().c_str()));
  19. }
  20. void CommandHierarchyItemToggleIsExpanded::undo()
  21. {
  22. UndoStackExecutionScope s(m_stack);
  23. SetIsExpanded(!m_toIsExpanded);
  24. }
  25. void CommandHierarchyItemToggleIsExpanded::redo()
  26. {
  27. UndoStackExecutionScope s(m_stack);
  28. SetIsExpanded(m_toIsExpanded);
  29. }
  30. void CommandHierarchyItemToggleIsExpanded::SetIsExpanded(bool isExpanded)
  31. {
  32. AZ::Entity* element = EntityHelpers::GetEntity(m_id);
  33. if (!element)
  34. {
  35. // The element DOESN'T exist.
  36. // Nothing to do.
  37. return;
  38. }
  39. // This will do both the Runtime-side and Editor-side.
  40. HierarchyItem::RttiCast(HierarchyHelpers::ElementToItem(m_hierarchy, element, false))->SetIsExpanded(isExpanded);
  41. }
  42. void CommandHierarchyItemToggleIsExpanded::Push(UndoStack* stack,
  43. HierarchyWidget* hierarchy,
  44. HierarchyItem* item)
  45. {
  46. if (stack->GetIsExecuting())
  47. {
  48. // This is a redundant Qt notification.
  49. // Nothing else to do.
  50. return;
  51. }
  52. stack->push(new CommandHierarchyItemToggleIsExpanded(stack,
  53. hierarchy,
  54. item));
  55. }