-
Notifications
You must be signed in to change notification settings - Fork 36
/
QNodeViewBlock.cpp
executable file
·249 lines (196 loc) · 6.49 KB
/
QNodeViewBlock.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
/*!
@file QNodeViewBlock.cpp
Copyright (c) 2014 Graham Wihlidal
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
@author Graham Wihlidal
@date January 19, 2014
*/
#include <QPen>
#include <QGraphicsScene>
#include <QFontMetrics>
#include <QPainter>
#include <QStyleOptionGraphicsItem>
#include <QNodeViewBlock.h>
#include <QNodeViewPort.h>
QNodeViewBlock::QNodeViewBlock(QGraphicsItem* parent)
: QGraphicsPathItem(parent)
, m_width(100)
, m_height(5)
, m_horizontalMargin(20)
, m_verticalMargin(5)
{
setCacheMode(DeviceCoordinateCache);
setFlag(QGraphicsItem::ItemIsMovable);
setFlag(QGraphicsItem::ItemIsSelectable);
QPainterPath path;
path.addRoundedRect(-50, -15, 100, 30, 5, 5);
setPath(path);
setPen(QPen(QColor(30, 30, 30))); // GW-TODO: Expose to QStyle
setBrush(QColor(50, 50, 50)); // GW-TODO: Expose to QStyle
// GW-TODO:
// This causes a crash in the BSP intersect code deep within Qt5.
// Need to investigate further, but many issues are open it appears...
// https://codereview.qt-project.org/#change,40931
// https://bugreports.qt-project.org/browse/QTBUG-23205
// https://bugreports.qt-project.org/browse/QTBUG-24792
// https://bugreports.qt-project.org/browse/QTBUG-18021
// https://bugreports.qt-project.org/browse/QTBUG-5090
#if 0
m_dropShadow.setBlurRadius(16);
m_dropShadow.setXOffset(0.0);
m_dropShadow.setYOffset(5.0);
setGraphicsEffect(&m_dropShadow);
#endif
}
QNodeViewBlock::~QNodeViewBlock()
{
}
QNodeViewPort* QNodeViewBlock::addPort(const QString& name, bool isOutput, qint32 flags, qint32 index)
{
QNodeViewPort* port = new QNodeViewPort(this);
port->setName(name);
port->setIsOutput(isOutput);
port->setBlock(this);
port->setPortFlags(flags);
port->setIndex(index);
QFontMetrics fontMetrics(scene()->font());
const qint32 width = fontMetrics.width(name);
const qint32 height = fontMetrics.height();
if (width > m_width - m_horizontalMargin)
m_width = width + m_horizontalMargin;
m_height += height;
QPainterPath path;
path.addRoundedRect(-(m_width >> 1), -(m_height >> 1), m_width, m_height, 5, 5);
setPath(path);
qint32 y = -(m_height >> 1) + m_verticalMargin + port->radius();
Q_FOREACH (QGraphicsItem* childItem, childItems())
{
if (childItem->type() != QNodeViewType_Port)
continue;
QNodeViewPort* childPort = static_cast<QNodeViewPort*>(childItem);
if (childPort->isOutput())
childPort->setPos((m_width >> 1) + childPort->radius(), y);
else
childPort->setPos(-(m_width >> 1) - childPort->radius(), y);
y += height;
}
return port;
}
void QNodeViewBlock::addInputPort(const QString& name)
{
addPort(name, false);
}
void QNodeViewBlock::addOutputPort(const QString& name)
{
addPort(name, true);
}
void QNodeViewBlock::addInputPorts(const QStringList& names)
{
Q_FOREACH (const QString& name, names)
addInputPort(name);
}
void QNodeViewBlock::addOutputPorts(const QStringList& names)
{
Q_FOREACH (const QString& name, names)
addOutputPort(name);
}
void QNodeViewBlock::save(QDataStream& stream)
{
stream << pos();
qint32 count = 0;
Q_FOREACH (QGraphicsItem* childItem, childItems())
{
if (childItem->type() != QNodeViewType_Port)
continue;
++count;
}
stream << count;
Q_FOREACH (QGraphicsItem* childItem, childItems())
{
if (childItem->type() != QNodeViewType_Port)
continue;
QNodeViewPort* port = static_cast<QNodeViewPort*>(childItem);
stream << reinterpret_cast<quint64>(port);
stream << port->portName();
stream << port->isOutput();
stream << port->portFlags();
}
}
void QNodeViewBlock::load(QDataStream& stream, QMap<quint64, QNodeViewPort*>& portMap)
{
QPointF position;
stream >> position;
setPos(position);
qint32 count;
stream >> count;
for (qint32 iter = 0; iter < count; iter++)
{
quint64 index;
stream >> index;
QString name;
stream >> name;
bool output;
stream >> output;
qint32 flags;
stream >> flags;
portMap[index] = addPort(name, output, flags, index);
}
}
void QNodeViewBlock::paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget)
{
Q_UNUSED(widget);
// Only paint dirty regions for increased performance
painter->setClipRect(option->exposedRect);
if (isSelected())
{
painter->setPen(QPen(QColor(30, 30, 30))); // GW-TODO: Expose to QStyle
painter->setBrush(QColor(100, 100, 100)); // GW-TODO: Expose to QStyle
}
else
{
painter->setPen(QPen(QColor(30, 30, 30))); // GW-TODO: Expose to QStyle
painter->setBrush(QColor(80, 80, 80)); // GW-TODO: Expose to QStyle
}
painter->drawPath(path());
}
QNodeViewBlock* QNodeViewBlock::clone()
{
QNodeViewBlock* block = new QNodeViewBlock(NULL);
this->scene()->addItem(block);
Q_FOREACH (QGraphicsItem* childPort, childItems())
{
if (childPort->type() == QNodeViewType_Port)
{
QNodeViewPort* clonePort = static_cast<QNodeViewPort*>(childPort);
block->addPort(
clonePort->portName(),
clonePort->isOutput(),
clonePort->portFlags(),
clonePort->index());
}
}
return block;
}
QVector<QNodeViewPort*> QNodeViewBlock::ports()
{
QVector<QNodeViewPort*> result;
Q_FOREACH (QGraphicsItem* childItem, childItems())
{
if (childItem->type() == QNodeViewType_Port)
result.append(static_cast<QNodeViewPort*>(childItem));
}
return result;
}
QVariant QNodeViewBlock::itemChange(GraphicsItemChange change, const QVariant& value)
{
Q_UNUSED(change);
return value;
}