|
@@ -1,4 +1,4 @@
|
|
|
-#include "DataFlowGraphModel.hpp"
|
|
|
+#include "DataFlowGraphModel.hpp"
|
|
|
#include "ConnectionIdHash.hpp"
|
|
|
|
|
|
#include <QJsonArray>
|
|
@@ -8,9 +8,9 @@
|
|
|
namespace QtNodes {
|
|
|
|
|
|
DataFlowGraphModel::DataFlowGraphModel(std::shared_ptr<NodeDelegateModelRegistry> registry)
|
|
|
- : _registry(std::move(registry))
|
|
|
- , _nextNodeId{0}
|
|
|
-{}
|
|
|
+ : _registry(std::move(registry)), _nextNodeId { 0 }
|
|
|
+{
|
|
|
+}
|
|
|
|
|
|
std::unordered_set<NodeId> DataFlowGraphModel::allNodeIds() const
|
|
|
{
|
|
@@ -24,28 +24,20 @@ std::unordered_set<ConnectionId> DataFlowGraphModel::allConnectionIds(NodeId con
|
|
|
{
|
|
|
std::unordered_set<ConnectionId> result;
|
|
|
|
|
|
- std::copy_if(_connectivity.begin(),
|
|
|
- _connectivity.end(),
|
|
|
- std::inserter(result, std::end(result)),
|
|
|
- [&nodeId](ConnectionId const &cid) {
|
|
|
- return cid.inNodeId == nodeId || cid.outNodeId == nodeId;
|
|
|
- });
|
|
|
+ std::copy_if(_connectivity.begin(), _connectivity.end(), std::inserter(result, std::end(result)),
|
|
|
+ [&nodeId](ConnectionId const &cid) { return cid.inNodeId == nodeId || cid.outNodeId == nodeId; });
|
|
|
|
|
|
return result;
|
|
|
}
|
|
|
|
|
|
-std::unordered_set<ConnectionId> DataFlowGraphModel::connections(NodeId nodeId,
|
|
|
- PortType portType,
|
|
|
+std::unordered_set<ConnectionId> DataFlowGraphModel::connections(NodeId nodeId, PortType portType,
|
|
|
PortIndex portIndex) const
|
|
|
{
|
|
|
std::unordered_set<ConnectionId> result;
|
|
|
|
|
|
- std::copy_if(_connectivity.begin(),
|
|
|
- _connectivity.end(),
|
|
|
- std::inserter(result, std::end(result)),
|
|
|
+ std::copy_if(_connectivity.begin(), _connectivity.end(), std::inserter(result, std::end(result)),
|
|
|
[&portType, &portIndex, &nodeId](ConnectionId const &cid) {
|
|
|
- return (getNodeId(portType, cid) == nodeId
|
|
|
- && getPortIndex(portType, cid) == portIndex);
|
|
|
+ return (getNodeId(portType, cid) == nodeId && getPortIndex(portType, cid) == portIndex);
|
|
|
});
|
|
|
|
|
|
return result;
|
|
@@ -63,35 +55,22 @@ NodeId DataFlowGraphModel::addNode(QString const nodeType)
|
|
|
if (model) {
|
|
|
NodeId newId = newNodeId();
|
|
|
|
|
|
- connect(model.get(),
|
|
|
- &NodeDelegateModel::dataUpdated,
|
|
|
- [newId, this](PortIndex const portIndex) {
|
|
|
- onOutPortDataUpdated(newId, portIndex);
|
|
|
- });
|
|
|
+ connect(model.get(), &NodeDelegateModel::dataUpdated,
|
|
|
+ [newId, this](PortIndex const portIndex) { onOutPortDataUpdated(newId, portIndex); });
|
|
|
|
|
|
- connect(model.get(),
|
|
|
- &NodeDelegateModel::portsAboutToBeDeleted,
|
|
|
- this,
|
|
|
+ connect(model.get(), &NodeDelegateModel::portsAboutToBeDeleted, this,
|
|
|
[newId, this](PortType const portType, PortIndex const first, PortIndex const last) {
|
|
|
portsAboutToBeDeleted(newId, portType, first, last);
|
|
|
});
|
|
|
|
|
|
- connect(model.get(),
|
|
|
- &NodeDelegateModel::portsDeleted,
|
|
|
- this,
|
|
|
- &DataFlowGraphModel::portsDeleted);
|
|
|
+ connect(model.get(), &NodeDelegateModel::portsDeleted, this, &DataFlowGraphModel::portsDeleted);
|
|
|
|
|
|
- connect(model.get(),
|
|
|
- &NodeDelegateModel::portsAboutToBeInserted,
|
|
|
- this,
|
|
|
+ connect(model.get(), &NodeDelegateModel::portsAboutToBeInserted, this,
|
|
|
[newId, this](PortType const portType, PortIndex const first, PortIndex const last) {
|
|
|
portsAboutToBeInserted(newId, portType, first, last);
|
|
|
});
|
|
|
|
|
|
- connect(model.get(),
|
|
|
- &NodeDelegateModel::portsInserted,
|
|
|
- this,
|
|
|
- &DataFlowGraphModel::portsInserted);
|
|
|
+ connect(model.get(), &NodeDelegateModel::portsInserted, this, &DataFlowGraphModel::portsInserted);
|
|
|
|
|
|
_models[newId] = std::move(model);
|
|
|
|
|
@@ -106,26 +85,23 @@ NodeId DataFlowGraphModel::addNode(QString const nodeType)
|
|
|
bool DataFlowGraphModel::connectionPossible(ConnectionId const connectionId) const
|
|
|
{
|
|
|
auto getDataType = [&](PortType const portType) {
|
|
|
- return portData(getNodeId(portType, connectionId),
|
|
|
- portType,
|
|
|
- getPortIndex(portType, connectionId),
|
|
|
+ return portData(getNodeId(portType, connectionId), portType, getPortIndex(portType, connectionId),
|
|
|
PortRole::DataType)
|
|
|
- .value<NodeDataType>();
|
|
|
+ .value<NodeDataType>();
|
|
|
};
|
|
|
|
|
|
auto portVacant = [&](PortType const portType) {
|
|
|
- NodeId const nodeId = getNodeId(portType, connectionId);
|
|
|
+ NodeId const nodeId = getNodeId(portType, connectionId);
|
|
|
PortIndex const portIndex = getPortIndex(portType, connectionId);
|
|
|
- auto const connected = connections(nodeId, portType, portIndex);
|
|
|
+ auto const connected = connections(nodeId, portType, portIndex);
|
|
|
|
|
|
- auto policy = portData(nodeId, portType, portIndex, PortRole::ConnectionPolicyRole)
|
|
|
- .value<ConnectionPolicy>();
|
|
|
+ auto policy = portData(nodeId, portType, portIndex, PortRole::ConnectionPolicyRole).value<ConnectionPolicy>();
|
|
|
|
|
|
return connected.empty() || (policy == ConnectionPolicy::Many);
|
|
|
};
|
|
|
|
|
|
- return getDataType(PortType::Out).id == getDataType(PortType::In).id
|
|
|
- && portVacant(PortType::Out) && portVacant(PortType::In);
|
|
|
+ return getDataType(PortType::Out).id == getDataType(PortType::In).id && portVacant(PortType::Out)
|
|
|
+ && portVacant(PortType::In);
|
|
|
}
|
|
|
|
|
|
void DataFlowGraphModel::addConnection(ConnectionId const connectionId)
|
|
@@ -134,16 +110,10 @@ void DataFlowGraphModel::addConnection(ConnectionId const connectionId)
|
|
|
|
|
|
sendConnectionCreation(connectionId);
|
|
|
|
|
|
- QVariant const portDataToPropagate = portData(connectionId.outNodeId,
|
|
|
- PortType::Out,
|
|
|
- connectionId.outPortIndex,
|
|
|
- PortRole::Data);
|
|
|
+ QVariant const portDataToPropagate =
|
|
|
+ portData(connectionId.outNodeId, PortType::Out, connectionId.outPortIndex, PortRole::Data);
|
|
|
|
|
|
- setPortData(connectionId.inNodeId,
|
|
|
- PortType::In,
|
|
|
- connectionId.inPortIndex,
|
|
|
- portDataToPropagate,
|
|
|
- PortRole::Data);
|
|
|
+ setPortData(connectionId.inNodeId, PortType::In, connectionId.inPortIndex, portDataToPropagate, PortRole::Data);
|
|
|
}
|
|
|
|
|
|
void DataFlowGraphModel::sendConnectionCreation(ConnectionId const connectionId)
|
|
@@ -212,7 +182,7 @@ QVariant DataFlowGraphModel::nodeData(NodeId nodeId, NodeRole role) const
|
|
|
|
|
|
case NodeRole::Style: {
|
|
|
auto style = StyleCollection::nodeStyle();
|
|
|
- result = style.toJson().toVariantMap();
|
|
|
+ result = style.toJson().toVariantMap();
|
|
|
} break;
|
|
|
|
|
|
case NodeRole::InternalData: {
|
|
@@ -272,13 +242,14 @@ bool DataFlowGraphModel::setNodeData(NodeId nodeId, NodeRole role, QVariant valu
|
|
|
|
|
|
case NodeRole::Size: {
|
|
|
_nodeGeometryData[nodeId].size = value.value<QSize>();
|
|
|
- result = true;
|
|
|
+ result = true;
|
|
|
} break;
|
|
|
|
|
|
case NodeRole::CaptionVisible:
|
|
|
break;
|
|
|
|
|
|
case NodeRole::Caption:
|
|
|
+ _captionData[nodeId] = value.value<QString>();
|
|
|
break;
|
|
|
|
|
|
case NodeRole::Style:
|
|
@@ -300,10 +271,7 @@ bool DataFlowGraphModel::setNodeData(NodeId nodeId, NodeRole role, QVariant valu
|
|
|
return result;
|
|
|
}
|
|
|
|
|
|
-QVariant DataFlowGraphModel::portData(NodeId nodeId,
|
|
|
- PortType portType,
|
|
|
- PortIndex portIndex,
|
|
|
- PortRole role) const
|
|
|
+QVariant DataFlowGraphModel::portData(NodeId nodeId, PortType portType, PortIndex portIndex, PortRole role) const
|
|
|
{
|
|
|
QVariant result;
|
|
|
|
|
@@ -340,8 +308,8 @@ QVariant DataFlowGraphModel::portData(NodeId nodeId,
|
|
|
return result;
|
|
|
}
|
|
|
|
|
|
-bool DataFlowGraphModel::setPortData(
|
|
|
- NodeId nodeId, PortType portType, PortIndex portIndex, QVariant const &value, PortRole role)
|
|
|
+bool DataFlowGraphModel::setPortData(NodeId nodeId, PortType portType, PortIndex portIndex, QVariant const &value,
|
|
|
+ PortRole role)
|
|
|
{
|
|
|
Q_UNUSED(nodeId);
|
|
|
|
|
@@ -385,8 +353,7 @@ bool DataFlowGraphModel::deleteConnection(ConnectionId const connectionId)
|
|
|
if (disconnected) {
|
|
|
sendConnectionDeletion(connectionId);
|
|
|
|
|
|
- propagateEmptyDataTo(getNodeId(PortType::In, connectionId),
|
|
|
- getPortIndex(PortType::In, connectionId));
|
|
|
+ propagateEmptyDataTo(getNodeId(PortType::In, connectionId), getPortIndex(PortType::In, connectionId));
|
|
|
}
|
|
|
|
|
|
return disconnected;
|
|
@@ -420,8 +387,8 @@ QJsonObject DataFlowGraphModel::saveNode(NodeId const nodeId) const
|
|
|
QPointF const pos = nodeData(nodeId, NodeRole::Position).value<QPointF>();
|
|
|
|
|
|
QJsonObject posJson;
|
|
|
- posJson["x"] = pos.x();
|
|
|
- posJson["y"] = pos.y();
|
|
|
+ posJson["x"] = pos.x();
|
|
|
+ posJson["y"] = pos.y();
|
|
|
nodeJson["position"] = posJson;
|
|
|
}
|
|
|
|
|
@@ -467,11 +434,8 @@ void DataFlowGraphModel::loadNode(QJsonObject const &nodeJson)
|
|
|
std::unique_ptr<NodeDelegateModel> model = _registry->create(delegateModelName);
|
|
|
|
|
|
if (model) {
|
|
|
- connect(model.get(),
|
|
|
- &NodeDelegateModel::dataUpdated,
|
|
|
- [restoredNodeId, this](PortIndex const portIndex) {
|
|
|
- onOutPortDataUpdated(restoredNodeId, portIndex);
|
|
|
- });
|
|
|
+ connect(model.get(), &NodeDelegateModel::dataUpdated,
|
|
|
+ [restoredNodeId, this](PortIndex const portIndex) { onOutPortDataUpdated(restoredNodeId, portIndex); });
|
|
|
|
|
|
_models[restoredNodeId] = std::move(model);
|
|
|
|
|
@@ -484,8 +448,7 @@ void DataFlowGraphModel::loadNode(QJsonObject const &nodeJson)
|
|
|
|
|
|
_models[restoredNodeId]->load(internalDataJson);
|
|
|
} else {
|
|
|
- throw std::logic_error(std::string("No registered model with name ")
|
|
|
- + delegateModelName.toLocal8Bit().data());
|
|
|
+ throw std::logic_error(std::string("No registered model with name ") + delegateModelName.toLocal8Bit().data());
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -511,9 +474,7 @@ void DataFlowGraphModel::load(QJsonObject const &jsonDocument)
|
|
|
|
|
|
void DataFlowGraphModel::onOutPortDataUpdated(NodeId const nodeId, PortIndex const portIndex)
|
|
|
{
|
|
|
- std::unordered_set<ConnectionId> const &connected = connections(nodeId,
|
|
|
- PortType::Out,
|
|
|
- portIndex);
|
|
|
+ std::unordered_set<ConnectionId> const &connected = connections(nodeId, PortType::Out, portIndex);
|
|
|
|
|
|
QVariant const portDataToPropagate = portData(nodeId, PortType::Out, portIndex, PortRole::Data);
|
|
|
|
|
@@ -524,9 +485,9 @@ void DataFlowGraphModel::onOutPortDataUpdated(NodeId const nodeId, PortIndex con
|
|
|
|
|
|
void DataFlowGraphModel::propagateEmptyDataTo(NodeId const nodeId, PortIndex const portIndex)
|
|
|
{
|
|
|
- QVariant emptyData{};
|
|
|
+ QVariant emptyData {};
|
|
|
|
|
|
setPortData(nodeId, PortType::In, portIndex, emptyData, PortRole::Data);
|
|
|
}
|
|
|
|
|
|
-} // namespace QtNodes
|
|
|
+} // namespace QtNodes
|