undo-stack-obs.hpp 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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, const undo_redo_cb &redo,
  43. const std::string &undo_data, const std::string &redo_data, bool repeatable = false);
  44. void undo();
  45. void redo();
  46. };