-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcanvas.cpp
More file actions
343 lines (283 loc) · 10.9 KB
/
canvas.cpp
File metadata and controls
343 lines (283 loc) · 10.9 KB
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
#include "Canvas.h"
#include <QGraphicsRectItem>
#include <QMouseEvent>
#include <QTimeLine>
#include <cmath>
#ifdef QT_DEBUG
#include <QDebug>
#include <QGraphicsDropShadowEffect>
#include <QGraphicsSceneContextMenuEvent>
#include <QMenu>
#endif
#include "NodeScene.h"
#include "GraphicsSocket.h"
#include "Edge.h"
#include "Node.h"
#include "GraphicsEdge.h"
#include "SceneNode.h"
Canvas::Canvas(QWidget *parent) : QGraphicsView(parent)
{
setRenderHints(QPainter::Antialiasing | QPainter::HighQualityAntialiasing | QPainter::TextAntialiasing | QPainter::SmoothPixmapTransform);
setViewportUpdateMode(QGraphicsView::FullViewportUpdate);
// setViewportUpdateMode(QGraphicsView::BoundingRectViewportUpdate);
setStyleSheet("border: 0");
setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
setCacheMode(QGraphicsView::CacheBackground);
installEventFilter(this);
// setMouseTracking(true);
// Grimey!
zoomInFactor = 1.25;
zoomClamp = false;
zoom = 10;
zoomStep = 1;
zoomRange = {5, 16};
// setTransformationAnchor(QGraphicsView::AnchorUnderMouse);
nodeOp = NodeOperation::NoOp;
// menu = QMenu("Context Menu", this);
menu.setContextMenuPolicy(Qt::ActionsContextMenu);
menu.setStyleSheet(
"QMenu { background-color: #1A1A1A; color: #EEE; padding: 0; margin: 0; }"
"QMenu::item { background-color: #1A1A1A; padding: 6px 8px; margin: 0; }"
"QMenu::item:selected { background-color: #3498db; color: #EEE; padding: 6px 8px; margin: 0; }"
"QMenu::item : disabled { color: #555; }"
);
}
Canvas::~Canvas()
{
// delete graphicsScene;
}
void Canvas::setNodeScene(NodeScene *ns)
{
nodeScene = ns;
setScene(nodeScene->graphicsScene);
auto masterNode = new OutputNode;
ns->addNode(masterNode);
}
int Canvas::getItemAtClick(QMouseEvent *event)
{
// Get the entity currently under the cursor
auto item = itemAt(event->pos());
// https://doc.qt.io/archives/qt-4.8/qgraphicsitem.html#type
// https://stackoverflow.com/a/23129179
// We are looking for sockets whose type is UserType + 2
// Nodes are UserType + 1 and Edges are UserType + 3
if (item) return item->type();
return -1;
}
void Canvas::edgeDragStart(GraphicsSocket *item)
{
auto startingSocket = item->parent->getModel()
->getSocketByIndex(static_cast<Type>(item->getSocketType()), item->getIndex());
// We are creating if we are dragging from an output socket or an input socket that has no edges
bool creating = item->getSocketType() == (int) Type::OUTO || (item->getSocketType() == (int) Type::INTO && !startingSocket->hasEdge());
if (creating) {
nodeOp = NodeOperation::EdgeDrag;
qDebug() << "Starting From" << item->getSocketType() << item->getIndex() << startingSocket->getParent()->getTitle() << startingSocket->getParent()->id;
tempDraggingEdge = new Edge(startingSocket, nullptr);
nodeScene->addEdge(tempDraggingEdge);
}
else {
nodeOp = NodeOperation::EdgeRemove;
qDebug() << "Removing" << item->getSocketType() << item->getIndex() << startingSocket->getParent()->getTitle() << startingSocket->getParent()->id;
tempDraggingEdge = startingSocket->getEdges().first();
}
}
void Canvas::edgeDragEnd(QMouseEvent *event)
{
if (getItemAtClick(event) == QGraphicsItem::UserType + 2) {
if (nodeOp == NodeOperation::EdgeDrag) {
nodeOp = NodeOperation::NoOp;
auto graphicsSocket = qgraphicsitem_cast<GraphicsSocket*>(itemAt(event->pos()));
auto targetSocket = graphicsSocket->parent->getModel()
->getSocketByIndex(static_cast<Type>(graphicsSocket->getSocketType()), graphicsSocket->getIndex());
nodeScene->connectEdgeToSocket(tempDraggingEdge, targetSocket);
qDebug() << "Connected to" << graphicsSocket->getSocketType() << graphicsSocket->getIndex() << targetSocket->getParent()->getTitle() << targetSocket->getParent()->id;
emit nodeConnected();
}
} else {
if (nodeOp == NodeOperation::EdgeDrag) {
nodeOp = NodeOperation::NoOp;
nodeScene->removeEdge(tempDraggingEdge);
} else if (nodeOp == NodeOperation::EdgeRemove) {
nodeOp = NodeOperation::NoOp;
nodeScene->removeEdge(tempDraggingEdge);
}
}
}
void Canvas::clearNodeScene()
{
return;
}
void Canvas::mousePressEvent(QMouseEvent *event)
{
if (event->button() == Qt::LeftButton) {
if (getItemAtClick(event) == QGraphicsItem::UserType + 2) {
if (nodeOp == NodeOperation::NoOp) {
edgeDragStart(qgraphicsitem_cast<GraphicsSocket*>(itemAt(event->pos())));
}
// return;
}
}
QGraphicsView::mousePressEvent(event);
}
void Canvas::mouseReleaseEvent(QMouseEvent *event)
{
if (event->button() == Qt::LeftButton) {
edgeDragEnd(event);
}
if (event->button() == Qt::MiddleButton) {
setDragMode(QGraphicsView::NoDrag);
}
QGraphicsView::mouseReleaseEvent(event);
}
void Canvas::mouseMoveEvent(QMouseEvent *event)
{
if (nodeOp == NodeOperation::EdgeDrag || nodeOp == NodeOperation::EdgeRemove) {
tempDraggingEdge->getRenderer()->setDest(mapToScene(event->pos()).toPoint());
tempDraggingEdge->getRenderer()->update();
}
QGraphicsView::mouseMoveEvent(event);
}
void Canvas::contextMenuEvent(QContextMenuEvent *event)
{
menu.setContextMenuPolicy(Qt::ActionsContextMenu);
menu.setStyleSheet(
"QMenu { background-color: #1A1A1A; color: #EEE; padding: 0; margin: 0; }"
"QMenu::item { background-color: #1A1A1A; padding: 6px 8px; margin: 0; }"
"QMenu::item:selected { background-color: #3498db; color: #EEE; padding: 6px 8px; margin: 0; }"
"QMenu::item : disabled { color: #555; }"
);
// QGraphicsDropShadowEffect dropShadowEffect;
// dropShadowEffect.setBlurRadius(20);
// dropShadowEffect.setYOffset(4);
// dropShadowEffect.setXOffset(0);
// dropShadowEffect.setColor(QColor(0, 0, 0, 32));
// menu.setGraphicsEffect(&dropShadowEffect);
QAction floatNode("Float", this);
QAction vec3Node("Vec3", this);
QAction timeNode("Time", this);
QAction sphereNode("Sphere", this);
QAction planeNode("Plane", this);
QAction minNode("Min", this);
QAction sinNode("Sin", this);
QAction outNode("Out", this);
QPoint pos = mapToScene(event->pos()).toPoint();
connect(&floatNode, &QAction::triggered, this, [&]() {
auto node = new FloatNode;
node->setPosition(pos.x(), pos.y());
nodeScene->addNode(node);
});
connect(&vec3Node, &QAction::triggered, this, [&]() {
auto node = new Vec3Node;
node->setPosition(pos.x(), pos.y());
nodeScene->addNode(node);
});
connect(&timeNode, &QAction::triggered, this, [&]() {
auto node = new TimeNode;
node->setPosition(pos.x(), pos.y());
nodeScene->addNode(node);
});
connect(&sphereNode, &QAction::triggered, this, [&]() {
auto node = new SphereNode;
node->setPosition(pos.x(), pos.y());
nodeScene->addNode(node);
});
connect(&planeNode, &QAction::triggered, this, [&]() {
auto node = new PlaneNode;
node->setPosition(pos.x(), pos.y());
nodeScene->addNode(node);
});
connect(&minNode, &QAction::triggered, this, [&]() {
auto node = new MinNode;
node->setPosition(pos.x(), pos.y());
nodeScene->addNode(node);
});
connect(&sinNode, &QAction::triggered, this, [&]() {
auto node = new SinNode;
node->setPosition(pos.x(), pos.y());
nodeScene->addNode(node);
});
// connect(&outNode, &QAction::triggered, this, [&]() {
// auto node = new OutputNode;
// node->setPosition(pos.x(), pos.y());
// nodeScene->addNode(node);
// });
menu.addAction(&floatNode);
menu.addAction(&vec3Node);
menu.addAction(&timeNode);
menu.addAction(&sphereNode);
menu.addAction(&planeNode);
menu.addAction(&minNode);
menu.addAction(&sinNode);
// menu.addAction(&outNode);
// for (auto node : qAsConst(nodeScene->registeredNodes.keys())) {
// QAction action;
// action.setText(node->);
// action->setData(index++);
// menu.addAction(&action);
// }
menu.exec(event->globalPos());
}
void Canvas::scalingTime(qreal x)
{
qreal factor = 1.0 + qreal(_numScheduledScalings) / 300.0;
scale(factor, factor);
}
void Canvas::animFinished() {
if (_numScheduledScalings > 0) _numScheduledScalings--;
else _numScheduledScalings++;
sender()->~QObject();
}
void Canvas::wheelEvent(QWheelEvent *event)
{
auto angleDelta = event->angleDelta();
#ifdef SMOOTHZOOM
int numDegrees = angleDelta.y() / 8;
int numSteps = numDegrees / 15; // see QWheelEvent documentation
_numScheduledScalings += numSteps;
if (_numScheduledScalings * numSteps < 0) // if user moved the wheel in another direction, we reset previously scheduled scalings
_numScheduledScalings = numSteps;
QTimeLine *anim = new QTimeLine(200, this);
anim->setUpdateInterval(20);
connect(anim, SIGNAL(valueChanged(qreal)), SLOT(scalingTime(qreal)));
connect(anim, SIGNAL(finished()), SLOT(animFinished()));
anim->start();
#else
float zoomOutFactor = 1 / zoomInFactor;
qreal zoomFactor;
if (angleDelta.y() > 0) {
zoomFactor = zoomInFactor;
zoom += zoomStep;
} else {
zoomFactor = zoomOutFactor;
zoom -= zoomStep;
}
bool clamped = false;
if (zoom < zoomRange[0]) { zoom= zoomRange[0]; clamped = true; }
if (zoom > zoomRange[1]) { zoom= zoomRange[1]; clamped = true; }
if (!clamped) scale(zoomFactor, zoomFactor);
#endif
}
// Qt's graphics view wants to drag with left click only by default, override this
bool Canvas::eventFilter(QObject *object, QEvent *event) {
Q_UNUSED(object);
if (event->type() == QEvent::MouseButtonPress) {
QMouseEvent* mouseEvent = static_cast<QMouseEvent*>(event);
if (mouseEvent->button() == Qt::MiddleButton) {
// Temporarly enable dragging mode
this->setDragMode(QGraphicsView::ScrollHandDrag);
// Emit a left mouse click (the default button for the drag mode)
QMouseEvent* pressEvent = new QMouseEvent(QEvent::GraphicsSceneMousePress,
mouseEvent->pos(), Qt::MouseButton::LeftButton,
Qt::MouseButton::LeftButton,
Qt::KeyboardModifier::NoModifier);
this->mousePressEvent(pressEvent);
}
else if (event->type() == QEvent::MouseButtonRelease) {
// Disable drag mode if dragging is finished
this->setDragMode(QGraphicsView::DragMode::NoDrag);
}
}
return false;
}