Zrythm v2.0.0-alpha.1
a highly automated and intuitive digital audio workstation
Loading...
Searching...
No Matches
port_connections_manager.h
1// SPDX-FileCopyrightText: © 2021, 2024-2025 Alexandros Theodotou <alex@zrythm.org>
2// SPDX-License-Identifier: LicenseRef-ZrythmLicense
3
4#pragma once
5
6#include "dsp/port_connection.h"
7#include "utils/icloneable.h"
8#include "utils/qt.h"
9
10#include <nlohmann/json_fwd.hpp>
11
12namespace zrythm::dsp
13{
14
18class PortConnectionsManager : public QObject
19{
20 Q_OBJECT
21 QML_ELEMENT
22
23public:
24 using PortConnection = dsp::PortConnection;
25 using ConnectionsVector = std::vector<PortConnection *>;
26
27 explicit PortConnectionsManager (QObject * parent = nullptr);
28
29private:
36 using ConnectionHashTable = std::unordered_map<PortUuid, ConnectionsVector>;
37
38 static const auto &
39 connection_ref_predicate (const utils::QObjectUniquePtr<PortConnection> &conn)
40 {
41 return *conn;
42 }
43
44public:
51
64 int
65 get_sources_or_dests (ConnectionsVector * arr, const PortUuid &id, bool sources)
66 const;
67
68 int get_sources (ConnectionsVector * arr, const PortUuid &id) const
69 {
70 return get_sources_or_dests (arr, id, true);
71 }
72
73 int get_dests (ConnectionsVector * arr, const PortUuid &id) const
74 {
75 return get_sources_or_dests (arr, id, false);
76 }
77
78 int get_num_sources (const PortUuid &id) const
79 {
80 return get_sources (nullptr, id);
81 }
82 int get_num_dests (const PortUuid &id) const
83 {
84 return get_dests (nullptr, id);
85 }
86
87 auto get_connection_count () const noexcept { return connections_.size (); }
88
102 ConnectionsVector * arr,
103 const PortUuid &id,
104 bool sources) const;
105
114 PortConnection * get_source_or_dest (const PortUuid &id, bool sources) const;
115
123 PortConnection *
124 get_connection (const PortUuid &src, const PortUuid &dest) const;
125
126 bool connection_exists (const PortUuid &src, const PortUuid &dest) const
127 {
128 return get_connection (src, dest) != nullptr;
129 }
130
136 bool
137 update_connection (const PortConnection &before, const PortConnection &after);
138
139 bool update_connection (
140 const PortUuid &src,
141 const PortUuid &dest,
142 float multiplier,
143 bool locked,
144 bool enabled);
145
154 const PortConnection * add_connection (
155 const PortUuid &src,
156 const PortUuid &dest,
157 float multiplier,
158 bool locked,
159 bool enabled);
160
164 const PortConnection *
165 add_default_connection (const PortUuid &src, const PortUuid &dest, bool locked)
166 {
167 return add_connection (src, dest, 1.0f, locked, true);
168 }
169
170 const PortConnection * add_connection (const PortConnection &conn)
171 {
172 return add_connection (
173 conn.src_id_, conn.dest_id_, conn.multiplier_, conn.locked_,
174 conn.enabled_);
175 }
176
184 bool remove_connection (const PortUuid &src, const PortUuid &dest);
185
191 void remove_all_connections (const PortUuid &pi);
192
198 void reset_connections_from_other (const PortConnectionsManager * other);
199
200 bool contains_connection (const PortConnection &conn) const;
201
202 void print_ht (const ConnectionHashTable &ht);
203
204 void print () const;
205
206 friend void init_from (
207 PortConnectionsManager &obj,
208 const PortConnectionsManager &other,
209 utils::ObjectCloneType clone_type);
210
211 void clear_all ()
212 {
213 connections_.clear ();
215 }
216
217 auto &connections () const { return connections_; }
218
219private:
220 friend void to_json (nlohmann::json &j, const PortConnectionsManager &pcm);
221 friend void from_json (const nlohmann::json &j, PortConnectionsManager &pcm);
222
223 void add_or_replace_connection (
224 ConnectionHashTable &ht,
225 const PortUuid &id,
226 const PortConnection &conn);
227
228 void remove_connection (size_t idx);
229
230private:
232 std::vector<utils::QObjectUniquePtr<PortConnection>> connections_;
233
240 ConnectionHashTable src_ht_;
241
248 ConnectionHashTable dest_ht_;
249};
250
251} // namespace zrythm::dsp
A connection between two ports.
bool enabled_
Whether the connection is enabled.
float multiplier_
Multiplier to apply, where applicable.
bool locked_
Whether the connection can be removed or the multiplier edited by the user.
PortConnection * get_connection(const PortUuid &src, const PortUuid &dest) const
void regenerate_hashtables()
Regenerates the hash tables.
void reset_connections_from_other(const PortConnectionsManager *other)
Removes all connections from this.
bool update_connection(const PortConnection &before, const PortConnection &after)
Replaces the given before connection with after.
int get_sources_or_dests(ConnectionsVector *arr, const PortUuid &id, bool sources) const
Adds the sources/destinations of id in the given array.
PortConnection * get_source_or_dest(const PortUuid &id, bool sources) const
Wrapper over get_sources_or_dests() that returns the first connection.
bool remove_connection(const PortUuid &src, const PortUuid &dest)
Removes the connection for the given ports if it exists.
const PortConnection * add_default_connection(const PortUuid &src, const PortUuid &dest, bool locked)
Overload for default settings (multiplier = 1.0, enabled = true).
int get_unlocked_sources_or_dests(ConnectionsVector *arr, const PortUuid &id, bool sources) const
Adds the sources/destinations of id in the given array.
void remove_all_connections(const PortUuid &pi)
Disconnect all sources and dests of the given port identifier.
const PortConnection * add_connection(const PortUuid &src, const PortUuid &dest, float multiplier, bool locked, bool enabled)
Stores the connection for the given ports if it doesn't exist, otherwise updates the existing connect...
A unique pointer for QObject objects that also works with QObject-based ownership.
Definition qt.h:36