Blender V5.3
strip_connect.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2024 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include "BLI_listbase.hh"
10
11#include "DNA_sequence_types.h"
12
13#include "SEQ_connect.hh"
14
15namespace blender::seq {
16
17void connections_duplicate(ListBaseT<StripConnection> *connections_dst,
18 ListBaseT<StripConnection> *connections_src)
19{
20 for (StripConnection &con : *connections_src) {
21 StripConnection *con_duplicate = MEM_new<StripConnection>(__func__, con);
22 BLI_addtail(connections_dst, con_duplicate);
23 }
24}
25
26static void connection_remove(Strip *strip, StripConnection &con_strip)
27{
28 Strip *other = con_strip.strip_ref;
29 /* Remove the `StripConnection` from the `other` `connections` list that points to `strip`. */
30 for (StripConnection &con_other : other->connections.items_mutable()) {
31 if (con_other.strip_ref == strip) {
32 BLI_remlink(&other->connections, &con_other);
33 MEM_delete(&con_other);
34 }
35 }
36 /* Now clear `connections` for `strip` itself. */
37 BLI_remlink(&strip->connections, &con_strip);
38 MEM_delete(&con_strip);
39}
40
41bool disconnect(Strip *strip)
42{
43 if (strip == nullptr || strip->connections.is_empty()) {
44 return false;
45 }
46 for (StripConnection &con_strip : strip->connections.items_mutable()) {
47 connection_remove(strip, con_strip);
48 }
49
50 return true;
51}
52
54{
55 bool changed = false;
56 for (Strip *strip : strip_list) {
57 changed |= disconnect(strip);
58 }
59
60 return changed;
61}
62
64{
65 if (strip == nullptr) {
66 return;
67 }
68 for (StripConnection &con_strip : strip->connections.items_mutable()) {
69 Strip *other = con_strip.strip_ref;
70 bool is_one_way = true;
71 for (StripConnection &con_other : other->connections) {
72 if (con_other.strip_ref == strip) {
73 /* The `other` sequence has a bidirectional connection with `strip`. */
74 is_one_way = false;
75 break;
76 }
77 }
78 if (is_one_way) {
79 BLI_remlink(&strip->connections, &con_strip);
80 MEM_delete(&con_strip);
81 }
82 }
83}
84
86{
87 for (Strip *strip : strip_list) {
88 for (StripConnection &con_strip : strip->connections.items_mutable()) {
89 if (!strip_list.contains(con_strip.strip_ref)) {
90 connection_remove(strip, con_strip);
91 }
92 }
93 }
94}
95
96void connect(Strip *strip1, Strip *strip2)
97{
98 if (strip1 == nullptr || strip2 == nullptr) {
99 return;
100 }
101 VectorSet<Strip *> strip_list;
102 strip_list.add(strip1);
103 strip_list.add(strip2);
104
105 connect(strip_list);
106}
107
109{
110 strip_list.remove_if([&](Strip *strip) { return strip == nullptr; });
111
112 for (Strip *strip1 : strip_list) {
113 disconnect(strip1);
114 for (Strip *strip2 : strip_list) {
115 if (strip1 == strip2) {
116 continue;
117 }
118 StripConnection *con = MEM_new<StripConnection>("stripconnection");
119 con->strip_ref = strip2;
120 BLI_addtail(&strip1->connections, con);
121 }
122 }
123}
124
126{
127 VectorSet<Strip *> connections;
128 if (strip != nullptr) {
129 for (StripConnection &con : strip->connections) {
130 connections.add(con.strip_ref);
131 }
132 }
133 return connections;
134}
135
136bool is_strip_connected(const Strip *strip)
137{
138 if (strip == nullptr) {
139 return false;
140 }
141 return !strip->connections.is_empty();
142}
143
145{
146 const int expected_connection_num = strip_list.size() - 1;
147 for (Strip *strip1 : strip_list) {
148 VectorSet<Strip *> connections = connected_strips_get(strip1);
149 int found_connection_num = connections.size();
150 if (found_connection_num != expected_connection_num) {
151 return false;
152 }
153 for (Strip *strip2 : connections) {
154 if (!strip_list.contains(strip2)) {
155 return false;
156 }
157 }
158 }
159 return true;
160}
161
162} // namespace blender::seq
bool add(const Key &key)
int64_t size() const
bool contains(const Key &key) const
int64_t remove_if(Predicate &&predicate)
bool disconnect(Strip *strip)
bool are_strips_connected_together(VectorSet< Strip * > &strip_list)
void connections_duplicate(ListBaseT< StripConnection > *connections_dst, ListBaseT< StripConnection > *connections_src)
void connect(Strip *strip1, Strip *strip2)
void cut_one_way_connections(Strip *strip)
bool is_strip_connected(const Strip *strip)
static void connection_remove(Strip *strip, StripConnection &con_strip)
void cut_outside_connections(const VectorSet< Strip * > &strip_list)
VectorSet< Strip * > connected_strips_get(const Strip *strip)
void BLI_remlink(ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition listbase.cc:138
void BLI_addtail(ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition listbase.cc:118