undo_stack.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. #include "undo_stack.hpp"
  2. #include <OBSApp.hpp>
  3. #include "moc_undo_stack.cpp"
  4. #define MAX_STACK_SIZE 5000
  5. undo_stack::undo_stack(ui_ptr ui) : ui(ui)
  6. {
  7. QObject::connect(&repeat_reset_timer, &QTimer::timeout, this, &undo_stack::reset_repeatable_state);
  8. repeat_reset_timer.setSingleShot(true);
  9. repeat_reset_timer.setInterval(3000);
  10. }
  11. void undo_stack::reset_repeatable_state()
  12. {
  13. last_is_repeatable = false;
  14. }
  15. void undo_stack::clear()
  16. {
  17. undo_items.clear();
  18. redo_items.clear();
  19. last_is_repeatable = false;
  20. ui->actionMainUndo->setText(QTStr("Undo.Undo"));
  21. ui->actionMainRedo->setText(QTStr("Undo.Redo"));
  22. ui->actionMainUndo->setDisabled(true);
  23. ui->actionMainRedo->setDisabled(true);
  24. }
  25. void undo_stack::add_action(const QString &name, const undo_redo_cb &undo, const undo_redo_cb &redo,
  26. const std::string &undo_data, const std::string &redo_data, bool repeatable)
  27. {
  28. if (!is_enabled())
  29. return;
  30. while (undo_items.size() >= MAX_STACK_SIZE) {
  31. undo_redo_t item = undo_items.back();
  32. undo_items.pop_back();
  33. }
  34. if (repeatable) {
  35. repeat_reset_timer.start();
  36. }
  37. if (last_is_repeatable && repeatable && name == undo_items[0].name) {
  38. undo_items[0].redo = redo;
  39. undo_items[0].redo_data = redo_data;
  40. return;
  41. }
  42. undo_redo_t n = {name, undo_data, redo_data, undo, redo};
  43. last_is_repeatable = repeatable;
  44. undo_items.push_front(n);
  45. clear_redo();
  46. ui->actionMainUndo->setText(QTStr("Undo.Item.Undo").arg(name));
  47. ui->actionMainUndo->setEnabled(true);
  48. ui->actionMainRedo->setText(QTStr("Undo.Redo"));
  49. ui->actionMainRedo->setDisabled(true);
  50. }
  51. void undo_stack::undo()
  52. {
  53. if (undo_items.size() == 0 || !is_enabled())
  54. return;
  55. last_is_repeatable = false;
  56. undo_redo_t temp = undo_items.front();
  57. temp.undo(temp.undo_data);
  58. redo_items.push_front(temp);
  59. undo_items.pop_front();
  60. ui->actionMainRedo->setText(QTStr("Undo.Item.Redo").arg(temp.name));
  61. ui->actionMainRedo->setEnabled(true);
  62. if (undo_items.size() == 0) {
  63. ui->actionMainUndo->setDisabled(true);
  64. ui->actionMainUndo->setText(QTStr("Undo.Undo"));
  65. } else {
  66. ui->actionMainUndo->setText(QTStr("Undo.Item.Undo").arg(undo_items.front().name));
  67. }
  68. }
  69. void undo_stack::redo()
  70. {
  71. if (redo_items.size() == 0 || !is_enabled())
  72. return;
  73. last_is_repeatable = false;
  74. undo_redo_t temp = redo_items.front();
  75. temp.redo(temp.redo_data);
  76. undo_items.push_front(temp);
  77. redo_items.pop_front();
  78. ui->actionMainUndo->setText(QTStr("Undo.Item.Undo").arg(temp.name));
  79. ui->actionMainUndo->setEnabled(true);
  80. if (redo_items.size() == 0) {
  81. ui->actionMainRedo->setDisabled(true);
  82. ui->actionMainRedo->setText(QTStr("Undo.Redo"));
  83. } else {
  84. ui->actionMainRedo->setText(QTStr("Undo.Item.Redo").arg(redo_items.front().name));
  85. }
  86. }
  87. void undo_stack::enable_internal()
  88. {
  89. last_is_repeatable = false;
  90. ui->actionMainUndo->setDisabled(false);
  91. if (redo_items.size() > 0)
  92. ui->actionMainRedo->setDisabled(false);
  93. }
  94. void undo_stack::disable_internal()
  95. {
  96. last_is_repeatable = false;
  97. ui->actionMainUndo->setDisabled(true);
  98. ui->actionMainRedo->setDisabled(true);
  99. }
  100. void undo_stack::enable()
  101. {
  102. enabled = true;
  103. if (is_enabled())
  104. enable_internal();
  105. }
  106. void undo_stack::disable()
  107. {
  108. if (is_enabled())
  109. disable_internal();
  110. enabled = false;
  111. }
  112. void undo_stack::push_disabled()
  113. {
  114. if (is_enabled())
  115. disable_internal();
  116. disable_refs++;
  117. }
  118. void undo_stack::pop_disabled()
  119. {
  120. disable_refs--;
  121. if (is_enabled())
  122. enable_internal();
  123. }
  124. void undo_stack::clear_redo()
  125. {
  126. redo_items.clear();
  127. }