Zrythm v2.0.0-DEV
a highly automated and intuitive digital audio workstation
Loading...
Searching...
No Matches
undo_manager.h
1// SPDX-FileCopyrightText: © 2019-2022, 2024-2025 Alexandros Theodotou <alex@zrythm.org>
2// SPDX-License-Identifier: LicenseRef-ZrythmLicense
3
4#pragma once
5
6#include <semaphore>
7
8#include "gui/backend/backend/actions/undo_stack.h"
9
10#define UNDO_MANAGER (PROJECT->undo_manager_)
11
12namespace zrythm::gui::actions
13{
14
18class UndoManager : public QObject
19{
20 Q_OBJECT
21 Q_PROPERTY (UndoStack * undoStack READ getUndoStack CONSTANT)
22 Q_PROPERTY (UndoStack * redoStack READ getRedoStack CONSTANT)
23 QML_ELEMENT
24
25public:
26 UndoManager (QObject * parent = nullptr);
27 Q_DISABLE_COPY_MOVE (UndoManager)
28 ~UndoManager () override = default;
29
33 void init_loaded (sample_rate_t engine_sample_rate);
34
35 UndoStack * getUndoStack () const { return undo_stack_; }
36 UndoStack * getRedoStack () const { return redo_stack_; }
37
43 Q_INVOKABLE void undo ();
44
50 Q_INVOKABLE void redo ();
51
59 Q_INVOKABLE void perform (QObject * action_qobject);
60
66 void get_plugins (std::vector<zrythm::plugins::Plugin *> &arr) const;
67
72 std::optional<UndoableActionPtrVariant> get_last_action () const;
73
77 void clear_stacks ();
78
79 friend void init_from (
80 UndoManager &obj,
81 const UndoManager &other,
82 utils::ObjectCloneType clone_type);
83
84private:
85 static constexpr auto kUndoStackKey = "undoStack"sv;
86 static constexpr auto kRedoStackKey = "redoStack"sv;
87 friend void to_json (nlohmann::json &j, const UndoManager &undo_manager)
88 {
89 j[kUndoStackKey] = undo_manager.undo_stack_;
90 }
91 friend void from_json (const nlohmann::json &j, UndoManager &undo_manager)
92 {
93 j.at (kUndoStackKey).get_to (*undo_manager.undo_stack_);
94 }
95
103 template <UndoableActionSubclass T>
104 void
105 do_or_undo_action (T * action, UndoStack &main_stack, UndoStack &opposite_stack);
106
111 void do_undo_redo (bool is_undo);
112
113public:
114 UndoStack * undo_stack_ = nullptr;
115 UndoStack * redo_stack_ = nullptr;
116
122 bool redo_stack_locked_ = false;
123
125 std::binary_semaphore action_sem_{ 1 };
126};
127
128extern template void
129UndoManager::do_or_undo_action (
130 ArrangerSelectionsAction * action,
131 UndoStack &main_stack,
132 UndoStack &opposite_stack);
133extern template void
134UndoManager::do_or_undo_action (
135 ChannelSendAction * action,
136 UndoStack &main_stack,
137 UndoStack &opposite_stack);
138extern template void
139UndoManager::do_or_undo_action (
140 ChordAction * action,
141 UndoStack &main_stack,
142 UndoStack &opposite_stack);
143extern template void
144UndoManager::do_or_undo_action (
145 MidiMappingAction * action,
146 UndoStack &main_stack,
147 UndoStack &opposite_stack);
148extern template void
149UndoManager::do_or_undo_action (
150 MixerSelectionsAction * action,
151 UndoStack &main_stack,
152 UndoStack &opposite_stack);
153extern template void
154UndoManager::do_or_undo_action (
155 PortAction * action,
156 UndoStack &main_stack,
157 UndoStack &opposite_stack);
158extern template void
159UndoManager::do_or_undo_action (
160 PortConnectionAction * action,
161 UndoStack &main_stack,
162 UndoStack &opposite_stack);
163extern template void
164UndoManager::do_or_undo_action (
165 RangeAction * action,
166 UndoStack &main_stack,
167 UndoStack &opposite_stack);
168extern template void
169UndoManager::do_or_undo_action (
170 TracklistSelectionsAction * action,
171 UndoStack &main_stack,
172 UndoStack &opposite_stack);
173extern template void
174UndoManager::do_or_undo_action (
175 TransportAction * action,
176 UndoStack &main_stack,
177 UndoStack &opposite_stack);
178
179}; // namespace zrythm::gui::actions
void init_loaded(sample_rate_t engine_sample_rate)
Inits the undo manager by populating the undo/redo stacks.
std::optional< UndoableActionPtrVariant > get_last_action() const
Returns the last performed action, or NULL if the stack is empty.
void clear_stacks()
Clears the undo and redo stacks.
Q_INVOKABLE void perform(QObject *action_qobject)
Performs the action and pushes it to the undo stack.
std::binary_semaphore action_sem_
Semaphore for performing actions.
void get_plugins(std::vector< zrythm::plugins::Plugin * > &arr) const
Returns all plugins in the undo stacks.
Q_INVOKABLE void undo()
Undo last action.
bool redo_stack_locked_
Whether the redo stack is currently locked.
Q_INVOKABLE void redo()
Redo last undone action.
Serializable stack for undoable actions.
Definition undo_stack.h:19