undo-stack-obs.hpp 1.2 KB

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