Zrythm v2.0.0-DEV
a highly automated and intuitive digital audio workstation
Loading...
Searching...
No Matches
undo_stack.h
1// SPDX-FileCopyrightText: © 2020-2022, 2024-2025 Alexandros Theodotou <alex@zrythm.org>
2// SPDX-License-Identifier: LicenseRef-ZrythmLicense
3
4#pragma once
5
6#include "gui/backend/backend/actions/undoable_action_all.h"
7#include "utils/icloneable.h"
8#include "utils/serialization.h"
9
10namespace zrythm::gui::actions
11{
12
18class UndoStack : public QAbstractListModel
19{
20 Q_OBJECT
21 QML_ELEMENT
22 Q_PROPERTY (int rowCount READ rowCount NOTIFY rowCountChanged FINAL)
23
24public:
25 UndoStack (QObject * parent = nullptr);
26 Q_DISABLE_COPY_MOVE (UndoStack)
27 ~UndoStack () override = default;
28
29 // ====================================================================
30 // QML Interface
31 // ====================================================================
32
33 enum UndoableActionRoles
34 {
35 UndoableActionPtrRole = Qt::UserRole + 1,
36 };
37
38 QHash<int, QByteArray> roleNames () const override;
39 int rowCount (const QModelIndex &parent = QModelIndex ()) const override;
40 QVariant
41 data (const QModelIndex &index, int role = Qt::DisplayRole) const override;
42
43 Q_SIGNAL void rowCountChanged (int rowCount);
44
45 // ====================================================================
46
47 void init_loaded (sample_rate_t engine_sample_rate);
48
52 [[nodiscard]] std::string get_as_string (int limit) const;
53
54 /* --- start wrappers --- */
55
62 template <UndoableActionSubclass T> void push (T * action);
63
69 std::optional<UndoableActionPtrVariant> pop ();
70
76 std::optional<UndoableActionPtrVariant> pop_last ();
77 auto size () const { return actions_.size (); }
78 bool is_empty () const { return actions_.empty (); }
79 bool empty () const { return is_empty (); }
80 bool is_full () const { return size () == max_size_; }
81
89 std::optional<UndoableActionPtrVariant> peek () const;
90
91 void clear () { actions_.clear (); };
92
93 /* --- end wrappers --- */
94
98 template <UndoableActionSubclass T>
99 [[nodiscard]] bool contains_action (const T &ua) const;
100
104 void get_plugins (std::vector<zrythm::plugins::Plugin *> &arr) const;
105
106 friend void init_from (
107 UndoStack &obj,
108 const UndoStack &other,
109 utils::ObjectCloneType clone_type);
110
111private:
112 static constexpr auto kMaxLengthKey = "maxLength"sv;
113 friend void to_json (nlohmann::json &j, const UndoStack &stack)
114 {
115 // rest TODO
116 j[kMaxLengthKey] = stack.max_size_;
117 }
118 friend void from_json (const nlohmann::json &j, UndoStack &stack)
119 {
120 j.at (kMaxLengthKey).get_to (stack.max_size_);
121 }
122
123public:
127 std::vector<UndoableActionPtrVariant> actions_;
128
132 size_t max_size_ = 0;
133};
134
135extern template void
137extern template void
139extern template void
141extern template void
143extern template void
145extern template void
146UndoStack::push (PortAction * action);
147extern template void
149extern template void
151extern template void
153extern template void
155
156extern template bool
158extern template bool
160extern template bool
161UndoStack::contains_action (const ChordAction &action) const;
162extern template bool
164extern template bool
166extern template bool
167UndoStack::contains_action (const PortAction &action) const;
168extern template bool
170extern template bool
171UndoStack::contains_action (const RangeAction &action) const;
172extern template bool
174extern template bool
175UndoStack::contains_action (const TransportAction &action) const;
176
177}; // namespace zrythm::gui::actions
An action that performs changes to the arranger selections.
Action for channel send changes.
Action for chord pad changes.
Action for manipulating plugins (plugin slot selections in the mixer).
Serializable stack for undoable actions.
Definition undo_stack.h:19
void push(T *action)
Take ownership of the given action.
std::optional< UndoableActionPtrVariant > pop()
std::optional< UndoableActionPtrVariant > pop_last()
Pops the last (first added) element and moves everything back.
bool contains_action(const T &ua) const
Checks if the undo stack contains the given action pointer.
std::vector< UndoableActionPtrVariant > actions_
Actions on the stack.
Definition undo_stack.h:127
size_t max_size_
Max size of the stack (if 0, unlimited).
Definition undo_stack.h:132
std::optional< UndoableActionPtrVariant > peek() const
std::string get_as_string(int limit) const
Gets the list of actions as a string.
void get_plugins(std::vector< zrythm::plugins::Plugin * > &arr) const
Returns the plugins referred to in the undo stack.
uint32_t sample_rate_t
Sample rate.
Definition types.h:61