Zrythm v2.0.0-DEV
a highly automated and intuitive digital audio workstation
Loading...
Searching...
No Matches
track_collection.h
1// SPDX-FileCopyrightText: © 2025 Alexandros Theodotou <alex@zrythm.org>
2// SPDX-License-Identifier: LicenseRef-ZrythmLicense
3
4#pragma once
5
6#include "structure/tracks/track_span.h"
7
8#include <QtQmlIntegration/qqmlintegration.h>
9
10namespace zrythm::structure::tracks
11{
12
20class TrackCollection : public QAbstractListModel
21{
22 Q_OBJECT
23 Q_PROPERTY (
24 int numSoloedTracks READ numSoloedTracks NOTIFY numSoloedTracksChanged)
25 Q_PROPERTY (
26 int numMutedTracks READ numMutedTracks NOTIFY numMutedTracksChanged)
27 Q_PROPERTY (
28 int numListenedTracks READ numListenedTracks NOTIFY numListenedTracksChanged)
29 QML_ELEMENT
30 QML_UNCREATABLE ("")
31
32public:
33 enum TrackRoles
34 {
35 TrackPtrRole = Qt::UserRole + 1,
36
37 // Whether a track is foldable
38 TrackFoldableRole,
39
40 // Whether a foldable track is expanded
41 TrackExpandedRole,
42
43 // A foldable track's depth
44 TrackDepthRole,
45 };
46 Q_ENUM (TrackRoles)
47
48 TrackCollection (
49 TrackRegistry &track_registry,
50 QObject * parent = nullptr) noexcept;
51 ~TrackCollection () noexcept override;
52 Z_DISABLE_COPY_MOVE (TrackCollection)
53
54 // ========================================================================
55 // QML Interface
56 // ========================================================================
57
58 QHash<int, QByteArray> roleNames () const override;
59 int rowCount (const QModelIndex &parent = QModelIndex ()) const override;
60 QVariant
61 data (const QModelIndex &index, int role = Qt::DisplayRole) const override;
62
63 Q_INVOKABLE void setTrackExpanded (const Track * track, bool expanded);
64
65 int numSoloedTracks () const;
66 Q_SIGNAL void numSoloedTracksChanged ();
67 int numMutedTracks () const;
68 Q_SIGNAL void numMutedTracksChanged ();
69 int numListenedTracks () const;
70 Q_SIGNAL void numListenedTracksChanged ();
71
72 // ========================================================================
73 // Track Management
74 // ========================================================================
75
79 TrackPtrVariant get_track_at_index (size_t index) const;
80
84 TrackUuidReference get_track_ref_at_index (size_t index) const;
85
86 /**
87 * @brief Get the index of the track with the given UUID.
88 */
89 auto get_track_index (const Track::Uuid &track_id) const
90 {
91 return std::distance (
92 tracks_.begin (),
93 std::ranges::find (tracks_, track_id, &TrackUuidReference::id));
94 }
95
96 TrackUuidReference track_ref_at_id (const Track::Uuid &track_id) const
97 {
98 return tracks ().at (get_track_index (track_id));
99 }
100
104 auto track_count () const { return tracks_.size (); }
105
109 bool contains (const Track::Uuid &track_id) const;
110
114 void add_track (const TrackUuidReference &track_id);
115
119 void insert_track (const TrackUuidReference &track_id, int pos);
120
124 void remove_track (const Track::Uuid &track_id);
125
129 void move_track (const Track::Uuid &track_id, int pos);
130
134 void clear ();
135
139 auto get_track_span () const { return TrackSpan{ tracks_ }; }
140
144 const std::vector<TrackUuidReference> &tracks () const { return tracks_; }
145
149 TrackRegistry &get_track_registry () const { return track_registry_; }
150
154 void set_track_expanded (const Track::Uuid &track_id, bool expanded);
155
159 bool get_track_expanded (const Track::Uuid &track_id) const;
160
164 void
165 set_folder_parent (const Track::Uuid &child_id, const Track::Uuid &parent_id);
166
170 std::optional<Track::Uuid>
171 get_folder_parent (const Track::Uuid &child_id) const;
172
176 void remove_folder_parent (const Track::Uuid &child_id);
177
181 bool is_track_foldable (const Track::Uuid &track_id) const;
182
186 size_t get_child_count (const Track::Uuid &parent_id) const;
187
191 size_t get_last_child_index (const Track::Uuid &parent_id) const;
192
193private:
194 // ========================================================================
195 // Serialization
196 // ========================================================================
197
198 static constexpr auto kTracksKey = "tracks"sv;
199 static constexpr auto kExpandedMapKey = "expandedMap"sv;
200 static constexpr auto kFolderParentMapKey = "folderParentMap"sv;
201 friend void to_json (nlohmann::json &j, const TrackCollection &collection);
202 friend void from_json (const nlohmann::json &j, TrackCollection &collection);
203
204private:
205 TrackRegistry &track_registry_;
206 std::vector<TrackUuidReference> tracks_;
207
211 std::unordered_map<Track::Uuid, bool> track_expanded_;
212
218 std::unordered_map<Track::Uuid, Track::Uuid> folder_parent_;
219};
220
221} // namespace zrythm::structure::tracks
void clear()
Clear all tracks from the collection.
auto get_track_index(const Track::Uuid &track_id) const
Get the index of the track with the given UUID.
auto track_count() const
Get the number of tracks in the collection.
TrackRegistry & get_track_registry() const
Get the track registry.
void move_track(const Track::Uuid &track_id, int pos)
Move a track from one position to another.
void add_track(const TrackUuidReference &track_id)
Add a track to the collection.
bool get_track_expanded(const Track::Uuid &track_id) const
Get the expanded state of a foldable track.
auto get_track_span() const
Get a span view of all tracks.
std::optional< Track::Uuid > get_folder_parent(const Track::Uuid &child_id) const
Get the folder parent for a track.
size_t get_last_child_index(const Track::Uuid &parent_id) const
Get the last child index for a foldable track.
TrackUuidReference get_track_ref_at_index(size_t index) const
Get the track reference at the given index.
bool is_track_foldable(const Track::Uuid &track_id) const
Check if a track is foldable.
bool contains(const Track::Uuid &track_id) const
Check if the collection contains a track with the given UUID.
size_t get_child_count(const Track::Uuid &parent_id) const
Get the number of children for a foldable track.
void set_track_expanded(const Track::Uuid &track_id, bool expanded)
Set the expanded state of a foldable track.
void remove_folder_parent(const Track::Uuid &child_id)
Remove the folder parent for a track.
void insert_track(const TrackUuidReference &track_id, int pos)
Insert a track at the given position.
void set_folder_parent(const Track::Uuid &child_id, const Track::Uuid &parent_id)
Set the folder parent for a track.
void remove_track(const Track::Uuid &track_id)
Remove a track from the collection.
TrackPtrVariant get_track_at_index(size_t index) const
Get the track at the given index.
const std::vector< TrackUuidReference > & tracks() const
Get the underlying tracks vector.
Track span that offers helper methods on a range of tracks.
Definition track_span.h:14
Represents a track in the project.
Definition track.h:54