CommandCanvasSizeToolbarIndex.cpp 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. CommandCanvasSizeToolbarIndex::CommandCanvasSizeToolbarIndex(UndoStack* stack,
  10. CanvasSizeToolbarSection* canvasSizeToolbar,
  11. int from,
  12. int to)
  13. : QUndoCommand()
  14. , m_stack(stack)
  15. , m_canvasSizeToolbar(canvasSizeToolbar)
  16. , m_from(from)
  17. , m_to(to)
  18. {
  19. UpdateText();
  20. }
  21. void CommandCanvasSizeToolbarIndex::undo()
  22. {
  23. UndoStackExecutionScope s(m_stack);
  24. SetIndex(m_from);
  25. }
  26. void CommandCanvasSizeToolbarIndex::redo()
  27. {
  28. UndoStackExecutionScope s(m_stack);
  29. SetIndex(m_to);
  30. }
  31. int CommandCanvasSizeToolbarIndex::id() const
  32. {
  33. return (int)FusibleCommand::kCanvasSizeToolbarIndex;
  34. }
  35. bool CommandCanvasSizeToolbarIndex::mergeWith(const QUndoCommand* other)
  36. {
  37. if (other->id() != id())
  38. {
  39. // NOT the same command type.
  40. return false;
  41. }
  42. const CommandCanvasSizeToolbarIndex* subsequent = static_cast<const CommandCanvasSizeToolbarIndex*>(other);
  43. AZ_Assert(subsequent, "There is no command to merge with");
  44. if (!((subsequent->m_stack == m_stack) &&
  45. (subsequent->m_canvasSizeToolbar == m_canvasSizeToolbar)))
  46. {
  47. // NOT the same context.
  48. return false;
  49. }
  50. m_to = subsequent->m_to;
  51. UpdateText();
  52. return true;
  53. }
  54. void CommandCanvasSizeToolbarIndex::UpdateText()
  55. {
  56. setText(QString("canvas size change to %1").arg(m_canvasSizeToolbar->IndexToString(m_to)));
  57. }
  58. void CommandCanvasSizeToolbarIndex::SetIndex(int index) const
  59. {
  60. // IMPORTANT: It's NOT necessary to prevent this from executing on the
  61. // first run. We WON'T get a redundant Qt notification by this point.
  62. m_canvasSizeToolbar->SetIndex(index);
  63. }
  64. void CommandCanvasSizeToolbarIndex::Push(UndoStack* stack,
  65. CanvasSizeToolbarSection* canvasSizeToolbar,
  66. int from,
  67. int to)
  68. {
  69. if (stack->GetIsExecuting())
  70. {
  71. // This is a redundant Qt notification.
  72. // Nothing else to do.
  73. return;
  74. }
  75. stack->push(new CommandCanvasSizeToolbarIndex(stack, canvasSizeToolbar, from, to));
  76. }