Zrythm v2.0.0-DEV
a highly automated and intuitive digital audio workstation
Loading...
Searching...
No Matches
track_collection_operator.h
1// SPDX-FileCopyrightText: © 2026 Alexandros Theodotou <alex@zrythm.org>
2// SPDX-License-Identifier: LicenseRef-ZrythmLicense
3
4#pragma once
5
6#include <ranges>
7#include <unordered_set>
8
9#include "structure/tracks/track_collection.h"
10#include "undo/undo_stack.h"
11#include "utils/traits.h"
12
13#include <QtQmlIntegration/qqmlintegration.h>
14
15namespace zrythm::actions
16{
17
24class TrackCollectionOperator : public QObject
25{
26 Q_OBJECT
27 Q_PROPERTY (
28 zrythm::structure::tracks::TrackCollection * collection READ collection
29 WRITE setCollection NOTIFY collectionChanged)
30 Q_PROPERTY (
31 zrythm::undo::UndoStack * undoStack READ undoStack WRITE setUndoStack NOTIFY
32 undoStackChanged)
33 QML_ELEMENT
34
35public:
36 explicit TrackCollectionOperator (QObject * parent = nullptr)
37 : QObject (parent)
38 {
39 }
40
41 structure::tracks::TrackCollection * collection () const
42 {
43 return collection_;
44 }
45 void setCollection (structure::tracks::TrackCollection * collection)
46 {
47 if (collection_ != collection)
48 {
49 collection_ = collection;
50 Q_EMIT collectionChanged ();
51 }
52 }
53 Q_SIGNAL void collectionChanged ();
54
55 undo::UndoStack * undoStack () const { return undo_stack_; }
56 void setUndoStack (undo::UndoStack * undoStack)
57 {
58 if (undo_stack_ != undoStack)
59 {
60 undo_stack_ = undoStack;
61 Q_EMIT undoStackChanged ();
62 }
63 }
64 Q_SIGNAL void undoStackChanged ();
65
75 Q_INVOKABLE void moveTracks (
76 const QList<zrythm::structure::tracks::Track *> &tracks,
77 int targetPosition,
79
82 *
83 * QML cannot resolve C++ default parameters on Q_INVOKABLE methods.
84 * Use this overload when no target folder is needed.
85 */
86 Q_INVOKABLE void moveTracks (
87 const QList<zrythm::structure::tracks::Track *> &tracks,
88 int targetPosition)
89 {
90 moveTracks (tracks, targetPosition, nullptr);
91 }
92
99 Q_INVOKABLE void
100 deleteTracks (const QList<zrythm::structure::tracks::Track *> &tracks);
101
102private:
109 [[nodiscard]] std::vector<structure::tracks::TrackUuidReference>
110 expand_with_descendants (
112 {
113 assert (collection_ != nullptr);
114
115 auto &registry = collection_->get_track_registry ();
116
117 auto seen =
118 track_refs
119 | std::views::transform ([] (const auto &ref) { return ref.id (); })
120 | std::ranges::to<std::unordered_set> ();
121
122 auto expanded =
123 track_refs
124 | std::ranges::to<std::vector<structure::tracks::TrackUuidReference>> ();
125
126 for (const auto &ref : track_refs)
127 {
128 if (collection_->is_track_foldable (ref.id ()))
129 {
130 for (
131 const auto &desc_id : collection_->get_all_descendants (ref.id ()))
132 {
133 if (!seen.contains (desc_id))
134 {
135 expanded.emplace_back (desc_id, registry);
136 seen.insert (desc_id);
137 }
138 }
139 }
140 }
141
142 return expanded;
143 }
144
145 structure::tracks::TrackCollection * collection_{};
146 undo::UndoStack * undo_stack_{};
147};
148
149} // namespace zrythm::actions
Q_INVOKABLE void deleteTracks(const QList< zrythm::structure::tracks::Track * > &tracks)
Deletes the given tracks (and their descendants if foldable).
Q_INVOKABLE void moveTracks(const QList< zrythm::structure::tracks::Track * > &tracks, int targetPosition, zrythm::structure::tracks::Track *targetFolder)
Moves tracks to a new position in the collection.
A collection of tracks that provides a QAbstractListModel interface.
bool is_track_foldable(const Track::Uuid &track_id) const
Check if a track is foldable.
std::vector< Track::Uuid > get_all_descendants(const Track::Uuid &parent_id) const
Get all descendant track UUIDs of a folder, in list order.
Represents a track in the project.
Definition track.h:54