GUndoStack.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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/GUndoStack.h>
  27. GUndoStack::GUndoStack()
  28. {
  29. }
  30. GUndoStack::~GUndoStack()
  31. {
  32. }
  33. void GUndoStack::undo()
  34. {
  35. if (!can_undo())
  36. return;
  37. auto& undo_container = m_stack[m_stack_index];
  38. auto& undo_vector = undo_container.m_undo_vector;
  39. //If we try to undo a empty vector, delete it and skip over.
  40. if (undo_vector.is_empty()) {
  41. m_stack.remove(m_stack_index);
  42. undo();
  43. return;
  44. }
  45. for (int i = 0; i < undo_vector.size(); i++) {
  46. auto& undo_command = undo_vector[i];
  47. undo_command.undo();
  48. }
  49. m_stack_index++;
  50. }
  51. void GUndoStack::redo()
  52. {
  53. if (!can_redo())
  54. return;
  55. auto& undo_container = m_stack[m_stack_index - 1];
  56. auto& redo_vector = undo_container.m_undo_vector;
  57. for (int i = redo_vector.size() - 1; i >= 0; i--) {
  58. auto& undo_command = redo_vector[i];
  59. undo_command.redo();
  60. }
  61. m_stack_index--;
  62. }
  63. void GUndoStack::push(NonnullOwnPtr<GCommand>&& command)
  64. {
  65. if (m_stack.is_empty()) {
  66. auto undo_commands_container = make<UndoCommandsContainer>();
  67. m_stack.prepend(move(undo_commands_container));
  68. }
  69. // Clear the elements of the stack before the m_undo_stack_index (Excluding our new element)
  70. for (int i = 1; i < m_stack_index; i++)
  71. m_stack.remove(1);
  72. if (m_stack_index > 0 && !m_stack.is_empty())
  73. m_stack[0].m_undo_vector.clear();
  74. m_stack_index = 0;
  75. m_stack[0].m_undo_vector.prepend(move(command));
  76. }
  77. void GUndoStack::finalize_current_combo()
  78. {
  79. if (m_stack.is_empty())
  80. return;
  81. auto& undo_vector = m_stack[0].m_undo_vector;
  82. if (undo_vector.size() == m_last_updated_undo_vector_size && !undo_vector.is_empty()) {
  83. auto undo_commands_container = make<UndoCommandsContainer>();
  84. m_stack.prepend(move(undo_commands_container));
  85. // Note: Remove dbg() if we're 100% sure there are no bugs left.
  86. dbg() << "Undo stack increased to " << m_stack.size();
  87. // Shift the index to the left since we're adding an empty container.
  88. if (m_stack_index > 0)
  89. m_stack_index++;
  90. }
  91. m_last_updated_undo_vector_size = undo_vector.size();
  92. }