CommandHierarchyItemRename.cpp 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. CommandHierarchyItemRename::CommandHierarchyItemRename(UndoStack* stack,
  10. HierarchyWidget* hierarchy,
  11. const AZ::EntityId& entityId,
  12. const QString& fromName,
  13. const QString& toName)
  14. : QUndoCommand()
  15. , m_stack(stack)
  16. , m_hierarchy(hierarchy)
  17. , m_id(entityId)
  18. , m_from(fromName)
  19. , m_to(toName)
  20. {
  21. setText(QString("rename to \"%1\"").arg(m_to));
  22. }
  23. void CommandHierarchyItemRename::undo()
  24. {
  25. UndoStackExecutionScope s(m_stack);
  26. SetName(m_from);
  27. }
  28. void CommandHierarchyItemRename::redo()
  29. {
  30. UndoStackExecutionScope s(m_stack);
  31. SetName(m_to);
  32. }
  33. void CommandHierarchyItemRename::SetName(const QString& name) const
  34. {
  35. // Runtime-side.
  36. AZ::Entity* element = EntityHelpers::GetEntity(m_id);
  37. {
  38. if (!element)
  39. {
  40. // The element DOESN'T exist.
  41. // Nothing to do.
  42. return;
  43. }
  44. element->SetName(name.toStdString().c_str());
  45. }
  46. m_hierarchy->GetEditorWindow()->GetProperties()->TriggerRefresh(AzToolsFramework::PropertyModificationRefreshLevel::Refresh_Values);
  47. // Editor-side.
  48. //
  49. // IMPORTANT: It's NOT necessary to prevent this from executing
  50. // on the first run. We WON'T get a redundant Qt notification.
  51. HierarchyHelpers::ElementToItem(m_hierarchy, element, false)->setText(0, name);
  52. }
  53. void CommandHierarchyItemRename::Push(UndoStack* stack,
  54. HierarchyWidget* hierarchy,
  55. const AZ::EntityId& entityId,
  56. const QString& fromName,
  57. const QString& toName)
  58. {
  59. // IMPORTANT: Using QSignalBlocker with hierarchy->model() here
  60. // DOESN'T prevent multiple notifications. Therefore we HAVE to
  61. // filter-out redundant notifications manually.
  62. if (fromName == toName)
  63. {
  64. // Both strings are the same.
  65. // Nothing to do.
  66. return;
  67. }
  68. if (stack->GetIsExecuting())
  69. {
  70. // This is a redundant Qt notification.
  71. // Nothing else to do.
  72. return;
  73. }
  74. stack->push(new CommandHierarchyItemRename(stack,
  75. hierarchy,
  76. entityId,
  77. fromName,
  78. toName));
  79. }