undo-stack-obs.cpp 3.4 KB

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