-
Notifications
You must be signed in to change notification settings - Fork 0
/
nodegraph.h
353 lines (274 loc) · 10 KB
/
nodegraph.h
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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
#pragma once
#include <QtGui>
#include <QtOpenGL>
#include "node.h"
#include "vstnode.h"
#include "mixernode.h"
#include "nodegroup.h"
namespace En
{
class RootGraphicsItem;
class NodeGraphicsItem;
class NodeConnectionArrow;
class NodeGraphEditor;
class RootGraphicsItem : public QGraphicsItem
{
public:
QRectF boundingRect() const
{
return QRectF(0, 0, 0, 0);
}
void paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget)
{
Q_UNUSED(painter);
Q_UNUSED(option);
Q_UNUSED(widget);
// do nothing;
}
};
class NodeConnectionArrow;
class NodeGraphicsItem : public QGraphicsItem
{
Node* m_node; // not owned!
QSet<NodeConnectionArrow*> m_connectionArrows;
bool m_isOutputNode;
static constexpr qreal s_selectedPadding = 5.;
public:
NodeGraphicsItem(Node* node);
Node* node() const
{
return m_node;
}
QRectF nodeRect() const
{
return QRectF(QPointF(-100, -20), QSizeF(200, 40));
}
QRectF selectedNodeRect() const
{
const qreal p = s_selectedPadding;
QRectF r = nodeRect();
r.adjust(-p, -p, p, p);
return r;
}
QRectF boundingRect() const
{
return selectedNodeRect();
}
void paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget);
void addConnectionArrow(NodeConnectionArrow* arrow);
void removeConnectionArrow(NodeConnectionArrow* arrow);
bool isOutputNode() const
{
return m_isOutputNode;
}
void setIsOutputNode(bool value)
{
m_isOutputNode = value;
update();
}
protected:
virtual void mousePressEvent(QGraphicsSceneMouseEvent *event);
virtual void mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event);
virtual void contextMenuEvent(QGraphicsSceneContextMenuEvent *event);
virtual QVariant itemChange(GraphicsItemChange change, const QVariant &value)
{
if (change == ItemPositionChange) {
// value is the new position.
QPointF newPos = value.toPointF();
m_node->setPosition(newPos);
updateConnectionArrows();
}
return QGraphicsItem::itemChange(change, value);
}
private:
void updateConnectionArrows() const;
};
class NodeConnectionArrow: public QGraphicsLineItem {
Node* m_node;
Node* m_inputNode;
QGraphicsPolygonItem* m_arrowhead;
QGraphicsEllipseItem* m_arrowtail;
public:
NodeConnectionArrow(Node* node, Node* inputNode)
: m_node(node)
, m_inputNode(inputNode)
, m_arrowhead(0)
, m_arrowtail(0) {
QPen pen(this->pen());
pen.setWidth(2);
setPen(pen);
QPolygonF polygon;
polygon << QPointF(0, 0) << QPointF(-12, 6) << /*QPointF(10, 0) <<*/ QPointF(-12, -6);
m_arrowhead = new QGraphicsPolygonItem(polygon);
m_arrowhead->setPen(Qt::NoPen);
QBrush brush(m_arrowhead->brush());
brush.setStyle(Qt::SolidPattern);
m_arrowhead->setBrush(brush);
m_arrowhead->setParentItem(this);
m_arrowtail = new QGraphicsEllipseItem(-5, -5, 10, 10);
m_arrowtail->setPen(Qt::NoPen);
m_arrowtail->setBrush(brush);
m_arrowtail->setParentItem(this);
updatePositions();
}
void updatePositions() {
// FIXME: these offsets are hardcoded...
QLineF arrowLine(m_inputNode->position() + QPointF(0, 20), m_node->position() + QPointF(0, -20));
qreal angle = arrowLine.angle();
QLineF arrowBodyLine(arrowLine);
arrowBodyLine.setLength(arrowBodyLine.length() - 5); // shave a bit off the line so we don't overdraw at the tip of the arrowhead.
setLine(arrowBodyLine);
m_arrowhead->setPos(arrowLine.p2());
m_arrowhead->setRotation(-angle);
m_arrowtail->setPos(arrowLine.p1());
}
Node* node() const
{
return m_node;
}
Node* inputNode() const
{
return m_inputNode;
}
};
class NodeGraphEditor: public QGraphicsView {
Q_OBJECT
NodeGroup* m_nodeGroup; // not owned
QGraphicsScene* m_scene;
QGraphicsItem* m_rootItem;
QMap<Node*, NodeGraphicsItem*> m_nodeItemMap;
QMap<Node*, QSet<NodeConnectionArrow*> > m_nodeInputArrows;
QPointer<Node> m_currentOutputNode;
public:
NodeGraphEditor(NodeGroup* nodeGroup, QWidget* parent = 0)
: QGraphicsView(parent)
, m_nodeGroup(nodeGroup)
, m_scene(0)
, m_rootItem(0)
{
setFocusPolicy(Qt::StrongFocus);
// Enable multisampling for AA in the graph
// arse. enabling this causes us to somehow lose our graphics buffer when fiddling with CurveCM's interface
if (false) {
QGLFormat glFormat;
glFormat.setSampleBuffers(true);
glFormat.setSamples(4);
setViewport(new QGLWidget(glFormat));
}
setRenderHint(QPainter::Antialiasing);
setDragMode(QGraphicsView::RubberBandDrag);
m_scene = new QGraphicsScene(this);
m_scene->setSceneRect(-1000, -1000, 2000, 2000);
setScene(m_scene);
m_rootItem = new RootGraphicsItem;
scene()->addItem(m_rootItem);
// this code /will/ move at some point
for (int i = 0; i < m_nodeGroup->numChildNodes(); ++i) {
nodeAdded(m_nodeGroup->childNode(i));
}
// dodgy, but it'll do for now
for (int i = 0; i < m_nodeGroup->numChildNodes(); ++i) {
nodeInputsChanged(m_nodeGroup->childNode(i));
#if 0
Node* node = m_nodeGroup->childNode(i);
MixerNode* mixerNode = dynamic_cast<MixerNode*>(node);
if (mixerNode) {
for (int i = 0; i < mixerNode->numInputs(); ++i) {
Node* inputNode = mixerNode->input(i);
NodeConnectionArrow* arrow = new NodeConnectionArrow(node, inputNode);
m_nodeItemMap[node]->addConnectionArrow(arrow);
m_nodeItemMap[inputNode]->addConnectionArrow(arrow);
arrow->setParentItem(m_rootItem);
}
}
VstNode* vstNode = dynamic_cast<VstNode*>(node);
if (vstNode) {
Node* inputNode = vstNode->input();
if (inputNode) {
NodeConnectionArrow* arrow = new NodeConnectionArrow(node, inputNode);
m_nodeItemMap[node]->addConnectionArrow(arrow);
m_nodeItemMap[inputNode]->addConnectionArrow(arrow);
arrow->setParentItem(m_rootItem);
}
}
#endif
}
connect(m_nodeGroup, SIGNAL(nodeAdded(Node*)), SLOT(nodeAdded(Node*)));
connect(m_nodeGroup, SIGNAL(nodePreRemoved(Node*)), SLOT(nodePreRemoved(Node*)));
connect(m_nodeGroup, SIGNAL(nodeInputsChanged(Node*)), SLOT(nodeInputsChanged(Node*)));
outputNodeChanged();
connect(m_nodeGroup, SIGNAL(outputNodeChanged(Node*)), SLOT(outputNodeChanged()));
QShortcut* shortcut = 0;
shortcut = new QShortcut(Qt::Key_Tab, this, SLOT(launchNodeCreationDialog()));
shortcut = new QShortcut(Qt::Key_Delete, this, SLOT(deleteSelectedNodes()));
shortcut = new QShortcut(Qt::Key_O, this, SLOT(setSelectedNodeAsOutput()));
}
protected slots:
void nodeAdded(Node* node)
{
//qDebug() << "NODE ADDED" << node->displayLabel();
NodeGraphicsItem* item = new NodeGraphicsItem(node);
item->setParentItem(m_rootItem);
m_nodeItemMap[node] = item;
//nodeInputsChanged(node);
}
void nodePreRemoved(Node* node)
{
// FIXME: don't assume the connections have already been removed.
// we need to do that before deleting this object!
delete m_nodeItemMap[node];
}
void nodeInputsChanged(Node* node)
{
// FIXME: do nothing if the node isn't in the graph yet - we'll get to that after nodeAdded!
//qDebug() << "rejigging node inputs for node" << node->displayLabel();
// delete all current arrows and re-add
// this could probably use a re-think. contain the madness!
// so bored of this code, apologies that it's retarded.
Q_FOREACH (NodeConnectionArrow* arrow, m_nodeInputArrows[node]) {
m_nodeItemMap[arrow->inputNode()]->removeConnectionArrow(arrow);
m_nodeItemMap[node]->removeConnectionArrow(arrow);
delete arrow;
}
// clear set of dangling pointers
m_nodeInputArrows[node].clear();
MixerNode* mixerNode = dynamic_cast<MixerNode*>(node);
if (mixerNode) {
for (unsigned i = 0; i < mixerNode->numInputs(); ++i) {
Node* inputNode = mixerNode->input(i);
NodeConnectionArrow* arrow = new NodeConnectionArrow(node, inputNode);
m_nodeItemMap[node]->addConnectionArrow(arrow);
m_nodeItemMap[inputNode]->addConnectionArrow(arrow);
m_nodeInputArrows[node] << arrow;
arrow->setParentItem(m_rootItem);
}
}
VstNode* vstNode = dynamic_cast<VstNode*>(node);
if (vstNode) {
Node* inputNode = vstNode->input();
if (inputNode) {
NodeConnectionArrow* arrow = new NodeConnectionArrow(node, inputNode);
m_nodeItemMap[node]->addConnectionArrow(arrow);
m_nodeItemMap[inputNode]->addConnectionArrow(arrow);
m_nodeInputArrows[node] << arrow;
arrow->setParentItem(m_rootItem);
}
}
}
void outputNodeChanged();
QList<Node*> selectedNodes() const
{
QList<Node*> ret;
Q_FOREACH (QGraphicsItem* item, scene()->selectedItems()) {
NodeGraphicsItem* nodeItem = qgraphicsitem_cast<NodeGraphicsItem*>(item);
if (nodeItem) {
ret << nodeItem->node();
}
}
return ret;
}
void deleteSelectedNodes();
void setSelectedNodeAsOutput();
void launchNodeCreationDialog();
};
}