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 "utils/qt.h"
7
8#include <QUndoCommand>
9#include <QUndoStack>
10#include <QtQmlIntegration/qqmlintegration.h>
11
12#include "nlohmann/json_fwd.hpp"
13
14namespace zrythm::undo
15{
16
17class UndoStack : public QObject
18{
19 Q_OBJECT
20 Q_PROPERTY (QStringList undoActions READ undoActions NOTIFY undoActionsChanged)
21 Q_PROPERTY (QStringList redoActions READ redoActions NOTIFY redoActionsChanged)
22 Q_PROPERTY (int index READ index WRITE setIndex NOTIFY indexChanged)
23 Q_PROPERTY (bool canUndo READ canUndo NOTIFY canUndoChanged)
24 Q_PROPERTY (bool canRedo READ canRedo NOTIFY canRedoChanged)
25 QML_ELEMENT
26 QML_UNCREATABLE ("")
27public:
34 std::function<void (const std::function<void ()> &, bool)>;
35
36 explicit UndoStack (
37 CallbackWithPausedEngineRequester callback_with_paused_engine_requester,
38 QObject * parent = nullptr);
39
40 // all users of the undo stack should use these wrappers instead of
41 // QUndoStack's push/undo/redo. all other functionality should handed off to
42 // the underlying QUndoStack
43 void push (QUndoCommand * cmd);
44 Q_INVOKABLE void undo ();
45 Q_INVOKABLE void redo ();
46
47 bool canUndo () const { return stack_->canUndo (); }
48 bool canRedo () const { return stack_->canRedo (); }
49
50 const QUndoCommand * command (int index) const
51 {
52 return stack_->command (index);
53 }
54
55 Q_INVOKABLE void beginMacro (const QString &text)
56 {
57 stack_->beginMacro (text);
58 }
59 Q_INVOKABLE void endMacro () { stack_->endMacro (); }
60
61 QStringList undoActions ();
62 QStringList redoActions ();
63
64 int index () const { return stack_->index (); }
65 void setIndex (int idx);
66
67 auto count () const { return stack_->count (); }
68 auto text (int idx) const { return stack_->text (idx); }
69
70Q_SIGNALS:
71 void undoActionsChanged ();
72 void redoActionsChanged ();
73 void indexChanged ();
74 void canUndoChanged ();
75 void canRedoChanged ();
76
77private:
78 // persistence (TODO)
79 static constexpr auto kStackContentKey = "stackContent"sv;
80 friend void to_json (nlohmann::json &j, const UndoStack &u);
81 friend void from_json (const nlohmann::json &j, UndoStack &u);
82
89 bool command_or_children_require_graph_recalculation (
90 const QUndoCommand &cmd) const;
91
96 bool command_or_children_require_engine_pause (const QUndoCommand &cmd) const;
97
104 void execute_with_engine_pause_if_needed (
105 const QUndoCommand &cmd,
106 const std::function<void ()> &action);
107
108private:
109 utils::QObjectUniquePtr<QUndoStack> stack_;
110
111 // Engine operations
112 CallbackWithPausedEngineRequester callback_with_paused_engine_requester_;
113};
114};
std::function< void(const std::function< void()> &, bool)> CallbackWithPausedEngineRequester
A function for requesting a callback with the engine paused.
Definition undo_stack.h:33