-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqdataflowcanvas.h
358 lines (283 loc) · 10.2 KB
/
qdataflowcanvas.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
354
355
356
357
358
/* QDataflowCanvas - a dataflow widget for Qt
* Copyright (C) 2017-2018 Federico Ferri
* Copyright (C) 2018 Kuba Ober
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef QDATAFLOWCANVAS_H
#define QDATAFLOWCANVAS_H
#include <QGraphicsItem>
#include <QGraphicsView>
#include "qdataflowmodel.h"
class QDataflowNode;
class QDataflowInlet;
class QDataflowOutlet;
class QDataflowConnection;
class QDataflowTextCompletion;
class QDataflowNodeTextLabel;
class QDataflowTooltip;
class QGraphicsSceneMouseEvent;
class QMouseEvent;
enum QDataflowItemType {
QDataflowItemTypeNode = QGraphicsItem::UserType + 1,
QDataflowItemTypeConnection = QGraphicsItem::UserType + 2,
QDataflowItemTypeInlet = QGraphicsItem::UserType + 3,
QDataflowItemTypeOutlet = QGraphicsItem::UserType + 4
};
class QDataflowCanvas : public QGraphicsView
{
Q_OBJECT
public:
QDataflowCanvas(QWidget *parent = {});
~QDataflowCanvas() override;
QDataflowModel * model();
void setModel(QDataflowModel *model);
QDataflowNode * node(QDataflowModelNode *node);
QDataflowConnection * connection(QDataflowModelConnection *conn);
QList<QDataflowNode*> selectedNodes();
QList<QDataflowConnection*> selectedConnections();
bool isSomeNodeInEditMode() const;
QDataflowTextCompletion * completion() const {return completion_;}
void setCompletion(QDataflowTextCompletion *completion) {completion_ = completion;}
void raiseItem(QGraphicsItem *item);
bool showIOletTooltips();
void setShowIOletTooltips(bool show);
bool showObjectHoverFeedback();
void setShowObjectHoverFeedback(bool show);
bool showConnectionHoverFeedback();
void setShowConnectionHoverFeedback(bool show);
qreal gridSize();
void setGridSize(qreal sz);
bool drawGrid();
void setDrawGrid(bool draw);
protected:
template<typename T>
T * itemAtT(const QPointF &point);
void drawBackground(QPainter *painter, const QRectF &rect) override;
void mouseDoubleClickEvent(QMouseEvent *event) override;
void keyPressEvent(QKeyEvent *event) override;
protected Q_SLOTS:
void itemTextEditorTextChange();
void onNodeAdded(QDataflowModelNode *mdlnode);
void onNodeRemoved(QDataflowModelNode *mdlnode);
void onNodeValidChanged(QDataflowModelNode *mdlnode, bool valid);
void onNodePosChanged(QDataflowModelNode *mdlnode, const QPoint &pos);
void onNodeTextChanged(QDataflowModelNode *mdlnode, const QString &text);
void onNodeInletCountChanged(QDataflowModelNode *mdlnode, int count);
void onNodeOutletCountChanged(QDataflowModelNode *mdlnode, int count);
void onConnectionAdded(QDataflowModelConnection *mdlconn);
void onConnectionRemoved(QDataflowModelConnection *mdlconn);
friend class QDataflowNode;
friend class QDataflowIOlet;
friend class QDataflowInlet;
friend class QDataflowOutlet;
friend class QDataflowConnection;
private:
QDataflowModel *model_;
QDataflowTextCompletion *completion_;
QSet<QDataflowNode*> ownedNodes_;
QSet<QDataflowConnection*> ownedConnections_;
QMap<QDataflowModelNode*, QDataflowNode*> nodes_;
QMap<QDataflowModelConnection*, QDataflowConnection*> connections_;
bool showIOletsTooltips_;
bool showObjectHoverFeedback_;
bool showConnectionHoverFeedback_;
qreal gridSize_;
bool drawGrid_;
};
class QDataflowNode : public QGraphicsItem
{
protected:
QDataflowNode(QDataflowCanvas *canvas_, QDataflowModelNode *modelNode);
public:
QDataflowModelNode * modelNode() const;
QDataflowInlet * inlet(int index) const {return inlets_.at(index);}
int inletCount() const {return inlets_.size();}
void setInletCount(int count, bool skipAdjust = false);
QDataflowOutlet * outlet(int index) const {return outlets_.at(index);}
int outletCount() const {return outlets_.size();}
void setOutletCount(int count, bool skipAdjust = false);
int type() const override {return QDataflowItemTypeNode;}
void setText(const QString &text);
QString text() const;
void setValid(bool valid);
bool isValid() const;
void adjustConnections() const;
QRectF boundingRect() const override;
void adjust();
QDataflowCanvas * canvas() const {return canvas_;}
qreal ioletWidth() const {return 10;}
qreal ioletHeight() const {return 4;}
qreal ioletSpacing() const {return 13;}
qreal inletsWidth() const;
qreal outletsWidth() const;
QPen objectPen() const;
QBrush objectBrush() const;
QBrush headerBrush() const;
QPen tempConnectionPen() const;
QPen connectionPen() const;
QPen invalidConnectionPen() const;
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) override;
void enterEditMode();
void exitEditMode(bool revertText);
bool isInEditMode() const;
protected:
QVariant itemChange(GraphicsItemChange change, const QVariant &value) override;
void mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event) override;
private:
QDataflowCanvas *canvas_;
QDataflowModelNode *modelNode_;
QList<QDataflowInlet*> inlets_;
QList<QDataflowOutlet*> outlets_;
QGraphicsRectItem *inputHeader_;
QGraphicsRectItem *objectBox_;
QGraphicsRectItem *outputHeader_;
QDataflowNodeTextLabel *textItem_;
bool valid_;
QString oldText_;
friend class QDataflowCanvas;
};
class QDataflowIOlet : public QGraphicsItem
{
protected:
QDataflowIOlet(QDataflowNode *node, int index);
public:
QDataflowNode * node() const {return node_;}
int index() const {return index_;}
void addConnection(QDataflowConnection *connection);
void removeConnection(QDataflowConnection *connection);
QList<QDataflowConnection*> connections() const;
void adjustConnections() const;
QDataflowCanvas * canvas() const {return canvas_;}
protected:
void hoverEnterEvent(QGraphicsSceneHoverEvent *event) override;
void hoverLeaveEvent(QGraphicsSceneHoverEvent *event) override;
QRectF boundingRect() const override;
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) override;
private:
QDataflowCanvas *canvas_;
QList<QDataflowConnection*> connections_;
QDataflowNode *node_;
int index_;
protected:
QDataflowTooltip *tooltip_;
friend class QDataflowCanvas;
friend class QDataflowNode;
};
class QDataflowInlet : public QDataflowIOlet
{
protected:
QDataflowInlet(QDataflowNode *node, int index);
public:
int type() const override {return QDataflowItemTypeInlet;}
void onDataRecevied(void *data);
friend class QDataflowCanvas;
friend class QDataflowNode;
};
class QDataflowOutlet : public QDataflowIOlet
{
protected:
QDataflowOutlet(QDataflowNode *node, int index);
public:
int type() const override {return QDataflowItemTypeOutlet;}
protected:
void mousePressEvent(QGraphicsSceneMouseEvent *event) override;
void mouseReleaseEvent(QGraphicsSceneMouseEvent *event) override;
void mouseMoveEvent(QGraphicsSceneMouseEvent *event) override;
private:
QGraphicsLineItem *tmpConn_;
friend class QDataflowCanvas;
friend class QDataflowNode;
};
class QDataflowConnection : public QGraphicsItem
{
protected:
QDataflowConnection(QDataflowCanvas *canvas, QDataflowModelConnection *modelConnection);
public:
QDataflowModelConnection * modelConnection() const;
QDataflowOutlet * source() const {return source_;}
QDataflowInlet * dest() const {return dest_;}
void adjust();
QDataflowCanvas * canvas() const {return canvas_;}
int type() const override {return QDataflowItemTypeConnection;}
protected:
QRectF boundingRect() const override;
QPainterPath shape() const override;
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) override;
private:
QDataflowCanvas *canvas_;
QDataflowModelConnection *modelConnection_;
QDataflowOutlet *source_;
QDataflowInlet *dest_;
QPointF sourcePoint_;
QPointF destPoint_;
friend class QDataflowCanvas;
friend class QDataflowInlet;
friend class QDataflowOutlet;
};
class QDataflowNodeTextLabel : public QGraphicsTextItem
{
protected:
QDataflowNodeTextLabel(QDataflowNode *node, QGraphicsItem *parent);
public:
void setCompletion(const QStringList &list);
void clearCompletion();
void acceptCompletion();
void cycleCompletion(int d);
void updateCompletion();
void complete();
protected:
bool sceneEvent(QEvent *event) override;
void focusOutEvent(QFocusEvent *event) override;
private:
QDataflowNode *node_;
QList<QGraphicsSimpleTextItem*> completionItems_;
QList<QGraphicsRectItem*> completionRectItems_;
int completionIndex_;
bool completionActive_;
friend class QDataflowNode;
};
class QDataflowTooltip : public QGraphicsItemGroup
{
protected:
QDataflowTooltip(QGraphicsItem *parentItem, const QString &text, const QPointF &offset);
public:
void setText(const QString &text);
void adjust();
private:
QGraphicsSimpleTextItem *text_;
QGraphicsPathItem *shape_;
QPointF offset_;
friend class QDataflowCanvas;
friend class QDataflowInlet;
friend class QDataflowOutlet;
};
class QDataflowTextCompletion
{
public:
virtual QStringList complete(const QString &nodeText);
virtual ~QDataflowTextCompletion() = default;
};
template<typename T>
T * QDataflowCanvas::itemAtT(const QPointF &point)
{
auto const items = scene()->items(point, Qt::IntersectsItemShape, Qt::DescendingOrder, transform());
for(QGraphicsItem *item : items)
{
if(T *itemT = dynamic_cast<T*>(item))
return itemT;
}
return {};
}
#endif // QDATAFLOWCANVAS_H