UndoStack.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <LibGUI/Command.h>
  27. #include <LibGUI/UndoStack.h>
  28. namespace GUI {
  29. UndoStack::UndoStack()
  30. {
  31. }
  32. UndoStack::~UndoStack()
  33. {
  34. }
  35. void UndoStack::undo()
  36. {
  37. if (!can_undo())
  38. return;
  39. auto& undo_container = m_stack[m_stack_index];
  40. auto& undo_vector = undo_container.m_undo_vector;
  41. //If we try to undo a empty vector, delete it and skip over.
  42. if (undo_vector.is_empty()) {
  43. m_stack.remove(m_stack_index);
  44. undo();
  45. return;
  46. }
  47. for (size_t i = 0; i < undo_vector.size(); i++) {
  48. auto& undo_command = undo_vector[i];
  49. undo_command.undo();
  50. }
  51. m_stack_index++;
  52. }
  53. void UndoStack::redo()
  54. {
  55. if (!can_redo())
  56. return;
  57. auto& undo_container = m_stack[m_stack_index - 1];
  58. auto& redo_vector = undo_container.m_undo_vector;
  59. for (int i = static_cast<int>(redo_vector.size()) - 1; i >= 0; i--) {
  60. auto& undo_command = redo_vector[i];
  61. undo_command.redo();
  62. }
  63. m_stack_index--;
  64. }
  65. void UndoStack::push(NonnullOwnPtr<Command>&& command)
  66. {
  67. if (m_stack.is_empty()) {
  68. auto undo_commands_container = make<UndoCommandsContainer>();
  69. m_stack.prepend(move(undo_commands_container));
  70. }
  71. // Clear the elements of the stack before the m_undo_stack_index (Excluding our new element)
  72. for (size_t i = 1; i < m_stack_index; i++)
  73. m_stack.remove(1);
  74. if (m_stack_index > 0 && !m_stack.is_empty())
  75. m_stack[0].m_undo_vector.clear();
  76. m_stack_index = 0;
  77. m_stack[0].m_undo_vector.prepend(move(command));
  78. }
  79. void UndoStack::finalize_current_combo()
  80. {
  81. if (m_stack.is_empty())
  82. return;
  83. auto& undo_vector = m_stack[0].m_undo_vector;
  84. if (undo_vector.size() == m_last_updated_undo_vector_size && !undo_vector.is_empty()) {
  85. auto undo_commands_container = make<UndoCommandsContainer>();
  86. m_stack.prepend(move(undo_commands_container));
  87. // Note: Remove dbg() if we're 100% sure there are no bugs left.
  88. dbg() << "Undo stack increased to " << m_stack.size();
  89. // Shift the index to the left since we're adding an empty container.
  90. if (m_stack_index > 0)
  91. m_stack_index++;
  92. }
  93. m_last_updated_undo_vector_size = undo_vector.size();
  94. }
  95. }