-
Notifications
You must be signed in to change notification settings - Fork 948
Expand file tree
/
Copy pathDataFlowGraphModel.hpp
More file actions
141 lines (96 loc) · 4.04 KB
/
DataFlowGraphModel.hpp
File metadata and controls
141 lines (96 loc) · 4.04 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
#pragma once
#include "AbstractGraphModel.hpp"
#include "ConnectionIdUtils.hpp"
#include "NodeDelegateModelRegistry.hpp"
#include "Serializable.hpp"
#include "StyleCollection.hpp"
#include "Export.hpp"
#include <QJsonObject>
#include <memory>
namespace QtNodes {
class NODE_EDITOR_PUBLIC DataFlowGraphModel : public AbstractGraphModel, public Serializable
{
Q_OBJECT
public:
struct NodeGeometryData
{
QSize size;
QPointF pos;
};
public:
DataFlowGraphModel(std::shared_ptr<NodeDelegateModelRegistry> registry);
std::shared_ptr<NodeDelegateModelRegistry> dataModelRegistry() { return _registry; }
public:
std::unordered_set<NodeId> allNodeIds() const override;
std::unordered_set<ConnectionId> allConnectionIds(NodeId const nodeId) const override;
std::unordered_set<ConnectionId> connections(NodeId nodeId,
PortType portType,
PortIndex portIndex) const override;
bool connectionExists(ConnectionId const connectionId) const override;
NodeId addNode(QString const nodeType) override;
bool connectionPossible(ConnectionId const connectionId) const override;
void addConnection(ConnectionId const connectionId) override;
bool nodeExists(NodeId const nodeId) const override;
QVariant nodeData(NodeId nodeId, NodeRole role) const override;
NodeFlags nodeFlags(NodeId nodeId) const override;
bool setNodeData(NodeId nodeId, NodeRole role, QVariant value) override;
QVariant portData(NodeId nodeId,
PortType portType,
PortIndex portIndex,
PortRole role) const override;
bool setPortData(NodeId nodeId,
PortType portType,
PortIndex portIndex,
QVariant const &value,
PortRole role = PortRole::Data) override;
bool deleteConnection(ConnectionId const connectionId) override;
bool deleteNode(NodeId const nodeId) override;
QJsonObject saveNode(NodeId const) const override;
QJsonObject save() const override;
void loadNode(QJsonObject const &nodeJson) override;
void load(QJsonObject const &json) override;
/**
* Fetches the NodeDelegateModel for the given `nodeId` and tries to cast the
* stored pointer to the given type
*/
template<typename NodeDelegateModelType>
NodeDelegateModelType *delegateModel(NodeId const nodeId)
{
auto it = _models.find(nodeId);
if (it == _models.end())
return nullptr;
auto model = dynamic_cast<NodeDelegateModelType *>(it->second.get());
return model;
}
public Q_SLOTS:
virtual void propagate(NodeId const nodeId);
protected:
virtual bool canPropagate(ConnectionId const) const { return true; };
Q_SIGNALS:
void inPortDataWasSet(NodeId const, PortType const, PortIndex const);
private:
NodeId newNodeId() override { return _nextNodeId++; }
protected Q_SLOTS:
void sendConnectionCreation(ConnectionId const connectionId);
void sendConnectionDeletion(ConnectionId const connectionId);
/**
* Fuction is called in three cases:
*
* - By underlying NodeDelegateModel when a node has new data to propagate.
* @see DataFlowGraphModel::addNode
* - When a new connection is created.
* @see DataFlowGraphModel::addConnection
* - When a node restored from JSON an needs to send data downstream.
* @see DataFlowGraphModel::loadNode
*/
virtual void onOutPortDataUpdated(NodeId const nodeId, PortIndex const portIndex);
/// Function is called after detaching a connection.
virtual void propagateEmptyDataTo(NodeId const nodeId, PortIndex const portIndex);
private:
std::shared_ptr<NodeDelegateModelRegistry> _registry;
NodeId _nextNodeId;
std::unordered_map<NodeId, std::unique_ptr<NodeDelegateModel>> _models;
std::unordered_set<ConnectionId> _connectivity;
mutable std::unordered_map<NodeId, NodeGeometryData> _nodeGeometryData;
};
} // namespace QtNodes