-
Notifications
You must be signed in to change notification settings - Fork 0
/
connector.cpp
258 lines (216 loc) · 4.75 KB
/
connector.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
// SPDX-License-Identifier: GPL-2.0
#include "connector.hpp"
#include "operator.hpp"
#include "scene.hpp"
#include "edge.hpp"
#include <QBrush>
#include <QCursor>
#include <QGraphicsSceneMouseEvent>
#include <cassert>
static QBrush standard_brush(Qt::black);
static QBrush highlighted_brush(Qt::red);
Connector::Connector(size_t id_, bool output_, Operator *op)
: QGraphicsRectItem(0.0, 0.0, width, height, op)
, id(id_)
, output(output_)
, highlighted(false)
, selected(false)
, parent(nullptr)
{
children.reserve(5); // Heuristics
setBrush(standard_brush);
setAcceptHoverEvents(true);
setAcceptTouchEvents(true);
// Prioritize mouseclicks over those of edges
setZValue(1.0);
setCursor(Qt::CrossCursor);
}
ConnectorDesc Connector::connector_desc()
{
return { op(), is_output()
? ConnectorType::output_connector(id)
: ConnectorType::input_connector(id) };
}
size_t Connector::get_id() const
{
return id;
}
void Connector::set_highlighted(bool highlighted_)
{
if (highlighted == highlighted_)
return;
highlighted = highlighted_;
setBrush(highlighted || selected ? highlighted_brush : standard_brush);
}
void Connector::set_selected(bool selected_)
{
if (selected == selected_)
return;
selected = selected_;
setBrush(highlighted || selected ? highlighted_brush : standard_brush);
}
bool Connector::is_output() const
{
return output;
}
Operator *Connector::op()
{
return static_cast<Operator *>(parentItem());
}
bool Connector::is_empty_buffer() const
{
if (!output && !parent)
return true;
return get_buffer().is_empty();
}
bool Connector::is_complex_buffer() const
{
if (!output && !parent)
return false;
return get_buffer().is_complex();
}
const FFTBuf &Connector::get_buffer() const
{
if (output) {
return op()->get_output_buffer(id);
} else {
assert(parent);
Connector *from = parent->get_connector_from();
assert(from);
return from->op()->get_output_buffer(from->id);
}
}
// Waiting for C++23 deducing this
FFTBuf &Connector::get_buffer()
{
if (output) {
return op()->get_output_buffer(id);
} else {
assert(parent);
Connector *from = parent->get_connector_from();
assert(from);
return from->op()->get_output_buffer(from->id);
}
}
const Operator *Connector::op() const
{
return static_cast<Operator *>(parentItem());
}
OperatorList::view_list &Connector::get_view_list()
{
return view_list;
}
const OperatorList::view_list &Connector::get_view_list() const
{
return view_list;
}
double Connector::y_dist(double y) const
{
return fabs(pos().y() + height / 2.0 - y);
}
QPointF Connector::line_from() const
{
return scenePos() + QPointF(width / 2.0, height / 2.0);
}
void Connector::set_safety_pos(const QPointF &p)
{
safety_pos = p;
}
QPointF Connector::get_safety_pos() const
{
return safety_pos;
}
void Connector::hoverEnterEvent(QGraphicsSceneHoverEvent *)
{
set_highlighted(true);
}
void Connector::hoverLeaveEvent(QGraphicsSceneHoverEvent *)
{
set_highlighted(false);
}
void Connector::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
if (!(event->buttons() & Qt::LeftButton))
return;
static_cast<Scene *>(scene())->connector_clicked(this);
}
bool Connector::has_input_connection() const
{
assert(!output);
return !!parent;
}
void Connector::set_input_connection(Edge *c)
{
assert(!output);
assert(!parent);
parent = c;
}
void Connector::remove_input_connection(Edge *c)
{
assert(!output);
assert(parent == c);
parent = nullptr;
}
void Connector::add_output_connection(Edge *c)
{
assert(output);
children.push_back(c);
}
void Connector::remove_output_connection(Edge *c)
{
assert(output);
auto it = std::find(children.begin(), children.end(), c);
assert(it != children.end());
*it = children.back();
children.pop_back();
}
const Connector *Connector::get_parent() const
{
assert(!output);
if (!parent)
return nullptr;
return parent->get_connector_from();
}
Edge *Connector::get_parent_edge()
{
assert(!output);
return parent;
}
std::vector<Connector *> Connector::get_children() const
{
assert(output);
size_t size = children.size();
std::vector<Connector *> res(size);
for (size_t i = 0; i < size; ++i)
res[i] = children[i]->get_connector_to();
return res;
}
const std::vector<Edge *> &Connector::get_children_edges() const
{
assert(output);
return children;
}
void Connector::remove_edges()
{
if (parent) {
parent->remove();
assert(!parent);
}
// Note that we copy the children list because the edges remove
// themselves from the vector during deletion!
std::vector<Edge *> children_copy = children;
for (Edge *e: children_copy)
e->remove();
}
void Connector::recalculate_edges()
{
if (parent)
parent->recalculate();
for (Edge *e: children)
e->recalculate();
}
void Connector::out_edges_to_json(QJsonArray &out) const
{
for (const Edge *e: children)
out.push_back(e->to_json());
}