forked from paceholder/nodeeditor
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDataFlowGraphicsScene.cpp
More file actions
308 lines (239 loc) · 9.16 KB
/
DataFlowGraphicsScene.cpp
File metadata and controls
308 lines (239 loc) · 9.16 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
#include "DataFlowGraphicsScene.hpp"
#include "ConnectionGraphicsObject.hpp"
#include "GraphicsView.hpp"
#include "NodeDelegateModelRegistry.hpp"
#include "NodeGraphicsObject.hpp"
#include "UndoCommands.hpp"
#include <QtWidgets/QFileDialog>
#include <QtWidgets/QGraphicsSceneMoveEvent>
#include <QtWidgets/QHeaderView>
#include <QtWidgets/QLineEdit>
#include <QtWidgets/QTreeWidget>
#include <QtWidgets/QWidgetAction>
#include <QtCore/QBuffer>
#include <QtCore/QByteArray>
#include <QtCore/QDataStream>
#include <QtCore/QDebug>
#include <QtCore/QFile>
#include <QtCore/QJsonArray>
#include <QtCore/QJsonDocument>
#include <QtCore/QJsonObject>
#include <QtCore/QJsonParseError>
#include <QtCore/QJsonValue>
#include <QtCore/QUuid>
#include <QtCore/QtGlobal>
#include <stdexcept>
#include <utility>
#include <vector>
namespace {
using QtNodes::GroupId;
using QtNodes::InvalidGroupId;
GroupId jsonValueToGroupId(QJsonValue const &value)
{
if (value.isDouble()) {
return static_cast<GroupId>(value.toInt());
}
if (value.isString()) {
auto const textValue = value.toString();
bool ok = false;
auto const numericValue = textValue.toULongLong(&ok, 10);
if (ok) {
return static_cast<GroupId>(numericValue);
}
QUuid uuidValue(textValue);
if (!uuidValue.isNull()) {
auto const bytes = uuidValue.toRfc4122();
if (bytes.size() >= static_cast<int>(sizeof(quint32))) {
QDataStream stream(bytes);
quint32 value32 = 0U;
stream >> value32;
return static_cast<GroupId>(value32);
}
}
}
return InvalidGroupId;
}
} // namespace
namespace QtNodes {
DataFlowGraphicsScene::DataFlowGraphicsScene(DataFlowGraphModel &graphModel, QObject *parent)
: BasicGraphicsScene(graphModel, parent)
, _graphModel(graphModel)
{
connect(&_graphModel,
&DataFlowGraphModel::inPortDataWasSet,
[this](NodeId const nodeId, PortType const, PortIndex const) { onNodeUpdated(nodeId); });
}
// TODO constructor for an empyt scene?
std::vector<NodeId> DataFlowGraphicsScene::selectedNodes() const
{
QList<QGraphicsItem *> graphicsItems = selectedItems();
std::vector<NodeId> result;
result.reserve(graphicsItems.size());
for (QGraphicsItem *item : graphicsItems) {
auto ngo = qgraphicsitem_cast<NodeGraphicsObject *>(item);
if (ngo != nullptr) {
result.push_back(ngo->nodeId());
}
}
return result;
}
QMenu *DataFlowGraphicsScene::createSceneMenu(QPointF const scenePos)
{
QMenu *modelMenu = new QMenu();
// Add filterbox to the context menu
auto *txtBox = new QLineEdit(modelMenu);
txtBox->setPlaceholderText(QStringLiteral("Filter"));
txtBox->setClearButtonEnabled(true);
auto *txtBoxAction = new QWidgetAction(modelMenu);
txtBoxAction->setDefaultWidget(txtBox);
// 1.
modelMenu->addAction(txtBoxAction);
// Add result treeview to the context menu
QTreeWidget *treeView = new QTreeWidget(modelMenu);
treeView->header()->close();
auto *treeViewAction = new QWidgetAction(modelMenu);
treeViewAction->setDefaultWidget(treeView);
// 2.
modelMenu->addAction(treeViewAction);
auto registry = _graphModel.dataModelRegistry();
for (auto const &cat : registry->categories()) {
auto item = new QTreeWidgetItem(treeView);
item->setText(0, cat);
item->setFlags(item->flags() & ~Qt::ItemIsSelectable);
}
for (auto const &assoc : registry->registeredModelsCategoryAssociation()) {
QList<QTreeWidgetItem *> parent = treeView->findItems(assoc.second, Qt::MatchExactly);
if (parent.count() <= 0)
continue;
auto item = new QTreeWidgetItem(parent.first());
item->setText(0, assoc.first);
}
treeView->expandAll();
connect(treeView,
&QTreeWidget::itemClicked,
[this, modelMenu, scenePos](QTreeWidgetItem *item, int) {
if (!(item->flags() & (Qt::ItemIsSelectable))) {
return;
}
this->undoStack().push(new CreateCommand(this, item->text(0), scenePos));
modelMenu->close();
});
//Setup filtering
connect(txtBox, &QLineEdit::textChanged, [treeView](const QString &text) {
QTreeWidgetItemIterator categoryIt(treeView, QTreeWidgetItemIterator::HasChildren);
while (*categoryIt)
(*categoryIt++)->setHidden(true);
QTreeWidgetItemIterator it(treeView, QTreeWidgetItemIterator::NoChildren);
while (*it) {
auto modelName = (*it)->text(0);
const bool match = (modelName.contains(text, Qt::CaseInsensitive));
(*it)->setHidden(!match);
if (match) {
QTreeWidgetItem *parent = (*it)->parent();
while (parent) {
parent->setHidden(false);
parent = parent->parent();
}
}
++it;
}
});
// make sure the text box gets focus so the user doesn't have to click on it
txtBox->setFocus();
// QMenu's instance auto-destruction
modelMenu->setAttribute(Qt::WA_DeleteOnClose);
return modelMenu;
}
bool DataFlowGraphicsScene::save() const
{
QString fileName = QFileDialog::getSaveFileName(nullptr,
tr("Open Flow Scene"),
QDir::homePath(),
tr("Flow Scene Files (*.flow)"));
if (!fileName.isEmpty()) {
if (!fileName.endsWith("flow", Qt::CaseInsensitive))
fileName += ".flow";
QFile file(fileName);
if (file.open(QIODevice::WriteOnly)) {
QJsonObject sceneJson = _graphModel.save();
QJsonArray groupsJsonArray;
for (auto const &[groupId, groupPtr] : groups()) {
if (!groupPtr)
continue;
QJsonObject groupJson;
groupJson["id"] = static_cast<qint64>(groupId);
groupJson["name"] = groupPtr->name();
QJsonArray nodeIdsJson;
for (NodeId const nodeId : groupPtr->nodeIDs()) {
nodeIdsJson.append(static_cast<qint64>(nodeId));
}
groupJson["nodes"] = nodeIdsJson;
groupJson["locked"] = groupPtr->groupGraphicsObject().locked();
groupsJsonArray.append(groupJson);
}
if (!groupsJsonArray.isEmpty()) {
sceneJson["groups"] = groupsJsonArray;
}
file.write(QJsonDocument(sceneJson).toJson());
return true;
}
}
return false;
}
bool DataFlowGraphicsScene::load()
{
QString fileName = QFileDialog::getOpenFileName(nullptr,
tr("Open Flow Scene"),
QDir::homePath(),
tr("Flow Scene Files (*.flow)"));
if (!QFileInfo::exists(fileName))
return false;
QFile file(fileName);
if (!file.open(QIODevice::ReadOnly))
return false;
clearScene();
QByteArray const wholeFile = file.readAll();
QJsonParseError parseError{};
QJsonDocument const sceneDocument = QJsonDocument::fromJson(wholeFile, &parseError);
if (parseError.error != QJsonParseError::NoError || !sceneDocument.isObject())
return false;
QJsonObject const sceneJson = sceneDocument.object();
_graphModel.load(sceneJson);
if (sceneJson.contains("groups")) {
QJsonArray const groupsJsonArray = sceneJson["groups"].toArray();
for (QJsonValue groupValue : groupsJsonArray) {
QJsonObject const groupObject = groupValue.toObject();
QJsonArray const nodeIdsJson = groupObject["nodes"].toArray();
std::vector<NodeGraphicsObject *> groupNodes;
groupNodes.reserve(nodeIdsJson.size());
for (QJsonValue idValue : nodeIdsJson) {
NodeId const nodeId = static_cast<NodeId>(idValue.toInt());
if (auto *nodeObject = nodeGraphicsObject(nodeId)) {
groupNodes.push_back(nodeObject);
}
}
if (groupNodes.empty())
continue;
QString const groupName = groupObject["name"].toString();
GroupId const groupId = jsonValueToGroupId(groupObject["id"]);
auto const groupWeak = createGroup(groupNodes, groupName, groupId);
if (auto group = groupWeak.lock()) {
bool const locked = groupObject["locked"].toBool(true);
group->groupGraphicsObject().lock(locked);
}
}
}
Q_EMIT sceneLoaded();
return true;
}
void DataFlowGraphicsScene::updateConnectionGraphics(
const std::unordered_set<ConnectionId> &connections, bool state)
{
for (auto const &c : connections) {
if (auto *cgo = connectionGraphicsObject(c)) {
cgo->connectionState().setFrozen(state);
cgo->update();
}
}
}
} // namespace QtNodes