PeriDyno 1.2.1
Loading...
Searching...
No Matches
WtFlowScene.cpp
Go to the documentation of this file.
1#include "WtFlowScene.h"
2
3WtFlowScene::WtFlowScene(std::shared_ptr<WtDataModelRegistry> registry)
5{
6
7}
8
12
17
18std::shared_ptr<WtConnection> WtFlowScene::createConnection(
19 PortType connectedPort,
20 WtNode& node,
21 PortIndex portIndex,
22 Wt::WPainter* painter)
23{
24 auto connection = std::make_shared<WtConnection>(connectedPort, node, portIndex);
25
26 auto cgo = detail::make_unique<WtConnectionGraphicsObject>(*this, *connection, painter);
27
28 // after this function connection points are set to node port
29 connection->setGraphicsObject(std::move(cgo));
30
31 _connections[connection->id()] = connection;
32
33 return connection;
34}
35
36std::shared_ptr<WtConnection> WtFlowScene::createConnection(
37 WtNode& nodeIn,
38 PortIndex portIndexIn,
39 WtNode& nodeOut,
40 PortIndex portIndexOut,
41 Wt::WPainter* painter,
42 TypeConverter const& converter)
43{
44 auto connection = std::make_shared<WtConnection>(
45 nodeIn,
46 portIndexIn,
47 nodeOut,
48 portIndexOut,
49 converter);
50
51 nodeIn.addConnection(portIndexIn, connection);
52
53 auto cgo = detail::make_unique<WtConnectionGraphicsObject>(*this, *connection, painter);
54
55 nodeIn.nodeState().setConnection(PortType::In, portIndexIn, *connection);
56
57 nodeOut.nodeState().setConnection(PortType::Out, portIndexOut, *connection);
58
59 // after this function connection points are set to node port
60 connection->setGraphicsObject(std::move(cgo));
61
62 // trigger data propagation
63 nodeOut.onDataUpdated(portIndexOut);
64
65 _connections[connection->id()] = connection;
66
67 //signal
68 //connectionCreated(*connection);
69
70 return connection;
71}
72
74{
75 auto it = _connections.find(connection.id());
76 if (it != _connections.end())
77 {
78 connection.removeFromNodes();
79 _connections.erase(it);
80 }
81}
82
83WtNode& WtFlowScene::createNode(std::unique_ptr<WtNodeDataModel>&& dataModel, Wt::WPainter* painter, int selectType)
84{
85 Wt::WPaintDevice* paintDevice = painter->device();
86
87 auto node = detail::make_unique<WtNode>(std::move(dataModel), paintDevice);
88 auto ngo = detail::make_unique<WtNodeGraphicsObject>(*this, *node, painter, selectType);
89
90 node->setGraphicsObject(std::move(ngo));
91
92 auto nodePtr = node.get();
93 _nodes[node->id()] = std::move(node);
94
95 return *nodePtr;
96}
97
102
103void WtFlowScene::setRegistry(std::shared_ptr<WtDataModelRegistry> registry)
104{
105 _registry = std::move(registry);
106}
107
108void WtFlowScene::iterateOverNodes(std::function<void(WtNode*)> const& visitor)
109{
110 for (const auto& _node : _nodes)
111 {
112 visitor(_node.second.get());
113 }
114}
115
116void WtFlowScene::iterateOverNodeData(std::function<void(WtNodeDataModel*)> const& visitor)
117{
118 for (const auto& _node : _nodes)
119 {
120 visitor(_node.second->nodeDataModel());
121 }
122}
123
124void WtFlowScene::iterateOverNodeDataDependentOrder(std::function<void(WtNodeDataModel*)> const& visitor)
125{
126 std::set<Wt::Guid> visitedNodesSet;
127
128 auto isNodeLeaf = [](WtNode const& node, WtNodeDataModel const& model)
129 {
130 for (unsigned int i = 0; i < model.nPorts(PortType::In); ++i)
131 {
133 if (!connections.empty())
134 {
135 return false;
136 }
137 }
138 return true;
139 };
140
141 //Iterate over "leaf" nodes
142 for (auto const& _node : _nodes)
143 {
144 auto const& node = _node.second;
145 auto model = node->nodeDataModel();
146
147 if (isNodeLeaf(*node, *model))
148 {
149 visitor(model);
150 visitedNodesSet.insert(node->id());
151 }
152 }
153
154 auto areNodeInputsVisitedBefore =
155 [&](WtNode const& node, WtNodeDataModel const& model)
156 {
157 for (size_t i = 0; i < model.nPorts(PortType::In); ++i)
158 {
160
161 for (auto& conn : connections)
162 {
163 /*if (visitedNodesSet.find(conn.second->getNode(PortType::Out)->id()) == visitedNodesSet.end())
164 {
165 return false;
166 }*/
167 return false;
168 }
169 }
170
171 return true;
172 };
173
174 //Iterate over dependent nodes
175 while (_nodes.size() != visitedNodesSet.size())
176 {
177 for (auto const& _node : _nodes)
178 {
179 auto const& node = _node.second;
180 if (visitedNodesSet.find(node->id()) != visitedNodesSet.end())
181 continue;
182
183 auto model = node->nodeDataModel();
184
185 if (areNodeInputsVisitedBefore(*node, *model))
186 {
187 visitor(model);
188 visitedNodesSet.insert(node->id());
189 }
190 }
191 }
192}
193
194Wt::WPointF WtFlowScene::getNodePosition(const WtNode& node) const
195{
196 return node.nodeGraphicsObject().getPos();
197}
198
199//Wt::WPointF WtFlowScene::setNodePosition(WtNode& node, const Wt::WPointF& pos) const
200//{
201// //node.nodeGraphicsObject().setPos(pos);
202// node.nodeGraphicsObject().moveConnections();
203//}
204
206{
207 // call signal
208 //nodeDeleted(node);
209 clearNode(node);
210}
211
213{
214 for (auto portType : { PortType::In, PortType::Out })
215 {
216 auto nodeState = node.nodeState();
217 auto const& nodeEntries = nodeState.getEntries(portType);
218
219 for (auto& connections : nodeEntries)
220 {
221 for (auto const& pair : connections)
222 {
223 deleteConnection(*pair.second);
224 }
225 }
226 }
227 _nodes.erase(node.id());
228}
229
230std::map<dyno::ObjectId, WtNode*> WtFlowScene::getNodeMap()
231{
232 return OutNodeMap;
233}
234
235std::unordered_map<Wt::Guid, std::unique_ptr<WtNode> > const& WtFlowScene::nodes() const
236{
237 return _nodes;
238}
239
240std::unordered_map<Wt::Guid, std::shared_ptr<WtConnection> > const& WtFlowScene::connections() const
241{
242 return _connections;
243}
244
246{
247 //Manual node cleanup. Simply clearing the holding datastructures doesn't work, the code crashes when
248// there are both nodes and connections in the scene. (The data propagation internal logic tries to propagate
249// data through already freed connections.)
250 while (_connections.size() > 0)
251 {
252 deleteConnection(*_connections.begin()->second);
253 }
254
255 while (_nodes.size() > 0)
256 {
257 clearNode(*_nodes.begin()->second);
258 }
259}
260
261std::vector<WtNode*> WtFlowScene::allNodes() const
262{
263 std::vector<WtNode*> nodes;
264
265 std::transform(_nodes.begin(),
266 _nodes.end(),
267 std::back_inserter(nodes),
268 [](std::pair<Wt::Guid const, std::unique_ptr<WtNode>> const& p) { return p.second.get(); });
269
270 return nodes;
271}
272
273
274//std::vector<WtNode*> WtFlowScene::selectedNodes() const
275//{
276// //QList<QGraphicsItem*> graphicsItems = selectedItems();
277//
278// //std::vector<WtNode*> ret;
279// //ret.reserve(graphicsItems.size());
280//
281// //for (QGraphicsItem* item : graphicsItems)
282// //{
283// // auto ngo = qgraphicsitem_cast<QtNodeGraphicsObject*>(item);
284//
285// // if (ngo != nullptr)
286// // {
287// // ret.push_back(&ngo->node());
288// // }
289// //}
290//
291// //return ret;
292//}
std::function< SharedNodeData(SharedNodeData)> TypeConverter
int PortIndex
PortType
Wt::Guid id() const
void removeFromNodes() const
void iterateOverNodes(std::function< void(WtNode *)> const &visitor)
void iterateOverNodeDataDependentOrder(std::function< void(WtNodeDataModel *)> const &visitor)
void setRegistry(std::shared_ptr< WtDataModelRegistry > registry)
std::map< dyno::ObjectId, WtNode * > getNodeMap()
std::vector< WtNode * > allNodes() const
WtNode & createNode(std::unique_ptr< WtNodeDataModel > &&dataModel, Wt::WPainter *painter, int selectType)
void deleteConnection(WtConnection &connection)
std::shared_ptr< WtDataModelRegistry > _registry
void iterateOverNodeData(std::function< void(WtNodeDataModel *)> const &visitor)
std::map< dyno::ObjectId, WtNode * > OutNodeMap
void clearNode(WtNode &node)
std::shared_ptr< WtConnection > createConnection(PortType connectedPort, WtNode &node, PortIndex portIndex, Wt::WPainter *painter)
std::unordered_map< Wt::Guid, std::unique_ptr< WtNode > > const & nodes() const
Wt::WPointF getNodePosition(WtNode const &node) const
void clearScene()
void removeNode(WtNode &node)
WtDataModelRegistry & registry() const
std::unordered_map< Wt::Guid, SharedConnection > _connections
std::unordered_map< Wt::Guid, std::shared_ptr< WtConnection > > const & connections() const
std::unordered_map< Wt::Guid, UniqueNode > _nodes
WtNodeDataModel * nodeDataModel() const
Definition WtNode.cpp:727
Wt::Guid id() const
Definition WtNode.cpp:679
void onDataUpdated(PortIndex index)
Definition WtNode.cpp:744
WtNodeGraphicsObject const & nodeGraphicsObject() const
Definition WtNode.cpp:690
void addConnection(PortIndex portIndex, std::shared_ptr< WtConnection > connection)
Definition WtNode.h:298
WtNodeState const & nodeState() const
Definition WtNode.cpp:717
std::vector< ConnectionPtrSet > const & getEntries(PortType) const
Definition WtNode.cpp:582
ConnectionPtrSet connections(PortType portType, PortIndex portIndex) const
Definition WtNode.cpp:598
void setConnection(PortType portType, PortIndex portIndex, WtConnection &connection)
Definition WtNode.cpp:605
std::unique_ptr< T > make_unique(Args &&... args)
Definition Export.hpp:13