1
0

undo_stack.hpp 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. #pragma once
  2. #include "ui_OBSBasic.h"
  3. #include <QObject>
  4. #include <QString>
  5. #include <QTimer>
  6. #include <deque>
  7. #include <string>
  8. class undo_stack : public QObject {
  9. Q_OBJECT
  10. typedef std::function<void(const std::string &data)> undo_redo_cb;
  11. typedef std::function<void(bool is_undo)> func;
  12. typedef std::unique_ptr<Ui::OBSBasic> &ui_ptr;
  13. struct undo_redo_t {
  14. QString name;
  15. std::string undo_data;
  16. std::string redo_data;
  17. undo_redo_cb undo;
  18. undo_redo_cb redo;
  19. };
  20. ui_ptr ui;
  21. std::deque<undo_redo_t> undo_items;
  22. std::deque<undo_redo_t> redo_items;
  23. int disable_refs = 0;
  24. bool enabled = true;
  25. bool last_is_repeatable = false;
  26. QTimer repeat_reset_timer;
  27. inline bool is_enabled() const { return !disable_refs && enabled; }
  28. void enable_internal();
  29. void disable_internal();
  30. void clear_redo();
  31. private slots:
  32. void reset_repeatable_state();
  33. public:
  34. undo_stack(ui_ptr ui);
  35. void enable();
  36. void disable();
  37. void push_disabled();
  38. void pop_disabled();
  39. void clear();
  40. void add_action(const QString &name, const undo_redo_cb &undo, const undo_redo_cb &redo,
  41. const std::string &undo_data, const std::string &redo_data, bool repeatable = false);
  42. void undo();
  43. void redo();
  44. };