Zrythm v2.0.0-DEV
a highly automated and intuitive digital audio workstation
Loading...
Searching...
No Matches
undo_stack.h
1// SPDX-FileCopyrightText: © 2025 Alexandros Theodotou <alex@zrythm.org>
2// SPDX-License-Identifier: LicenseRef-ZrythmLicense
3
4#pragma once
5
6#include <string_view>
7
8#include "utils/qt.h"
9
10#include <QUndoCommand>
11#include <QUndoStack>
12#include <QtQmlIntegration/qqmlintegration.h>
13
14#include "nlohmann/json_fwd.hpp"
15
16namespace zrythm::undo
17{
18using namespace std::string_view_literals;
19
20class UndoStack : public QObject
21{
22 Q_OBJECT
23 Q_PROPERTY (QStringList undoActions READ undoActions NOTIFY undoActionsChanged)
24 Q_PROPERTY (QStringList redoActions READ redoActions NOTIFY redoActionsChanged)
25 Q_PROPERTY (int index READ index WRITE setIndex NOTIFY indexChanged)
26 Q_PROPERTY (bool canUndo READ canUndo NOTIFY canUndoChanged)
27 Q_PROPERTY (bool canRedo READ canRedo NOTIFY canRedoChanged)
28 QML_ELEMENT
29 QML_UNCREATABLE ("")
30public:
37 std::function<void (const std::function<void ()> &, bool)>;
38
39 explicit UndoStack (
40 CallbackWithPausedEngineRequester callback_with_paused_engine_requester,
41 QObject * parent = nullptr);
42
43 // all users of the undo stack should use these wrappers instead of
44 // QUndoStack's push/undo/redo. all other functionality should handed off to
45 // the underlying QUndoStack
46 void push (QUndoCommand * cmd);
47 Q_INVOKABLE void undo ();
48 Q_INVOKABLE void redo ();
49
50 bool canUndo () const { return stack_->canUndo (); }
51 bool canRedo () const { return stack_->canRedo (); }
52
53 const QUndoCommand * command (int index) const
54 {
55 return stack_->command (index);
56 }
57
58 Q_INVOKABLE void beginMacro (const QString &text)
59 {
60 stack_->beginMacro (text);
61 }
62 Q_INVOKABLE void endMacro () { stack_->endMacro (); }
63
64 QStringList undoActions ();
65 QStringList redoActions ();
66
67 int index () const { return stack_->index (); }
68 void setIndex (int idx);
69
70 auto count () const { return stack_->count (); }
71 auto text (int idx) const { return stack_->text (idx); }
72
73Q_SIGNALS:
74 void undoActionsChanged ();
75 void redoActionsChanged ();
76 void indexChanged ();
77 void canUndoChanged ();
78 void canRedoChanged ();
79
80private:
81 // persistence (TODO)
82 static constexpr auto kStackContentKey = "stackContent"sv;
83 friend void to_json (nlohmann::json &j, const UndoStack &u);
84 friend void from_json (const nlohmann::json &j, UndoStack &u);
85
92 bool command_or_children_require_graph_recalculation (
93 const QUndoCommand &cmd) const;
94
99 bool command_or_children_require_engine_pause (const QUndoCommand &cmd) const;
100
107 void execute_with_engine_pause_if_needed (
108 const QUndoCommand &cmd,
109 const std::function<void ()> &action);
110
111private:
112 utils::QObjectUniquePtr<QUndoStack> stack_;
113
114 // Engine operations
115 CallbackWithPausedEngineRequester callback_with_paused_engine_requester_;
116};
117};
std::function< void(const std::function< void()> &, bool)> CallbackWithPausedEngineRequester
A function for requesting a callback with the engine paused.
Definition undo_stack.h:36